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
29 #include <sys/types.h>
37 if (raise (SIGUSR1
) < 0)
41 /* This test relies on non-POSIX functionality since the child
42 processes call write, nanosleep and getpid. */
49 signal (SIGUSR1
, SIG_IGN
);
53 sigemptyset (&sa
.sa_mask
);
55 if (sigaction (SIGALRM
, &sa
, NULL
) < 0)
57 puts ("couldn't set up SIGALRM handler");
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");
77 /* First vfork() without previous getpid(). */
79 if ((p1
= vfork ()) == 0)
86 TEMP_FAILURE_RETRY (nanosleep (&ts
, &ts
));
87 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
91 puts ("1st vfork failed");
95 memset (&it
, 0, sizeof (it
));
96 setitimer (ITIMER_REAL
, &it
, NULL
);
99 if (TEMP_FAILURE_RETRY (read (fd
[0], &p2
, sizeof (pid_t
))) != sizeof (pid_t
))
101 puts ("1st read failed");
105 if (TEMP_FAILURE_RETRY (waitpid (p1
, &r
, 0)) != p1
)
107 puts ("1st waitpid failed");
112 puts ("write in 1st child failed");
116 /* Main process' ID. */
117 pid_t p0
= getpid ();
119 /* vfork() again, but after a getpid() in the main process. */
121 if ((p3
= vfork ()) == 0)
124 _exit (TEMP_FAILURE_RETRY (write (fd
[1], &p
, sizeof (p
))) != sizeof (p
));
128 puts ("2nd vfork failed");
133 if (TEMP_FAILURE_RETRY (read (fd
[0], &p4
, sizeof (pid_t
))) != sizeof (pid_t
))
135 puts ("2nd read failed");
138 if (TEMP_FAILURE_RETRY (waitpid (p3
, &r
, 0)) != p3
)
140 puts ("2nd waitpid failed");
145 puts ("write in 2nd child failed");
149 /* And getpid in the main process again. */
150 pid_t p5
= getpid ();
152 /* Analysis of the results. */
155 printf ("p0(%ld) != p5(%ld)\n", (long int) p0
, (long int) p5
);
161 printf ("p0(%ld) == p1(%ld)\n", (long int) p0
, (long int) p1
);
167 printf ("p1(%ld) != p2(%ld)\n", (long int) p1
, (long int) p2
);
173 printf ("p0(%ld) == p3(%ld)\n", (long int) p0
, (long int) p3
);
179 printf ("p3(%ld) != p4(%ld)\n", (long int) p3
, (long int) p4
);
188 puts ("raise failed");
198 #define TEST_FUNCTION do_test ()
199 #include "../test-skeleton.c"