tst-sigset2.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* sigset_SIG_HOLD_bug.c [BZ #1951] */
  2. #include <errno.h>
  3. #include <error.h>
  4. #include <inttypes.h>
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #define TEST_SIG SIGINT
  13. /* Print mask of blocked signals for this process */
  14. static void
  15. printSigMask (const char *msg)
  16. {
  17. sigset_t currMask;
  18. int sig;
  19. int cnt;
  20. if (msg != NULL)
  21. printf ("%s", msg);
  22. if (sigprocmask (SIG_BLOCK, NULL, &currMask) == -1)
  23. error (1, errno, "sigaction");
  24. cnt = 0;
  25. for (sig = 1; sig < NSIG; sig++)
  26. {
  27. if (sigismember (&currMask, sig))
  28. {
  29. cnt++;
  30. printf ("\t\t%d (%s)\n", sig, strsignal (sig));
  31. }
  32. }
  33. if (cnt == 0)
  34. printf ("\t\t<empty signal set>\n");
  35. } /* printSigMask */
  36. static void
  37. handler (int sig)
  38. {
  39. printf ("Caught signal %d\n", sig);
  40. printSigMask ("Signal mask in handler\n");
  41. printf ("Handler returning\n");
  42. _exit (1);
  43. } /* handler */
  44. static void
  45. printDisposition (sighandler_t disp)
  46. {
  47. if (disp == SIG_HOLD)
  48. printf ("SIG_HOLD");
  49. else if (disp == SIG_DFL)
  50. printf ("SIG_DFL");
  51. else if (disp == SIG_IGN)
  52. printf ("SIG_IGN");
  53. else
  54. printf ("handled at %" PRIxPTR, (uintptr_t) disp);
  55. } /* printDisposition */
  56. static int
  57. returnTest1 (void)
  58. {
  59. sighandler_t prev;
  60. printf ("===== TEST 1 =====\n");
  61. printf ("Blocking signal with sighold()\n");
  62. if (sighold (TEST_SIG) == -1)
  63. error (1, errno, "sighold");
  64. printSigMask ("Signal mask after sighold()\n");
  65. printf ("About to use sigset() to establish handler\n");
  66. prev = sigset (TEST_SIG, handler);
  67. if (prev == SIG_ERR)
  68. error(1, errno, "sigset");
  69. printf ("Previous disposition: ");
  70. printDisposition (prev);
  71. printf (" (should be SIG_HOLD)\n");
  72. if (prev != SIG_HOLD)
  73. {
  74. printf("TEST FAILED!!!\n");
  75. return 1;
  76. }
  77. return 0;
  78. } /* returnTest1 */
  79. static int
  80. returnTest2 (void)
  81. {
  82. sighandler_t prev;
  83. printf ("\n===== TEST 2 =====\n");
  84. printf ("About to use sigset() to set SIG_HOLD\n");
  85. prev = sigset (TEST_SIG, SIG_HOLD);
  86. if (prev == SIG_ERR)
  87. error (1, errno, "sigset");
  88. printf ("Previous disposition: ");
  89. printDisposition (prev);
  90. printf (" (should be SIG_DFL)\n");
  91. if (prev != SIG_DFL)
  92. {
  93. printf("TEST FAILED!!!\n");
  94. return 1;
  95. }
  96. return 0;
  97. } /* returnTest2 */
  98. static int
  99. returnTest3 (void)
  100. {
  101. sighandler_t prev;
  102. printf ("\n===== TEST 3 =====\n");
  103. printf ("About to use sigset() to set SIG_HOLD\n");
  104. prev = sigset (TEST_SIG, SIG_HOLD);
  105. if (prev == SIG_ERR)
  106. error (1, errno, "sigset");
  107. printf ("About to use sigset() to set SIG_HOLD (again)\n");
  108. prev = sigset (TEST_SIG, SIG_HOLD);
  109. if (prev == SIG_ERR)
  110. error (1, errno, "sigset");
  111. printf ("Previous disposition: ");
  112. printDisposition (prev);
  113. printf (" (should be SIG_HOLD)\n");
  114. if (prev != SIG_HOLD)
  115. {
  116. printf("TEST FAILED!!!\n");
  117. return 1;
  118. }
  119. return 0;
  120. } /* returnTest3 */
  121. int
  122. main (int argc, char *argv[])
  123. {
  124. pid_t childPid;
  125. childPid = fork();
  126. if (childPid == -1)
  127. error (1, errno, "fork");
  128. if (childPid == 0)
  129. exit (returnTest1 ());
  130. int status;
  131. if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
  132. error (1, errno, "waitpid");
  133. int result = !WIFEXITED (status) || WEXITSTATUS (status) != 0;
  134. childPid = fork();
  135. if (childPid == -1)
  136. error (1, errno, "fork");
  137. if (childPid == 0)
  138. exit (returnTest2 ());
  139. if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
  140. error (1, errno, "waitpid");
  141. result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
  142. childPid = fork();
  143. if (childPid == -1)
  144. error (1, errno, "fork");
  145. if (childPid == 0)
  146. exit (returnTest3 ());
  147. if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
  148. error (1, errno, "waitpid");
  149. result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;
  150. return result;
  151. } /* main */