tst-signal2.c 3.9 KB

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