#include #include #include #include #include #include #include #include #define MAX_FD_SZ 1024 int nfds = 0; pthread_mutex_t fd_mutex; char fds[MAX_FD_SZ]; int fd_valid(int fd) { if (fd < 3 || fd >= FD_SETSIZE) return 0; if (fcntl(fd, F_GETFL) == -1 && errno == EBADF) return 0; return 1; } void watch_fd(int fd) { pthread_mutex_lock(&fd_mutex); // printf("watching fd=%d\n", fd); nfds = fd > nfds ? fd : nfds; fds[fd] = fds[fd] == 0 ? 1 : fds[fd]; pthread_mutex_unlock(&fd_mutex); } void *monitor_events() { while(1) { usleep(10000); pthread_mutex_lock(&fd_mutex); struct sockaddr_in addr; socklen_t addrlen; int i; for(i=0; id_ino = %d\n", dp->d_ino); //printf("dp->d_off = %d\n", dp->d_off); //printf("dp->d_reclen = %d\n", dp->d_reclen); //printf("dp->d_type = %d\n", dp->d_type); /* printf("\n\ndp->d_name = %s\n", dp->d_name); if(dp->d_type == DT_BLK) printf("\tDT_BLK\n"); if(dp->d_type == DT_CHR) printf("\tDT_CHR\n"); if(dp->d_type == DT_DIR) printf("\tDT_DIR\n"); if(dp->d_type == DT_FIFO) printf("\tDT_FIFO\n"); if(dp->d_type == DT_LNK) printf("\tDT_LNK\n"); if(dp->d_type == DT_REG) printf("\tDT_REG\n"); if(dp->d_type == DT_SOCK) printf("\tDT_SOCK\n"); if(dp->d_type == DT_UNKNOWN) printf("\tDT_UNKNOWN\n"); */ if(dp->d_type == DT_LNK || dp->d_type == DT_UNKNOWN) { int fd = atoi(dp->d_name); if(fd_valid(fd)) { watch_fd(fd); } } } closedir(dir); } } int main() { // Create test fd int sz; char input[32]; int i=0; for(i=0;i