From 87a524d9313428d55e5a04c2538042629bdc467a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 31 Mar 2020 18:19:27 +0200 Subject: cleanup: delete remains of libutil - exec_t belongs to service.h, the main place where it is used/needed - code for executing exec_t is moved to runsvc for the same reason - what is left are NORETURN and ARRAY_SIZE - the former can be replaced with direct attribute usage since the only relevant compilers all support the attribute. - the later is only used in 3 places and can be trivially replaced with direct usage of sizeof(). Signed-off-by: David Oberhollenzer --- cmd/Makemodule.am | 4 ++-- cmd/killall5.c | 4 +--- cmd/runsvc/runsvc.c | 43 +++++++++++++++++++++++++++++++++++++++++++ cmd/runsvc/runsvc.h | 4 +++- cmd/service/servicecmd.c | 1 - cmd/service/servicecmd.h | 5 +++-- cmd/shutdown.c | 4 +--- 7 files changed, 53 insertions(+), 12 deletions(-) (limited to 'cmd') diff --git a/cmd/Makemodule.am b/cmd/Makemodule.am index d6ed4fc..0af619f 100644 --- a/cmd/Makemodule.am +++ b/cmd/Makemodule.am @@ -7,7 +7,7 @@ runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h runsvc_CPPFLAGS = $(AM_CPPFLAGS) runsvc_CFLAGS = $(AM_CFLAGS) runsvc_LDFLAGS = $(AM_LDFLAGS) -runsvc_LDADD = libinit.a libcfg.a libutil.a +runsvc_LDADD = libinit.a libcfg.a killall5_SOURCES = cmd/killall5.c killall5_CPPFLAGS = $(AM_CPPFLAGS) @@ -30,7 +30,7 @@ service_SOURCES += $(SRVHEADERS) service_CPPFLAGS = $(AM_CPPFLAGS) service_CFLAGS = $(AM_CFLAGS) service_LDFLAGS = $(AM_LDFLAGS) -service_LDADD = libinit.a libcfg.a libutil.a +service_LDADD = libinit.a libcfg.a dist_man8_MANS += cmd/shutdown.8 cmd/service/service.8 diff --git a/cmd/killall5.c b/cmd/killall5.c index ccab429..733c0eb 100644 --- a/cmd/killall5.c +++ b/cmd/killall5.c @@ -9,9 +9,7 @@ #include #include -#include "util.h" - -static NORETURN void usage_and_exit(void) +static __attribute__((noreturn)) void usage_and_exit(void) { fputs("Usage: killall5 SIGNAL\n", stderr); exit(EXIT_FAILURE); diff --git a/cmd/runsvc/runsvc.c b/cmd/runsvc/runsvc.c index 794aa29..75fdc46 100644 --- a/cmd/runsvc/runsvc.c +++ b/cmd/runsvc/runsvc.c @@ -1,6 +1,49 @@ /* SPDX-License-Identifier: ISC */ #include "runsvc.h" +static int setup_tty(const char *tty, bool truncate) +{ + int fd; + + if (tty == NULL) + return 0; + + fd = open(tty, O_RDWR); + if (fd < 0) { + perror(tty); + return -1; + } + + if (truncate) + ftruncate(fd, 0); + + 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 __attribute__((noreturn)) void argv_exec(exec_t *e) +{ + char **argv = alloca(sizeof(char *) * (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); +} + static int run_sequentially(exec_t *list) { pid_t ret, pid; diff --git a/cmd/runsvc/runsvc.h b/cmd/runsvc/runsvc.h index 9f1a946..2a6ae49 100644 --- a/cmd/runsvc/runsvc.h +++ b/cmd/runsvc/runsvc.h @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include #include @@ -12,7 +14,7 @@ #include "service.h" #include "libcfg.h" -#include "util.h" +#include "config.h" #define ENVFILE ETCPATH "/initd.env" diff --git a/cmd/service/servicecmd.c b/cmd/service/servicecmd.c index 799a6f1..55afaf5 100644 --- a/cmd/service/servicecmd.c +++ b/cmd/service/servicecmd.c @@ -5,7 +5,6 @@ #include "servicecmd.h" #include "service.h" #include "config.h" -#include "util.h" command_t *commands; diff --git a/cmd/service/servicecmd.h b/cmd/service/servicecmd.h index e7cc998..d65c4b5 100644 --- a/cmd/service/servicecmd.h +++ b/cmd/service/servicecmd.h @@ -2,13 +2,14 @@ #ifndef SERVICECMD_H #define SERVICECMD_H +#include #include #include #include #include #include "service.h" -#include "util.h" +#include "config.h" /* Describes a command that can be launched by passing its name as @@ -45,7 +46,7 @@ service_t *loadsvc(const char *directory, const char *filename, int flags); Implemented in servicecmd.c. Prints program usage message and terminates with the given exit status. */ -void usage(int status) NORETURN; +void usage(int status) __attribute__((noreturn)); /* Write a message to stderr that advises the user how to consult the diff --git a/cmd/shutdown.c b/cmd/shutdown.c index 546b076..afceec2 100644 --- a/cmd/shutdown.c +++ b/cmd/shutdown.c @@ -10,8 +10,6 @@ #include #include -#include "util.h" - #define FL_FORCE 0x01 #define FL_NOSYNC 0x02 @@ -29,7 +27,7 @@ static const char *shortopt = "hprfn"; static const char *defact_str = "power-off"; static int defact = RB_POWER_OFF; -static NORETURN void usage(const char *progname, int status) +static __attribute__((noreturn)) void usage(const char *progname, int status) { fprintf(status == EXIT_SUCCESS ? stdout : stderr, "%s [OPTIONS...]\n\n" -- cgit v1.2.3