tst-signal2.c 4.0 KB

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