Fix typo in comment
[glibc.git] / posix / tst-vfork2.c
blobc6a26e4797539ba7a8800b9999ef8a6b8cde593f
1 /* Test for vfork functions.
2 Copyright (C) 2004-2013 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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
31 int raise_fail;
33 static void
34 alrm (int sig)
36 if (raise (SIGUSR1) < 0)
37 raise_fail = 1;
40 /* This test relies on non-POSIX functionality since the child
41 processes call write, nanosleep and getpid. */
42 static int
43 do_test (void)
45 int result = 0;
46 int fd[2];
48 signal (SIGUSR1, SIG_IGN);
50 struct sigaction sa;
51 sa.sa_handler = alrm;
52 sigemptyset (&sa.sa_mask);
53 sa.sa_flags = 0;
54 if (sigaction (SIGALRM, &sa, NULL) < 0)
56 puts ("couldn't set up SIGALRM handler");
57 return 1;
60 if (pipe (fd) == -1)
62 puts ("pipe failed");
63 return 1;
66 struct itimerval it;
67 it.it_value.tv_sec = 0;
68 it.it_value.tv_usec = 200 * 1000;
69 it.it_interval = it.it_value;
70 if (setitimer (ITIMER_REAL, &it, NULL) < 0)
72 puts ("couldn't set up timer");
73 return 1;
76 /* First vfork() without previous getpid(). */
77 pid_t p1;
78 if ((p1 = vfork ()) == 0)
80 pid_t p = getpid ();
82 struct timespec ts;
83 ts.tv_sec = 1;
84 ts.tv_nsec = 0;
85 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
86 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
88 else if (p1 == -1)
90 puts ("1st vfork failed");
91 result = 1;
94 memset (&it, 0, sizeof (it));
95 setitimer (ITIMER_REAL, &it, NULL);
97 pid_t p2 = 0;
98 if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
100 puts ("1st read failed");
101 result = 1;
103 int r;
104 if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
106 puts ("1st waitpid failed");
107 result = 1;
109 else if (r != 0)
111 puts ("write in 1st child failed");
112 result = 1;
115 /* Main process' ID. */
116 pid_t p0 = getpid ();
118 /* vfork() again, but after a getpid() in the main process. */
119 pid_t p3;
120 if ((p3 = vfork ()) == 0)
122 pid_t p = getpid ();
123 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
125 else if (p1 == -1)
127 puts ("2nd vfork failed");
128 result = 1;
131 pid_t p4;
132 if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
134 puts ("2nd read failed");
135 result = 1;
137 if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
139 puts ("2nd waitpid failed");
140 result = 1;
142 else if (r != 0)
144 puts ("write in 2nd child failed");
145 result = 1;
148 /* And getpid in the main process again. */
149 pid_t p5 = getpid ();
151 /* Analysis of the results. */
152 if (p0 != p5)
154 printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
155 result = 1;
158 if (p0 == p1)
160 printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
161 result = 1;
164 if (p1 != p2)
166 printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
167 result = 1;
170 if (p0 == p3)
172 printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
173 result = 1;
176 if (p3 != p4)
178 printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
179 result = 1;
182 close (fd[0]);
183 close (fd[1]);
185 if (raise_fail)
187 puts ("raise failed");
188 result = 1;
191 if (result == 0)
192 puts ("All OK");
194 return result;
197 #define TEST_FUNCTION do_test ()
198 #include "../test-skeleton.c"