posix_spawn.c 2.6 KB
Newer Older
1 2 3 4
#include <spawn.h>
#include <unistd.h>
#include <signal.h>
#include <stdint.h>
5
#include <fcntl.h>
6
#include "syscall.h"
7
#include "pthread_impl.h"
8
#include "fdop.h"
9
#include "libc.h"
10 11 12

extern char **environ;

13 14 15 16 17 18
static void dummy_0()
{
}
weak_alias(dummy_0, __acquire_ptc);
weak_alias(dummy_0, __release_ptc);

19 20
pid_t __vfork(void);

21
int __posix_spawnx(pid_t *restrict res, const char *restrict path,
22
	int (*exec)(const char *, char *const *, char *const *),
23
	const posix_spawn_file_actions_t *fa,
24 25
	const posix_spawnattr_t *restrict attr,
	char *const argv[restrict], char *const envp[restrict])
26 27 28 29 30 31 32 33
{
	pid_t pid;
	sigset_t oldmask;
	int i;
	posix_spawnattr_t dummy_attr = { 0 };

	if (!attr) attr = &dummy_attr;

34
	sigprocmask(SIG_BLOCK, SIGALL_SET, &oldmask);
35 36

	__acquire_ptc();
37
	pid = __vfork();
38 39

	if (pid) {
R
Rich Felker 已提交
40
		__release_ptc();
41 42 43 44 45 46
		sigprocmask(SIG_SETMASK, &oldmask, 0);
		if (pid < 0) return -pid;
		*res = pid;
		return 0;
	}

47
	for (i=1; i<=8*__SYSCALL_SSLEN; i++) {
48
		struct sigaction sa;
49 50
		__libc_sigaction(i, 0, &sa);
		if (sa.sa_handler!=SIG_DFL && (sa.sa_handler!=SIG_IGN ||
51
		    ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
52
		     && sigismember(&attr->__def, i) ))) {
53
			sa.sa_handler = SIG_DFL;
54
			__libc_sigaction(i, &sa, 0);
55 56 57 58 59 60 61 62 63 64 65 66 67
		}
	}

	if ((attr->__flags&POSIX_SPAWN_SETPGROUP) && setpgid(0, attr->__pgrp))
		_exit(127);

	/* Use syscalls directly because pthread state is not consistent
	 * for making calls to the library wrappers... */
	if ((attr->__flags&POSIX_SPAWN_RESETIDS) && (
		__syscall(SYS_setgid, __syscall(SYS_getgid)) ||
		__syscall(SYS_setuid, __syscall(SYS_getuid)) ))
		_exit(127);

68
	if (fa && fa->__actions) {
69 70
		struct fdop *op;
		int ret, fd;
71 72
		for (op = fa->__actions; op->next; op = op->next);
		for (; op; op = op->prev) {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
			switch(op->cmd) {
			case FDOP_CLOSE:
				ret = __syscall(SYS_close, op->fd);
				break;
			case FDOP_DUP2:
				ret = __syscall(SYS_dup2, op->fd, op->newfd)<0;
				break;
			case FDOP_OPEN:
				fd = __syscall(SYS_open, op->path,
					op->oflag | O_LARGEFILE, op->mode);
				if (fd == op->fd) {
					ret = 0;
				} else {
					ret = __syscall(SYS_dup2, fd, op->fd)<0;
					__syscall(SYS_close, fd);
				}
				break;
			}
			if (ret) _exit(127);
		}
	}

95 96 97
	sigprocmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
		? &attr->__mask : &oldmask, 0);

98
	exec(path, argv, envp ? envp : environ);
99 100 101 102 103
	_exit(127);

	return 0;
}

104
int posix_spawn(pid_t *restrict res, const char *restrict path,
105
	const posix_spawn_file_actions_t *fa,
106 107
	const posix_spawnattr_t *restrict attr,
	char *const argv[restrict], char *const envp[restrict])
108
{
109
	return __posix_spawnx(res, path, execve, fa, attr, argv, envp);
110
}