chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / posix / tst-vfork1.c
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
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.
10
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.
15
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.  */
20
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>
27
28 /* This test relies on non-POSIX functionality since the child
29    processes call write and getpid.  */
30 static int
31 do_test (void)
32 {
33   int result = 0;
34   int fd[2];
35
36   if (pipe (fd) == -1)
37     {
38       puts ("pipe failed");
39       return 1;
40     }
41
42   /* First vfork() without previous getpid().  */
43   pid_t p1;
44   if ((p1 = vfork ()) == 0)
45     {
46       pid_t p = getpid ();
47       _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
48     }
49   else if (p1 == -1)
50     {
51       puts ("1st vfork failed");
52       result = 1;
53     }
54
55   pid_t p2 = 0;
56   if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
57     {
58       puts ("1st read failed");
59       result = 1;
60     }
61   int r;
62   if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
63     {
64       puts ("1st waitpid failed");
65       result = 1;
66     }
67   else if (r != 0)
68     {
69       puts ("write in 1st child failed");
70       result = 1;
71     }
72
73   /* Main process' ID.  */
74   pid_t p0 = getpid ();
75
76   /* vfork() again, but after a getpid() in the main process.  */
77   pid_t p3;
78   if ((p3 = vfork ()) == 0)
79     {
80       pid_t p = getpid ();
81       _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
82     }
83   else if (p1 == -1)
84     {
85       puts ("2nd vfork failed");
86       result = 1;
87     }
88
89   pid_t p4;
90   if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
91     {
92       puts ("2nd read failed");
93       result = 1;
94     }
95   if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
96     {
97       puts ("2nd waitpid failed");
98       result = 1;
99     }
100   else if (r != 0)
101     {
102       puts ("write in 2nd child failed");
103       result = 1;
104     }
105
106   /* And getpid in the main process again.  */
107   pid_t p5 = getpid ();
108
109   /* Analysis of the results.  */
110   if (p0 != p5)
111     {
112       printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
113       result = 1;
114     }
115
116   if (p0 == p1)
117     {
118       printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
119       result = 1;
120     }
121
122   if (p1 != p2)
123     {
124       printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
125       result = 1;
126     }
127
128   if (p0 == p3)
129     {
130       printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
131       result = 1;
132     }
133
134   if (p3 != p4)
135     {
136       printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
137       result = 1;
138     }
139
140   close (fd[0]);
141   close (fd[1]);
142
143   if (result == 0)
144     puts ("All OK");
145
146   return result;
147 }
148
149 #define TEST_FUNCTION do_test ()
150 #include "../test-skeleton.c"