summaryrefslogtreecommitdiff
path: root/minix/lib/libc/sys/posix_spawn.c
blob: 467fd5989afd2d992ce0e440696d2458d5602279 (plain)
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
/*
 * Taken from newlib/libc/posix/posix_spawn.c
 */

/*-
 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
 * All rights reserved.
 *
 * 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 AUTHOR 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 AUTHOR 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 <sys/cdefs.h>

#include <sys/queue.h>
#include <sys/wait.h>

#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <spawn.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

extern char **environ;

/* Only deal with a pointer to environ, to work around subtle bugs with shared
   libraries and/or small data systems where the user declares his own
   'environ'.  */
static char ***p_environ = &environ;

/*
 * Spawn routines
 */

static int
process_spawnattr(const posix_spawnattr_t * sa)
{
	struct sigaction sigact = { .sa_flags = 0, .sa_handler = SIG_DFL };
	int i;

	/*
	 * POSIX doesn't really describe in which order everything
	 * should be set. We'll just set them in the order in which they
	 * are mentioned.
	 */

	/* Set process group */
	if (sa->sa_flags & POSIX_SPAWN_SETPGROUP) {
		if (setpgid(0, sa->sa_pgroup) != 0)
			return errno;
	}

	/* Set scheduler policy */
	/* XXX: We don't have scheduler policy for now */
#if 0
	if (sa->sa_flags & POSIX_SPAWN_SETSCHEDULER) {
		if (sched_setscheduler(0, sa->sa_schedpolicy,
		    &sa->sa_schedparam) != 0)
			return errno;
	} else if (sa->sa_flags & POSIX_SPAWN_SETSCHEDPARAM) {
		if (sched_setparam(0, &sa->sa_schedparam) != 0)
			return errno;
	}
#endif

	/* Reset user ID's */
	if (sa->sa_flags & POSIX_SPAWN_RESETIDS) {
		if (setegid(getgid()) != 0)
			return errno;
		if (seteuid(getuid()) != 0)
			return errno;
	}

	/* Set signal masks/defaults */
	if (sa->sa_flags & POSIX_SPAWN_SETSIGMASK) {
		sigprocmask(SIG_SETMASK, &sa->sa_sigmask, NULL);
	}

	if (sa->sa_flags & POSIX_SPAWN_SETSIGDEF) {
		for (i = 1; i < NSIG; i++) {
			if (sigismember(&sa->sa_sigdefault, i))
				if (sigaction(i, &sigact, NULL) != 0)
					return errno;
		}
	}

	return 0;
}

static int
move_fd_up(int * statusfd)
{
	/*
	 * Move given file descriptor on a higher fd number.
	 *
	 * This is used to hide the status file descriptor from the application
	 * by pushing it out of the way if it tries to use its number.
	 */
	int newstatusfd;

	newstatusfd = fcntl(*statusfd, F_DUPFD, *statusfd+1);
	if (newstatusfd == -1)
		return -1;

	close(*statusfd);
	*statusfd = newstatusfd;
	return 0;
}

static int
process_file_actions_entry(posix_spawn_file_actions_entry_t * fae,
	int * statusfd)
{
	int fd;

	switch (fae->fae_action) {
	case FAE_OPEN:
		/* Perform an open(), make it use the right fd */
		fd = open(fae->fae_path, fae->fae_oflag, fae->fae_mode);
		if (fd < 0)
			return errno;
		if (fd != fae->fae_fildes) {
			if (fae->fae_fildes == *statusfd) {
				/* Move the status fd out of the way */
				if (move_fd_up(statusfd) == -1)
					return errno;
			}
			if (dup2(fd, fae->fae_fildes) == -1)
				return errno;
			if (close(fd) != 0) {
				if (errno == EBADF)
					return EBADF;
			}
		}
		if (fcntl(fae->fae_fildes, F_SETFD, 0) == -1)
			return errno;
		break;

	case FAE_DUP2:
		if (fae->fae_fildes == *statusfd) {
			/* Nice try */
			return EBADF;
		}
		if (fae->fae_newfildes == *statusfd) {
			/* Move the status file descriptor out of the way */
			if (move_fd_up(statusfd) == -1)
				return errno;
		}
		/* Perform a dup2() */
		if (dup2(fae->fae_fildes, fae->fae_newfildes) == -1)
			return errno;
		if (fcntl(fae->fae_newfildes, F_SETFD, 0) == -1)
			return errno;
		break;

	case FAE_CLOSE:
		/* Perform a close(), do not fail if already closed */
		if (fae->fae_fildes != *statusfd)
			(void)close(fae->fae_fildes);
		break;
	}
	return 0;
}

static int
process_file_actions(const posix_spawn_file_actions_t * fa, int * statusfd)
{
	posix_spawn_file_actions_entry_t *fae;
	int error;

	/* Replay all file descriptor modifications */
	for (unsigned i = 0; i < fa->len; i++) {
		fae = &fa->fae[i];
		error = process_file_actions_entry(fae, statusfd);
		if (error)
			return error;
	}
	return 0;
}

int
posix_spawn(pid_t * __restrict pid, const char * __restrict path,
	const posix_spawn_file_actions_t * fa,
	const posix_spawnattr_t * __restrict sa,
	char * const * __restrict argv, char * const * __restrict envp)
{
	pid_t p;
	int r, error, pfd[2];

	/*
	 * Due to the lack of vfork() in Minix, an alternative solution with
	 * pipes is used. The writing end is set to close on exec() and the
	 * parent performs a read() on it.
	 *
	 * On success, a successful 0-length read happens.
	 * On failure, the child writes the errno to the pipe before exiting,
	 * the error is thus transmitted to the parent.
	 *
	 * This solution was taken from stackoverflow.com question 3703013.
	 */
	if (pipe(pfd) == -1)
		return errno;

	p = fork();
	switch (p) {
	case -1:
		close(pfd[0]);
		close(pfd[1]);

		return errno;

	case 0:
		close(pfd[0]);

		if (fcntl(pfd[1], F_SETFD, FD_CLOEXEC) != 0) {
			error = errno;
			break;
		}

		if (sa != NULL) {
			error = process_spawnattr(sa);
			if (error)
				break;
		}
		if (fa != NULL) {
			error = process_file_actions(fa, &pfd[1]);
			if (error)
				break;
		}

		(void)execve(path, argv, envp != NULL ? envp : *p_environ);

		error = errno;
		break;

	default:
		close(pfd[1]);

		/* Retrieve child process status through pipe. */
		r = read(pfd[0], &error, sizeof(error));
		if (r == 0)
			error = 0;
		else if (r == -1)
			error = errno;
		close(pfd[0]);

		if (error != 0)
			(void)waitpid(p, NULL, 0);

		if (pid != NULL)
			*pid = p;
		return error;
	}

	/* Child failed somewhere, propagate error through pipe and exit. */
	write(pfd[1], &error, sizeof(error));
	close(pfd[1]);
	_exit(127);
}