* debug/chk_fail.c (__chk_fail): Add a while (1) loop around
[glibc.git] / posix / tst-vfork2.c
blob1d096e14a34181f0ca0725a79da39c7ba9412e92
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 <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
32 int raise_fail;
34 static void
35 alrm (int sig)
37 if (raise (SIGUSR1) < 0)
38 raise_fail = 1;
41 /* This test relies on non-POSIX functionality since the child
42 processes call write, nanosleep and getpid. */
43 static int
44 do_test (void)
46 int result = 0;
47 int fd[2];
49 signal (SIGUSR1, SIG_IGN);
51 struct sigaction sa;
52 sa.sa_handler = alrm;
53 sigemptyset (&sa.sa_mask);
54 sa.sa_flags = 0;
55 if (sigaction (SIGALRM, &sa, NULL) < 0)
57 puts ("couldn't set up SIGALRM handler");
58 return 1;
61 if (pipe (fd) == -1)
63 puts ("pipe failed");
64 return 1;
67 struct itimerval it;
68 it.it_value.tv_sec = 0;
69 it.it_value.tv_usec = 200 * 1000;
70 it.it_interval = it.it_value;
71 if (setitimer (ITIMER_REAL, &it, NULL) < 0)
73 puts ("couldn't set up timer");
74 return 1;
77 /* First vfork() without previous getpid(). */
78 pid_t p1;
79 if ((p1 = vfork ()) == 0)
81 pid_t p = getpid ();
83 struct timespec ts;
84 ts.tv_sec = 1;
85 ts.tv_nsec = 0;
86 TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
87 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
89 else if (p1 == -1)
91 puts ("1st vfork failed");
92 result = 1;
95 memset (&it, 0, sizeof (it));
96 setitimer (ITIMER_REAL, &it, NULL);
98 pid_t p2 = 0;
99 if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
101 puts ("1st read failed");
102 result = 1;
104 int r;
105 if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
107 puts ("1st waitpid failed");
108 result = 1;
110 else if (r != 0)
112 puts ("write in 1st child failed");
113 result = 1;
116 /* Main process' ID. */
117 pid_t p0 = getpid ();
119 /* vfork() again, but after a getpid() in the main process. */
120 pid_t p3;
121 if ((p3 = vfork ()) == 0)
123 pid_t p = getpid ();
124 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
126 else if (p1 == -1)
128 puts ("2nd vfork failed");
129 result = 1;
132 pid_t p4;
133 if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
135 puts ("2nd read failed");
136 result = 1;
138 if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
140 puts ("2nd waitpid failed");
141 result = 1;
143 else if (r != 0)
145 puts ("write in 2nd child failed");
146 result = 1;
149 /* And getpid in the main process again. */
150 pid_t p5 = getpid ();
152 /* Analysis of the results. */
153 if (p0 != p5)
155 printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
156 result = 1;
159 if (p0 == p1)
161 printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
162 result = 1;
165 if (p1 != p2)
167 printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
168 result = 1;
171 if (p0 == p3)
173 printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
174 result = 1;
177 if (p3 != p4)
179 printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
180 result = 1;
183 close (fd[0]);
184 close (fd[1]);
186 if (raise_fail)
188 puts ("raise failed");
189 result = 1;
192 if (result == 0)
193 puts ("All OK");
195 return result;
198 #define TEST_FUNCTION do_test ()
199 #include "../test-skeleton.c"