tst-vfork2x.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Test for vfork functions.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include "../test-skeleton.h"
  27. int raise_fail;
  28. static void
  29. alrm (int sig)
  30. {
  31. if (raise (SIGUSR1) < 0)
  32. raise_fail = 1;
  33. }
  34. /* This test relies on non-POSIX functionality since the child
  35. processes call write, nanosleep and getpid. */
  36. static int
  37. do_test (void)
  38. {
  39. int result = 0;
  40. int fd[2];
  41. signal (SIGUSR1, SIG_IGN);
  42. struct sigaction sa;
  43. sa.sa_handler = alrm;
  44. sigemptyset (&sa.sa_mask);
  45. sa.sa_flags = 0;
  46. if (sigaction (SIGALRM, &sa, NULL) < 0)
  47. {
  48. puts ("couldn't set up SIGALRM handler");
  49. return 1;
  50. }
  51. if (pipe (fd) == -1)
  52. {
  53. puts ("pipe failed");
  54. return 1;
  55. }
  56. struct itimerval it;
  57. it.it_value.tv_sec = 0;
  58. it.it_value.tv_usec = 200 * 1000;
  59. it.it_interval = it.it_value;
  60. if (setitimer (ITIMER_REAL, &it, NULL) < 0)
  61. {
  62. puts ("couldn't set up timer");
  63. return 1;
  64. }
  65. /* First vfork() without previous getpid(). */
  66. pid_t p1;
  67. if ((p1 = vfork ()) == 0)
  68. {
  69. pid_t p = getpid ();
  70. struct timespec ts;
  71. ts.tv_sec = 1;
  72. ts.tv_nsec = 0;
  73. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  74. _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
  75. }
  76. else if (p1 == -1)
  77. {
  78. puts ("1st vfork failed");
  79. result = 1;
  80. }
  81. memset (&it, 0, sizeof (it));
  82. setitimer (ITIMER_REAL, &it, NULL);
  83. pid_t p2 = 0;
  84. if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
  85. {
  86. puts ("1st read failed");
  87. result = 1;
  88. }
  89. int r;
  90. if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
  91. {
  92. puts ("1st waitpid failed");
  93. result = 1;
  94. }
  95. else if (r != 0)
  96. {
  97. puts ("write in 1st child failed");
  98. result = 1;
  99. }
  100. /* Main process' ID. */
  101. pid_t p0 = getpid ();
  102. /* vfork() again, but after a getpid() in the main process. */
  103. pid_t p3;
  104. if ((p3 = vfork ()) == 0)
  105. {
  106. pid_t p = getpid ();
  107. _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
  108. }
  109. else if (p1 == -1)
  110. {
  111. puts ("2nd vfork failed");
  112. result = 1;
  113. }
  114. pid_t p4;
  115. if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
  116. {
  117. puts ("2nd read failed");
  118. result = 1;
  119. }
  120. if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
  121. {
  122. puts ("2nd waitpid failed");
  123. result = 1;
  124. }
  125. else if (r != 0)
  126. {
  127. puts ("write in 2nd child failed");
  128. result = 1;
  129. }
  130. /* And getpid in the main process again. */
  131. pid_t p5 = getpid ();
  132. /* Analysis of the results. */
  133. if (p0 != p5)
  134. {
  135. printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
  136. result = 1;
  137. }
  138. if (p0 == p1)
  139. {
  140. printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
  141. result = 1;
  142. }
  143. if (p1 != p2)
  144. {
  145. printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
  146. result = 1;
  147. }
  148. if (p0 == p3)
  149. {
  150. printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
  151. result = 1;
  152. }
  153. if (p3 != p4)
  154. {
  155. printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
  156. result = 1;
  157. }
  158. close (fd[0]);
  159. close (fd[1]);
  160. if (raise_fail)
  161. {
  162. puts ("raise failed");
  163. result = 1;
  164. }
  165. if (result == 0)
  166. puts ("All OK");
  167. return result;
  168. }
  169. #define TEST_FUNCTION do_test ()
  170. #include "../test-skeleton.c"