From 34f542b7cc6e84403326acf145487a7d83175a11 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 4 Nov 2018 14:27:28 +0100 Subject: Seperate init specific code from utility code Signed-off-by: David Oberhollenzer --- lib/Makemodule.am | 16 ++- lib/include/service.h | 10 -- lib/include/util.h | 10 ++ lib/init/del_svc_list.c | 35 ++++++ lib/init/delsvc.c | 43 +++++++ lib/init/opensock.c | 50 ++++++++ lib/init/rdsvc.c | 297 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/init/svc_tsort.c | 95 ++++++++++++++++ lib/init/svcmap.c | 57 ++++++++++ lib/init/svcscan.c | 110 ++++++++++++++++++ lib/util/del_svc_list.c | 35 ------ lib/util/delsvc.c | 43 ------- lib/util/opensock.c | 50 -------- lib/util/rdsvc.c | 297 ------------------------------------------------ lib/util/svc_tsort.c | 95 ---------------- lib/util/svcmap.c | 57 ---------- lib/util/svcscan.c | 110 ------------------ 17 files changed, 707 insertions(+), 703 deletions(-) create mode 100644 lib/init/del_svc_list.c create mode 100644 lib/init/delsvc.c create mode 100644 lib/init/opensock.c create mode 100644 lib/init/rdsvc.c create mode 100644 lib/init/svc_tsort.c create mode 100644 lib/init/svcmap.c create mode 100644 lib/init/svcscan.c delete mode 100644 lib/util/del_svc_list.c delete mode 100644 lib/util/delsvc.c delete mode 100644 lib/util/opensock.c delete mode 100644 lib/util/rdsvc.c delete mode 100644 lib/util/svc_tsort.c delete mode 100644 lib/util/svcmap.c delete mode 100644 lib/util/svcscan.c (limited to 'lib') diff --git a/lib/Makemodule.am b/lib/Makemodule.am index 51bce72..66652bc 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -1,13 +1,17 @@ HEADRS = lib/include/util.h lib/include/service.h lib/include/telinit.h -libinit_a_SOURCES = lib/util/delsvc.c lib/util/svcmap.c lib/util/enum_by_name.c -libinit_a_SOURCES += lib/util/rdsvc.c lib/util/svcscan.c lib/util/mksock.c -libinit_a_SOURCES += lib/util/del_svc_list.c lib/util/svc_tsort.c -libinit_a_SOURCES += lib/util/opensock.c lib/util/enum_to_name.c -libinit_a_SOURCES += lib/util/print_version.c lib/util/argv_exec.c $(HEADRS) +libinit_a_SOURCES = lib/init/delsvc.c lib/init/svcmap.c lib/init/rdsvc.c +libinit_a_SOURCES += lib/init/svcscan.c lib/init/del_svc_list.c +libinit_a_SOURCES += lib/init/svc_tsort.c lib/init/opensock.c $(HEADRS) 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/mksock.c +libutil_a_SOURCES += lib/util/print_version.c $(HEADRS) +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) @@ -20,4 +24,4 @@ libcron_a_CFLAGS = $(AM_CFLAGS) EXTRA_DIST += $(HEADRS) lib/include/libcfg.h lib/include/crontab.h -noinst_LIBRARIES += libinit.a libcfg.a libcron.a +noinst_LIBRARIES += libinit.a libcfg.a libcron.a libutil.a diff --git a/lib/include/service.h b/lib/include/service.h index abe4968..ca96e58 100644 --- a/lib/include/service.h +++ b/lib/include/service.h @@ -57,12 +57,6 @@ enum { SVC_FLAG_TRUNCATE_OUT = 0x01, }; -typedef struct exec_t { - struct exec_t *next; - int argc; /* number of elements in argument vector */ - char args[]; /* argument vectot string blob */ -} exec_t; - typedef struct service_t { struct service_t *next; @@ -125,9 +119,5 @@ const char *svc_target_to_string(int target); int svc_target_from_string(const char *target); -int setup_tty(const char *tty, bool truncate); - -NORETURN void argv_exec(exec_t *e); - #endif /* SERVICE_H */ diff --git a/lib/include/util.h b/lib/include/util.h index 6d2bf5d..925a1f0 100644 --- a/lib/include/util.h +++ b/lib/include/util.h @@ -35,6 +35,12 @@ typedef struct { int value; } enum_map_t; +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 { /* only allow root to connect */ SOCK_FLAG_ROOT_ONLY = 0x01, @@ -73,5 +79,9 @@ int mksock(const char *path, int flags); /* 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); + #endif /* UTIL_H */ diff --git a/lib/init/del_svc_list.c b/lib/init/del_svc_list.c new file mode 100644 index 0000000..553a701 --- /dev/null +++ b/lib/init/del_svc_list.c @@ -0,0 +1,35 @@ +/* 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 "service.h" + +void del_svc_list(service_list_t *list) +{ + service_t *svc; + int i; + + for (i = 0; i < TGT_MAX; ++i) { + while (list->targets[i] != NULL) { + svc = list->targets[i]; + list->targets[i] = svc->next; + + delsvc(svc); + } + } +} diff --git a/lib/init/delsvc.c b/lib/init/delsvc.c new file mode 100644 index 0000000..9ab51f1 --- /dev/null +++ b/lib/init/delsvc.c @@ -0,0 +1,43 @@ +/* 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 "service.h" + +void delsvc(service_t *svc) +{ + exec_t *e; + + if (svc == NULL) + return; + + while (svc->exec != NULL) { + e = svc->exec; + svc->exec = e->next; + + free(e); + } + + free(svc->before); + free(svc->after); + free(svc->fname); + free(svc->desc); + free(svc->exec); + free(svc->ctty); + free(svc); +} diff --git a/lib/init/opensock.c b/lib/init/opensock.c new file mode 100644 index 0000000..0c2bf56 --- /dev/null +++ b/lib/init/opensock.c @@ -0,0 +1,50 @@ +/* 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 "telinit.h" + +int opensock(void) +{ + struct sockaddr_un un; + int fd; + + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { + perror("socket"); + return -1; + } + + memset(&un, 0, sizeof(un)); + un.sun_family = AF_UNIX; + + strcpy(un.sun_path, INITSOCK); + + if (connect(fd, (struct sockaddr *)&un, sizeof(un))) { + perror("connect: " INITSOCK); + close(fd); + return -1; + } + + return fd; +} diff --git a/lib/init/rdsvc.c b/lib/init/rdsvc.c new file mode 100644 index 0000000..ca60731 --- /dev/null +++ b/lib/init/rdsvc.c @@ -0,0 +1,297 @@ +/* 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 +#include +#include + +#include "service.h" +#include "libcfg.h" +#include "util.h" + +static int try_unescape(char *arg, rdline_t *rd) +{ + if (unescape(arg)) { + fprintf(stderr, "%s: %zu: malformed string constant\n", + rd->filename, rd->lineno); + return -1; + } + return 0; +} + +static char *try_strdup(const char *str, rdline_t *rd) +{ + char *out = strdup(str); + + if (out == NULL) { + fprintf(stderr, "%s: %zu: out of memory\n", + rd->filename, rd->lineno); + } + return out; +} + +static int try_pack_argv(char *str, rdline_t *rd) +{ + int count = pack_argv(str); + if (count < 0) { + fprintf(stderr, "%s: %zu: malformed string constant\n", + rd->filename, rd->lineno); + } + return count; +} + +static int svc_desc(void *user, char *arg, rdline_t *rd, int flags) +{ + service_t *svc = user; + (void)flags; + + if (try_unescape(arg, rd)) + return -1; + svc->desc = try_strdup(arg, rd); + return svc->desc == NULL ? -1 : 0; +} + +static int svc_tty(void *user, char *arg, rdline_t *rd, int flags) +{ + service_t *svc = user; + + if (flags & RDSVC_NO_CTTY) + return 0; + + if (strncmp(arg, "truncate", 8) == 0 && isspace(arg[8])) { + svc->flags |= SVC_FLAG_TRUNCATE_OUT; + arg += 8; + while (isspace(*arg)) + ++arg; + } + + if (try_unescape(arg, rd)) + return -1; + + svc->ctty = try_strdup(arg, rd); + return svc->ctty == NULL ? -1 : 0; +} + +static int svc_exec(void *user, char *arg, rdline_t *rd, int flags) +{ + service_t *svc = user; + exec_t *e, *end; + + if (flags & RDSVC_NO_EXEC) + return 0; + + e = calloc(1, sizeof(*e) + strlen(arg) + 1); + if (e == NULL) { + fprintf(stderr, "%s: %zu: out of memory\n", + rd->filename, rd->lineno); + return -1; + } + + strcpy(e->args, arg); + + e->argc = try_pack_argv(e->args, rd); + if (e->argc < 0) + return -1; + + if (svc->exec == NULL) { + svc->exec = e; + } else { + for (end = svc->exec; end->next != NULL; end = end->next) + ; + end->next = e; + } + return 0; +} + +static int svc_before(void *user, char *arg, rdline_t *rd, int flags) +{ + service_t *svc = user; + + if (flags & RDSVC_NO_DEPS) + return 0; + + if (svc->before != NULL) { + fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n", + rd->filename, rd->lineno); + return -1; + } + + svc->before = try_strdup(arg, rd); + if (svc->before == NULL) + return -1; + + svc->num_before = try_pack_argv(svc->before, rd); + return (svc->num_before < 0) ? -1 : 0; +} + +static int svc_after(void *user, char *arg, rdline_t *rd, int flags) +{ + service_t *svc = user; + + if (flags & RDSVC_NO_DEPS) + return 0; + + if (svc->after != NULL) { + fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n", + rd->filename, rd->lineno); + return -1; + } + + svc->after = try_strdup(arg, rd); + if (svc->after == NULL) + return -1; + + svc->num_after = try_pack_argv(svc->after, rd); + return (svc->num_after < 0) ? -1 : 0; +} + +static int svc_type(void *user, char *arg, rdline_t *rd, int flags) +{ + service_t *svc = user; + int count = try_pack_argv(arg, rd); + (void)flags; + + if (count < 1) + return -1; + + svc->type = svc_type_from_string(arg); + + if (svc->type == -1) { + fprintf(stderr, "%s: %zu: unknown service type '%s'\n", + rd->filename, rd->lineno, arg); + return -1; + } + + if (count > 1) { + arg += strlen(arg) + 1; + + switch (svc->type) { + case SVC_RESPAWN: + if (strcmp(arg, "limit") != 0) + goto fail_limit; + arg += strlen(arg) + 1; + + if (count > 3) + goto fail_args; + if (!isdigit(*arg)) + goto fail_limit; + svc->rspwn_limit = atoi(arg); + break; + default: + goto fail_args; + } + } + + return 0; +fail_args: + fprintf(stderr, "%s: %zu: unexpected extra arguments\n", + rd->filename, rd->lineno); + return -1; +fail_limit: + fprintf(stderr, "%s: %zu: expected 'limit ' after 'respawn'\n", + rd->filename, rd->lineno); + return -1; +} + +static int svc_target(void *user, char *arg, rdline_t *rd, int flags) +{ + service_t *svc = user; + int target; + (void)flags; + + if (try_unescape(arg, rd)) + return -1; + + target = svc_target_from_string(arg); + + if (target == -1) { + fprintf(stderr, "%s: %zu: unknown service target '%s'\n", + rd->filename, rd->lineno, arg); + return -1; + } + + svc->target = target; + return 0; +} + +static const cfg_param_t svc_params[] = { + { "description", 0, svc_desc }, + { "exec", 1, svc_exec }, + { "type", 0, svc_type }, + { "target", 0, svc_target }, + { "tty", 0, svc_tty }, + { "before", 0, svc_before }, + { "after", 0, svc_after }, +}; + +service_t *rdsvc(int dirfd, const char *filename, int flags) +{ + const char *arg, *args[1]; + service_t *svc = NULL; + size_t argc, nlen; + rdline_t rd; + int fd; + + fd = openat(dirfd, filename, O_RDONLY); + if (fd < 0) { + perror(filename); + return NULL; + } + + arg = strchr(filename, '@'); + if (arg != NULL) { + args[0] = arg + 1; + argc = 1; + } else { + argc = 0; + } + + nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename); + + svc = calloc(1, sizeof(*svc) + nlen + 1); + if (svc == NULL) + goto fail_oom; + + if (!(flags & RDSVC_NO_FNAME)) { + svc->fname = strdup(filename); + if (svc->fname == NULL) + goto fail_oom; + } + + memcpy(svc->name, filename, nlen); + + rdline_init(&rd, fd, filename, argc, args); + + if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags)) { + delsvc(svc); + svc = NULL; + } + + rdline_cleanup(&rd); + return svc; +fail_oom: + fputs("out of memory\n", stderr); + delsvc(svc); + close(fd); + return NULL; +} diff --git a/lib/init/svc_tsort.c b/lib/init/svc_tsort.c new file mode 100644 index 0000000..5f83fa0 --- /dev/null +++ b/lib/init/svc_tsort.c @@ -0,0 +1,95 @@ +/* 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 "service.h" + +static bool has_dependencies(service_t *list, service_t *svc) +{ + const char *ptr; + int i; + + while (list != NULL) { + for (ptr = svc->after, i = 0; i < svc->num_after; ++i) { + if (!strcmp(ptr, list->name)) + return true; + ptr += strlen(ptr) + 1; + } + + for (ptr = list->before, i = 0; i < list->num_before; ++i) { + if (!strcmp(ptr, svc->name)) + return true; + ptr += strlen(ptr) + 1; + } + + list = list->next; + } + + return false; +} + +service_t *svc_tsort(service_t *list) +{ + service_t *nl = NULL, *end = NULL; + service_t *svc, *prev; + + while (list != NULL) { + /* remove first service without dependencies */ + prev = NULL; + svc = list; + + while (svc != NULL) { + if (has_dependencies(list, svc)) { + prev = svc; + svc = svc->next; + } else { + if (prev != NULL) { + prev->next = svc->next; + } else { + list = svc->next; + } + svc->next = NULL; + break; + } + } + + /* cycle! */ + if (svc == NULL) { + if (end == NULL) { + nl = list; + } else { + end->next = list; + } + errno = ELOOP; + break; + } + + /* append to new list */ + if (end == NULL) { + nl = end = svc; + } else { + end->next = svc; + end = svc; + } + } + + return nl; +} diff --git a/lib/init/svcmap.c b/lib/init/svcmap.c new file mode 100644 index 0000000..41fb950 --- /dev/null +++ b/lib/init/svcmap.c @@ -0,0 +1,57 @@ +/* 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 "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 enum_map_t target_map[] = { + { "boot", TGT_BOOT }, + { "shutdown", TGT_SHUTDOWN }, + { "reboot", TGT_REBOOT }, + { NULL, 0 }, +}; + +const char *svc_type_to_string(int type) +{ + return enum_to_name(type_map, type); +} + +int svc_type_from_string(const char *type) +{ + const enum_map_t *ent = enum_by_name(type_map, type); + + return ent == NULL ? -1 : ent->value; +} + +const char *svc_target_to_string(int target) +{ + return enum_to_name(target_map, target); +} + +int svc_target_from_string(const char *target) +{ + const enum_map_t *ent = enum_by_name(target_map, target); + + return ent == NULL ? -1 : ent->value; +} diff --git a/lib/init/svcscan.c b/lib/init/svcscan.c new file mode 100644 index 0000000..f4c7ef1 --- /dev/null +++ b/lib/init/svcscan.c @@ -0,0 +1,110 @@ +/* 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 +#include +#include + +#include "service.h" + +int svcscan(const char *directory, service_list_t *list, int flags) +{ + int i, dfd, type, ret = 0; + struct dirent *ent; + const char *ptr; + service_t *svc; + struct stat sb; + DIR *dir; + + for (i = 0; i < TGT_MAX; ++i) + list->targets[i] = NULL; + + dir = opendir(directory); + if (dir == NULL) { + perror(directory); + return -1; + } + + dfd = dirfd(dir); + if (dfd < 0) { + perror(directory); + closedir(dir); + return -1; + } + + for (;;) { + errno = 0; + ent = readdir(dir); + + if (ent == NULL) { + if (errno != 0) { + perror(directory); + ret = -1; + } + break; + } + + for (ptr = ent->d_name; isalnum(*ptr) || *ptr == '_'; ++ptr) + ; + + if (*ptr != '\0' && *ptr != '@') + continue; + + if (fstatat(dfd, ent->d_name, &sb, AT_SYMLINK_NOFOLLOW)) { + fprintf(stderr, "stat %s/%s: %s\n", + directory, ent->d_name, strerror(errno)); + ret = -1; + continue; + } + + type = (sb.st_mode & S_IFMT); + + if (type != S_IFREG && type != S_IFLNK) + continue; + + svc = rdsvc(dfd, ent->d_name, flags); + if (svc == NULL) { + ret = -1; + continue; + } + + svc->next = list->targets[svc->target]; + list->targets[svc->target] = svc; + } + + for (i = 0; i < TGT_MAX; ++i) { + if (list->targets[i] == NULL) + continue; + + errno = 0; + list->targets[i] = svc_tsort(list->targets[i]); + + if (errno != 0) { + fprintf(stderr, "sorting services read from %s: %s\n", + directory, strerror(errno)); + } + } + + closedir(dir); + return ret; +} diff --git a/lib/util/del_svc_list.c b/lib/util/del_svc_list.c deleted file mode 100644 index 553a701..0000000 --- a/lib/util/del_svc_list.c +++ /dev/null @@ -1,35 +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 "service.h" - -void del_svc_list(service_list_t *list) -{ - service_t *svc; - int i; - - for (i = 0; i < TGT_MAX; ++i) { - while (list->targets[i] != NULL) { - svc = list->targets[i]; - list->targets[i] = svc->next; - - delsvc(svc); - } - } -} diff --git a/lib/util/delsvc.c b/lib/util/delsvc.c deleted file mode 100644 index 9ab51f1..0000000 --- a/lib/util/delsvc.c +++ /dev/null @@ -1,43 +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 "service.h" - -void delsvc(service_t *svc) -{ - exec_t *e; - - if (svc == NULL) - return; - - while (svc->exec != NULL) { - e = svc->exec; - svc->exec = e->next; - - free(e); - } - - free(svc->before); - free(svc->after); - free(svc->fname); - free(svc->desc); - free(svc->exec); - free(svc->ctty); - free(svc); -} diff --git a/lib/util/opensock.c b/lib/util/opensock.c deleted file mode 100644 index 0c2bf56..0000000 --- a/lib/util/opensock.c +++ /dev/null @@ -1,50 +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 "telinit.h" - -int opensock(void) -{ - struct sockaddr_un un; - int fd; - - fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (fd < 0) { - perror("socket"); - return -1; - } - - memset(&un, 0, sizeof(un)); - un.sun_family = AF_UNIX; - - strcpy(un.sun_path, INITSOCK); - - if (connect(fd, (struct sockaddr *)&un, sizeof(un))) { - perror("connect: " INITSOCK); - close(fd); - return -1; - } - - return fd; -} diff --git a/lib/util/rdsvc.c b/lib/util/rdsvc.c deleted file mode 100644 index ca60731..0000000 --- a/lib/util/rdsvc.c +++ /dev/null @@ -1,297 +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 -#include -#include - -#include "service.h" -#include "libcfg.h" -#include "util.h" - -static int try_unescape(char *arg, rdline_t *rd) -{ - if (unescape(arg)) { - fprintf(stderr, "%s: %zu: malformed string constant\n", - rd->filename, rd->lineno); - return -1; - } - return 0; -} - -static char *try_strdup(const char *str, rdline_t *rd) -{ - char *out = strdup(str); - - if (out == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", - rd->filename, rd->lineno); - } - return out; -} - -static int try_pack_argv(char *str, rdline_t *rd) -{ - int count = pack_argv(str); - if (count < 0) { - fprintf(stderr, "%s: %zu: malformed string constant\n", - rd->filename, rd->lineno); - } - return count; -} - -static int svc_desc(void *user, char *arg, rdline_t *rd, int flags) -{ - service_t *svc = user; - (void)flags; - - if (try_unescape(arg, rd)) - return -1; - svc->desc = try_strdup(arg, rd); - return svc->desc == NULL ? -1 : 0; -} - -static int svc_tty(void *user, char *arg, rdline_t *rd, int flags) -{ - service_t *svc = user; - - if (flags & RDSVC_NO_CTTY) - return 0; - - if (strncmp(arg, "truncate", 8) == 0 && isspace(arg[8])) { - svc->flags |= SVC_FLAG_TRUNCATE_OUT; - arg += 8; - while (isspace(*arg)) - ++arg; - } - - if (try_unescape(arg, rd)) - return -1; - - svc->ctty = try_strdup(arg, rd); - return svc->ctty == NULL ? -1 : 0; -} - -static int svc_exec(void *user, char *arg, rdline_t *rd, int flags) -{ - service_t *svc = user; - exec_t *e, *end; - - if (flags & RDSVC_NO_EXEC) - return 0; - - e = calloc(1, sizeof(*e) + strlen(arg) + 1); - if (e == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", - rd->filename, rd->lineno); - return -1; - } - - strcpy(e->args, arg); - - e->argc = try_pack_argv(e->args, rd); - if (e->argc < 0) - return -1; - - if (svc->exec == NULL) { - svc->exec = e; - } else { - for (end = svc->exec; end->next != NULL; end = end->next) - ; - end->next = e; - } - return 0; -} - -static int svc_before(void *user, char *arg, rdline_t *rd, int flags) -{ - service_t *svc = user; - - if (flags & RDSVC_NO_DEPS) - return 0; - - if (svc->before != NULL) { - fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n", - rd->filename, rd->lineno); - return -1; - } - - svc->before = try_strdup(arg, rd); - if (svc->before == NULL) - return -1; - - svc->num_before = try_pack_argv(svc->before, rd); - return (svc->num_before < 0) ? -1 : 0; -} - -static int svc_after(void *user, char *arg, rdline_t *rd, int flags) -{ - service_t *svc = user; - - if (flags & RDSVC_NO_DEPS) - return 0; - - if (svc->after != NULL) { - fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n", - rd->filename, rd->lineno); - return -1; - } - - svc->after = try_strdup(arg, rd); - if (svc->after == NULL) - return -1; - - svc->num_after = try_pack_argv(svc->after, rd); - return (svc->num_after < 0) ? -1 : 0; -} - -static int svc_type(void *user, char *arg, rdline_t *rd, int flags) -{ - service_t *svc = user; - int count = try_pack_argv(arg, rd); - (void)flags; - - if (count < 1) - return -1; - - svc->type = svc_type_from_string(arg); - - if (svc->type == -1) { - fprintf(stderr, "%s: %zu: unknown service type '%s'\n", - rd->filename, rd->lineno, arg); - return -1; - } - - if (count > 1) { - arg += strlen(arg) + 1; - - switch (svc->type) { - case SVC_RESPAWN: - if (strcmp(arg, "limit") != 0) - goto fail_limit; - arg += strlen(arg) + 1; - - if (count > 3) - goto fail_args; - if (!isdigit(*arg)) - goto fail_limit; - svc->rspwn_limit = atoi(arg); - break; - default: - goto fail_args; - } - } - - return 0; -fail_args: - fprintf(stderr, "%s: %zu: unexpected extra arguments\n", - rd->filename, rd->lineno); - return -1; -fail_limit: - fprintf(stderr, "%s: %zu: expected 'limit ' after 'respawn'\n", - rd->filename, rd->lineno); - return -1; -} - -static int svc_target(void *user, char *arg, rdline_t *rd, int flags) -{ - service_t *svc = user; - int target; - (void)flags; - - if (try_unescape(arg, rd)) - return -1; - - target = svc_target_from_string(arg); - - if (target == -1) { - fprintf(stderr, "%s: %zu: unknown service target '%s'\n", - rd->filename, rd->lineno, arg); - return -1; - } - - svc->target = target; - return 0; -} - -static const cfg_param_t svc_params[] = { - { "description", 0, svc_desc }, - { "exec", 1, svc_exec }, - { "type", 0, svc_type }, - { "target", 0, svc_target }, - { "tty", 0, svc_tty }, - { "before", 0, svc_before }, - { "after", 0, svc_after }, -}; - -service_t *rdsvc(int dirfd, const char *filename, int flags) -{ - const char *arg, *args[1]; - service_t *svc = NULL; - size_t argc, nlen; - rdline_t rd; - int fd; - - fd = openat(dirfd, filename, O_RDONLY); - if (fd < 0) { - perror(filename); - return NULL; - } - - arg = strchr(filename, '@'); - if (arg != NULL) { - args[0] = arg + 1; - argc = 1; - } else { - argc = 0; - } - - nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename); - - svc = calloc(1, sizeof(*svc) + nlen + 1); - if (svc == NULL) - goto fail_oom; - - if (!(flags & RDSVC_NO_FNAME)) { - svc->fname = strdup(filename); - if (svc->fname == NULL) - goto fail_oom; - } - - memcpy(svc->name, filename, nlen); - - rdline_init(&rd, fd, filename, argc, args); - - if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags)) { - delsvc(svc); - svc = NULL; - } - - rdline_cleanup(&rd); - return svc; -fail_oom: - fputs("out of memory\n", stderr); - delsvc(svc); - close(fd); - return NULL; -} diff --git a/lib/util/svc_tsort.c b/lib/util/svc_tsort.c deleted file mode 100644 index 5f83fa0..0000000 --- a/lib/util/svc_tsort.c +++ /dev/null @@ -1,95 +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 "service.h" - -static bool has_dependencies(service_t *list, service_t *svc) -{ - const char *ptr; - int i; - - while (list != NULL) { - for (ptr = svc->after, i = 0; i < svc->num_after; ++i) { - if (!strcmp(ptr, list->name)) - return true; - ptr += strlen(ptr) + 1; - } - - for (ptr = list->before, i = 0; i < list->num_before; ++i) { - if (!strcmp(ptr, svc->name)) - return true; - ptr += strlen(ptr) + 1; - } - - list = list->next; - } - - return false; -} - -service_t *svc_tsort(service_t *list) -{ - service_t *nl = NULL, *end = NULL; - service_t *svc, *prev; - - while (list != NULL) { - /* remove first service without dependencies */ - prev = NULL; - svc = list; - - while (svc != NULL) { - if (has_dependencies(list, svc)) { - prev = svc; - svc = svc->next; - } else { - if (prev != NULL) { - prev->next = svc->next; - } else { - list = svc->next; - } - svc->next = NULL; - break; - } - } - - /* cycle! */ - if (svc == NULL) { - if (end == NULL) { - nl = list; - } else { - end->next = list; - } - errno = ELOOP; - break; - } - - /* append to new list */ - if (end == NULL) { - nl = end = svc; - } else { - end->next = svc; - end = svc; - } - } - - return nl; -} diff --git a/lib/util/svcmap.c b/lib/util/svcmap.c deleted file mode 100644 index 41fb950..0000000 --- a/lib/util/svcmap.c +++ /dev/null @@ -1,57 +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 "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 enum_map_t target_map[] = { - { "boot", TGT_BOOT }, - { "shutdown", TGT_SHUTDOWN }, - { "reboot", TGT_REBOOT }, - { NULL, 0 }, -}; - -const char *svc_type_to_string(int type) -{ - return enum_to_name(type_map, type); -} - -int svc_type_from_string(const char *type) -{ - const enum_map_t *ent = enum_by_name(type_map, type); - - return ent == NULL ? -1 : ent->value; -} - -const char *svc_target_to_string(int target) -{ - return enum_to_name(target_map, target); -} - -int svc_target_from_string(const char *target) -{ - const enum_map_t *ent = enum_by_name(target_map, target); - - return ent == NULL ? -1 : ent->value; -} diff --git a/lib/util/svcscan.c b/lib/util/svcscan.c deleted file mode 100644 index f4c7ef1..0000000 --- a/lib/util/svcscan.c +++ /dev/null @@ -1,110 +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 -#include -#include - -#include "service.h" - -int svcscan(const char *directory, service_list_t *list, int flags) -{ - int i, dfd, type, ret = 0; - struct dirent *ent; - const char *ptr; - service_t *svc; - struct stat sb; - DIR *dir; - - for (i = 0; i < TGT_MAX; ++i) - list->targets[i] = NULL; - - dir = opendir(directory); - if (dir == NULL) { - perror(directory); - return -1; - } - - dfd = dirfd(dir); - if (dfd < 0) { - perror(directory); - closedir(dir); - return -1; - } - - for (;;) { - errno = 0; - ent = readdir(dir); - - if (ent == NULL) { - if (errno != 0) { - perror(directory); - ret = -1; - } - break; - } - - for (ptr = ent->d_name; isalnum(*ptr) || *ptr == '_'; ++ptr) - ; - - if (*ptr != '\0' && *ptr != '@') - continue; - - if (fstatat(dfd, ent->d_name, &sb, AT_SYMLINK_NOFOLLOW)) { - fprintf(stderr, "stat %s/%s: %s\n", - directory, ent->d_name, strerror(errno)); - ret = -1; - continue; - } - - type = (sb.st_mode & S_IFMT); - - if (type != S_IFREG && type != S_IFLNK) - continue; - - svc = rdsvc(dfd, ent->d_name, flags); - if (svc == NULL) { - ret = -1; - continue; - } - - svc->next = list->targets[svc->target]; - list->targets[svc->target] = svc; - } - - for (i = 0; i < TGT_MAX; ++i) { - if (list->targets[i] == NULL) - continue; - - errno = 0; - list->targets[i] = svc_tsort(list->targets[i]); - - if (errno != 0) { - fprintf(stderr, "sorting services read from %s: %s\n", - directory, strerror(errno)); - } - } - - closedir(dir); - return ret; -} -- cgit v1.2.3