From a71c92b33dc69bc6cbc5eae161f82a5bca27a82f Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 25 Mar 2018 12:14:06 +0200 Subject: Unify naming of service to shorthand svc Signed-off-by: David Oberhollenzer --- initd/main.c | 14 +-- lib/Makemodule.am | 6 +- lib/include/service.h | 10 +- lib/src/del_srv_list.c | 35 ------- lib/src/del_svc_list.c | 35 +++++++ lib/src/delsrv.c | 42 --------- lib/src/delsvc.c | 42 +++++++++ lib/src/rdsrv.c | 251 ------------------------------------------------- lib/src/rdsvc.c | 251 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/src/srv_tsort.c | 92 ------------------ lib/src/srvscan.c | 110 ---------------------- lib/src/svc_tsort.c | 92 ++++++++++++++++++ lib/src/svcscan.c | 110 ++++++++++++++++++++++ servicecmd/list.c | 4 +- 14 files changed, 547 insertions(+), 547 deletions(-) delete mode 100644 lib/src/del_srv_list.c create mode 100644 lib/src/del_svc_list.c delete mode 100644 lib/src/delsrv.c create mode 100644 lib/src/delsvc.c delete mode 100644 lib/src/rdsrv.c create mode 100644 lib/src/rdsvc.c delete mode 100644 lib/src/srv_tsort.c delete mode 100644 lib/src/srvscan.c create mode 100644 lib/src/svc_tsort.c create mode 100644 lib/src/svcscan.c diff --git a/initd/main.c b/initd/main.c index be9fc0d..d3e22b6 100644 --- a/initd/main.c +++ b/initd/main.c @@ -39,14 +39,14 @@ static void handle_exited(service_t *svc) switch (svc->type) { case SVC_RESPAWN: if (target == TGT_REBOOT || target == TGT_SHUTDOWN) { - delsrv(svc); + delsvc(svc); break; } svc->pid = runlst(svc->exec, svc->num_exec, svc->ctty); if (svc->pid == -1) { print_status(svc->desc, STATUS_FAIL, false); - delsrv(svc); + delsvc(svc); } svclist_add(svc); @@ -57,7 +57,7 @@ static void handle_exited(service_t *svc) STATUS_OK : STATUS_FAIL, false); /* fall-through */ default: - delsrv(svc); + delsvc(svc); break; } } @@ -107,7 +107,7 @@ static void start_runlevel(int level) if (!svc->num_exec) { print_status(svc->desc, STATUS_OK, false); - delsrv(svc); + delsvc(svc); continue; } @@ -121,12 +121,12 @@ static void start_runlevel(int level) status == EXIT_SUCCESS ? STATUS_OK : STATUS_FAIL, true); - delsrv(svc); + delsvc(svc); } else { svc->pid = runlst(svc->exec, svc->num_exec, svc->ctty); if (svc->pid == -1) { print_status(svc->desc, STATUS_FAIL, false); - delsrv(svc); + delsvc(svc); continue; } @@ -196,7 +196,7 @@ int main(void) if (reboot(LINUX_REBOOT_CMD_CAD_OFF)) perror("cannot disable CTRL+ALT+DEL"); - if (srvscan(SVCDIR, &cfg)) { + if (svcscan(SVCDIR, &cfg)) { fputs("Error reading service list from " SVCDIR "\n" "Trying to continue anyway\n", stderr); } diff --git a/lib/Makemodule.am b/lib/Makemodule.am index a9d7e0c..1d7da73 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -1,9 +1,9 @@ HEADRS = lib/include/util.h lib/include/service.h lib/include/telinit.h -libinit_a_SOURCES = lib/src/delsrv.c lib/src/rdline.c lib/src/svcmap.c +libinit_a_SOURCES = lib/src/delsvc.c lib/src/rdline.c lib/src/svcmap.c libinit_a_SOURCES += lib/src/splitkv.c lib/src/enum_by_name.c -libinit_a_SOURCES += lib/src/strexpand.c lib/src/rdsrv.c lib/src/srvscan.c -libinit_a_SOURCES += lib/src/del_srv_list.c lib/src/srv_tsort.c +libinit_a_SOURCES += lib/src/strexpand.c lib/src/rdsvc.c lib/src/svcscan.c +libinit_a_SOURCES += lib/src/del_svc_list.c lib/src/svc_tsort.c libinit_a_SOURCES += lib/src/opensock.c lib/src/enum_to_name.c $(HEADRS) libinit_a_CPPFLAGS = $(AM_CPPFLAGS) libinit_a_CFLAGS = $(AM_CFLAGS) diff --git a/lib/include/service.h b/lib/include/service.h index dd7700c..28d73f1 100644 --- a/lib/include/service.h +++ b/lib/include/service.h @@ -71,9 +71,9 @@ typedef struct { /* Read a service from a file. */ -service_t *rdsrv(int dirfd, const char *filename); +service_t *rdsvc(int dirfd, const char *filename); -void delsrv(service_t *srv); +void delsvc(service_t *svc); /* Rebuild a service list by scanning a directory and parsing all @@ -82,14 +82,14 @@ void delsrv(service_t *srv); Returns 0 on success, -1 on failure. The function takes care of printing error messages on failure. */ -int srvscan(const char *directory, service_list_t *list); +int svcscan(const char *directory, service_list_t *list); -void del_srv_list(service_list_t *list); +void del_svc_list(service_list_t *list); /* Sort a list of services by dependencies. */ -service_t *srv_tsort(service_t *list); +service_t *svc_tsort(service_t *list); const char *svc_type_to_string(int type); diff --git a/lib/src/del_srv_list.c b/lib/src/del_srv_list.c deleted file mode 100644 index d54c40b..0000000 --- a/lib/src/del_srv_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_srv_list(service_list_t *list) -{ - service_t *srv; - int i; - - for (i = 0; i < TGT_MAX; ++i) { - while (list->targets[i] != NULL) { - srv = list->targets[i]; - list->targets[i] = srv->next; - - delsrv(srv); - } - } -} diff --git a/lib/src/del_svc_list.c b/lib/src/del_svc_list.c new file mode 100644 index 0000000..553a701 --- /dev/null +++ b/lib/src/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/src/delsrv.c b/lib/src/delsrv.c deleted file mode 100644 index 87b65bc..0000000 --- a/lib/src/delsrv.c +++ /dev/null @@ -1,42 +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 delsrv(service_t *srv) -{ - size_t i; - - for (i = 0; i < srv->num_exec; ++i) - free(srv->exec[i]); - - for (i = 0; i < srv->num_before; ++i) - free(srv->before[i]); - - for (i = 0; i < srv->num_after; ++i) - free(srv->after[i]); - - free(srv->before); - free(srv->after); - free(srv->name); - free(srv->desc); - free(srv->exec); - free(srv->ctty); - free(srv); -} diff --git a/lib/src/delsvc.c b/lib/src/delsvc.c new file mode 100644 index 0000000..3e077fb --- /dev/null +++ b/lib/src/delsvc.c @@ -0,0 +1,42 @@ +/* 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) +{ + size_t i; + + for (i = 0; i < svc->num_exec; ++i) + free(svc->exec[i]); + + for (i = 0; i < svc->num_before; ++i) + free(svc->before[i]); + + for (i = 0; i < svc->num_after; ++i) + free(svc->after[i]); + + free(svc->before); + free(svc->after); + free(svc->name); + free(svc->desc); + free(svc->exec); + free(svc->ctty); + free(svc); +} diff --git a/lib/src/rdsrv.c b/lib/src/rdsrv.c deleted file mode 100644 index 88d41e8..0000000 --- a/lib/src/rdsrv.c +++ /dev/null @@ -1,251 +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 "service.h" -#include "util.h" - -static int srv_name(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - (void)filename; (void)lineno; - srv->name = arg; - return 0; -} - -static int srv_desc(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - (void)filename; (void)lineno; - srv->desc = arg; - return 0; -} - -static int srv_tty(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - (void)filename; (void)lineno; - srv->ctty = arg; - return 0; -} - -static int srv_exec(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - char **new = realloc(srv->exec, sizeof(char*) * (srv->num_exec + 1)); - - if (new == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); - free(arg); - return -1; - } - - srv->exec = new; - srv->exec[srv->num_exec++] = arg; - return 0; -} - -static int srv_before(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - char **new = realloc(srv->before, - sizeof(char*) * (srv->num_before + 1)); - - if (new == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); - free(arg); - return -1; - } - - srv->before = new; - srv->before[srv->num_before++] = arg; - return 0; -} - -static int srv_after(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - char **new = realloc(srv->after, sizeof(char*) * (srv->num_after + 1)); - - if (new == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); - free(arg); - return -1; - } - - srv->after = new; - srv->after[srv->num_after++] = arg; - return 0; -} - -static int srv_type(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - int type = svc_type_from_string(arg); - - if (type == -1) { - fprintf(stderr, "%s: %zu: unknown service type '%s'\n", - filename, lineno, arg); - return -1; - } - - srv->type = type; - free(arg); - return 0; -} - -static int srv_target(service_t *srv, char *arg, - const char *filename, size_t lineno) -{ - int target = svc_target_from_string(arg); - - if (target == -1) { - fprintf(stderr, "%s: %zu: unknown service target '%s'\n", - filename, lineno, arg); - return -1; - } - - srv->target = target; - free(arg); - return 0; -} - - -static const struct { - const char *key; - - int (*handle)(service_t *srv, char *arg, - const char *filename, size_t lineno); -} srv_params[] = { - { "name", srv_name }, - { "description", srv_desc }, - { "exec", srv_exec }, - { "type", srv_type }, - { "target", srv_target }, - { "tty", srv_tty }, - { "before", srv_before }, - { "after", srv_after }, -}; - - -service_t *rdsrv(int dirfd, const char *filename) -{ - const char *arg, *args[1]; - char *line, *key, *value; - size_t i, argc, lineno; - service_t *srv; - 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; - } - - srv = calloc(1, sizeof(*srv)); - if (srv == NULL) { - fputs("out of memory\n", stderr); - close(fd); - return NULL; - } - - for (lineno = 1; ; ++lineno) { - errno = 0; - line = rdline(fd); - - if (line == NULL) { - if (errno != 0) { - fprintf(stderr, "read: %s: %zu: %s\n", - filename, lineno, strerror(errno)); - goto fail; - } - break; - } - - if (splitkv(line, &key, &value)) { - if (key == NULL) { - fprintf(stderr, - "%s: %zu: expected = \n", - filename, lineno); - } else if (value == NULL) { - fprintf(stderr, - "%s: %zu: expected value after %s\n", - filename, lineno, key); - } else { - fprintf(stderr, - "%s: %zu: unexpected arguments " - "after key-value pair\n", - filename, lineno); - } - goto fail; - } - - if (key == NULL) { - free(line); - continue; - } - - for (i = 0; i < ARRAY_SIZE(srv_params); ++i) { - if (!strcmp(srv_params[i].key, key)) - break; - } - - if (i >= ARRAY_SIZE(srv_params)) { - fprintf(stderr, "%s: %zu: unknown parameter '%s'\n", - filename, lineno, key); - goto fail_line; - } - - value = strexpand(value, argc, args); - if (value == NULL) { - fputs("out of memory", stderr); - goto fail_line; - } - - if (srv_params[i].handle(srv, value, filename, lineno)) { - free(value); - goto fail_line; - } - - free(line); - } - - close(fd); - return srv; -fail_line: - free(line); -fail: - close(fd); - delsrv(srv); - return NULL; -} diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c new file mode 100644 index 0000000..972f7e4 --- /dev/null +++ b/lib/src/rdsvc.c @@ -0,0 +1,251 @@ +/* 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 "service.h" +#include "util.h" + +static int svc_name(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + (void)filename; (void)lineno; + svc->name = arg; + return 0; +} + +static int svc_desc(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + (void)filename; (void)lineno; + svc->desc = arg; + return 0; +} + +static int svc_tty(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + (void)filename; (void)lineno; + svc->ctty = arg; + return 0; +} + +static int svc_exec(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + char **new = realloc(svc->exec, sizeof(char*) * (svc->num_exec + 1)); + + if (new == NULL) { + fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); + free(arg); + return -1; + } + + svc->exec = new; + svc->exec[svc->num_exec++] = arg; + return 0; +} + +static int svc_before(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + char **new = realloc(svc->before, + sizeof(char*) * (svc->num_before + 1)); + + if (new == NULL) { + fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); + free(arg); + return -1; + } + + svc->before = new; + svc->before[svc->num_before++] = arg; + return 0; +} + +static int svc_after(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + char **new = realloc(svc->after, sizeof(char*) * (svc->num_after + 1)); + + if (new == NULL) { + fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); + free(arg); + return -1; + } + + svc->after = new; + svc->after[svc->num_after++] = arg; + return 0; +} + +static int svc_type(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + int type = svc_type_from_string(arg); + + if (type == -1) { + fprintf(stderr, "%s: %zu: unknown service type '%s'\n", + filename, lineno, arg); + return -1; + } + + svc->type = type; + free(arg); + return 0; +} + +static int svc_target(service_t *svc, char *arg, + const char *filename, size_t lineno) +{ + int target = svc_target_from_string(arg); + + if (target == -1) { + fprintf(stderr, "%s: %zu: unknown service target '%s'\n", + filename, lineno, arg); + return -1; + } + + svc->target = target; + free(arg); + return 0; +} + + +static const struct { + const char *key; + + int (*handle)(service_t *svc, char *arg, + const char *filename, size_t lineno); +} svc_params[] = { + { "name", svc_name }, + { "description", svc_desc }, + { "exec", svc_exec }, + { "type", svc_type }, + { "target", svc_target }, + { "tty", svc_tty }, + { "before", svc_before }, + { "after", svc_after }, +}; + + +service_t *rdsvc(int dirfd, const char *filename) +{ + const char *arg, *args[1]; + char *line, *key, *value; + size_t i, argc, lineno; + service_t *svc; + 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; + } + + svc = calloc(1, sizeof(*svc)); + if (svc == NULL) { + fputs("out of memory\n", stderr); + close(fd); + return NULL; + } + + for (lineno = 1; ; ++lineno) { + errno = 0; + line = rdline(fd); + + if (line == NULL) { + if (errno != 0) { + fprintf(stderr, "read: %s: %zu: %s\n", + filename, lineno, strerror(errno)); + goto fail; + } + break; + } + + if (splitkv(line, &key, &value)) { + if (key == NULL) { + fprintf(stderr, + "%s: %zu: expected = \n", + filename, lineno); + } else if (value == NULL) { + fprintf(stderr, + "%s: %zu: expected value after %s\n", + filename, lineno, key); + } else { + fprintf(stderr, + "%s: %zu: unexpected arguments " + "after key-value pair\n", + filename, lineno); + } + goto fail; + } + + if (key == NULL) { + free(line); + continue; + } + + for (i = 0; i < ARRAY_SIZE(svc_params); ++i) { + if (!strcmp(svc_params[i].key, key)) + break; + } + + if (i >= ARRAY_SIZE(svc_params)) { + fprintf(stderr, "%s: %zu: unknown parameter '%s'\n", + filename, lineno, key); + goto fail_line; + } + + value = strexpand(value, argc, args); + if (value == NULL) { + fputs("out of memory", stderr); + goto fail_line; + } + + if (svc_params[i].handle(svc, value, filename, lineno)) { + free(value); + goto fail_line; + } + + free(line); + } + + close(fd); + return svc; +fail_line: + free(line); +fail: + close(fd); + delsvc(svc); + return NULL; +} diff --git a/lib/src/srv_tsort.c b/lib/src/srv_tsort.c deleted file mode 100644 index d375c4f..0000000 --- a/lib/src/srv_tsort.c +++ /dev/null @@ -1,92 +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) -{ - size_t i; - - while (list != NULL) { - for (i = 0; i < svc->num_after; ++i) { - if (!strcmp(svc->after[i], list->name)) - return true; - } - - for (i = 0; i < list->num_before; ++i) { - if (!strcmp(list->before[i], svc->name)) - return true; - } - - list = list->next; - } - - return false; -} - -service_t *srv_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/src/srvscan.c b/lib/src/srvscan.c deleted file mode 100644 index 69d0b91..0000000 --- a/lib/src/srvscan.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 srvscan(const char *directory, service_list_t *list) -{ - int i, dfd, type, ret = 0; - struct dirent *ent; - const char *ptr; - service_t *srv; - 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; - - srv = rdsrv(dfd, ent->d_name); - if (srv == NULL) { - ret = -1; - continue; - } - - srv->next = list->targets[srv->target]; - list->targets[srv->target] = srv; - } - - for (i = 0; i < TGT_MAX; ++i) { - if (list->targets[i] == NULL) - continue; - - errno = 0; - list->targets[i] = srv_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/src/svc_tsort.c b/lib/src/svc_tsort.c new file mode 100644 index 0000000..50517f2 --- /dev/null +++ b/lib/src/svc_tsort.c @@ -0,0 +1,92 @@ +/* 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) +{ + size_t i; + + while (list != NULL) { + for (i = 0; i < svc->num_after; ++i) { + if (!strcmp(svc->after[i], list->name)) + return true; + } + + for (i = 0; i < list->num_before; ++i) { + if (!strcmp(list->before[i], svc->name)) + return true; + } + + 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/src/svcscan.c b/lib/src/svcscan.c new file mode 100644 index 0000000..32c1dee --- /dev/null +++ b/lib/src/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 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); + 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/servicecmd/list.c b/servicecmd/list.c index 45d2b19..07153f2 100644 --- a/servicecmd/list.c +++ b/servicecmd/list.c @@ -53,7 +53,7 @@ static int cmd_list(int argc, char **argv) if (check_arguments(argv[0], argc, 1, 2)) return EXIT_FAILURE; - if (srvscan(SVCDIR, &list)) { + if (svcscan(SVCDIR, &list)) { fprintf(stderr, "Error while reading services from %s\n", SVCDIR); ret = EXIT_FAILURE; @@ -75,7 +75,7 @@ static int cmd_list(int argc, char **argv) print_services(list.targets[i]); } out: - del_srv_list(&list); + del_svc_list(&list); return ret; } -- cgit v1.2.3