tst-vfork1.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. /* This test relies on non-POSIX functionality since the child
  24. processes call write and getpid. */
  25. static int
  26. do_test (void)
  27. {
  28. int result = 0;
  29. int fd[2];
  30. if (pipe (fd) == -1)
  31. {
  32. puts ("pipe failed");
  33. return 1;
  34. }
  35. /* First vfork() without previous getpid(). */
  36. pid_t p1;
  37. if ((p1 = vfork ()) == 0)
  38. {
  39. pid_t p = getpid ();
  40. _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
  41. }
  42. else if (p1 == -1)
  43. {
  44. puts ("1st vfork failed");
  45. result = 1;
  46. }
  47. pid_t p2 = 0;
  48. if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
  49. {
  50. puts ("1st read failed");
  51. result = 1;
  52. }
  53. int r;
  54. if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
  55. {
  56. puts ("1st waitpid failed");
  57. result = 1;
  58. }
  59. else if (r != 0)
  60. {
  61. puts ("write in 1st child failed");
  62. result = 1;
  63. }
  64. /* Main process' ID. */
  65. pid_t p0 = getpid ();
  66. /* vfork() again, but after a getpid() in the main process. */
  67. pid_t p3;
  68. if ((p3 = vfork ()) == 0)
  69. {
  70. pid_t p = getpid ();
  71. _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
  72. }
  73. else if (p1 == -1)
  74. {
  75. puts ("2nd vfork failed");
  76. result = 1;
  77. }
  78. pid_t p4;
  79. if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
  80. {
  81. puts ("2nd read failed");
  82. result = 1;
  83. }
  84. if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
  85. {
  86. puts ("2nd waitpid failed");
  87. result = 1;
  88. }
  89. else if (r != 0)
  90. {
  91. puts ("write in 2nd child failed");
  92. result = 1;
  93. }
  94. /* And getpid in the main process again. */
  95. pid_t p5 = getpid ();
  96. /* Analysis of the results. */
  97. if (p0 != p5)
  98. {
  99. printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
  100. result = 1;
  101. }
  102. if (p0 == p1)
  103. {
  104. printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
  105. result = 1;
  106. }
  107. if (p1 != p2)
  108. {
  109. printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
  110. result = 1;
  111. }
  112. if (p0 == p3)
  113. {
  114. printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
  115. result = 1;
  116. }
  117. if (p3 != p4)
  118. {
  119. printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
  120. result = 1;
  121. }
  122. close (fd[0]);
  123. close (fd[1]);
  124. if (result == 0)
  125. puts ("All OK");
  126. return result;
  127. }
  128. #define TEST_FUNCTION do_test ()
  129. #include "../test-skeleton.c"