2020-06-16 04:36:08 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Nico Weber <thakis@chromium.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-16 04:36:08 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* posix_spawn and friends
|
|
|
|
*
|
|
|
|
* values from POSIX standard unix specification
|
|
|
|
*
|
|
|
|
* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/spawn.h.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <spawn.h>
|
|
|
|
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/Vector.h>
|
2021-05-14 17:32:57 +03:00
|
|
|
#include <errno.h>
|
2020-06-19 17:46:45 +03:00
|
|
|
#include <fcntl.h>
|
2020-06-16 04:36:08 +03:00
|
|
|
#include <stdio.h>
|
2020-06-19 17:46:45 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <unistd.h>
|
2020-06-19 17:46:45 +03:00
|
|
|
|
|
|
|
struct posix_spawn_file_actions_state {
|
|
|
|
Vector<Function<int()>, 4> actions;
|
|
|
|
};
|
2020-06-16 04:36:08 +03:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
[[noreturn]] static void posix_spawn_child(char const* path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const argv[], char* const envp[], int (*exec)(char const*, char* const[], char* const[]))
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
if (attr) {
|
|
|
|
short flags = attr->flags;
|
2020-06-19 15:22:47 +03:00
|
|
|
if (flags & POSIX_SPAWN_RESETIDS) {
|
|
|
|
if (seteuid(getuid()) < 0) {
|
|
|
|
perror("posix_spawn seteuid");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-19 15:22:47 +03:00
|
|
|
}
|
|
|
|
if (setegid(getgid()) < 0) {
|
|
|
|
perror("posix_spawn setegid");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-19 15:22:47 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 04:36:08 +03:00
|
|
|
if (flags & POSIX_SPAWN_SETPGROUP) {
|
|
|
|
if (setpgid(0, attr->pgroup) < 0) {
|
|
|
|
perror("posix_spawn setpgid");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & POSIX_SPAWN_SETSCHEDPARAM) {
|
|
|
|
if (sched_setparam(0, &attr->schedparam) < 0) {
|
|
|
|
perror("posix_spawn sched_setparam");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & POSIX_SPAWN_SETSIGDEF) {
|
|
|
|
struct sigaction default_action;
|
|
|
|
default_action.sa_flags = 0;
|
|
|
|
sigemptyset(&default_action.sa_mask);
|
|
|
|
default_action.sa_handler = SIG_DFL;
|
|
|
|
|
|
|
|
sigset_t sigdefault = attr->sigdefault;
|
|
|
|
for (int i = 0; i < NSIG; ++i) {
|
|
|
|
if (sigismember(&sigdefault, i) && sigaction(i, &default_action, nullptr) < 0) {
|
|
|
|
perror("posix_spawn sigaction");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags & POSIX_SPAWN_SETSIGMASK) {
|
2020-06-19 23:28:39 +03:00
|
|
|
if (sigprocmask(SIG_SETMASK, &attr->sigmask, nullptr) < 0) {
|
2020-06-16 04:36:08 +03:00
|
|
|
perror("posix_spawn sigprocmask");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-19 23:28:43 +03:00
|
|
|
if (flags & POSIX_SPAWN_SETSID) {
|
|
|
|
if (setsid() < 0) {
|
|
|
|
perror("posix_spawn setsid");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-19 23:28:43 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 04:36:08 +03:00
|
|
|
|
2020-06-19 15:22:47 +03:00
|
|
|
// FIXME: POSIX_SPAWN_SETSCHEDULER
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:46:45 +03:00
|
|
|
if (file_actions) {
|
2022-04-01 20:58:27 +03:00
|
|
|
for (auto const& action : file_actions->state->actions) {
|
2020-06-19 17:46:45 +03:00
|
|
|
if (action() < 0) {
|
|
|
|
perror("posix_spawn file action");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-19 17:46:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 04:36:08 +03:00
|
|
|
exec(path, argv, envp);
|
|
|
|
perror("posix_spawn exec");
|
2020-06-19 23:44:44 +03:00
|
|
|
_exit(127);
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawn(pid_t* out_pid, char const* path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const argv[], char* const envp[])
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
if (child_pid < 0)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
if (child_pid != 0) {
|
|
|
|
*out_pid = child_pid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
posix_spawn_child(path, file_actions, attr, argv, envp, execve);
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnp.html
|
2022-04-03 23:59:53 +03:00
|
|
|
int posix_spawnp(pid_t* out_pid, char const* file, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const argv[], char* const envp[])
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
if (child_pid < 0)
|
|
|
|
return errno;
|
|
|
|
|
|
|
|
if (child_pid != 0) {
|
|
|
|
*out_pid = child_pid;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-03 23:59:53 +03:00
|
|
|
posix_spawn_child(file, file_actions, attr, argv, envp, execvpe);
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addchdir.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t* actions, char const* path)
|
2020-06-19 23:41:43 +03:00
|
|
|
{
|
|
|
|
actions->state->actions.append([path]() { return chdir(path); });
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int posix_spawn_file_actions_addfchdir(posix_spawn_file_actions_t* actions, int fd)
|
|
|
|
{
|
|
|
|
actions->state->actions.append([fd]() { return fchdir(fd); });
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addclose.html
|
2020-06-19 17:46:45 +03:00
|
|
|
int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
2020-06-19 17:46:45 +03:00
|
|
|
actions->state->actions.append([fd]() { return close(fd); });
|
|
|
|
return 0;
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_adddup2.html
|
2020-06-19 17:46:45 +03:00
|
|
|
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int old_fd, int new_fd)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
2020-06-19 17:46:45 +03:00
|
|
|
actions->state->actions.append([old_fd, new_fd]() { return dup2(old_fd, new_fd); });
|
|
|
|
return 0;
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_addopen.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions, int want_fd, char const* path, int flags, mode_t mode)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
2020-06-19 17:46:45 +03:00
|
|
|
actions->state->actions.append([want_fd, path, flags, mode]() {
|
|
|
|
int opened_fd = open(path, flags, mode);
|
|
|
|
if (opened_fd < 0 || opened_fd == want_fd)
|
|
|
|
return opened_fd;
|
|
|
|
if (int rc = dup2(opened_fd, want_fd); rc < 0)
|
|
|
|
return rc;
|
|
|
|
return close(opened_fd);
|
|
|
|
});
|
|
|
|
return 0;
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_destroy.html
|
2020-06-19 17:46:45 +03:00
|
|
|
int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
2020-06-19 17:46:45 +03:00
|
|
|
delete actions->state;
|
|
|
|
return 0;
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn_file_actions_init.html
|
2020-06-19 17:46:45 +03:00
|
|
|
int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
2020-06-19 17:46:45 +03:00
|
|
|
actions->state = new posix_spawn_file_actions_state;
|
|
|
|
return 0;
|
2020-06-16 04:36:08 +03:00
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_destroy.html
|
2020-06-16 04:36:08 +03:00
|
|
|
int posix_spawnattr_destroy(posix_spawnattr_t*)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getflags.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_getflags(posix_spawnattr_t const* attr, short* out_flags)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
*out_flags = attr->flags;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getpgroup.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_getpgroup(posix_spawnattr_t const* attr, pid_t* out_pgroup)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
*out_pgroup = attr->pgroup;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getschedparam.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_getschedparam(posix_spawnattr_t const* attr, struct sched_param* out_schedparam)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
*out_schedparam = attr->schedparam;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getschedpolicy.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_getschedpolicy(posix_spawnattr_t const* attr, int* out_schedpolicy)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
2022-01-07 15:04:05 +03:00
|
|
|
*out_schedpolicy = attr->schedpolicy;
|
2020-06-16 04:36:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigdefault.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_getsigdefault(posix_spawnattr_t const* attr, sigset_t* out_sigdefault)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
*out_sigdefault = attr->sigdefault;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigmask.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_getsigmask(posix_spawnattr_t const* attr, sigset_t* out_sigmask)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
*out_sigmask = attr->sigmask;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_init.html
|
2020-06-16 04:36:08 +03:00
|
|
|
int posix_spawnattr_init(posix_spawnattr_t* attr)
|
|
|
|
{
|
|
|
|
attr->flags = 0;
|
|
|
|
attr->pgroup = 0;
|
|
|
|
// attr->schedparam intentionally not written; its default value is unspecified.
|
|
|
|
// attr->schedpolicy intentionally not written; its default value is unspecified.
|
|
|
|
sigemptyset(&attr->sigdefault);
|
|
|
|
// attr->sigmask intentionally not written; its default value is unspecified.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setflags.html
|
2020-06-16 04:36:08 +03:00
|
|
|
int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags)
|
|
|
|
{
|
2020-06-19 23:28:43 +03:00
|
|
|
if (flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSID))
|
2020-06-16 04:36:08 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
attr->flags = flags;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setpgroup.html
|
2020-06-16 04:36:08 +03:00
|
|
|
int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup)
|
|
|
|
{
|
|
|
|
attr->pgroup = pgroup;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setschedparam.html
|
2020-06-16 04:36:08 +03:00
|
|
|
int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* schedparam)
|
|
|
|
{
|
|
|
|
attr->schedparam = *schedparam;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setschedpolicy.html
|
2020-06-16 04:36:08 +03:00
|
|
|
int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int schedpolicy)
|
|
|
|
{
|
|
|
|
attr->schedpolicy = schedpolicy;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setsigdefault.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, sigset_t const* sigdefault)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
attr->sigdefault = *sigdefault;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 02:59:15 +03:00
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_setsigmask.html
|
2022-04-01 20:58:27 +03:00
|
|
|
int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, sigset_t const* sigmask)
|
2020-06-16 04:36:08 +03:00
|
|
|
{
|
|
|
|
attr->sigmask = *sigmask;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|