]>
Commit | Line | Data |
---|---|---|
08c7f6b0 | 1 | /* Tests for spawn. |
688903eb | 2 | Copyright (C) 2000-2018 Free Software Foundation, Inc. |
41bdb6e2 | 3 | This file is part of the GNU C Library. |
08c7f6b0 UD |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
08c7f6b0 UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
08c7f6b0 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
08c7f6b0 UD |
19 | |
20 | #include <errno.h> | |
21 | #include <error.h> | |
22 | #include <fcntl.h> | |
23 | #include <spawn.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
08c7f6b0 UD |
26 | #include <wait.h> |
27 | #include <sys/param.h> | |
5ba41de9 SN |
28 | #include <support/check.h> |
29 | #include <support/xunistd.h> | |
08c7f6b0 UD |
30 | |
31 | ||
32 | /* Nonzero if the program gets called via `exec'. */ | |
33 | static int restart; | |
34 | ||
35 | ||
36 | #define CMDLINE_OPTIONS \ | |
37 | { "restart", no_argument, &restart, 1 }, | |
38 | ||
39 | /* Prototype for our test function. */ | |
40 | extern void do_prepare (int argc, char *argv[]); | |
41 | extern int do_test (int argc, char *argv[]); | |
42 | ||
43 | /* We have a preparation function. */ | |
44 | #define PREPARE do_prepare | |
45 | ||
46 | #include "../test-skeleton.c" | |
47 | ||
48 | ||
49 | /* Name of the temporary files. */ | |
50 | static char *name1; | |
51 | static char *name2; | |
52 | static char *name3; | |
53 | ||
c22553ef FW |
54 | /* Descriptors for the temporary files. */ |
55 | static int temp_fd1 = -1; | |
56 | static int temp_fd2 = -1; | |
57 | static int temp_fd3 = -1; | |
58 | ||
08c7f6b0 UD |
59 | /* The contents of our files. */ |
60 | static const char fd1string[] = "This file should get closed"; | |
61 | static const char fd2string[] = "This file should stay opened"; | |
62 | static const char fd3string[] = "This file will be opened"; | |
63 | ||
64 | ||
65 | /* We have a preparation function. */ | |
66 | void | |
67 | do_prepare (int argc, char *argv[]) | |
68 | { | |
c22553ef FW |
69 | /* We must not open any files in the restart case. */ |
70 | if (restart) | |
71 | return; | |
72 | ||
73 | temp_fd1 = create_temp_file ("spawn", &name1); | |
74 | temp_fd2 = create_temp_file ("spawn", &name2); | |
75 | temp_fd3 = create_temp_file ("spawn", &name3); | |
76 | if (temp_fd1 < 0 || temp_fd2 < 0 || temp_fd3 < 0) | |
77 | exit (1); | |
08c7f6b0 UD |
78 | } |
79 | ||
80 | ||
81 | static int | |
82 | handle_restart (const char *fd1s, const char *fd2s, const char *fd3s, | |
83 | const char *fd4s, const char *name) | |
84 | { | |
85 | char buf[100]; | |
86 | int fd1; | |
87 | int fd2; | |
88 | int fd3; | |
89 | int fd4; | |
90 | ||
91 | /* First get the descriptors. */ | |
92 | fd1 = atol (fd1s); | |
93 | fd2 = atol (fd2s); | |
94 | fd3 = atol (fd3s); | |
95 | fd4 = atol (fd4s); | |
96 | ||
97 | /* Sanity check. */ | |
98 | if (fd1 == fd2) | |
99 | error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same"); | |
100 | if (fd1 == fd3) | |
101 | error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same"); | |
102 | if (fd1 == fd4) | |
103 | error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same"); | |
104 | if (fd2 == fd3) | |
105 | error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same"); | |
106 | if (fd2 == fd4) | |
107 | error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same"); | |
108 | if (fd3 == fd4) | |
109 | error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same"); | |
110 | ||
111 | /* First the easy part: read from the file descriptor which is | |
112 | supposed to be open. */ | |
113 | if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string)) | |
114 | error (EXIT_FAILURE, errno, "file 2 not in right position"); | |
115 | /* The duped descriptor must have the same position. */ | |
116 | if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string)) | |
117 | error (EXIT_FAILURE, errno, "file 4 not in right position"); | |
118 | if (lseek (fd2, 0, SEEK_SET) != 0) | |
119 | error (EXIT_FAILURE, 0, "cannot reset position in file 2"); | |
120 | if (lseek (fd4, 0, SEEK_CUR) != 0) | |
121 | error (EXIT_FAILURE, errno, "file 4 not set back, too"); | |
122 | if (read (fd2, buf, sizeof buf) != strlen (fd2string)) | |
123 | error (EXIT_FAILURE, 0, "cannot read file 2"); | |
124 | if (memcmp (fd2string, buf, strlen (fd2string)) != 0) | |
125 | error (EXIT_FAILURE, 0, "file 2 does not match"); | |
126 | ||
127 | /* Now read from the third file. */ | |
128 | if (read (fd3, buf, sizeof buf) != strlen (fd3string)) | |
129 | error (EXIT_FAILURE, 0, "cannot read file 3"); | |
130 | if (memcmp (fd3string, buf, strlen (fd3string)) != 0) | |
131 | error (EXIT_FAILURE, 0, "file 3 does not match"); | |
132 | /* Try to write to the file. This should not be allowed. */ | |
133 | if (write (fd3, "boo!", 4) != -1 || errno != EBADF) | |
134 | error (EXIT_FAILURE, 0, "file 3 is writable"); | |
135 | ||
136 | /* Now try to read the first file. First make sure it is not opened. */ | |
137 | if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF) | |
138 | error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1); | |
139 | ||
140 | /* Now open the file and read it. */ | |
141 | fd1 = open (name, O_RDONLY); | |
142 | if (fd1 == -1) | |
143 | error (EXIT_FAILURE, errno, | |
144 | "cannot open first file \"%s\" for verification", name); | |
145 | ||
146 | if (read (fd1, buf, sizeof buf) != strlen (fd1string)) | |
147 | error (EXIT_FAILURE, errno, "cannot read file 1"); | |
148 | if (memcmp (fd1string, buf, strlen (fd1string)) != 0) | |
149 | error (EXIT_FAILURE, 0, "file 1 does not match"); | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
154 | ||
155 | int | |
156 | do_test (int argc, char *argv[]) | |
157 | { | |
158 | pid_t pid; | |
08c7f6b0 UD |
159 | int fd4; |
160 | int status; | |
161 | posix_spawn_file_actions_t actions; | |
162 | char fd1name[18]; | |
163 | char fd2name[18]; | |
164 | char fd3name[18]; | |
165 | char fd4name[18]; | |
89e435f3 | 166 | char *name3_copy; |
08c7f6b0 | 167 | char *spargv[12]; |
4cf8f209 | 168 | int i; |
08c7f6b0 UD |
169 | |
170 | /* We must have | |
4cf8f209 L |
171 | - one or four parameters left if called initially |
172 | + path for ld.so optional | |
173 | + "--library-path" optional | |
174 | + the library path optional | |
08c7f6b0 | 175 | + the application name |
726b7b0f | 176 | - five parameters left if called through re-execution |
08c7f6b0 UD |
177 | + file descriptor number which is supposed to be closed |
178 | + the open file descriptor | |
179 | + the newly opened file descriptor | |
180 | + thhe duped second descriptor | |
181 | + the name of the closed descriptor | |
182 | */ | |
4cf8f209 | 183 | if (argc != (restart ? 6 : 2) && argc != (restart ? 6 : 5)) |
08c7f6b0 UD |
184 | error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc); |
185 | ||
186 | if (restart) | |
726b7b0f | 187 | return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]); |
08c7f6b0 UD |
188 | |
189 | /* Prepare the test. We are creating two files: one which file descriptor | |
190 | will be marked with FD_CLOEXEC, another which is not. */ | |
191 | ||
08c7f6b0 | 192 | /* Write something in the files. */ |
c22553ef | 193 | if (write (temp_fd1, fd1string, strlen (fd1string)) != strlen (fd1string)) |
08c7f6b0 | 194 | error (EXIT_FAILURE, errno, "cannot write to first file"); |
c22553ef | 195 | if (write (temp_fd2, fd2string, strlen (fd2string)) != strlen (fd2string)) |
08c7f6b0 | 196 | error (EXIT_FAILURE, errno, "cannot write to second file"); |
c22553ef | 197 | if (write (temp_fd3, fd3string, strlen (fd3string)) != strlen (fd3string)) |
08c7f6b0 UD |
198 | error (EXIT_FAILURE, errno, "cannot write to third file"); |
199 | ||
200 | /* Close the third file. It'll be opened by `spawn'. */ | |
c22553ef | 201 | close (temp_fd3); |
08c7f6b0 UD |
202 | |
203 | /* Tell `spawn' what to do. */ | |
204 | if (posix_spawn_file_actions_init (&actions) != 0) | |
205 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init"); | |
c22553ef FW |
206 | /* Close `temp_fd1'. */ |
207 | if (posix_spawn_file_actions_addclose (&actions, temp_fd1) != 0) | |
08c7f6b0 UD |
208 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose"); |
209 | /* We want to open the third file. */ | |
89e435f3 FW |
210 | name3_copy = strdup (name3); |
211 | if (name3_copy == NULL) | |
212 | error (EXIT_FAILURE, errno, "strdup"); | |
c22553ef | 213 | if (posix_spawn_file_actions_addopen (&actions, temp_fd3, name3_copy, |
08c7f6b0 UD |
214 | O_RDONLY, 0666) != 0) |
215 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen"); | |
89e435f3 FW |
216 | /* Overwrite the name to check that a copy has been made. */ |
217 | memset (name3_copy, 'X', strlen (name3_copy)); | |
218 | ||
08c7f6b0 | 219 | /* We dup the second descriptor. */ |
c22553ef FW |
220 | fd4 = MAX (2, MAX (temp_fd1, MAX (temp_fd2, temp_fd3))) + 1; |
221 | if (posix_spawn_file_actions_adddup2 (&actions, temp_fd2, fd4) != 0) | |
08c7f6b0 UD |
222 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2"); |
223 | ||
224 | /* Now spawn the process. */ | |
c22553ef FW |
225 | snprintf (fd1name, sizeof fd1name, "%d", temp_fd1); |
226 | snprintf (fd2name, sizeof fd2name, "%d", temp_fd2); | |
227 | snprintf (fd3name, sizeof fd3name, "%d", temp_fd3); | |
08c7f6b0 UD |
228 | snprintf (fd4name, sizeof fd4name, "%d", fd4); |
229 | ||
4cf8f209 L |
230 | for (i = 0; i < (argc == (restart ? 6 : 5) ? 4 : 1); i++) |
231 | spargv[i] = argv[i + 1]; | |
232 | spargv[i++] = (char *) "--direct"; | |
233 | spargv[i++] = (char *) "--restart"; | |
234 | spargv[i++] = fd1name; | |
235 | spargv[i++] = fd2name; | |
236 | spargv[i++] = fd3name; | |
237 | spargv[i++] = fd4name; | |
238 | spargv[i++] = name1; | |
239 | spargv[i] = NULL; | |
08c7f6b0 | 240 | |
726b7b0f | 241 | if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0) |
08c7f6b0 UD |
242 | error (EXIT_FAILURE, errno, "posix_spawn"); |
243 | ||
2a69f853 AZ |
244 | /* Same test but with a NULL pid argument. */ |
245 | if (posix_spawn (NULL, argv[1], &actions, NULL, spargv, environ) != 0) | |
246 | error (EXIT_FAILURE, errno, "posix_spawn"); | |
247 | ||
08c7f6b0 UD |
248 | /* Cleanup. */ |
249 | if (posix_spawn_file_actions_destroy (&actions) != 0) | |
250 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy"); | |
89e435f3 | 251 | free (name3_copy); |
08c7f6b0 | 252 | |
5ba41de9 SN |
253 | /* Wait for the children. */ |
254 | TEST_VERIFY (xwaitpid (pid, &status, 0) == pid); | |
255 | TEST_VERIFY (WIFEXITED (status)); | |
256 | TEST_VERIFY (!WIFSIGNALED (status)); | |
257 | TEST_VERIFY (WEXITSTATUS (status) == 0); | |
08c7f6b0 | 258 | |
5ba41de9 SN |
259 | xwaitpid (-1, &status, 0); |
260 | TEST_VERIFY (WIFEXITED (status)); | |
261 | TEST_VERIFY (!WIFSIGNALED (status)); | |
262 | TEST_VERIFY (WEXITSTATUS (status) == 0); | |
08c7f6b0 | 263 | |
5ba41de9 | 264 | return 0; |
08c7f6b0 | 265 | } |