#include #include #include #include #include #include #include #include #include #include #include #define NUM 4 #define errExit(msg) do { perror(msg); _exit(EXIT_FAILURE); } while (0) #define errExit1(msg) { perror(msg); _exit(EXIT_FAILURE); } void child_handler(int sig) { pid_t pid; while((pid = waitpid(-1, NULL, WNOHANG)) > 0); } void int_handler(int sig){ printf("Ctrl-C pressed, do nothing...\n"); } int main(int argc, char *argv[]) { struct sigaction new_act, old_act; sigemptyset (&new_act.sa_mask); new_act.sa_flags = 0; new_act.sa_flags = SA_RESTART; new_act.sa_handler = child_handler; sigaction (SIGCHLD, &new_act, &old_act); struct sigaction nint_act, oint_act; sigemptyset (&nint_act.sa_mask); nint_act.sa_flags = 0; nint_act.sa_flags = SA_RESTART; nint_act.sa_handler = int_handler; sigaction (SIGINT, &nint_act, &oint_act); int pipefd[NUM][2]; pid_t cpid[NUM]; int nfds, num_open_fds; struct pollfd *pfds; int ready; unsigned char i,tt; char buf; int rr; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); _exit(EXIT_FAILURE); } srand(time(NULL)); num_open_fds = nfds = NUM; pfds = calloc(nfds, sizeof(struct pollfd)); if (pfds == NULL) errExit("malloc"); for (i=0;i 0) { do{ dprintf(STDOUT_FILENO,"\ntry poll:\n"); for (i = 0; i < NUM; i++) pfds[i].revents=0; ready = poll(pfds, nfds, -1); if(ready == -1) switch (errno) { case EINTR: dprintf(STDOUT_FILENO,"\nPoll interrupted, repeat.\n"); continue; default: errExit("poll"); } } while (ready <= 0); for (i = 0; i < NUM; i++) { if (pfds[i].revents != 0) { dprintf(STDOUT_FILENO," fd=%d; events: %7s%8s%9s%8s%8s ", pfds[i].fd, (pfds[i].revents & POLLIN) ? "POLLIN " : "", (pfds[i].revents & POLLHUP) ? "POLLHUP " : "", (pfds[i].revents & POLLNVAL) ? "POLLNVAL " : "", (pfds[i].revents & POLLPRI) ? "POLLPRI " : "", (pfds[i].revents & POLLERR) ? "POLLERR " : ""); if (pfds[i].revents & POLLIN) { do { rr=read(pfds[i].fd, &buf, 1); if (rr < 0) switch (errno) { case EINTR: dprintf(STDOUT_FILENO,"\nRead interrupted, repeat.\n"); continue; default: errExit("read"); } if(rr == 0 ) dprintf(STDOUT_FILENO,"\n"); else dprintf(STDOUT_FILENO,"[%2X]",buf); } while (rr > 0); } else if ((pfds[i].revents & (POLLHUP | ~POLLIN))) { dprintf(STDOUT_FILENO,"Closing [%d], pipefd %d, pfds.fd %d\n",i,pipefd[i][0],pfds[i].fd); if (close(pfds[i].fd) < 0 ) errExit("close pfds"); pfds[i].fd = -1; pfds[i].events=0; num_open_fds--; dprintf(STDOUT_FILENO,"Still opened fds %d\n",num_open_fds); } } } } printf("Niggers"); while (1) pause; return EXIT_SUCCESS; }