Minor cleanups throughout.
[glibc/history.git] / posix / tst-vfork1.c
blob1f36f1ff2db9acdf753352abb37bd20abaecfd3b
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.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
28 /* This test relies on non-POSIX functionality since the child
29 processes call write and getpid. */
30 static int
31 do_test (void)
33 int result = 0;
34 int fd[2];
36 if (pipe (fd) == -1)
38 puts ("pipe failed");
39 return 1;
42 /* First vfork() without previous getpid(). */
43 pid_t p1;
44 if ((p1 = vfork ()) == 0)
46 pid_t p = getpid ();
47 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
49 else if (p1 == -1)
51 puts ("1st vfork failed");
52 result = 1;
55 pid_t p2 = 0;
56 if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
58 puts ("1st read failed");
59 result = 1;
61 int r;
62 if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
64 puts ("1st waitpid failed");
65 result = 1;
67 else if (r != 0)
69 puts ("write in 1st child failed");
70 result = 1;
73 /* Main process' ID. */
74 pid_t p0 = getpid ();
76 /* vfork() again, but after a getpid() in the main process. */
77 pid_t p3;
78 if ((p3 = vfork ()) == 0)
80 pid_t p = getpid ();
81 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
83 else if (p1 == -1)
85 puts ("2nd vfork failed");
86 result = 1;
89 pid_t p4;
90 if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
92 puts ("2nd read failed");
93 result = 1;
95 if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
97 puts ("2nd waitpid failed");
98 result = 1;
100 else if (r != 0)
102 puts ("write in 2nd child failed");
103 result = 1;
106 /* And getpid in the main process again. */
107 pid_t p5 = getpid ();
109 /* Analysis of the results. */
110 if (p0 != p5)
112 printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
113 result = 1;
116 if (p0 == p1)
118 printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
119 result = 1;
122 if (p1 != p2)
124 printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
125 result = 1;
128 if (p0 == p3)
130 printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
131 result = 1;
134 if (p3 != p4)
136 printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
137 result = 1;
140 close (fd[0]);
141 close (fd[1]);
143 if (result == 0)
144 puts ("All OK");
146 return result;
149 #define TEST_FUNCTION do_test ()
150 #include "../test-skeleton.c"