1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Based off tests/lib/libc/gen/posix_spawn/t_spawn.c
*/
/* $NetBSD: t_spawn.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */
/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles Zhang <charles@NetBSD.org> and
* Martin Husemann <martin@NetBSD.org>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <fcntl.h>
#include <signal.h>
#include <spawn.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
#include "common.h"
/* reroute stdout to /dev/null while returning another fd for the old stdout */
/* this is just for aesthetics: we don't want to see the output of 'ls' */
static int
sink_stdout(void)
{
int fd, fd2;
if ((fd = fcntl(1, F_DUPFD, 3)) == -1 || close(1) == -1) {
e(0);
quit();
}
if ((fd2 = open("/dev/null", O_WRONLY)) != 1) {
if (fd2 == -1 || dup2(fd2, 1) != 1) {
dup2(fd, 1);
e(0);
quit();
}
}
return fd;
}
/* restore stdout */
static void
restore_stdout(int fd)
{
dup2(fd, 1);
close(fd);
}
/* tests a simple posix_spawn executing /bin/ls */
static void
test_posix_spawn_ls(void)
{
char * const args[] = { "ls", "-la", NULL };
int err;
err = posix_spawn(NULL, "/bin/ls", NULL, NULL, args, NULL);
if (err != 0)
e(1);
}
/* tests a simple posix_spawnp executing ls via $PATH */
static void
test_posix_spawnp_ls(void)
{
char * const args[] = { "ls", "-la", NULL };
int err;
err = posix_spawnp(NULL, "ls", NULL, NULL, args, NULL);
if(err != 0)
e(2);
}
/* posix_spawn a non existant binary */
static void
test_posix_spawn_missing(void)
{
char * const args[] = { "t84_h_nonexist", NULL };
int err;
err = posix_spawn(NULL, "../t84_h_nonexist", NULL, NULL, args, NULL);
if (err != ENOENT)
e(4);
}
/* posix_spawn a script with non existing interpreter */
static void
test_posix_spawn_nonexec(void)
{
char * const args[] = { "t84_h_nonexec", NULL };
int err;
err = posix_spawn(NULL, "../t84_h_nonexec", NULL, NULL, args, NULL);
if (err != ENOENT)
e(5);
}
/* posix_spawn a child and get it's return code */
static void
test_posix_spawn_child(void)
{
char * const args0[] = { "t84_h_spawn", "0", NULL };
char * const args1[] = { "t84_h_spawn", "1", NULL };
char * const args7[] = { "t84_h_spawn", "7", NULL };
int err, status;
pid_t pid;
err = posix_spawn(&pid, "../t84_h_spawn", NULL, NULL, args0, NULL);
if (err != 0 || pid < 1)
e(1);
waitpid(pid, &status, 0);
if (! (WIFEXITED(status) && WEXITSTATUS(status) == 0))
e(2);
err = posix_spawn(&pid, "../t84_h_spawn", NULL, NULL, args1, NULL);
if (err != 0 || pid < 1)
e(3);
waitpid(pid, &status, 0);
if (! (WIFEXITED(status) && WEXITSTATUS(status) == 1))
e(4);
err = posix_spawn(&pid, "../t84_h_spawn", NULL, NULL, args7, NULL);
if (err != 0 || pid < 1)
e(5);
waitpid(pid, &status, 0);
if (! (WIFEXITED(status) && WEXITSTATUS(status) == 7))
e(6);
}
/* test spawn attributes */
static void
test_posix_spawnattr(void)
{
int pid, status, err, pfd[2];
char helper_arg[128];
char * const args[] = { "t84_h_spawnattr", helper_arg, NULL };
sigset_t sig;
posix_spawnattr_t attr;
/*
* create a pipe to controll the child
*/
err = pipe(pfd);
if (err != 0)
e(1);
sprintf(helper_arg, "%d", pfd[0]);
posix_spawnattr_init(&attr);
sigemptyset(&sig);
sigaddset(&sig, SIGUSR1);
posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER |
POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP |
POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
POSIX_SPAWN_SETSIGDEF);
posix_spawnattr_setpgroup(&attr, 0);
#if 0
posix_spawnattr_setschedparam(&attr, &sp);
posix_spawnattr_setschedpolicy(&attr, scheduler);
#endif
posix_spawnattr_setsigmask(&attr, &sig);
posix_spawnattr_setsigdefault(&attr, &sig);
err = posix_spawn(&pid, "../t84_h_spawnattr", NULL, &attr, args, NULL);
if (err != 0)
e(2);
/* ready, let child go */
write(pfd[1], "q", 1);
close(pfd[0]);
close(pfd[1]);
/* wait and check result from child */
waitpid(pid, &status, 0);
if (! (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS))
e(3);
posix_spawnattr_destroy(&attr);
}
/* tests a simple posix_spawn executing /bin/ls with file actions */
static void
test_posix_spawn_file_actions(void)
{
char * const args[] = { "ls", "-la", NULL };
int err;
posix_spawn_file_actions_t file_actions;
/*
* Just do a bunch of random operations which should leave console
* output intact.
*/
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_adddup2(&file_actions, 1, 3);
posix_spawn_file_actions_adddup2(&file_actions, 1, 4);
posix_spawn_file_actions_adddup2(&file_actions, 1, 6);
posix_spawn_file_actions_adddup2(&file_actions, 1, 5);
posix_spawn_file_actions_addclose(&file_actions, 3);
posix_spawn_file_actions_addclose(&file_actions, 4);
posix_spawn_file_actions_addclose(&file_actions, 6);
posix_spawn_file_actions_addclose(&file_actions, 5);
posix_spawn_file_actions_addclose(&file_actions, 0);
posix_spawn_file_actions_addclose(&file_actions, 2);
posix_spawn_file_actions_addopen(&file_actions, 0, "/dev/null",
O_RDONLY, 0);
posix_spawn_file_actions_adddup2(&file_actions, 1, 2);
posix_spawn_file_actions_addclose(&file_actions, 1);
posix_spawn_file_actions_adddup2(&file_actions, 2, 1);
err = posix_spawn(NULL, "/bin/ls", &file_actions, NULL, args, NULL);
posix_spawn_file_actions_destroy(&file_actions);
if (err != 0)
e(1);
}
/* tests failures with file actions */
static void
test_posix_spawn_file_actions_failures(void)
{
char * const args[] = { "ls", "-la", NULL };
int err, i;
posix_spawn_file_actions_t file_actions;
/* Test bogus open */
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_addclose(&file_actions, 0);
posix_spawn_file_actions_addopen(&file_actions, 0, "t84_h_nonexist",
O_RDONLY, 0);
err = posix_spawn(NULL, "/bin/ls", &file_actions, NULL, args, NULL);
posix_spawn_file_actions_destroy(&file_actions);
if (err == 0)
e(1);
/* Test bogus dup2 */
for (i = 3; i < 10; i++) {
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_adddup2(&file_actions, i, i+1);
err = posix_spawn(NULL, "/bin/ls", &file_actions, NULL, args,
NULL);
posix_spawn_file_actions_destroy(&file_actions);
if (err == 0)
e(i-2);
}
/*
* Test bogus exec with dup2 (to mess with the pipe error reporting in
* posix_spawn.c)
*/
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_adddup2(&file_actions, 1, 3);
posix_spawn_file_actions_adddup2(&file_actions, 1, 4);
posix_spawn_file_actions_adddup2(&file_actions, 1, 6);
posix_spawn_file_actions_adddup2(&file_actions, 1, 5);
posix_spawn_file_actions_adddup2(&file_actions, 1, 7);
err = posix_spawn(NULL, "t84_h_nonexist", &file_actions, NULL, args,
NULL);
posix_spawn_file_actions_destroy(&file_actions);
if (err == 0)
e(9);
}
int
main(void)
{
int fd;
start(84);
subtest = 1;
fd = sink_stdout();
test_posix_spawn_ls();
test_posix_spawnp_ls();
restore_stdout(fd);
test_posix_spawn_missing();
test_posix_spawn_nonexec();
subtest = 2;
test_posix_spawn_child();
subtest = 3;
test_posix_spawnattr();
subtest = 4;
fd = sink_stdout();
test_posix_spawn_file_actions();
restore_stdout(fd);
subtest = 5;
test_posix_spawn_file_actions_failures();
/* TODO: Write/port more tests */
quit();
/* Not reached */
return -1;
}
|