Hi, I want to assess the safety and portability of the following trick, for getting outside poll. Replacing by using dup2 a read poll object by a writtable one. I think to use this for stopping polling in multithread program It work really well, and could for instance allow me to tear down eventfd of other event like file that does not support shutdown
CC also Michael Kerrisk (mtk.manpa...@gmail.com) for comments on this trick #include <errno.h> #include <poll.h> #include <pthread.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <unistd.h> static void *run_poll (void *arg) { struct pollfd fds[1]; int rc; fds[0].fd = ((int *)arg)[0]; fds[0].events = POLLERR | POLLHUP | POLLNVAL | POLLIN; // // Poll indefinitely // printf("starting poll\n"); fflush(stdout); rc = poll((struct pollfd *) &fds, 1, -1); if ( rc == -1 ) { printf("POLL returned error %d: %s\n", errno, strerror(errno)); } else { printf("POLL returned %d (revents = 0x%08x): %s %s %s %s %s\n", rc, fds[0].revents, ( ( fds[0].revents & POLLIN ) ? "pollin" : "noin" ), ( ( fds[0].revents & POLLOUT ) ? "pollout" : "noout" ), ( ( fds[0].revents & POLLERR ) ? "pollerr" : "noerr" ), ( ( fds[0].revents & POLLHUP ) ? "pollhup" : "nohup" ), ( ( fds[0].revents & POLLNVAL ) ? "pollnval" : "nonval" )); } return NULL; } int main (int argc, char **argv) { pthread_t thread; int rc; int fdst[2]; pipe(fdst); rc = pthread_create(&thread, NULL, run_poll, &fdst); sleep(3); printf("removing access\n"); dup2(fdst[0],fdst[1]); sleep(3); return 0; }
signature.asc
Description: This is a digitally signed message part.