tst-vfork2x.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. int raise_fail;
  27. static void
  28. alrm (int sig)
  29. {
  30. if (raise (SIGUSR1) < 0)
  31. raise_fail = 1;
  32. }
  33. /* This test relies on non-POSIX functionality since the child
  34. processes call write, nanosleep and getpid. */
  35. static int
  36. do_test (void)
  37. {
  38. int result = 0;
  39. int fd[2];
  40. signal (SIGUSR1, SIG_IGN);
  41. struct sigaction sa;
  42. sa.sa_handler = alrm;
  43. sigemptyset (&sa.sa_mask);
  44. sa.sa_flags = 0;
  45. if (sigaction (SIGALRM, &sa, NULL) < 0)
  46. {
  47. puts ("couldn't set up SIGALRM handler");
  48. return 1;
  49. }
  50. if (pipe (fd) == -1)
  51. {
  52. puts ("pipe failed");
  53. return 1;
  54. }
  55. struct itimerval it;
  56. it.it_value.tv_sec = 0;
  57. it.it_value.tv_usec = 200 * 1000;
  58. it.it_interval = it.it_value;
  59. if (setitimer (ITIMER_REAL, &it, NULL) < 0)
  60. {
  61. puts ("couldn't set up timer");
  62. return 1;
  63. }
  64. /* First vfork() without previous getpid(). */
  65. pid_t p1;
  66. if ((p1 = vfork ()) == 0)
  67. {
  68. pid_t p = getpid ();
  69. struct timespec ts;
  70. ts.tv_sec = 1;
  71. ts.tv_nsec = 0;
  72. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  73. _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
  74. }
  75. else if (p1 == -1)
  76. {
  77. puts ("1st vfork failed");
  78. result = 1;
  79. }
  80. memset (&it, 0, sizeof (it));
  81. setitimer (ITIMER_REAL, &it, NULL);
  82. pid_t p2 = 0;
  83. if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
  84. {
  85. puts ("1st read failed");
  86. result = 1;
  87. }
  88. int r;
  89. if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
  90. {
  91. puts ("1st waitpid failed");
  92. result = 1;
  93. }
  94. else if (r != 0)
  95. {
  96. puts ("write in 1st child failed");
  97. result = 1;
  98. }
  99. /* Main process' ID. */
  100. pid_t p0 = getpid ();
  101. /* vfork() again, but after a getpid() in the main process. */
  102. pid_t p3;
  103. if ((p3 = vfork ()) == 0)
  104. {
  105. pid_t p = getpid ();
  106. _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
  107. }
  108. else if (p1 == -1)
  109. {
  110. puts ("2nd vfork failed");
  111. result = 1;
  112. }
  113. pid_t p4;
  114. if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
  115. {
  116. puts ("2nd read failed");
  117. result = 1;
  118. }
  119. if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
  120. {
  121. puts ("2nd waitpid failed");
  122. result = 1;
  123. }
  124. else if (r != 0)
  125. {
  126. puts ("write in 2nd child failed");
  127. result = 1;
  128. }
  129. /* And getpid in the main process again. */
  130. pid_t p5 = getpid ();
  131. /* Analysis of the results. */
  132. if (p0 != p5)
  133. {
  134. printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
  135. result = 1;
  136. }
  137. if (p0 == p1)
  138. {
  139. printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
  140. result = 1;
  141. }
  142. if (p1 != p2)
  143. {
  144. printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
  145. result = 1;
  146. }
  147. if (p0 == p3)
  148. {
  149. printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
  150. result = 1;
  151. }
  152. if (p3 != p4)
  153. {
  154. printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
  155. result = 1;
  156. }
  157. close (fd[0]);
  158. close (fd[1]);
  159. if (raise_fail)
  160. {
  161. puts ("raise failed");
  162. result = 1;
  163. }
  164. if (result == 0)
  165. puts ("All OK");
  166. return result;
  167. }
  168. #define TEST_FUNCTION do_test ()
  169. #include "../test-skeleton.c"