tst-signal6.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <pthread.h>
  17. #include <signal.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #define N 2
  23. static pthread_barrier_t bar;
  24. static struct
  25. {
  26. void *p;
  27. pthread_t s;
  28. } ti[N];
  29. static int sig1;
  30. static void
  31. handler (int sig)
  32. {
  33. pthread_t self = pthread_self ();
  34. size_t i;
  35. for (i = 0; i < N; ++i)
  36. if (ti[i].s == self)
  37. {
  38. if ((uintptr_t) ti[i].p <= (uintptr_t) &self
  39. && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self)
  40. {
  41. puts ("alt stack not used");
  42. exit (1);
  43. }
  44. printf ("thread %zu used alt stack for signal %d\n", i, sig);
  45. return;
  46. }
  47. puts ("handler: thread not found");
  48. exit (1);
  49. }
  50. static void *
  51. tf (void *arg)
  52. {
  53. size_t nr = (uintptr_t) arg;
  54. if (nr >= N)
  55. {
  56. puts ("wrong nr parameter");
  57. exit (1);
  58. }
  59. sigset_t ss;
  60. sigemptyset (&ss);
  61. size_t i;
  62. for (i = 0; i < N; ++i)
  63. if (i != nr)
  64. if (sigaddset (&ss, sig1 + i) != 0)
  65. {
  66. puts ("tf: sigaddset failed");
  67. exit (1);
  68. }
  69. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  70. {
  71. puts ("tf: sigmask failed");
  72. exit (1);
  73. }
  74. void *p = malloc (2 * MINSIGSTKSZ);
  75. if (p == NULL)
  76. {
  77. puts ("tf: malloc failed");
  78. exit (1);
  79. }
  80. stack_t s;
  81. s.ss_sp = p;
  82. s.ss_size = 2 * MINSIGSTKSZ;
  83. s.ss_flags = 0;
  84. if (sigaltstack (&s, NULL) != 0)
  85. {
  86. puts ("tf: sigaltstack failed");
  87. exit (1);
  88. }
  89. ti[nr].p = p;
  90. ti[nr].s = pthread_self ();
  91. pthread_barrier_wait (&bar);
  92. pthread_barrier_wait (&bar);
  93. return NULL;
  94. }
  95. static int
  96. do_test (void)
  97. {
  98. sig1 = SIGRTMIN;
  99. if (sig1 + N > SIGRTMAX)
  100. {
  101. puts ("too few RT signals");
  102. return 0;
  103. }
  104. struct sigaction sa;
  105. sa.sa_handler = handler;
  106. sa.sa_flags = 0;
  107. sigemptyset (&sa.sa_mask);
  108. if (sigaction (sig1, &sa, NULL) != 0
  109. || sigaction (sig1 + 1, &sa, NULL) != 0
  110. || sigaction (sig1 + 2, &sa, NULL) != 0)
  111. {
  112. puts ("sigaction failed");
  113. return 1;
  114. }
  115. if (pthread_barrier_init (&bar, NULL, 1 + N) != 0)
  116. {
  117. puts ("barrier_init failed");
  118. return 1;
  119. }
  120. pthread_t th[N];
  121. size_t i;
  122. for (i = 0; i < N; ++i)
  123. if (pthread_create (&th[i], NULL, tf, (void *) (long int) i) != 0)
  124. {
  125. puts ("create failed");
  126. return 1;
  127. }
  128. /* Block the three signals. */
  129. sigset_t ss;
  130. sigemptyset (&ss);
  131. for (i = 0; i <= N; ++i)
  132. sigaddset (&ss, sig1 + i);
  133. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  134. {
  135. puts ("main: sigmask failed");
  136. return 1;
  137. }
  138. pthread_barrier_wait (&bar);
  139. /* Send some signals. */
  140. pid_t me = getpid ();
  141. kill (me, sig1 + N);
  142. for (i = 0; i < N; ++i)
  143. kill (me, sig1 + i);
  144. kill (me, sig1 + N);
  145. /* Give the signals a chance to be worked on. */
  146. sleep (1);
  147. pthread_barrier_wait (&bar);
  148. for (i = 0; i < N; ++i)
  149. if (pthread_join (th[i], NULL) != 0)
  150. {
  151. puts ("join failed");
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. #define TEST_FUNCTION do_test ()
  157. #include "../test-skeleton.c"