nptl: Change tst-typesizes to _Static_assert
[glibc.git] / posix / tst-vfork1.c
blobfa5f91fda6ef45418dde54c5c840774b371438d9
1 /* Test for vfork functions.
2 Copyright (C) 2004-2017 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 <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
27 /* This test relies on non-POSIX functionality since the child
28 processes call write and getpid. */
29 static int
30 do_test (void)
32 int result = 0;
33 int fd[2];
35 if (pipe (fd) == -1)
37 puts ("pipe failed");
38 return 1;
41 /* First vfork() without previous getpid(). */
42 pid_t p1;
43 if ((p1 = vfork ()) == 0)
45 pid_t p = getpid ();
46 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
48 else if (p1 == -1)
50 puts ("1st vfork failed");
51 result = 1;
54 pid_t p2 = 0;
55 if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t))
57 puts ("1st read failed");
58 result = 1;
60 int r;
61 if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1)
63 puts ("1st waitpid failed");
64 result = 1;
66 else if (r != 0)
68 puts ("write in 1st child failed");
69 result = 1;
72 /* Main process' ID. */
73 pid_t p0 = getpid ();
75 /* vfork() again, but after a getpid() in the main process. */
76 pid_t p3;
77 if ((p3 = vfork ()) == 0)
79 pid_t p = getpid ();
80 _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p));
82 else if (p1 == -1)
84 puts ("2nd vfork failed");
85 result = 1;
88 pid_t p4;
89 if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t))
91 puts ("2nd read failed");
92 result = 1;
94 if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3)
96 puts ("2nd waitpid failed");
97 result = 1;
99 else if (r != 0)
101 puts ("write in 2nd child failed");
102 result = 1;
105 /* And getpid in the main process again. */
106 pid_t p5 = getpid ();
108 /* Analysis of the results. */
109 if (p0 != p5)
111 printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5);
112 result = 1;
115 if (p0 == p1)
117 printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1);
118 result = 1;
121 if (p1 != p2)
123 printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2);
124 result = 1;
127 if (p0 == p3)
129 printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3);
130 result = 1;
133 if (p3 != p4)
135 printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4);
136 result = 1;
139 close (fd[0]);
140 close (fd[1]);
142 if (result == 0)
143 puts ("All OK");
145 return result;
148 #define TEST_FUNCTION do_test ()
149 #include "../test-skeleton.c"