tst-vfork1.c 3.4 KB

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