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 +--- initd/Makemodule.am | 2 +- initd/init.h | 3 ++- lib/Makemodule.am | 6 +----- lib/include/service.h | 6 +++++- lib/include/util.h | 28 -------------------------- lib/init/rdsvc.c | 5 +++-- lib/init/svcmap.c | 4 ++-- lib/libcfg/rdline.c | 1 - lib/util/argv_exec.c | 51 ------------------------------------------------ 16 files changed, 67 insertions(+), 104 deletions(-) delete mode 100644 lib/include/util.h delete mode 100644 lib/util/argv_exec.c 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" diff --git a/initd/Makemodule.am b/initd/Makemodule.am index 9b2b7a6..c68e098 100644 --- a/initd/Makemodule.am +++ b/initd/Makemodule.am @@ -3,6 +3,6 @@ init_SOURCES += initd/status.c initd/supervisor.c initd/initsock.c init_CPPFLAGS = $(AM_CPPFLAGS) init_CFLAGS = $(AM_CFLAGS) init_LDFLAGS = $(AM_LDFLAGS) -init_LDADD = libinit.a libcfg.a libutil.a +init_LDADD = libinit.a libcfg.a sbin_PROGRAMS += init diff --git a/initd/init.h b/initd/init.h index 6b68ef6..9e8a1fd 100644 --- a/initd/init.h +++ b/initd/init.h @@ -17,11 +17,12 @@ #include #include #include +#include #include #include "initsock.h" #include "service.h" -#include "util.h" +#include "config.h" #define RUNSVCBIN SCRIPTDIR "/runsvc" diff --git a/lib/Makemodule.am b/lib/Makemodule.am index 8dcff06..d8b94c8 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -7,13 +7,9 @@ libinit_a_SOURCES += lib/init/init_socket_recv_status.c libinit_a_CPPFLAGS = $(AM_CPPFLAGS) libinit_a_CFLAGS = $(AM_CFLAGS) -libutil_a_SOURCES = lib/util/argv_exec.c lib/include/util.h -libutil_a_CPPFLAGS = $(AM_CPPFLAGS) -libutil_a_CFLAGS = $(AM_CFLAGS) - libcfg_a_SOURCES = lib/libcfg/rdline.c lib/libcfg/unescape.c lib/libcfg/rdcfg.c libcfg_a_SOURCES += lib/libcfg/pack_argv.c lib/include/libcfg.h libcfg_a_CPPFLAGS = $(AM_CPPFLAGS) libcfg_a_CFLAGS = $(AM_CFLAGS) -noinst_LIBRARIES += libinit.a libcfg.a libutil.a +noinst_LIBRARIES += libinit.a libcfg.a diff --git a/lib/include/service.h b/lib/include/service.h index cc01c5d..a05c7ee 100644 --- a/lib/include/service.h +++ b/lib/include/service.h @@ -4,7 +4,11 @@ #include -#include "util.h" +typedef struct exec_t { + struct exec_t *next; + int argc; /* number of elements in argument vector */ + char args[]; /* argument vectot string blob */ +} exec_t; enum { /* diff --git a/lib/include/util.h b/lib/include/util.h deleted file mode 100644 index efd89fc..0000000 --- a/lib/include/util.h +++ /dev/null @@ -1,28 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#ifndef UTIL_H -#define UTIL_H - -#include -#include -#include -#include - -#include "config.h" - -#ifdef __GNUC__ - #define NORETURN __attribute__((noreturn)) -#endif - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - -typedef struct exec_t { - struct exec_t *next; - int argc; /* number of elements in argument vector */ - char args[]; /* argument vectot string blob */ -} exec_t; - -int setup_tty(const char *tty, bool truncate); - -NORETURN void argv_exec(exec_t *e); - -#endif /* UTIL_H */ diff --git a/lib/init/rdsvc.c b/lib/init/rdsvc.c index 27ce5e5..40e73bc 100644 --- a/lib/init/rdsvc.c +++ b/lib/init/rdsvc.c @@ -11,7 +11,6 @@ #include "service.h" #include "libcfg.h" -#include "util.h" static int try_unescape(char *arg, rdline_t *rd) { @@ -265,8 +264,10 @@ service_t *rdsvc(int dirfd, const char *filename, int flags) memcpy(svc->name, filename, nlen); svc->id = -1; - if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags)) + if (rdcfg(svc, &rd, svc_params, + sizeof(svc_params) / sizeof(svc_params[0]), flags)) { goto fail; + } out: rdline_cleanup(&rd); diff --git a/lib/init/svcmap.c b/lib/init/svcmap.c index 6358a4f..de45914 100644 --- a/lib/init/svcmap.c +++ b/lib/init/svcmap.c @@ -23,7 +23,7 @@ int svc_type_from_string(const char *type) { size_t i; - for (i = 0; i < ARRAY_SIZE(type_map); ++i) { + for (i = 0; i < sizeof(type_map) / sizeof(type_map[0]); ++i) { if (strcmp(type_map[i], type) == 0) return i; } @@ -40,7 +40,7 @@ int svc_target_from_string(const char *target) { size_t i; - for (i = 0; i < ARRAY_SIZE(target_map); ++i) { + for (i = 0; i < sizeof(target_map) / sizeof(target_map[0]); ++i) { if (strcmp(target_map[i], target) == 0) return i; } diff --git a/lib/libcfg/rdline.c b/lib/libcfg/rdline.c index ee9ee8b..fb6f533 100644 --- a/lib/libcfg/rdline.c +++ b/lib/libcfg/rdline.c @@ -8,7 +8,6 @@ #include #include "libcfg.h" -#include "util.h" int rdline_init(rdline_t *t, int dirfd, const char *filename, int argc, const char *const *argv) diff --git a/lib/util/argv_exec.c b/lib/util/argv_exec.c deleted file mode 100644 index d39b35a..0000000 --- a/lib/util/argv_exec.c +++ /dev/null @@ -1,51 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include "service.h" - -#include -#include -#include -#include -#include - -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; -} - -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); -} -- cgit v1.2.3