tst-signal6.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <pthread.h>
  16. #include <signal.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #define N 2
  22. static pthread_barrier_t bar;
  23. static struct
  24. {
  25. void *p;
  26. pthread_t s;
  27. } ti[N];
  28. static int sig1;
  29. static void
  30. handler (int sig)
  31. {
  32. pthread_t self = pthread_self ();
  33. size_t i;
  34. for (i = 0; i < N; ++i)
  35. if (ti[i].s == self)
  36. {
  37. if ((uintptr_t) ti[i].p <= (uintptr_t) &self
  38. && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self)
  39. {
  40. puts ("alt stack not used");
  41. exit (1);
  42. }
  43. printf ("thread %zu used alt stack for signal %d\n", i, sig);
  44. return;
  45. }
  46. puts ("handler: thread not found");
  47. exit (1);
  48. }
  49. static void *
  50. tf (void *arg)
  51. {
  52. size_t nr = (uintptr_t) arg;
  53. if (nr >= N)
  54. {
  55. puts ("wrong nr parameter");
  56. exit (1);
  57. }
  58. sigset_t ss;
  59. sigemptyset (&ss);
  60. size_t i;
  61. for (i = 0; i < N; ++i)
  62. if (i != nr)
  63. if (sigaddset (&ss, sig1 + i) != 0)
  64. {
  65. puts ("tf: sigaddset failed");
  66. exit (1);
  67. }
  68. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  69. {
  70. puts ("tf: sigmask failed");
  71. exit (1);
  72. }
  73. void *p = malloc (2 * MINSIGSTKSZ);
  74. if (p == NULL)
  75. {
  76. puts ("tf: malloc failed");
  77. exit (1);
  78. }
  79. stack_t s;
  80. s.ss_sp = p;
  81. s.ss_size = 2 * MINSIGSTKSZ;
  82. s.ss_flags = 0;
  83. if (sigaltstack (&s, NULL) != 0)
  84. {
  85. puts ("tf: sigaltstack failed");
  86. exit (1);
  87. }
  88. ti[nr].p = p;
  89. ti[nr].s = pthread_self ();
  90. pthread_barrier_wait (&bar);
  91. pthread_barrier_wait (&bar);
  92. return NULL;
  93. }
  94. static int
  95. do_test (void)
  96. {
  97. sig1 = SIGRTMIN;
  98. if (sig1 + N > SIGRTMAX)
  99. {
  100. puts ("too few RT signals");
  101. return 0;
  102. }
  103. struct sigaction sa;
  104. sa.sa_handler = handler;
  105. sa.sa_flags = 0;
  106. sigemptyset (&sa.sa_mask);
  107. if (sigaction (sig1, &sa, NULL) != 0
  108. || sigaction (sig1 + 1, &sa, NULL) != 0
  109. || sigaction (sig1 + 2, &sa, NULL) != 0)
  110. {
  111. puts ("sigaction failed");
  112. return 1;
  113. }
  114. if (pthread_barrier_init (&bar, NULL, 1 + N) != 0)
  115. {
  116. puts ("barrier_init failed");
  117. return 1;
  118. }
  119. pthread_t th[N];
  120. size_t i;
  121. for (i = 0; i < N; ++i)
  122. if (pthread_create (&th[i], NULL, tf, (void *) (long int) i) != 0)
  123. {
  124. puts ("create failed");
  125. return 1;
  126. }
  127. /* Block the three signals. */
  128. sigset_t ss;
  129. sigemptyset (&ss);
  130. for (i = 0; i <= N; ++i)
  131. sigaddset (&ss, sig1 + i);
  132. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  133. {
  134. puts ("main: sigmask failed");
  135. return 1;
  136. }
  137. pthread_barrier_wait (&bar);
  138. /* Send some signals. */
  139. pid_t me = getpid ();
  140. kill (me, sig1 + N);
  141. for (i = 0; i < N; ++i)
  142. kill (me, sig1 + i);
  143. kill (me, sig1 + N);
  144. /* Give the signals a chance to be worked on. */
  145. sleep (1);
  146. pthread_barrier_wait (&bar);
  147. for (i = 0; i < N; ++i)
  148. if (pthread_join (th[i], NULL) != 0)
  149. {
  150. puts ("join failed");
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. #define TEST_FUNCTION do_test ()
  156. #include "../test-skeleton.c"