]>
Commit | Line | Data |
---|---|---|
8b9d6054 | 1 | /* Test for vfork functions. |
dff8da6b | 2 | Copyright (C) 2004-2024 Free Software Foundation, Inc. |
c1b20290 | 3 | This file is part of the GNU C Library. |
c1b20290 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
c1b20290 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <unistd.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/wait.h> | |
25 | ||
26 | /* This test relies on non-POSIX functionality since the child | |
8b9d6054 | 27 | processes call write and getpid. */ |
c1b20290 UD |
28 | static int |
29 | do_test (void) | |
30 | { | |
31 | int result = 0; | |
32 | int fd[2]; | |
33 | ||
34 | if (pipe (fd) == -1) | |
35 | { | |
36 | puts ("pipe failed"); | |
37 | return 1; | |
38 | } | |
39 | ||
40 | /* First vfork() without previous getpid(). */ | |
41 | pid_t p1; | |
42 | if ((p1 = vfork ()) == 0) | |
43 | { | |
44 | pid_t p = getpid (); | |
45 | _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p)); | |
46 | } | |
47 | else if (p1 == -1) | |
48 | { | |
49 | puts ("1st vfork failed"); | |
50 | result = 1; | |
51 | } | |
52 | ||
53 | pid_t p2 = 0; | |
54 | if (TEMP_FAILURE_RETRY (read (fd[0], &p2, sizeof (pid_t))) != sizeof (pid_t)) | |
55 | { | |
56 | puts ("1st read failed"); | |
57 | result = 1; | |
58 | } | |
59 | int r; | |
60 | if (TEMP_FAILURE_RETRY (waitpid (p1, &r, 0)) != p1) | |
61 | { | |
62 | puts ("1st waitpid failed"); | |
63 | result = 1; | |
64 | } | |
65 | else if (r != 0) | |
66 | { | |
67 | puts ("write in 1st child failed"); | |
68 | result = 1; | |
69 | } | |
70 | ||
71 | /* Main process' ID. */ | |
72 | pid_t p0 = getpid (); | |
73 | ||
74 | /* vfork() again, but after a getpid() in the main process. */ | |
75 | pid_t p3; | |
76 | if ((p3 = vfork ()) == 0) | |
77 | { | |
78 | pid_t p = getpid (); | |
79 | _exit (TEMP_FAILURE_RETRY (write (fd[1], &p, sizeof (p))) != sizeof (p)); | |
80 | } | |
81 | else if (p1 == -1) | |
82 | { | |
83 | puts ("2nd vfork failed"); | |
84 | result = 1; | |
85 | } | |
86 | ||
87 | pid_t p4; | |
88 | if (TEMP_FAILURE_RETRY (read (fd[0], &p4, sizeof (pid_t))) != sizeof (pid_t)) | |
89 | { | |
90 | puts ("2nd read failed"); | |
91 | result = 1; | |
92 | } | |
93 | if (TEMP_FAILURE_RETRY (waitpid (p3, &r, 0)) != p3) | |
94 | { | |
95 | puts ("2nd waitpid failed"); | |
96 | result = 1; | |
97 | } | |
98 | else if (r != 0) | |
99 | { | |
100 | puts ("write in 2nd child failed"); | |
101 | result = 1; | |
102 | } | |
103 | ||
104 | /* And getpid in the main process again. */ | |
105 | pid_t p5 = getpid (); | |
106 | ||
107 | /* Analysis of the results. */ | |
108 | if (p0 != p5) | |
109 | { | |
110 | printf ("p0(%ld) != p5(%ld)\n", (long int) p0, (long int) p5); | |
111 | result = 1; | |
112 | } | |
113 | ||
114 | if (p0 == p1) | |
115 | { | |
116 | printf ("p0(%ld) == p1(%ld)\n", (long int) p0, (long int) p1); | |
117 | result = 1; | |
118 | } | |
119 | ||
120 | if (p1 != p2) | |
121 | { | |
122 | printf ("p1(%ld) != p2(%ld)\n", (long int) p1, (long int) p2); | |
123 | result = 1; | |
124 | } | |
125 | ||
126 | if (p0 == p3) | |
127 | { | |
128 | printf ("p0(%ld) == p3(%ld)\n", (long int) p0, (long int) p3); | |
129 | result = 1; | |
130 | } | |
131 | ||
132 | if (p3 != p4) | |
133 | { | |
134 | printf ("p3(%ld) != p4(%ld)\n", (long int) p3, (long int) p4); | |
135 | result = 1; | |
136 | } | |
137 | ||
138 | close (fd[0]); | |
139 | close (fd[1]); | |
140 | ||
141 | if (result == 0) | |
142 | puts ("All OK"); | |
143 | ||
144 | return result; | |
145 | } | |
146 | ||
147 | #define TEST_FUNCTION do_test () | |
148 | #include "../test-skeleton.c" |