From bf63f78b6698f33ed3acbd4aed8822a35688c1ba Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 23 May 2018 22:26:17 +0200 Subject: Split configuration parser and utility library Signed-off-by: David Oberhollenzer --- cmd/Makemodule.am | 4 +- cmd/runsvc/runsvc.h | 1 + initd/Makemodule.am | 2 +- lib/Makemodule.am | 18 ++- lib/include/libcfg.h | 98 +++++++++++++ lib/include/util.h | 77 ----------- lib/libcfg/pack_argv.c | 82 +++++++++++ lib/libcfg/rdline.c | 174 +++++++++++++++++++++++ lib/libcfg/unescape.c | 94 +++++++++++++ lib/src/del_svc_list.c | 35 ----- lib/src/delsvc.c | 43 ------ lib/src/enum_by_name.c | 32 ----- lib/src/enum_to_name.c | 32 ----- lib/src/opensock.c | 50 ------- lib/src/rdline.c | 174 ----------------------- lib/src/rdsvc.c | 356 ----------------------------------------------- lib/src/split_argv.c | 82 ----------- lib/src/svc_tsort.c | 95 ------------- lib/src/svcmap.c | 58 -------- lib/src/svcscan.c | 110 --------------- lib/src/unescape.c | 94 ------------- lib/util/del_svc_list.c | 35 +++++ lib/util/delsvc.c | 43 ++++++ lib/util/enum_by_name.c | 32 +++++ lib/util/enum_to_name.c | 32 +++++ lib/util/opensock.c | 50 +++++++ lib/util/rdsvc.c | 357 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/util/svc_tsort.c | 95 +++++++++++++ lib/util/svcmap.c | 58 ++++++++ lib/util/svcscan.c | 110 +++++++++++++++ 30 files changed, 1275 insertions(+), 1248 deletions(-) create mode 100644 lib/include/libcfg.h create mode 100644 lib/libcfg/pack_argv.c create mode 100644 lib/libcfg/rdline.c create mode 100644 lib/libcfg/unescape.c delete mode 100644 lib/src/del_svc_list.c delete mode 100644 lib/src/delsvc.c delete mode 100644 lib/src/enum_by_name.c delete mode 100644 lib/src/enum_to_name.c delete mode 100644 lib/src/opensock.c delete mode 100644 lib/src/rdline.c delete mode 100644 lib/src/rdsvc.c delete mode 100644 lib/src/split_argv.c delete mode 100644 lib/src/svc_tsort.c delete mode 100644 lib/src/svcmap.c delete mode 100644 lib/src/svcscan.c delete mode 100644 lib/src/unescape.c create mode 100644 lib/util/del_svc_list.c create mode 100644 lib/util/delsvc.c create mode 100644 lib/util/enum_by_name.c create mode 100644 lib/util/enum_to_name.c create mode 100644 lib/util/opensock.c create mode 100644 lib/util/rdsvc.c create mode 100644 lib/util/svc_tsort.c create mode 100644 lib/util/svcmap.c create mode 100644 lib/util/svcscan.c diff --git a/cmd/Makemodule.am b/cmd/Makemodule.am index 83d6174..f92610c 100644 --- a/cmd/Makemodule.am +++ b/cmd/Makemodule.am @@ -14,7 +14,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 +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 +service_LDADD = libinit.a libcfg.a EXTRA_DIST += $(SRVHEADERS) diff --git a/cmd/runsvc/runsvc.h b/cmd/runsvc/runsvc.h index 15785d3..7061eca 100644 --- a/cmd/runsvc/runsvc.h +++ b/cmd/runsvc/runsvc.h @@ -27,6 +27,7 @@ #include #include "service.h" +#include "libcfg.h" #include "util.h" #define ENVFILE ETCPATH "/initd.env" diff --git a/initd/Makemodule.am b/initd/Makemodule.am index f188c80..661742a 100644 --- a/initd/Makemodule.am +++ b/initd/Makemodule.am @@ -3,6 +3,6 @@ init_SOURCES += initd/status.c initd/mksock.c initd/svclist.c init_CPPFLAGS = $(AM_CPPFLAGS) init_CFLAGS = $(AM_CFLAGS) init_LDFLAGS = $(AM_LDFLAGS) -init_LDADD = libinit.a +init_LDADD = libinit.a libcfg.a sbin_PROGRAMS += init diff --git a/lib/Makemodule.am b/lib/Makemodule.am index b410ccc..c29ad2b 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/src/delsvc.c lib/src/rdline.c lib/src/svcmap.c -libinit_a_SOURCES += lib/src/unescape.c lib/src/enum_by_name.c -libinit_a_SOURCES += lib/src/rdsvc.c lib/src/svcscan.c lib/src/split_argv.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_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 +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 $(HEADRS) libinit_a_CPPFLAGS = $(AM_CPPFLAGS) libinit_a_CFLAGS = $(AM_CFLAGS) -EXTRA_DIST += $(HEADRS) +libcfg_a_SOURCES = lib/libcfg/rdline.c lib/libcfg/unescape.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 +EXTRA_DIST += $(HEADRS) lib/include/libcfg.h + +noinst_LIBRARIES += libinit.a libcfg.a diff --git a/lib/include/libcfg.h b/lib/include/libcfg.h new file mode 100644 index 0000000..5724bdc --- /dev/null +++ b/lib/include/libcfg.h @@ -0,0 +1,98 @@ +/* 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 . + */ +#ifndef LIBCONFIG_H +#define LIBCONFIG_H + +#include + +typedef struct { + int fd; /* input file descriptor */ + const char *argstr; /* if not NULL, read from this instead */ + + const char *filename; /* input file name */ + size_t lineno; /* current line number */ + + size_t i; /* buffer offset */ + char buffer[256]; /* current line, null-terminated */ + + int argc; + const char *const *argv; + + bool string; /* inside a string? */ + bool escape; /* reading an escape sequence? */ + bool comment; /* inside a comment */ +} rdline_t; + +/* + Initialize the config line scanner. + + The scanner reads from the provided fd. The filename is used for + error reporting. An argument count and vector can be set for argument + substitution in rdline. +*/ +void rdline_init(rdline_t *t, int fd, const char *filename, + int argc, const char *const *argv); + +/* + Read from file until end-of-file or a line feed is encountered. + + Returns -1 on failure, +1 if end of file was reached, + 0 if data was read successfully. + + The following transformations are applied: + - Space characters are replaced with regular white space characters. + - Sequences of space characters are truncated to a single space. + - A '#' sign is interpreted as the start of a comment and removed, + together with everything that follows. + - Padding spaces are removed from the line. + - If a '"' is encounterd, the above rules are disabled, until a + after the matching '"' is read. A '"' can be escaped by preceeding + it with a backslash. + - If a second, coresponding '"' is not found, processing fails. + - If a '%' character is encountered, the next character is expected + to be a single digit index into argv. If it is not a digit or + outside the bounds set by argc, processing fails. On success, + the argv value is inserted and processed as described above. + - A '%' character can be escaped by writing '%%' or, if inside + a double quite string, by writing \%. + - An attempt to use such an indexed argument inside an argument + expansion, results in failure. + - If the resulting line is empty, processing is restarted. +*/ +int rdline(rdline_t *t); + +/* + Remove double quotes ('"') from a string and substitute escape + sequences in between double quotes. +*/ +int unescape(char *src); + +/* + Replace spaces in 'str' with null bytes. Tread strings (started and + terminated with double-quotes which can be escaped) as a single block. + Such strings are run through unescap(). All elements are tightly + packed together and the function returns the number of consecutive + argument strings that are now inside 'str'. + + Returns a negative value if unescape() fails, a string is not + termianted or two such strings touch each other without a white + space in between. +*/ +int pack_argv(char *str); + +#endif /* LIBCONFIG_H */ diff --git a/lib/include/util.h b/lib/include/util.h index d6b68f1..f63a0d6 100644 --- a/lib/include/util.h +++ b/lib/include/util.h @@ -35,83 +35,6 @@ typedef struct { int value; } enum_map_t; - -typedef struct { - int fd; /* input file descriptor */ - const char *argstr; /* if not NULL, read from this instead */ - - const char *filename; /* input file name */ - size_t lineno; /* current line number */ - - size_t i; /* buffer offset */ - char buffer[256]; /* current line, null-terminated */ - - int argc; - const char *const *argv; - - bool string; /* inside a string? */ - bool escape; /* reading an escape sequence? */ - bool comment; /* inside a comment */ -} rdline_t; - - -/* - Initialize the config line scanner. - - The scanner reads from the provided fd. The filename is used for - error reporting. An argument count and vector can be set for argument - substitution in rdline. -*/ -void rdline_init(rdline_t *t, int fd, const char *filename, - int argc, const char *const *argv); - -/* - Read from file until end-of-file or a line feed is encountered. - - Returns -1 on failure, +1 if end of file was reached, - 0 if data was read successfully. - - The following transformations are applied: - - Space characters are replaced with regular white space characters. - - Sequences of space characters are truncated to a single space. - - A '#' sign is interpreted as the start of a comment and removed, - together with everything that follows. - - Padding spaces are removed from the line. - - If a '"' is encounterd, the above rules are disabled, until a - after the matching '"' is read. A '"' can be escaped by preceeding - it with a backslash. - - If a second, coresponding '"' is not found, processing fails. - - If a '%' character is encountered, the next character is expected - to be a single digit index into argv. If it is not a digit or - outside the bounds set by argc, processing fails. On success, - the argv value is inserted and processed as described above. - - A '%' character can be escaped by writing '%%' or, if inside - a double quite string, by writing \%. - - An attempt to use such an indexed argument inside an argument - expansion, results in failure. - - If the resulting line is empty, processing is restarted. -*/ -int rdline(rdline_t *t); - -/* - Remove double quotes ('"') from a string and substitute escape - sequences in between double quotes. -*/ -int unescape(char *src); - -/* - Replace spaces in 'str' with null bytes. Tread strings (started and - terminated with double-quotes which can be escaped) as a single block. - Such strings are run through unescap(). All elements are tightly - packed together and the function returns the number of consecutive - argument strings that are now inside 'str'. - - Returns a negative value if unescape() fails, a string is not - termianted or two such strings touch each other without a white - space in between. -*/ -int pack_argv(char *str); - /* 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 diff --git a/lib/libcfg/pack_argv.c b/lib/libcfg/pack_argv.c new file mode 100644 index 0000000..4d90171 --- /dev/null +++ b/lib/libcfg/pack_argv.c @@ -0,0 +1,82 @@ +/* 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 "libcfg.h" + +int pack_argv(char *str) +{ + char *dst, *start; + int count = 0; + + dst = str; + + for (;;) { + while (*str == ' ') + ++str; + + if (*str == '\0') + break; + + if (*str == '"') { + start = dst; + *(dst++) = *(str++); + + while (*str != '"') { + if (*str == '\0') + goto fail_str; + if (str[0] == '\\' && str[1] != '\0') + *(dst++) = *(str++); + *(dst++) = *(str++); + } + + *(dst++) = *(str++); + + if (*str != ' ' && *str != '\0') + goto fail_str; + if (*str == ' ') + ++str; + + *(dst++) = '\0'; + + if (unescape(start)) + goto fail_str; + + dst = start + strlen(start) + 1; + } else { + while (*str != '\0' && *str != ' ') + *(dst++) = *(str++); + if (*str == ' ') { + ++str; + *(dst++) = '\0'; + } + } + + ++count; + } + + *dst = '\0'; + return count; +fail_str: + errno = EINVAL; + return -1; +} diff --git a/lib/libcfg/rdline.c b/lib/libcfg/rdline.c new file mode 100644 index 0000000..ffbfd89 --- /dev/null +++ b/lib/libcfg/rdline.c @@ -0,0 +1,174 @@ +/* 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 "libcfg.h" + +static int rdline_getc(rdline_t *t) +{ + int ret; + char c; + + if (t->argstr != NULL) { + c = *(t->argstr++); + if (c != '\0') + goto out; + + t->argstr = NULL; + } + + do { + ret = read(t->fd, &c, 1); + } while (ret < 0 && errno == EINTR); + + if (ret < 0) + return -1; + + if (ret == 0) { + if (t->i == 0) { + errno = 0; + return -1; + } + c = '\0'; + } +out: + return (c == '\n') ? '\0' : c; +} + +static int rdline_append(rdline_t *t, int c) +{ + if (t->comment) { + if (c != '\0') + return 0; + } else if (t->string) { + if (t->escape) { + t->escape = false; + } else { + if (c == '\\') + t->escape = true; + if (c == '"') + t->string = false; + } + } else { + if (isspace(c)) + c = ' '; + if (c == ' ' && (t->i == 0 || t->buffer[t->i - 1] == ' ')) + return 0; + if (c == '#') { + t->comment = true; + return 0; + } + if (c == '"') + t->string = true; + } + + if (c == '\0') { + while (t->i > 0 && t->buffer[t->i - 1] == ' ') + t->i -= 1; + } + + if (t->i == sizeof(t->buffer)) + return -1; + + t->buffer[t->i++] = c; + return 0; +} + +void rdline_init(rdline_t *t, int fd, const char *filename, + int argc, const char *const *argv) +{ + memset(t, 0, sizeof(*t)); + t->fd = fd; + t->filename = filename; + t->argc = argc; + t->argv = argv; +} + +int rdline(rdline_t *t) +{ + const char *errstr; + int c; +retry: + t->i = 0; + t->argstr = NULL; + t->string = t->escape = t->comment = false; + t->lineno += 1; + + do { + errno = 0; + c = rdline_getc(t); + if (c < 0) { + if (errno == 0) + return 1; + errstr = strerror(errno); + goto fail; + } + if (c == 0 && t->string) { + errstr = "missing \""; + goto fail; + } + + if (c == '%') { + c = rdline_getc(t); + if (c == 0) { + errstr = "unexpected end of line after '%%'"; + goto fail; + } + if (c < 0) { + errstr = strerror(errno); + goto fail; + } + + if (c != '%') { + if (!isdigit(c)) { + errstr = "exptected digit after '%%'"; + goto fail; + } + if ((c - '0') >= t->argc) { + errstr = "argument out of range"; + goto fail; + } + if (t->argstr != NULL) { + errstr = "recursive argument " + "expansion"; + goto fail; + } + t->argstr = t->argv[c - '0']; + continue; + } + } + + if (rdline_append(t, c)) { + errstr = "line too long"; + goto fail; + } + } while (c != '\0'); + + if (t->buffer[0] == '\0') + goto retry; + + return 0; +fail: + fprintf(stderr, "%s: %zu: %s\n", t->filename, t->lineno, errstr); + return -1; +} diff --git a/lib/libcfg/unescape.c b/lib/libcfg/unescape.c new file mode 100644 index 0000000..e2b450c --- /dev/null +++ b/lib/libcfg/unescape.c @@ -0,0 +1,94 @@ +/* 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 "libcfg.h" + +static int xdigit(int x) +{ + if (isupper(x)) + return x - 'A' + 0x0A; + if (islower(x)) + return x - 'a' + 0x0A; + return x - '0'; +} + +int unescape(char *src) +{ + char *dst = src; + int c; + + for (;;) { + while (*src != '"' && *src != '\0') + *(dst++) = *(src++); + + if (*src == '\0') + break; + + ++src; + + while ((c = *(src++)) != '"') { + if (c == '\0') + return -1; + + if (c == '\\') { + c = *(src++); + + switch (c) { + case 'a': c = '\a'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 't': c = '\t'; break; + case '\\': + case '"': + case '%': + break; + case 'x': + c = 0; + if (isxdigit(*src)) + c = (c<<4) | xdigit(*(src++)); + if (isxdigit(*src)) + c = (c<<4) | xdigit(*(src++)); + break; + case '0': + c = 0; + if (isdigit(*src) && *src < '8') + c = (c<<3) | (*(src++) - '0'); + if (isdigit(*src) && *src < '8') + c = (c<<3) | (*(src++) - '0'); + if (isdigit(*src) && *src < '8') + c = (c<<3) | (*(src++) - '0'); + break; + default: + return -1; + } + + if (c == 0) + return -1; + } + + *(dst++) = c; + } + } + + *(dst++) = '\0'; + return 0; +} diff --git a/lib/src/del_svc_list.c b/lib/src/del_svc_list.c deleted file mode 100644 index 553a701..0000000 --- a/lib/src/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/src/delsvc.c b/lib/src/delsvc.c deleted file mode 100644 index 9ab51f1..0000000 --- a/lib/src/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/src/enum_by_name.c b/lib/src/enum_by_name.c deleted file mode 100644 index 95f306f..0000000 --- a/lib/src/enum_by_name.c +++ /dev/null @@ -1,32 +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 "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/src/enum_to_name.c b/lib/src/enum_to_name.c deleted file mode 100644 index 43fd4dc..0000000 --- a/lib/src/enum_to_name.c +++ /dev/null @@ -1,32 +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 "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/src/opensock.c b/lib/src/opensock.c deleted file mode 100644 index 0c2bf56..0000000 --- a/lib/src/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/src/rdline.c b/lib/src/rdline.c deleted file mode 100644 index 47a9d2a..0000000 --- a/lib/src/rdline.c +++ /dev/null @@ -1,174 +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 "util.h" - -static int rdline_getc(rdline_t *t) -{ - int ret; - char c; - - if (t->argstr != NULL) { - c = *(t->argstr++); - if (c != '\0') - goto out; - - t->argstr = NULL; - } - - do { - ret = read(t->fd, &c, 1); - } while (ret < 0 && errno == EINTR); - - if (ret < 0) - return -1; - - if (ret == 0) { - if (t->i == 0) { - errno = 0; - return -1; - } - c = '\0'; - } -out: - return (c == '\n') ? '\0' : c; -} - -static int rdline_append(rdline_t *t, int c) -{ - if (t->comment) { - if (c != '\0') - return 0; - } else if (t->string) { - if (t->escape) { - t->escape = false; - } else { - if (c == '\\') - t->escape = true; - if (c == '"') - t->string = false; - } - } else { - if (isspace(c)) - c = ' '; - if (c == ' ' && (t->i == 0 || t->buffer[t->i - 1] == ' ')) - return 0; - if (c == '#') { - t->comment = true; - return 0; - } - if (c == '"') - t->string = true; - } - - if (c == '\0') { - while (t->i > 0 && t->buffer[t->i - 1] == ' ') - t->i -= 1; - } - - if (t->i == sizeof(t->buffer)) - return -1; - - t->buffer[t->i++] = c; - return 0; -} - -void rdline_init(rdline_t *t, int fd, const char *filename, - int argc, const char *const *argv) -{ - memset(t, 0, sizeof(*t)); - t->fd = fd; - t->filename = filename; - t->argc = argc; - t->argv = argv; -} - -int rdline(rdline_t *t) -{ - const char *errstr; - int c; -retry: - t->i = 0; - t->argstr = NULL; - t->string = t->escape = t->comment = false; - t->lineno += 1; - - do { - errno = 0; - c = rdline_getc(t); - if (c < 0) { - if (errno == 0) - return 1; - errstr = strerror(errno); - goto fail; - } - if (c == 0 && t->string) { - errstr = "missing \""; - goto fail; - } - - if (c == '%') { - c = rdline_getc(t); - if (c == 0) { - errstr = "unexpected end of line after '%%'"; - goto fail; - } - if (c < 0) { - errstr = strerror(errno); - goto fail; - } - - if (c != '%') { - if (!isdigit(c)) { - errstr = "exptected digit after '%%'"; - goto fail; - } - if ((c - '0') >= t->argc) { - errstr = "argument out of range"; - goto fail; - } - if (t->argstr != NULL) { - errstr = "recursive argument " - "expansion"; - goto fail; - } - t->argstr = t->argv[c - '0']; - continue; - } - } - - if (rdline_append(t, c)) { - errstr = "line too long"; - goto fail; - } - } while (c != '\0'); - - if (t->buffer[0] == '\0') - goto retry; - - return 0; -fail: - fprintf(stderr, "%s: %zu: %s\n", t->filename, t->lineno, errstr); - return -1; -} diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c deleted file mode 100644 index 546962d..0000000 --- a/lib/src/rdsvc.c +++ /dev/null @@ -1,356 +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 "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(service_t *svc, char *arg, rdline_t *rd) -{ - if (try_unescape(arg, rd)) - return -1; - svc->desc = try_strdup(arg, rd); - return svc->desc == NULL ? -1 : 0; -} - -static int svc_tty(service_t *svc, char *arg, rdline_t *rd) -{ - if (try_unescape(arg, rd)) - return -1; - svc->ctty = try_strdup(arg, rd); - return svc->ctty == NULL ? -1 : 0; -} - -static int svc_exec(service_t *svc, char *arg, rdline_t *rd) -{ - exec_t *e, *end; - - 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(service_t *svc, char *arg, rdline_t *rd) -{ - 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(service_t *svc, char *arg, rdline_t *rd) -{ - 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(service_t *svc, char *arg, rdline_t *rd) -{ - int count = try_pack_argv(arg, rd); - - 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(service_t *svc, char *arg, rdline_t *rd) -{ - int target; - - 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 struct svc_param { - const char *key; - - unsigned int allow_block : 1; - - int flags; - - int (*handle)(service_t *svc, char *arg, rdline_t *rd); -} svc_params[] = { - { "description", 0, 0, svc_desc }, - { "exec", 1, RDSVC_NO_EXEC, svc_exec }, - { "type", 0, 0, svc_type }, - { "target", 0, 0, svc_target }, - { "tty", 0, RDSVC_NO_CTTY, svc_tty }, - { "before", 0, RDSVC_NO_DEPS, svc_before }, - { "after", 0, RDSVC_NO_DEPS, svc_after }, -}; - -static int splitkv(rdline_t *rd, char **k, char **v) -{ - char *key = rd->buffer, *value = rd->buffer; - - while (*value != ' ' && *value != '\0') { - if (!isalpha(*value)) { - fprintf(stderr, - "%s: %zu: unexpected '%c' in keyword\n", - rd->filename, rd->lineno, *value); - return -1; - } - ++value; - } - - if (*value != ' ') { - fprintf(stderr, "%s: %zu: expected argument after '%s'\n", - rd->filename, rd->lineno, key); - return -1; - } - - *(value++) = '\0'; - - *k = key; - *v = value; - return 0; -} - -static const struct svc_param *find_param(rdline_t *rd, const char *name) -{ - size_t i; - - for (i = 0; i < ARRAY_SIZE(svc_params); ++i) { - if (!strcmp(svc_params[i].key, name)) - return svc_params + i; - } - - fprintf(stderr, "%s: %zu: unknown keyword '%s'\n", - rd->filename, rd->lineno, name); - return NULL; -} - - -service_t *rdsvc(int dirfd, const char *filename, int flags) -{ - const struct svc_param *p; - const char *arg, *args[1]; - service_t *svc = NULL; - char *key, *value; - size_t argc, nlen; - rdline_t rd; - int fd, ret; - - 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; - } - - rdline_init(&rd, fd, filename, argc, args); - - 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); - - while ((ret = rdline(&rd)) == 0) { - if (splitkv(&rd, &key, &value)) - goto fail; - - p = find_param(&rd, key); - if (p == NULL) - goto fail; - - if (p->allow_block && *value == '{') { - for (++value; *value == ' '; ++value) - ; - - if (!(flags & p->flags)) { - if (*value != '\0' && - p->handle(svc, value, &rd)) { - goto fail; - } - } - - while ((ret = rdline(&rd)) == 0) { - if (strcmp(rd.buffer, "}") == 0) - break; - if (flags & p->flags) - continue; - if (p->handle(svc, rd.buffer, &rd)) - goto fail; - } - - if (ret < 0) - goto fail; - if (ret > 0) - goto fail_bra; - } else { - if (flags & p->flags) - continue; - if (p->handle(svc, value, &rd)) - goto fail; - } - } - - if (ret < 0) - goto fail; - - close(fd); - return svc; -fail_bra: - fprintf(stderr, "%s: missing '}' before end-of-file\n", filename); - goto fail; -fail_oom: - fputs("out of memory\n", stderr); -fail: - delsvc(svc); - close(fd); - return NULL; -} diff --git a/lib/src/split_argv.c b/lib/src/split_argv.c deleted file mode 100644 index 5930e2d..0000000 --- a/lib/src/split_argv.c +++ /dev/null @@ -1,82 +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 "util.h" - -int pack_argv(char *str) -{ - char *dst, *start; - int count = 0; - - dst = str; - - for (;;) { - while (*str == ' ') - ++str; - - if (*str == '\0') - break; - - if (*str == '"') { - start = dst; - *(dst++) = *(str++); - - while (*str != '"') { - if (*str == '\0') - goto fail_str; - if (str[0] == '\\' && str[1] != '\0') - *(dst++) = *(str++); - *(dst++) = *(str++); - } - - *(dst++) = *(str++); - - if (*str != ' ' && *str != '\0') - goto fail_str; - if (*str == ' ') - ++str; - - *(dst++) = '\0'; - - if (unescape(start)) - goto fail_str; - - dst = start + strlen(start) + 1; - } else { - while (*str != '\0' && *str != ' ') - *(dst++) = *(str++); - if (*str == ' ') { - ++str; - *(dst++) = '\0'; - } - } - - ++count; - } - - *dst = '\0'; - return count; -fail_str: - errno = EINVAL; - return -1; -} diff --git a/lib/src/svc_tsort.c b/lib/src/svc_tsort.c deleted file mode 100644 index 5f83fa0..0000000 --- a/lib/src/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/src/svcmap.c b/lib/src/svcmap.c deleted file mode 100644 index 5df8ca9..0000000 --- a/lib/src/svcmap.c +++ /dev/null @@ -1,58 +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 }, - { "ctrlaltdel", TGT_CAD }, - { 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/src/svcscan.c b/lib/src/svcscan.c deleted file mode 100644 index f4c7ef1..0000000 --- a/lib/src/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; -} diff --git a/lib/src/unescape.c b/lib/src/unescape.c deleted file mode 100644 index 2d48c92..0000000 --- a/lib/src/unescape.c +++ /dev/null @@ -1,94 +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 "util.h" - -static int xdigit(int x) -{ - if (isupper(x)) - return x - 'A' + 0x0A; - if (islower(x)) - return x - 'a' + 0x0A; - return x - '0'; -} - -int unescape(char *src) -{ - char *dst = src; - int c; - - for (;;) { - while (*src != '"' && *src != '\0') - *(dst++) = *(src++); - - if (*src == '\0') - break; - - ++src; - - while ((c = *(src++)) != '"') { - if (c == '\0') - return -1; - - if (c == '\\') { - c = *(src++); - - switch (c) { - case 'a': c = '\a'; break; - case 'b': c = '\b'; break; - case 'f': c = '\f'; break; - case 'n': c = '\n'; break; - case 't': c = '\t'; break; - case '\\': - case '"': - case '%': - break; - case 'x': - c = 0; - if (isxdigit(*src)) - c = (c<<4) | xdigit(*(src++)); - if (isxdigit(*src)) - c = (c<<4) | xdigit(*(src++)); - break; - case '0': - c = 0; - if (isdigit(*src) && *src < '8') - c = (c<<3) | (*(src++) - '0'); - if (isdigit(*src) && *src < '8') - c = (c<<3) | (*(src++) - '0'); - if (isdigit(*src) && *src < '8') - c = (c<<3) | (*(src++) - '0'); - break; - default: - return -1; - } - - if (c == 0) - return -1; - } - - *(dst++) = c; - } - } - - *(dst++) = '\0'; - return 0; -} diff --git a/lib/util/del_svc_list.c b/lib/util/del_svc_list.c new file mode 100644 index 0000000..553a701 --- /dev/null +++ b/lib/util/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/util/delsvc.c b/lib/util/delsvc.c new file mode 100644 index 0000000..9ab51f1 --- /dev/null +++ b/lib/util/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/util/enum_by_name.c b/lib/util/enum_by_name.c new file mode 100644 index 0000000..95f306f --- /dev/null +++ b/lib/util/enum_by_name.c @@ -0,0 +1,32 @@ +/* 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 "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 new file mode 100644 index 0000000..43fd4dc --- /dev/null +++ b/lib/util/enum_to_name.c @@ -0,0 +1,32 @@ +/* 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 "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/opensock.c b/lib/util/opensock.c new file mode 100644 index 0000000..0c2bf56 --- /dev/null +++ b/lib/util/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/util/rdsvc.c b/lib/util/rdsvc.c new file mode 100644 index 0000000..6a964f7 --- /dev/null +++ b/lib/util/rdsvc.c @@ -0,0 +1,357 @@ +/* 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(service_t *svc, char *arg, rdline_t *rd) +{ + if (try_unescape(arg, rd)) + return -1; + svc->desc = try_strdup(arg, rd); + return svc->desc == NULL ? -1 : 0; +} + +static int svc_tty(service_t *svc, char *arg, rdline_t *rd) +{ + if (try_unescape(arg, rd)) + return -1; + svc->ctty = try_strdup(arg, rd); + return svc->ctty == NULL ? -1 : 0; +} + +static int svc_exec(service_t *svc, char *arg, rdline_t *rd) +{ + exec_t *e, *end; + + 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(service_t *svc, char *arg, rdline_t *rd) +{ + 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(service_t *svc, char *arg, rdline_t *rd) +{ + 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(service_t *svc, char *arg, rdline_t *rd) +{ + int count = try_pack_argv(arg, rd); + + 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(service_t *svc, char *arg, rdline_t *rd) +{ + int target; + + 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 struct svc_param { + const char *key; + + unsigned int allow_block : 1; + + int flags; + + int (*handle)(service_t *svc, char *arg, rdline_t *rd); +} svc_params[] = { + { "description", 0, 0, svc_desc }, + { "exec", 1, RDSVC_NO_EXEC, svc_exec }, + { "type", 0, 0, svc_type }, + { "target", 0, 0, svc_target }, + { "tty", 0, RDSVC_NO_CTTY, svc_tty }, + { "before", 0, RDSVC_NO_DEPS, svc_before }, + { "after", 0, RDSVC_NO_DEPS, svc_after }, +}; + +static int splitkv(rdline_t *rd, char **k, char **v) +{ + char *key = rd->buffer, *value = rd->buffer; + + while (*value != ' ' && *value != '\0') { + if (!isalpha(*value)) { + fprintf(stderr, + "%s: %zu: unexpected '%c' in keyword\n", + rd->filename, rd->lineno, *value); + return -1; + } + ++value; + } + + if (*value != ' ') { + fprintf(stderr, "%s: %zu: expected argument after '%s'\n", + rd->filename, rd->lineno, key); + return -1; + } + + *(value++) = '\0'; + + *k = key; + *v = value; + return 0; +} + +static const struct svc_param *find_param(rdline_t *rd, const char *name) +{ + size_t i; + + for (i = 0; i < ARRAY_SIZE(svc_params); ++i) { + if (!strcmp(svc_params[i].key, name)) + return svc_params + i; + } + + fprintf(stderr, "%s: %zu: unknown keyword '%s'\n", + rd->filename, rd->lineno, name); + return NULL; +} + + +service_t *rdsvc(int dirfd, const char *filename, int flags) +{ + const struct svc_param *p; + const char *arg, *args[1]; + service_t *svc = NULL; + char *key, *value; + size_t argc, nlen; + rdline_t rd; + int fd, ret; + + 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; + } + + rdline_init(&rd, fd, filename, argc, args); + + 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); + + while ((ret = rdline(&rd)) == 0) { + if (splitkv(&rd, &key, &value)) + goto fail; + + p = find_param(&rd, key); + if (p == NULL) + goto fail; + + if (p->allow_block && *value == '{') { + for (++value; *value == ' '; ++value) + ; + + if (!(flags & p->flags)) { + if (*value != '\0' && + p->handle(svc, value, &rd)) { + goto fail; + } + } + + while ((ret = rdline(&rd)) == 0) { + if (strcmp(rd.buffer, "}") == 0) + break; + if (flags & p->flags) + continue; + if (p->handle(svc, rd.buffer, &rd)) + goto fail; + } + + if (ret < 0) + goto fail; + if (ret > 0) + goto fail_bra; + } else { + if (flags & p->flags) + continue; + if (p->handle(svc, value, &rd)) + goto fail; + } + } + + if (ret < 0) + goto fail; + + close(fd); + return svc; +fail_bra: + fprintf(stderr, "%s: missing '}' before end-of-file\n", filename); + goto fail; +fail_oom: + fputs("out of memory\n", stderr); +fail: + delsvc(svc); + close(fd); + return NULL; +} diff --git a/lib/util/svc_tsort.c b/lib/util/svc_tsort.c new file mode 100644 index 0000000..5f83fa0 --- /dev/null +++ b/lib/util/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/util/svcmap.c b/lib/util/svcmap.c new file mode 100644 index 0000000..5df8ca9 --- /dev/null +++ b/lib/util/svcmap.c @@ -0,0 +1,58 @@ +/* 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 }, + { "ctrlaltdel", TGT_CAD }, + { 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 new file mode 100644 index 0000000..f4c7ef1 --- /dev/null +++ b/lib/util/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; +} -- cgit v1.2.3