tst-signal1.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <errno.h>
  16. #include <pthread.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/mman.h>
  23. #include <sys/wait.h>
  24. #include "../test-skeleton.h"
  25. #ifdef __ARCH_USE_MMU__
  26. static sigset_t ss;
  27. static pthread_barrier_t *b;
  28. static void *
  29. tf (void *arg)
  30. {
  31. sigdelset (&ss, SIGINT);
  32. if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
  33. {
  34. puts ("2nd pthread_sigmask failed");
  35. exit (1);
  36. }
  37. pthread_barrier_wait (b);
  38. int sig;
  39. int res = sigwait (&ss, &sig);
  40. if (res == 0)
  41. {
  42. printf ("sigwait returned successfully with signal %d\n", sig);
  43. exit (1);
  44. }
  45. printf ("sigwait returned with %s (%d)\n", strerror (res), res);
  46. return NULL;
  47. }
  48. static void
  49. receiver (void)
  50. {
  51. pthread_t th;
  52. /* Make sure the process doesn't run forever. */
  53. alarm (10);
  54. sigfillset (&ss);
  55. if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
  56. {
  57. puts ("1st pthread_sigmask failed");
  58. exit (1);
  59. }
  60. if (pthread_create (&th, NULL, tf, NULL) != 0)
  61. {
  62. puts ("pthread_create failed");
  63. exit (1);
  64. }
  65. if (pthread_join (th, NULL) == 0)
  66. {
  67. puts ("thread joined?!");
  68. exit (1);
  69. }
  70. _exit (0);
  71. }
  72. static int
  73. do_test (void)
  74. {
  75. char tmp[] = "/tmp/tst-signal1-XXXXXX";
  76. int fd = mkstemp (tmp);
  77. if (fd == -1)
  78. {
  79. puts ("mkstemp failed");
  80. exit (1);
  81. }
  82. unlink (tmp);
  83. int i;
  84. for (i = 0; i < 20; ++i)
  85. write (fd, "foobar xyzzy", 12);
  86. b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
  87. MAP_SHARED, fd, 0);
  88. if (b == MAP_FAILED)
  89. {
  90. puts ("mmap failed");
  91. exit (1);
  92. }
  93. pthread_barrierattr_t ba;
  94. if (pthread_barrierattr_init (&ba) != 0)
  95. {
  96. puts ("barrierattr_init failed");
  97. exit (1);
  98. }
  99. if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
  100. {
  101. puts ("barrierattr_setpshared failed");
  102. exit (1);
  103. }
  104. if (pthread_barrier_init (b, &ba, 2) != 0)
  105. {
  106. puts ("barrier_init failed");
  107. exit (1);
  108. }
  109. if (pthread_barrierattr_destroy (&ba) != 0)
  110. {
  111. puts ("barrierattr_destroy failed");
  112. exit (1);
  113. }
  114. pid_t pid = fork ();
  115. if (pid == -1)
  116. {
  117. puts ("fork failed");
  118. exit (1);
  119. }
  120. if (pid == 0)
  121. receiver ();
  122. pthread_barrier_wait (b);
  123. /* Wait a bit more. */
  124. struct timespec ts = { .tv_sec = 0, .tv_nsec = 10000000 };
  125. nanosleep (&ts, NULL);
  126. /* Send the signal. */
  127. puts ("sending the signal now");
  128. kill (pid, SIGINT);
  129. /* Wait for the process to terminate. */
  130. int status;
  131. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  132. {
  133. puts ("wrong child reported terminated");
  134. exit (1);
  135. }
  136. if (!WIFSIGNALED (status))
  137. {
  138. puts ("child wasn't signalled");
  139. exit (1);
  140. }
  141. if (WTERMSIG (status) != SIGINT)
  142. {
  143. puts ("child not terminated with SIGINT");
  144. exit (1);
  145. }
  146. return 0;
  147. }
  148. #define TEST_FUNCTION do_test ()
  149. #include "../test-skeleton.c"
  150. #else
  151. int main(void)
  152. {
  153. printf("Skipping test on non-mmu host!\n");
  154. return 23;
  155. }
  156. #endif