1 | /* Tests for spawn.
|
---|
2 | Copyright (C) 2000 Free Software Foundation, Inc.
|
---|
3 | This file is part of the GNU C Library.
|
---|
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
|
---|
5 |
|
---|
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.
|
---|
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
|
---|
14 | Lesser General Public License for more details.
|
---|
15 |
|
---|
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
|
---|
19 | 02111-1307 USA. */
|
---|
20 |
|
---|
21 | #include <errno.h>
|
---|
22 | #include <error.h>
|
---|
23 | #include <fcntl.h>
|
---|
24 | #include <spawn.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <string.h>
|
---|
27 | #include <unistd.h>
|
---|
28 | #include <wait.h>
|
---|
29 | #include <sys/param.h>
|
---|
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 |
|
---|
54 | /* The contents of our files. */
|
---|
55 | static const char fd1string[] = "This file should get closed";
|
---|
56 | static const char fd2string[] = "This file should stay opened";
|
---|
57 | static const char fd3string[] = "This file will be opened";
|
---|
58 |
|
---|
59 |
|
---|
60 | /* We have a preparation function. */
|
---|
61 | void
|
---|
62 | do_prepare (int argc, char *argv[])
|
---|
63 | {
|
---|
64 | char name_len;
|
---|
65 |
|
---|
66 | name_len = strlen (test_dir);
|
---|
67 | name1 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
|
---|
68 | mempcpy (mempcpy (name1, test_dir, name_len),
|
---|
69 | "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
|
---|
70 | add_temp_file (name1);
|
---|
71 |
|
---|
72 | name2 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
|
---|
73 | mempcpy (mempcpy (name2, test_dir, name_len),
|
---|
74 | "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
|
---|
75 | add_temp_file (name2);
|
---|
76 |
|
---|
77 | name3 = (char *) malloc (name_len + sizeof ("/spawnXXXXXX"));
|
---|
78 | mempcpy (mempcpy (name3, test_dir, name_len),
|
---|
79 | "/spawnXXXXXX", sizeof ("/spawnXXXXXX"));
|
---|
80 | add_temp_file (name3);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | static int
|
---|
85 | handle_restart (const char *fd1s, const char *fd2s, const char *fd3s,
|
---|
86 | const char *fd4s, const char *name)
|
---|
87 | {
|
---|
88 | char buf[100];
|
---|
89 | int fd1;
|
---|
90 | int fd2;
|
---|
91 | int fd3;
|
---|
92 | int fd4;
|
---|
93 |
|
---|
94 | /* First get the descriptors. */
|
---|
95 | fd1 = atol (fd1s);
|
---|
96 | fd2 = atol (fd2s);
|
---|
97 | fd3 = atol (fd3s);
|
---|
98 | fd4 = atol (fd4s);
|
---|
99 |
|
---|
100 | /* Sanity check. */
|
---|
101 | if (fd1 == fd2)
|
---|
102 | error (EXIT_FAILURE, 0, "value of fd1 and fd2 is the same");
|
---|
103 | if (fd1 == fd3)
|
---|
104 | error (EXIT_FAILURE, 0, "value of fd1 and fd3 is the same");
|
---|
105 | if (fd1 == fd4)
|
---|
106 | error (EXIT_FAILURE, 0, "value of fd1 and fd4 is the same");
|
---|
107 | if (fd2 == fd3)
|
---|
108 | error (EXIT_FAILURE, 0, "value of fd2 and fd3 is the same");
|
---|
109 | if (fd2 == fd4)
|
---|
110 | error (EXIT_FAILURE, 0, "value of fd2 and fd4 is the same");
|
---|
111 | if (fd3 == fd4)
|
---|
112 | error (EXIT_FAILURE, 0, "value of fd3 and fd4 is the same");
|
---|
113 |
|
---|
114 | /* First the easy part: read from the file descriptor which is
|
---|
115 | supposed to be open. */
|
---|
116 | if (lseek (fd2, 0, SEEK_CUR) != strlen (fd2string))
|
---|
117 | error (EXIT_FAILURE, errno, "file 2 not in right position");
|
---|
118 | /* The duped descriptor must have the same position. */
|
---|
119 | if (lseek (fd4, 0, SEEK_CUR) != strlen (fd2string))
|
---|
120 | error (EXIT_FAILURE, errno, "file 4 not in right position");
|
---|
121 | if (lseek (fd2, 0, SEEK_SET) != 0)
|
---|
122 | error (EXIT_FAILURE, 0, "cannot reset position in file 2");
|
---|
123 | if (lseek (fd4, 0, SEEK_CUR) != 0)
|
---|
124 | error (EXIT_FAILURE, errno, "file 4 not set back, too");
|
---|
125 | if (read (fd2, buf, sizeof buf) != strlen (fd2string))
|
---|
126 | error (EXIT_FAILURE, 0, "cannot read file 2");
|
---|
127 | if (memcmp (fd2string, buf, strlen (fd2string)) != 0)
|
---|
128 | error (EXIT_FAILURE, 0, "file 2 does not match");
|
---|
129 |
|
---|
130 | /* Now read from the third file. */
|
---|
131 | if (read (fd3, buf, sizeof buf) != strlen (fd3string))
|
---|
132 | error (EXIT_FAILURE, 0, "cannot read file 3");
|
---|
133 | if (memcmp (fd3string, buf, strlen (fd3string)) != 0)
|
---|
134 | error (EXIT_FAILURE, 0, "file 3 does not match");
|
---|
135 | /* Try to write to the file. This should not be allowed. */
|
---|
136 | if (write (fd3, "boo!", 4) != -1 || errno != EBADF)
|
---|
137 | error (EXIT_FAILURE, 0, "file 3 is writable");
|
---|
138 |
|
---|
139 | /* Now try to read the first file. First make sure it is not opened. */
|
---|
140 | if (lseek (fd1, 0, SEEK_CUR) != (off_t) -1 || errno != EBADF)
|
---|
141 | error (EXIT_FAILURE, 0, "file 1 (%d) is not closed", fd1);
|
---|
142 |
|
---|
143 | /* Now open the file and read it. */
|
---|
144 | fd1 = open (name, O_RDONLY);
|
---|
145 | if (fd1 == -1)
|
---|
146 | error (EXIT_FAILURE, errno,
|
---|
147 | "cannot open first file \"%s\" for verification", name);
|
---|
148 |
|
---|
149 | if (read (fd1, buf, sizeof buf) != strlen (fd1string))
|
---|
150 | error (EXIT_FAILURE, errno, "cannot read file 1");
|
---|
151 | if (memcmp (fd1string, buf, strlen (fd1string)) != 0)
|
---|
152 | error (EXIT_FAILURE, 0, "file 1 does not match");
|
---|
153 |
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | int
|
---|
159 | do_test (int argc, char *argv[])
|
---|
160 | {
|
---|
161 | pid_t pid;
|
---|
162 | int fd1;
|
---|
163 | int fd2;
|
---|
164 | int fd3;
|
---|
165 | int fd4;
|
---|
166 | int status;
|
---|
167 | posix_spawn_file_actions_t actions;
|
---|
168 | char fd1name[18];
|
---|
169 | char fd2name[18];
|
---|
170 | char fd3name[18];
|
---|
171 | char fd4name[18];
|
---|
172 | char *spargv[12];
|
---|
173 |
|
---|
174 | /* We must have
|
---|
175 | - four parameters left of called initially
|
---|
176 | + path for ld.so
|
---|
177 | + "--library-path"
|
---|
178 | + the library path
|
---|
179 | + the application name
|
---|
180 | - five parameters left if called through re-execution
|
---|
181 | + file descriptor number which is supposed to be closed
|
---|
182 | + the open file descriptor
|
---|
183 | + the newly opened file descriptor
|
---|
184 | + thhe duped second descriptor
|
---|
185 | + the name of the closed descriptor
|
---|
186 | */
|
---|
187 | if (argc != (restart ? 6 : 5))
|
---|
188 | error (EXIT_FAILURE, 0, "wrong number of arguments (%d)", argc);
|
---|
189 |
|
---|
190 | if (restart)
|
---|
191 | return handle_restart (argv[1], argv[2], argv[3], argv[4], argv[5]);
|
---|
192 |
|
---|
193 | /* Prepare the test. We are creating two files: one which file descriptor
|
---|
194 | will be marked with FD_CLOEXEC, another which is not. */
|
---|
195 |
|
---|
196 | /* Open our test files. */
|
---|
197 | fd1 = mkstemp (name1);
|
---|
198 | if (fd1 == -1)
|
---|
199 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name1);
|
---|
200 | fd2 = mkstemp (name2);
|
---|
201 | if (fd2 == -1)
|
---|
202 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name2);
|
---|
203 | fd3 = mkstemp (name3);
|
---|
204 | if (fd3 == -1)
|
---|
205 | error (EXIT_FAILURE, errno, "cannot open test file `%s'", name3);
|
---|
206 |
|
---|
207 | /* Write something in the files. */
|
---|
208 | if (write (fd1, fd1string, strlen (fd1string)) != strlen (fd1string))
|
---|
209 | error (EXIT_FAILURE, errno, "cannot write to first file");
|
---|
210 | if (write (fd2, fd2string, strlen (fd2string)) != strlen (fd2string))
|
---|
211 | error (EXIT_FAILURE, errno, "cannot write to second file");
|
---|
212 | if (write (fd3, fd3string, strlen (fd3string)) != strlen (fd3string))
|
---|
213 | error (EXIT_FAILURE, errno, "cannot write to third file");
|
---|
214 |
|
---|
215 | /* Close the third file. It'll be opened by `spawn'. */
|
---|
216 | close (fd3);
|
---|
217 |
|
---|
218 | /* Tell `spawn' what to do. */
|
---|
219 | if (posix_spawn_file_actions_init (&actions) != 0)
|
---|
220 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_init");
|
---|
221 | /* Close `fd1'. */
|
---|
222 | if (posix_spawn_file_actions_addclose (&actions, fd1) != 0)
|
---|
223 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addclose");
|
---|
224 | /* We want to open the third file. */
|
---|
225 | if (posix_spawn_file_actions_addopen (&actions, fd3, name3,
|
---|
226 | O_RDONLY, 0666) != 0)
|
---|
227 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_addopen");
|
---|
228 | /* We dup the second descriptor. */
|
---|
229 | fd4 = MAX (2, MAX (fd1, MAX (fd2, fd3))) + 1;
|
---|
230 | if (posix_spawn_file_actions_adddup2 (&actions, fd2, fd4) != 0)
|
---|
231 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_adddup2");
|
---|
232 |
|
---|
233 | /* Now spawn the process. */
|
---|
234 | snprintf (fd1name, sizeof fd1name, "%d", fd1);
|
---|
235 | snprintf (fd2name, sizeof fd2name, "%d", fd2);
|
---|
236 | snprintf (fd3name, sizeof fd3name, "%d", fd3);
|
---|
237 | snprintf (fd4name, sizeof fd4name, "%d", fd4);
|
---|
238 |
|
---|
239 | spargv[0] = argv[1];
|
---|
240 | spargv[1] = argv[2];
|
---|
241 | spargv[2] = argv[3];
|
---|
242 | spargv[3] = argv[4];
|
---|
243 | spargv[4] = (char *) "--direct";
|
---|
244 | spargv[5] = (char *) "--restart";
|
---|
245 | spargv[6] = fd1name;
|
---|
246 | spargv[7] = fd2name;
|
---|
247 | spargv[8] = fd3name;
|
---|
248 | spargv[9] = fd4name;
|
---|
249 | spargv[10] = name1;
|
---|
250 | spargv[11] = NULL;
|
---|
251 |
|
---|
252 | if (posix_spawn (&pid, argv[1], &actions, NULL, spargv, environ) != 0)
|
---|
253 | error (EXIT_FAILURE, errno, "posix_spawn");
|
---|
254 |
|
---|
255 | /* Cleanup. */
|
---|
256 | if (posix_spawn_file_actions_destroy (&actions) != 0)
|
---|
257 | error (EXIT_FAILURE, errno, "posix_spawn_file_actions_destroy");
|
---|
258 |
|
---|
259 | /* Wait for the child. */
|
---|
260 | if (waitpid (pid, &status, 0) != pid)
|
---|
261 | error (EXIT_FAILURE, errno, "wrong child");
|
---|
262 |
|
---|
263 | if (WTERMSIG (status) != 0)
|
---|
264 | error (EXIT_FAILURE, 0, "Child terminated incorrectly");
|
---|
265 | status = WEXITSTATUS (status);
|
---|
266 |
|
---|
267 | /* Remove the test files. */
|
---|
268 | unlink (name1);
|
---|
269 | unlink (name2);
|
---|
270 | unlink (name3);
|
---|
271 |
|
---|
272 | return status;
|
---|
273 | }
|
---|