2021-03-08 04:14:07 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2022-10-12 12:26:58 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
2021-03-08 04:14:07 +00:00
|
|
|
#include "libc/errno.h"
|
2022-10-13 22:38:31 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2022-10-12 04:06:27 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-10-12 12:26:58 +00:00
|
|
|
#include "libc/stdio/posix_spawn.h"
|
|
|
|
#include "libc/stdio/posix_spawn.internal.h"
|
2022-10-13 22:38:31 +00:00
|
|
|
#include "libc/thread/thread.h"
|
|
|
|
#include "libc/thread/tls.h"
|
2022-10-12 12:26:58 +00:00
|
|
|
|
|
|
|
static int RunFileActions(struct _posix_faction *a) {
|
|
|
|
int t;
|
|
|
|
if (!a) return 0;
|
|
|
|
if (RunFileActions(a->next) == -1) return -1;
|
|
|
|
switch (a->action) {
|
|
|
|
case _POSIX_SPAWN_CLOSE:
|
|
|
|
return close(a->fildes);
|
|
|
|
case _POSIX_SPAWN_DUP2:
|
|
|
|
return dup2(a->fildes, a->newfildes);
|
|
|
|
case _POSIX_SPAWN_OPEN:
|
|
|
|
if ((t = open(a->path, a->oflag, a->mode)) == -1) return -1;
|
|
|
|
if (t != a->fildes) {
|
|
|
|
if (dup2(t, a->fildes) == -1) {
|
|
|
|
close(t);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (close(t) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 04:14:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Spawns process the POSIX way.
|
|
|
|
*
|
2022-10-12 12:26:58 +00:00
|
|
|
* @param pid if non-null shall be set to child pid on success
|
|
|
|
* @param path is resolved path of program which is not `$PATH` searched
|
|
|
|
* @param file_actions specifies close(), dup2(), and open() operations
|
|
|
|
* @param attrp specifies signal masks, user ids, scheduling, etc.
|
|
|
|
* @param envp is environment variables, or `environ` if null
|
2021-03-08 04:14:07 +00:00
|
|
|
* @return 0 on success or error number on failure
|
2022-10-12 12:26:58 +00:00
|
|
|
* @see posix_spawnp() for `$PATH` searching
|
2021-03-08 04:14:07 +00:00
|
|
|
*/
|
|
|
|
int posix_spawn(int *pid, const char *path,
|
|
|
|
const posix_spawn_file_actions_t *file_actions,
|
|
|
|
const posix_spawnattr_t *attrp, char *const argv[],
|
|
|
|
char *const envp[]) {
|
2022-10-12 12:26:58 +00:00
|
|
|
int s, child;
|
2021-03-08 04:14:07 +00:00
|
|
|
sigset_t allsigs;
|
|
|
|
struct sigaction dfl;
|
2022-10-13 22:38:31 +00:00
|
|
|
if (!(child = _weaken(pthread_create) ? fork() : vfork())) {
|
2022-10-12 12:26:58 +00:00
|
|
|
if (attrp && *attrp) {
|
|
|
|
if ((*attrp)->flags & POSIX_SPAWN_SETPGROUP) {
|
|
|
|
if (setpgid(0, (*attrp)->pgroup)) _Exit(127);
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
2022-10-12 12:26:58 +00:00
|
|
|
if ((*attrp)->flags & POSIX_SPAWN_SETSIGMASK) {
|
|
|
|
sigprocmask(SIG_SETMASK, &(*attrp)->sigmask, 0);
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
2022-10-12 12:26:58 +00:00
|
|
|
if ((*attrp)->flags & POSIX_SPAWN_RESETIDS) {
|
2021-03-08 04:14:07 +00:00
|
|
|
setuid(getuid());
|
|
|
|
setgid(getgid());
|
|
|
|
}
|
2022-10-12 12:26:58 +00:00
|
|
|
if ((*attrp)->flags & POSIX_SPAWN_SETSIGDEF) {
|
2021-03-08 04:14:07 +00:00
|
|
|
dfl.sa_handler = SIG_DFL;
|
|
|
|
dfl.sa_flags = 0;
|
|
|
|
sigfillset(&allsigs);
|
|
|
|
for (s = 0; sigismember(&allsigs, s); s++) {
|
2022-10-12 12:26:58 +00:00
|
|
|
if (sigismember(&(*attrp)->sigdefault, s)) {
|
2022-10-12 04:06:27 +00:00
|
|
|
if (sigaction(s, &dfl, 0) == -1) _Exit(127);
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file_actions) {
|
2022-10-12 12:26:58 +00:00
|
|
|
if (RunFileActions(*file_actions) == -1) {
|
|
|
|
_Exit(127);
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 12:26:58 +00:00
|
|
|
if (attrp && *attrp) {
|
|
|
|
if ((*attrp)->flags & POSIX_SPAWN_SETSCHEDULER) {
|
|
|
|
if (sched_setscheduler(0, (*attrp)->schedpolicy,
|
|
|
|
&(*attrp)->schedparam) == -1) {
|
2022-10-12 04:06:27 +00:00
|
|
|
if (errno != ENOSYS) _Exit(127);
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 12:26:58 +00:00
|
|
|
if ((*attrp)->flags & POSIX_SPAWN_SETSCHEDPARAM) {
|
|
|
|
if (sched_setparam(0, &(*attrp)->schedparam) == -1) {
|
2022-10-12 04:06:27 +00:00
|
|
|
if (errno != ENOSYS) _Exit(127);
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 04:06:27 +00:00
|
|
|
if (!envp) envp = environ;
|
2021-03-08 04:14:07 +00:00
|
|
|
execve(path, argv, envp);
|
2022-03-19 10:37:00 +00:00
|
|
|
_Exit(127);
|
2022-10-12 12:26:58 +00:00
|
|
|
} else if (child != -1) {
|
|
|
|
if (pid) *pid = child;
|
2021-03-08 04:14:07 +00:00
|
|
|
return 0;
|
2022-10-12 12:26:58 +00:00
|
|
|
} else {
|
|
|
|
return errno;
|
2021-03-08 04:14:07 +00:00
|
|
|
}
|
|
|
|
}
|