1 /* Test for vfork functions.
2 Copyright (C) 2004-2019 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 <https://www.gnu.org/licenses/>. */
28 #include <sys/types.h>
36 if (raise (SIGUSR1
) < 0)
40 /* This test relies on non-POSIX functionality since the child
41 processes call write, nanosleep and getpid. */
48 signal (SIGUSR1
, SIG_IGN
);
52 sigemptyset (&sa
.sa_mask
);
54 if (sigaction (SIGALRM
, &sa
, NULL
) < 0)
56 puts ("couldn't set up SIGALRM handler");
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");
76 /* First vfork() without previous getpid(). */
78 if ((p1
= vfork ()) == 0)
85 TEMP_FAILURE_RETRY (nanosleep (&ts
, &ts
));
86 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
90 puts ("1st vfork failed");
94 memset (&it
, 0, sizeof (it
));
95 setitimer (ITIMER_REAL
, &it
, NULL
);
98 if (TEMP_FAILURE_RETRY (read (fd
[0], &p2
, sizeof (pid_t
))) != sizeof (pid_t
))
100 puts ("1st read failed");
104 if (TEMP_FAILURE_RETRY (waitpid (p1
, &r
, 0)) != p1
)
106 puts ("1st waitpid failed");
111 puts ("write in 1st child failed");
115 /* Main process' ID. */
116 pid_t p0
= getpid ();
118 /* vfork() again, but after a getpid() in the main process. */
120 if ((p3
= vfork ()) == 0)
123 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
127 puts ("2nd vfork failed");
132 if (TEMP_FAILURE_RETRY (read (fd
[0], &p4
, sizeof (pid_t
))) != sizeof (pid_t
))
134 puts ("2nd read failed");
137 if (TEMP_FAILURE_RETRY (waitpid (p3
, &r
, 0)) != p3
)
139 puts ("2nd waitpid failed");
144 puts ("write in 2nd child failed");
148 /* And getpid in the main process again. */
149 pid_t p5
= getpid ();
151 /* Analysis of the results. */
154 printf ("p0(%ld) != p5(%ld)\n", (long int) p0
, (long int) p5
);
160 printf ("p0(%ld) == p1(%ld)\n", (long int) p0
, (long int) p1
);
166 printf ("p1(%ld) != p2(%ld)\n", (long int) p1
, (long int) p2
);
172 printf ("p0(%ld) == p3(%ld)\n", (long int) p0
, (long int) p3
);
178 printf ("p3(%ld) != p4(%ld)\n", (long int) p3
, (long int) p4
);
187 puts ("raise failed");
197 #define TEST_FUNCTION do_test ()
198 #include "../test-skeleton.c"