diff options
-rw-r--r-- | cmd/Makemodule.am | 1 | ||||
-rw-r--r-- | cmd/shutdown.c | 5 | ||||
-rw-r--r-- | lib/Makemodule.am | 4 | ||||
-rw-r--r-- | lib/include/service.h | 2 | ||||
-rw-r--r-- | lib/include/util.h | 28 | ||||
-rw-r--r-- | lib/init/svcmap.c | 42 | ||||
-rw-r--r-- | lib/libcfg/rdline.c | 15 | ||||
-rw-r--r-- | lib/util/enum_by_name.c | 16 | ||||
-rw-r--r-- | lib/util/enum_to_name.c | 16 | ||||
-rw-r--r-- | lib/util/fopenat.c | 54 | ||||
-rw-r--r-- | lib/util/print_version.c | 18 |
11 files changed, 41 insertions, 160 deletions
diff --git a/cmd/Makemodule.am b/cmd/Makemodule.am index 07b50ce..c503215 100644 --- a/cmd/Makemodule.am +++ b/cmd/Makemodule.am @@ -2,7 +2,6 @@ shutdown_SOURCES = cmd/shutdown.c shutdown_CPPFLAGS = $(AM_CPPFLAGS) shutdown_CFLAGS = $(AM_CFLAGS) shutdown_LDFLAGS = $(AM_LDFLAGS) -shutdown_LDADD = libutil.a runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h runsvc_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/cmd/shutdown.c b/cmd/shutdown.c index cc14e61..546b076 100644 --- a/cmd/shutdown.c +++ b/cmd/shutdown.c @@ -17,7 +17,6 @@ static const struct option options[] = { { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'V' }, { "poweroff", no_argument, NULL, 'p' }, { "reboot", no_argument, NULL, 'r' }, { "force", no_argument, NULL, 'f' }, @@ -25,7 +24,7 @@ static const struct option options[] = { { NULL, 0, NULL, 0 }, }; -static const char *shortopt = "hVprfn"; +static const char *shortopt = "hprfn"; static const char *defact_str = "power-off"; static int defact = RB_POWER_OFF; @@ -78,8 +77,6 @@ int main(int argc, char **argv) case 'r': defact = RB_AUTOBOOT; break; - case 'V': - print_version(ptr); case 'h': usage(ptr, EXIT_SUCCESS); default: diff --git a/lib/Makemodule.am b/lib/Makemodule.am index a60b9f2..258cfe3 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -4,9 +4,7 @@ libinit_a_SOURCES += lib/init/svc_tsort.c lib/include/service.h libinit_a_CPPFLAGS = $(AM_CPPFLAGS) libinit_a_CFLAGS = $(AM_CFLAGS) -libutil_a_SOURCES = lib/util/argv_exec.c lib/util/enum_by_name.c -libutil_a_SOURCES += lib/util/enum_to_name.c lib/util/print_version.c -libutil_a_SOURCES += lib/util/fopenat.c lib/include/util.h +libutil_a_SOURCES = lib/util/argv_exec.c lib/include/util.h libutil_a_CPPFLAGS = $(AM_CPPFLAGS) libutil_a_CFLAGS = $(AM_CFLAGS) diff --git a/lib/include/service.h b/lib/include/service.h index 594f8ca..e46cd0e 100644 --- a/lib/include/service.h +++ b/lib/include/service.h @@ -19,6 +19,8 @@ enum { it terminates. */ SVC_RESPAWN, + + SVC_MAX }; enum { diff --git a/lib/include/util.h b/lib/include/util.h index 90ca902..8550695 100644 --- a/lib/include/util.h +++ b/lib/include/util.h @@ -15,11 +15,6 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -typedef struct { - const char *name; - int value; -} enum_map_t; - typedef struct exec_t { struct exec_t *next; int argc; /* number of elements in argument vector */ @@ -37,31 +32,8 @@ enum { SOCK_FLAG_DGRAM = 0x04, }; -/* - Search through an array of enum_map_t entries to resolve a string to - a numeric value. The end of the map is indicated by a sentinel entry - with the name set to NULL. -*/ -const enum_map_t *enum_by_name(const enum_map_t *map, const char *name); - -/* - Search through an array of enum_map_t entries to resolve a numeric - value to a string name. The end of the map is indicated by a sentinel - entry with the name set to NULL. -*/ -const char *enum_to_name(const enum_map_t *map, int value); - -/* print a default version info and license string */ -NORETURN void print_version(const char *program); - int setup_tty(const char *tty, bool truncate); NORETURN void argv_exec(exec_t *e); -/* - Similar to openat: opens a file relative to a dirfd, but returns - a FILE pointer instead of an fd. - */ -FILE *fopenat(int fd, const char *filename, const char *mode); - #endif /* UTIL_H */ diff --git a/lib/init/svcmap.c b/lib/init/svcmap.c index fa2932d..6358a4f 100644 --- a/lib/init/svcmap.c +++ b/lib/init/svcmap.c @@ -1,41 +1,49 @@ /* SPDX-License-Identifier: ISC */ +#include <string.h> #include "service.h" -#include "util.h" -static const enum_map_t type_map[] = { - { "once", SVC_ONCE }, - { "wait", SVC_WAIT }, - { "respawn", SVC_RESPAWN }, - { NULL, 0 }, +static const char *type_map[] = { + "once", + "wait", + "respawn", }; -static const enum_map_t target_map[] = { - { "boot", TGT_BOOT }, - { "shutdown", TGT_SHUTDOWN }, - { "reboot", TGT_REBOOT }, - { NULL, 0 }, +static const char *target_map[] = { + "boot", + "shutdown", + "reboot", }; const char *svc_type_to_string(int type) { - return enum_to_name(type_map, type); + return type >= 0 && type < SVC_MAX ? type_map[type] : NULL; } int svc_type_from_string(const char *type) { - const enum_map_t *ent = enum_by_name(type_map, type); + size_t i; - return ent == NULL ? -1 : ent->value; + for (i = 0; i < ARRAY_SIZE(type_map); ++i) { + if (strcmp(type_map[i], type) == 0) + return i; + } + + return -1; } const char *svc_target_to_string(int target) { - return enum_to_name(target_map, target); + return target >= 0 && target < TGT_MAX ? target_map[target] : NULL; } int svc_target_from_string(const char *target) { - const enum_map_t *ent = enum_by_name(target_map, target); + size_t i; + + for (i = 0; i < ARRAY_SIZE(target_map); ++i) { + if (strcmp(target_map[i], target) == 0) + return i; + } - return ent == NULL ? -1 : ent->value; + return -1; } diff --git a/lib/libcfg/rdline.c b/lib/libcfg/rdline.c index ab7b101..ee9ee8b 100644 --- a/lib/libcfg/rdline.c +++ b/lib/libcfg/rdline.c @@ -5,6 +5,7 @@ #include <errno.h> #include <ctype.h> #include <stdio.h> +#include <fcntl.h> #include "libcfg.h" #include "util.h" @@ -12,18 +13,26 @@ int rdline_init(rdline_t *t, int dirfd, const char *filename, int argc, const char *const *argv) { + int fd = openat(dirfd, filename, O_RDONLY); + + if (fd == -1) + goto fail_open; + memset(t, 0, sizeof(*t)); - t->fp = fopenat(dirfd, filename, "r"); + t->fp = fdopen(fd, "r"); if (t->fp == NULL) { - perror(filename); - return -1; + close(fd); + goto fail_open; } t->filename = filename; t->argc = argc; t->argv = argv; return 0; +fail_open: + perror(filename); + return -1; } void rdline_cleanup(rdline_t *t) diff --git a/lib/util/enum_by_name.c b/lib/util/enum_by_name.c deleted file mode 100644 index fa0131b..0000000 --- a/lib/util/enum_by_name.c +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include <string.h> - -#include "util.h" - -const enum_map_t *enum_by_name(const enum_map_t *map, const char *name) -{ - size_t i; - - for (i = 0; map[i].name != NULL; ++i) { - if (!strcmp(map[i].name, name)) - return map + i; - } - - return NULL; -} diff --git a/lib/util/enum_to_name.c b/lib/util/enum_to_name.c deleted file mode 100644 index 70e3c2f..0000000 --- a/lib/util/enum_to_name.c +++ /dev/null @@ -1,16 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include <string.h> - -#include "util.h" - -const char *enum_to_name(const enum_map_t *map, int value) -{ - size_t i; - - for (i = 0; map[i].name != NULL; ++i) { - if (map[i].value == value) - return map[i].name; - } - - return NULL; -} diff --git a/lib/util/fopenat.c b/lib/util/fopenat.c deleted file mode 100644 index d47c002..0000000 --- a/lib/util/fopenat.c +++ /dev/null @@ -1,54 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> -#include <fcntl.h> -#include <stdio.h> -#include <errno.h> - -#include "util.h" - -FILE *fopenat(int dirfd, const char *filename, const char *mode) -{ - const char *ptr = mode; - int fd, flags = 0; - FILE *fp; - - switch (*(ptr++)) { - case 'r': - flags = O_RDONLY; - break; - case 'w': - flags = O_WRONLY | O_CREAT | O_TRUNC; - break; - case 'a': - flags = O_WRONLY | O_CREAT | O_APPEND; - break; - default: - errno = EINVAL; - return NULL; - } - - if (*ptr == '+') { - flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; - ++ptr; - } - - if (*ptr == 'b') - ++ptr; - - if (*ptr != '\0') { - errno = EINVAL; - return NULL; - } - - fd = openat(dirfd, filename, flags, 0644); - if (fd == -1) - return NULL; - - fp = fdopen(fd, mode); - if (fp == NULL) - close(fd); - - return fp; -} diff --git a/lib/util/print_version.c b/lib/util/print_version.c deleted file mode 100644 index 47508b5..0000000 --- a/lib/util/print_version.c +++ /dev/null @@ -1,18 +0,0 @@ -/* SPDX-License-Identifier: ISC */ -#include <stdlib.h> -#include <stdio.h> - -#include "config.h" -#include "util.h" - -static const char *version_string = -"%s (pygos init) " PACKAGE_VERSION "\n" -"Copyright (C) 2018 David Oberhollenzer\n\n" -"This is free software: you are free to change and redistribute it.\n" -"There is NO WARRANTY, to the extent permitted by law.\n"; - -void print_version(const char *program) -{ - fprintf(stdout, version_string, program); - exit(EXIT_SUCCESS); -} |