tst-pselect.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <sys/types.h>
  8. #include <sys/select.h>
  9. // our SIGALRM handler
  10. void handler(int signum) {
  11. (void)signum;
  12. puts("got signal\n");
  13. }
  14. static int
  15. do_test (void)
  16. {
  17. int rc;
  18. sigset_t wait_mask, mask_sigchld;
  19. struct sigaction act;
  20. // block SIGALRM. We want to handle it only when we're ready
  21. sigemptyset(&mask_sigchld);
  22. sigaddset(&mask_sigchld, SIGALRM);
  23. sigprocmask(SIG_BLOCK, &mask_sigchld, &wait_mask);
  24. sigdelset(&wait_mask, SIGALRM);
  25. // register a signal handler so we can see when the signal arrives
  26. memset(&act, 0, sizeof(act));
  27. sigemptyset(&act.sa_mask); // just in case an empty set isn't all 0's (total paranoia)
  28. act.sa_handler = handler;
  29. sigaction(SIGALRM, &act, NULL);
  30. // send ourselves a SIGARLM. It will pend until we unblock that signal in pselect()
  31. printf("sending ourselves a signal\n");
  32. kill(getpid(), SIGALRM);
  33. printf("signal is pending; calling pselect()\n");
  34. rc = pselect(0, NULL, NULL, NULL, NULL, &wait_mask);
  35. if (rc != -1 || errno != EINTR) {
  36. int e = errno;
  37. printf("pselect() returned %d, errno %d (%s)\n", rc, e, strerror(e));
  38. exit(1);
  39. }
  40. return 0;
  41. }
  42. #define TEST_FUNCTION do_test ()
  43. #include <test-skeleton.c>