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