tst-setcontext2.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* Testcase checks, if setcontext(), swapcontext() restores signal-mask
  2. and if pending signals are delivered after those calls.
  3. Copyright (C) 2015-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <signal.h>
  20. #include <ucontext.h>
  21. #include <unistd.h>
  22. volatile int global;
  23. volatile sig_atomic_t handlerCalled;
  24. static void
  25. check (const char *funcName)
  26. {
  27. sigset_t set;
  28. /* check if SIGUSR2 is unblocked after setcontext-call. */
  29. sigprocmask (SIG_BLOCK, NULL, &set);
  30. if (sigismember (&set, SIGUSR2) != 0)
  31. {
  32. printf ("FAIL: SIGUSR2 is blocked after %s.\n", funcName);
  33. exit (1);
  34. }
  35. if (sigismember (&set, SIGUSR1) != 1)
  36. {
  37. printf ("FAIL: SIGUSR1 is not blocked after %s.\n", funcName);
  38. exit (1);
  39. }
  40. }
  41. static void
  42. signalmask (int how, int signum)
  43. {
  44. sigset_t set;
  45. sigemptyset (&set);
  46. sigaddset (&set, signum);
  47. if (sigprocmask (how, &set, NULL) != 0)
  48. {
  49. printf ("FAIL: sigprocmaks (%d, %d, NULL): %m\n", how, signum);
  50. exit (1);
  51. }
  52. }
  53. static void
  54. signalpending (int signum, const char *msg)
  55. {
  56. sigset_t set;
  57. sigemptyset (&set);
  58. if (sigpending (&set) != 0)
  59. {
  60. printf ("FAIL: sigpending: %m\n");
  61. exit (1);
  62. }
  63. if (sigismember (&set, SIGUSR2) != 1)
  64. {
  65. printf ("FAIL: Signal %d is not pending %s\n", signum, msg);
  66. exit (1);
  67. }
  68. }
  69. static void
  70. handler (int __attribute__ ((unused)) signum)
  71. {
  72. handlerCalled ++;
  73. }
  74. static int
  75. do_test (void)
  76. {
  77. ucontext_t ctx, oldctx;
  78. struct sigaction action;
  79. pid_t pid;
  80. pid = getpid ();
  81. /* unblock SIGUSR2 */
  82. signalmask (SIG_UNBLOCK, SIGUSR2);
  83. /* block SIGUSR1 */
  84. signalmask (SIG_BLOCK, SIGUSR1);
  85. /* register handler for SIGUSR2 */
  86. action.sa_flags = 0;
  87. action.sa_handler = handler;
  88. sigemptyset (&action.sa_mask);
  89. sigaction (SIGUSR2, &action, NULL);
  90. if (getcontext (&ctx) != 0)
  91. {
  92. printf ("FAIL: getcontext: %m\n");
  93. exit (1);
  94. }
  95. global++;
  96. if (global == 1)
  97. {
  98. puts ("after getcontext");
  99. /* block SIGUSR2 */
  100. signalmask (SIG_BLOCK, SIGUSR2);
  101. /* send SIGUSR2 to me */
  102. handlerCalled = 0;
  103. kill (pid, SIGUSR2);
  104. /* was SIGUSR2 handler called? */
  105. if (handlerCalled != 0)
  106. {
  107. puts ("FAIL: signal handler was called, but signal was blocked.");
  108. exit (1);
  109. }
  110. /* is SIGUSR2 pending? */
  111. signalpending (SIGUSR2, "before setcontext");
  112. /* SIGUSR2 will be unblocked by setcontext-call. */
  113. if (setcontext (&ctx) != 0)
  114. {
  115. printf ("FAIL: setcontext: %m\n");
  116. exit (1);
  117. }
  118. }
  119. else if (global == 2)
  120. {
  121. puts ("after setcontext");
  122. /* check SIGUSR1/2 */
  123. check ("setcontext");
  124. /* was SIGUSR2 handler called? */
  125. if (handlerCalled != 1)
  126. {
  127. puts ("FAIL: signal handler was not called after setcontext.");
  128. exit (1);
  129. }
  130. /* block SIGUSR2 */
  131. signalmask (SIG_BLOCK, SIGUSR2);
  132. /* send SIGUSR2 to me */
  133. handlerCalled = 0;
  134. kill (pid, SIGUSR2);
  135. /* was SIGUSR2 handler called? */
  136. if (handlerCalled != 0)
  137. {
  138. puts ("FAIL: signal handler was called, but signal was blocked.");
  139. exit (1);
  140. }
  141. /* is SIGUSR2 pending? */
  142. signalpending (SIGUSR2, "before swapcontext");
  143. if (swapcontext (&oldctx, &ctx) != 0)
  144. {
  145. printf ("FAIL: swapcontext: %m\n");
  146. exit (1);
  147. }
  148. puts ("after returned from swapcontext");
  149. if (global != 3)
  150. {
  151. puts ("FAIL: returned from swapcontext without ctx-context called.");
  152. exit (1);
  153. }
  154. puts ("test succeeded");
  155. return 0;
  156. }
  157. else if ( global != 3 )
  158. {
  159. puts ("FAIL: 'global' not incremented three times");
  160. exit (1);
  161. }
  162. puts ("after swapcontext");
  163. /* check SIGUSR1/2 */
  164. check ("swapcontext");
  165. /* was SIGUSR2 handler called? */
  166. if (handlerCalled != 1)
  167. {
  168. puts ("FAIL: signal handler was not called after swapcontext.");
  169. exit (1);
  170. }
  171. /* check sigmask in old context of swapcontext-call */
  172. if (sigismember (&oldctx.uc_sigmask, SIGUSR2) != 1)
  173. {
  174. puts ("FAIL: SIGUSR2 is not blocked in oldctx.uc_sigmask.");
  175. exit (1);
  176. }
  177. if (sigismember (&oldctx.uc_sigmask, SIGUSR1) != 1)
  178. {
  179. puts ("FAIL: SIGUSR1 is not blocked in oldctx.uc_sigmaks.");
  180. exit (1);
  181. }
  182. /* change to old context, which was gathered by swapcontext() call. */
  183. setcontext (&oldctx);
  184. puts ("FAIL: returned from setcontext (&oldctx)");
  185. exit (1);
  186. }
  187. #define TEST_FUNCTION do_test ()
  188. #include "../test-skeleton.c"