From 59731dd69bb4be66c721b3de66fb2ab80bec3e7e Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 22 Apr 2018 01:02:27 +0200 Subject: Add helper program for running services We no longer need to keep entire scripts in init program (i.e. saving space) and reduce the code and complexity of the init program. The runsvc tool can later be extended to do more complex child setup, such as configuring namespaces or seccomp without adding complexity or memory footprint to init. Signed-off-by: David Oberhollenzer --- initd/runlst.c | 120 --------------------------------------------------------- 1 file changed, 120 deletions(-) delete mode 100644 initd/runlst.c (limited to 'initd/runlst.c') diff --git a/initd/runlst.c b/initd/runlst.c deleted file mode 100644 index 83823f9..0000000 --- a/initd/runlst.c +++ /dev/null @@ -1,120 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * Copyright (C) 2018 - David Oberhollenzer - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include -#include -#include -#include -#include - -#include "init.h" - -static int child_setup(const char *ctty) -{ - int fd; - - sigreset(); - - if (ctty != NULL) { - fd = open(ctty, O_RDWR); - if (fd < 0) { - perror(ctty); - return -1; - } - - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); - - setsid(); - - dup2(fd, STDIN_FILENO); - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); - close(fd); - } - - return 0; -} - -static NORETURN void argv_exec(exec_t *e) -{ - char **argv = alloca(e->argc + 1), *ptr; - int i; - - for (ptr = e->args, i = 0; i < e->argc; ++i, ptr += strlen(ptr) + 1) - argv[i] = ptr; - - argv[i] = NULL; - execvp(argv[0], argv); - perror(argv[0]); - exit(EXIT_FAILURE); -} - -int runlst_wait(exec_t *list, const char *ctty) -{ - pid_t ret, pid; - int status; - - for (; list != NULL; list = list->next) { - pid = fork(); - - if (pid == 0) { - if (child_setup(ctty)) - exit(EXIT_FAILURE); - argv_exec(list); - } - - if (pid == -1) { - perror("fork"); - return EXIT_FAILURE; - } - - do { - ret = waitpid(pid, &status, 0); - } while (ret != pid); - - if (!WIFEXITED(status)) - return EXIT_FAILURE; - - if (WEXITSTATUS(status) != EXIT_SUCCESS) - return WEXITSTATUS(status); - } - - return EXIT_SUCCESS; -} - -pid_t runlst(exec_t *list, const char *ctty) -{ - pid_t pid = fork(); - - if (pid == 0) { - if (child_setup(ctty)) - exit(EXIT_FAILURE); - - if (list->next != NULL) - exit(runlst_wait(list, NULL)); - - argv_exec(list); - } - - if (pid == -1) - perror("fork"); - - return pid; -} -- cgit v1.2.3