From 36f38d5a137cf718d580f08f8de3b18f9ed106cd Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 29 Jun 2017 22:21:33 +0200 Subject: [PATCH 1/8] initctl: Only append space between args, not at last arg Signed-off-by: Joachim Nilsson --- src/initctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/initctl.c b/src/initctl.c index bc4dfa00..5fe359a5 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -652,7 +652,8 @@ int main(int argc, char *argv[]) while (optind < argc) { strlcat(arg, argv[optind++], sizeof(arg)); - strlcat(arg, " ", sizeof(arg)); + if (optind < argc) + strlcat(arg, " ", sizeof(arg)); } for (c = 0; command[c].cmd; c++) { From 023a881fb97d0c2cd47a32eddd0898fe1242fe41 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 29 Jun 2017 22:23:09 +0200 Subject: [PATCH 2/8] Enable more compiler warnings by default, except unused parameters Signed-off-by: Joachim Nilsson --- src/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Makefile.am b/src/Makefile.am index 1f349064..52010f67 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -43,7 +43,7 @@ endif initctl_SOURCES = initctl.c svc.c svc.h cond.c cond.h \ util.c util.h -initctl_CFLAGS = $(lite_CFLAGS) +initctl_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter $(lite_CFLAGS) initctl_LDADD = $(lite_LIBS) reboot_SOURCES = reboot.c util.c util.h From 3428cd48b727dc4af2c191b4e61523604fdba78e Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 29 Jun 2017 22:23:38 +0200 Subject: [PATCH 3/8] Fix #71: Add support for enabling/disabling services in /etc/finit.d This patch adds support for enabling and disabling services in /etc/finit.d/ by adding another subdirectory: /etc/finit.d/available Enabled service/run/task stanzas are by convention symlinked to the parent dir. The initctl tool has three new convenience commands to facilitate operating with this: initctl list initctl enable initctl disable Signed-off-by: Joachim Nilsson --- src/Makefile.am | 4 +- src/initctl.c | 19 +++++- src/serv.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++ src/serv.h | 38 +++++++++++ 4 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 src/serv.c create mode 100644 src/serv.h diff --git a/src/Makefile.am b/src/Makefile.am index 52010f67..85991c6d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -41,8 +41,8 @@ else finit_LDADD += -ldl endif -initctl_SOURCES = initctl.c svc.c svc.h cond.c cond.h \ - util.c util.h +initctl_SOURCES = initctl.c serv.c serv.h svc.c svc.h \ + cond.c cond.h util.c util.h initctl_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter $(lite_CFLAGS) initctl_LDADD = $(lite_LIBS) diff --git a/src/initctl.c b/src/initctl.c index 5fe359a5..b4a8063a 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -21,11 +21,12 @@ * THE SOFTWARE. */ +#include "config.h" + #include #include #include #include -#include #include #include #include @@ -36,9 +37,9 @@ #include #include -#include "config.h" #include "finit.h" #include "cond.h" +#include "serv.h" #include "service.h" #include "util.h" @@ -54,6 +55,7 @@ int verbose = 0; int silent = 1; /* For helpers.c */ int runlevel = 0; + static int do_send(struct init_request *rq, ssize_t len) { int sd, result = 255; @@ -584,16 +586,24 @@ static int usage(int rc) " reload Reload *.conf in /etc/finit.d and activate changes\n" " runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n" " status | show Show status of services\n" + "\n" + " list List all .conf in /etc/finit.d/\n" + " enable Enable .conf in /etc/finit.d/available/\n" + " disable Disable .conf in /etc/finit.d/[enabled/]\n" + "\n" " cond set Set (assert) condition => +COND\n" " cond clear Clear (deassert) condition => -COND\n" " cond show Show condition status\n" " cond dump Show all conditions and their status\n" + "\n" " start [:ID] Start service by job# or name, with optional ID\n" " stop [:ID] Stop/Pause a running service by job# or name\n" " restart [:ID] Restart (stop/start) service by job# or name\n" + "\n" " reboot Reboot system\n" " halt Halt system\n" " poweroff Halt and power off system\n" + "\n" " utmp show Raw dump of UTMP/WTMP db\n" " version Show Finit version\n\n", prognm); @@ -610,6 +620,11 @@ int main(int argc, char *argv[]) { "runlevel", do_runlevel }, { "status", show_status }, { "show", show_status }, /* Convenience alias */ + + { "list", serv_list }, + { "enable", serv_enable }, + { "disable", serv_disable }, + { "cond", do_cond }, { "start", do_start }, { "stop", do_stop }, diff --git a/src/serv.c b/src/serv.c new file mode 100644 index 00000000..a5e976fc --- /dev/null +++ b/src/serv.c @@ -0,0 +1,163 @@ +/* List and enable/disable service configurations + * + * Copyright (c) 2017 Joachim Nilsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "config.h" + +#include +#include +#include +#include + +#define SCREEN_WIDTH 80 /* Calculate screen width as well, later */ + +static const char *cwd; +static const char *available = FINIT_RCSD "/available"; +static const char *enabled = FINIT_RCSD "/enabled"; + +static void pushd(const char *dir) +{ + cwd = get_current_dir_name(); + chdir(dir); +} + +static void popd(void) +{ + chdir(cwd); +} + +static int calc_width(char *arr[], size_t len) +{ + int width = 0; + size_t i; + + for (i = 0; i < len; i++) { + int w = strlen(arr[i]); + + if (w > width) + width = w; + } + + return width; +} + +static void do_list(const char *heading, const char *path) +{ + int num; + int width; + size_t i; + glob_t gl; + char h[SCREEN_WIDTH] = "==============================================================================="; + + pushd(path); + if (glob("*.conf", 0, NULL, &gl)) { + chdir(cwd); + return; + } + + if (gl.gl_pathc <= 0) + goto done; + + num = snprintf(h, sizeof(h) - 1, "%s", path); + h[num] = ' '; + puts(h); + + width = calc_width(gl.gl_pathv, gl.gl_pathc); + num = SCREEN_WIDTH / width; + if ((num - 1) * 2 + num * width > SCREEN_WIDTH) + num--; + + for (i = 0; i < gl.gl_pathc; i++) { + if (i > 0 && !(i % num)) + puts(""); + + printf("%-*s ", width, gl.gl_pathv[i]); + } + puts("\n"); + +done: + globfree(&gl); + popd(); +} + +int serv_list(char *arg) +{ + if (fisdir(available)) + do_list("Available", available); + if (fisdir(enabled)) + do_list("Enabled", enabled); + else if (fisdir(FINIT_RCSD)) + do_list("Always enabled", FINIT_RCSD); + + return 0; +} + +int serv_enable(char *arg) +{ + char corr[40]; + char link[80]; + char path[80]; + + if (!strstr(arg, ".conf")) { + snprintf(corr, sizeof(corr), "%s.conf", arg); + arg = corr; + } + + pushd(FINIT_RCSD); + snprintf(path, sizeof(path), "available/%s", arg); + if (!fexist(path)) + errx(1, "Cannot find %s", path); + + snprintf(link, sizeof(link), "%s/%s", FINIT_RCSD, arg); + if (fexist(link)) + errx(1, "%s already exists", link); + + return symlink(path, link) != 0; +} + +int serv_disable(char *arg) +{ + char corr[40]; + char link[80]; + struct stat st; + + if (!strstr(arg, ".conf")) { + snprintf(corr, sizeof(corr), "%s.conf", arg); + arg = corr; + } + + snprintf(link, sizeof(link), "%s/%s", FINIT_RCSD, arg); + if (stat(link, &st)) + err(1, "Cannot find %s", link); + + if ((st.st_mode & S_IFMT) == S_IFLNK) + errx(1, "%s is not a symlink, move manually to %s first", link, available); + + return remove(link) != 0; +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ diff --git a/src/serv.h b/src/serv.h new file mode 100644 index 00000000..9692d4cc --- /dev/null +++ b/src/serv.h @@ -0,0 +1,38 @@ +/* List and enable/disable service configurations + * + * Copyright (c) 2017 Joachim Nilsson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef FINIT_SERV_H_ +#define FINIT_SERV_H_ + +int serv_list (char *arg); +int serv_enable (char *arg); +int serv_disable (char *arg); + +#endif /* FINIT_SERV_H_ */ + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */ From 018a87286c655f74a05e80a8d26ae1b6a0499bdc Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Fri, 30 Jun 2017 12:04:44 +0200 Subject: [PATCH 4/8] initctl: Sort commands in usage according to topic Signed-off-by: Joachim Nilsson --- src/initctl.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/initctl.c b/src/initctl.c index b4a8063a..3d71fed7 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -574,66 +574,79 @@ static int show_status(char *arg) static int usage(int rc) { - fprintf(stderr, "Usage: %s [OPTIONS] \n\n" + fprintf(stderr, + "Usage: %s [OPTIONS] \n" + "\n" "Options:\n" " -d, --debug Debug initctl (client)\n" " -v, --verbose Verbose output\n" - " -h, --help This help text\n\n" + " -h, --help This help text\n" + "\n" "Commands:\n" " debug Toggle Finit (daemon) debug\n" " help This help text\n" - " log [NAME] Show ten last Finit, or NAME, messages from syslog\n" - " reload Reload *.conf in /etc/finit.d and activate changes\n" - " runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n" - " status | show Show status of services\n" + " version Show Finit version\n" "\n" " list List all .conf in /etc/finit.d/\n" " enable Enable .conf in /etc/finit.d/available/\n" " disable Disable .conf in /etc/finit.d/[enabled/]\n" + " reload Reload *.conf in /etc/finit.d/ (activates changes)\n" "\n" " cond set Set (assert) condition => +COND\n" " cond clear Clear (deassert) condition => -COND\n" " cond show Show condition status\n" " cond dump Show all conditions and their status\n" "\n" + " log [NAME] Show ten last Finit, or NAME, messages from syslog\n" " start [:ID] Start service by job# or name, with optional ID\n" " stop [:ID] Stop/Pause a running service by job# or name\n" " restart [:ID] Restart (stop/start) service by job# or name\n" + " status | show Show status of services\n" "\n" + " runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n" " reboot Reboot system\n" " halt Halt system\n" " poweroff Halt and power off system\n" "\n" " utmp show Raw dump of UTMP/WTMP db\n" - " version Show Finit version\n\n", prognm); + "\n", prognm); return rc; } +static int do_help(char *arg) +{ + return usage(0); +} + int main(int argc, char *argv[]) { int c; command_t command[] = { { "debug", toggle_debug }, - { "log", do_log }, - { "reload", do_reload }, - { "runlevel", do_runlevel }, - { "status", show_status }, - { "show", show_status }, /* Convenience alias */ + { "help", do_help }, + { "version", show_version }, { "list", serv_list }, { "enable", serv_enable }, { "disable", serv_disable }, + { "reload", do_reload }, { "cond", do_cond }, + + { "log", do_log }, { "start", do_start }, { "stop", do_stop }, { "restart", do_restart }, + { "status", show_status }, + { "show", show_status }, /* Convenience alias */ + + { "runlevel", do_runlevel }, { "reboot", do_reboot }, { "halt", do_halt }, { "poweroff", do_poweroff }, + { "utmp", do_utmp }, - { "version", show_version }, { NULL, NULL } }; struct option long_options[] = { From 1c2dcd9acc6463e6e8fa323105036de86759ec9d Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 1 Jul 2017 11:45:15 +0200 Subject: [PATCH 5/8] doc: Add distribution recommendations and tips, issue #71 Signed-off-by: Joachim Nilsson --- doc/distro.md | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 doc/distro.md diff --git a/doc/distro.md b/doc/distro.md new file mode 100644 index 00000000..f146d171 --- /dev/null +++ b/doc/distro.md @@ -0,0 +1,75 @@ +Distro Recommendations +====================== + +By default Finit uses the following directories for configuration files: + +``` + /etc/ + |-- finit.d/ -- Regular (enabled) services + | |-- lighttpd.conf + | `- *.conf + |-- finit.conf -- Bootstrap tasks and services + : +``` + +To enable a standard (non-bootstrap) service one simply drops a small +configuration file in `/etc/finit.d/`. This works fine on system that +keep disabled services elsewhere, or generates them as needed from some +other tool. + +Disitributions, however, may want a clearer separation of enabled and +available (installed but not enabled) services. They may even want to +customize the directories used, for brand labling or uniformity. + +To that end Finit allows for a sub-directory `/etc/finit.d/available/` +where installed but disabled services can reside. Adding a symlink to a +configuration in this sub-directory enables the service, but it will not +be started. + +To change the default configuration directory and configuration file +names the Finit `configure` script offers the following two options at +build time: + +```sh +./configure --with-rcsd=/etc/init.d --with-config=/etc/init.d/init.conf +``` + +The resulting directory structure is depicted below. Please notice how +`/etc/finit.conf` now resides in the same sub-directory as a non-symlink +`/etc/init.d/init.conf`: + +``` + /etc/ + |-- init.d/ + | |-- available/ -- Regular (disabled) services + | | |-- httpd.conf + | | |-- ntpd.conf + | | `-- sshd.conf + | |-- sshd.conf -- Symlink to available/sshd.conf + | `- init.conf -- Bootstrap tasks and services + : +``` + +To facilitate the task of managing configurations, be it a service, +task, run, or other stanza, the `initctl` tool has a a few built-in +commands: + +``` + list List all .conf in /etc/finit.d/ + enable Enable .conf in /etc/finit.d/available/ + disable Disable .conf in /etc/finit.d/[enabled/] + reload Reload *.conf in /etc/finit.d/ (activates changes) +``` + +To enable a service like `sshd.conf`, above, use + + initctl enable sshd + +The `.conf` suffix is not needed, `initctl` adds it implicitly if it's +missing. The `disable` command works in a similar fashion. + +Note, however, that `initctl` only operates on symlinks and it always +requires the `available/` sub-directory. Any non-symlink in the parent +directory, here `/etc/init.d/`, will be considered a system override by +`initctl`. + From 0ec5ad696134c4c3a6ce45f974a2a40406e5f04d Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 1 Jul 2017 12:18:03 +0200 Subject: [PATCH 6/8] TODO: Remember the idea of creating service stanzas using initctl See issue #69 for a background. Signed-off-by: Joachim Nilsson --- TODO.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TODO.md b/TODO.md index 461e9bd7..dba703aa 100644 --- a/TODO.md +++ b/TODO.md @@ -44,6 +44,8 @@ General * Improve `initctl show SVC` output. Add service uptime, last ten SVC logs/history (historian), etc. Similar to `systemctl show SVC` * Add PRE and POST hooks for when switching between runlevels +* `initctl add /sbin/service -n -- Service description`, for details + https://github.com/troglobit/finit/issues/69#issuecomment-287907610 Init From 76b5bdc2c45da14337c9a1701ba546b9cf4169be Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 1 Jul 2017 16:06:01 +0200 Subject: [PATCH 7/8] conf.c: Simplify .conf load/parse debug messages Signed-off-by: Joachim Nilsson --- src/conf.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/conf.c b/src/conf.c index 02e11e74..67d0dc4d 100644 --- a/src/conf.c +++ b/src/conf.c @@ -388,13 +388,15 @@ static void tabstospaces(char *line) static int parse_conf_dynamic(char *file, struct timeval *mtime) { - FILE *fp = fopen(file, "r"); + FILE *fp; + fp = fopen(file, "r"); if (!fp) { _pe("Failed opening %s", file); return 1; } + _d("Parsing %s", file); while (!feof(fp)) { char line[LINE_SIZE] = ""; @@ -403,7 +405,7 @@ static int parse_conf_dynamic(char *file, struct timeval *mtime) chomp(line); tabstospaces(line); - _d("dyn conf: %s", line); + _d("%s", line); parse_dynamic(line, mtime); } @@ -445,13 +447,15 @@ static int parse_conf(char *file) if (!silent) print(0, "Loading %sconfiguration", line); + + _d("Parsing %s", file); while (!feof(fp)) { if (!fgets(line, sizeof(line), fp)) continue; chomp(line); tabstospaces(line); - _d("conf: %s", line); + _d("%s", line); parse_static(line); parse_dynamic(line, NULL); From fd861e169166f0fd14cdccafe5339ff73cb5d171 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 1 Jul 2017 16:06:48 +0200 Subject: [PATCH 8/8] conf.c: Log warning for dangling .conf symlinks in /etc/finit.d/ Signed-off-by: Joachim Nilsson --- src/conf.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/conf.c b/src/conf.c index 67d0dc4d..6c7ecaf6 100644 --- a/src/conf.c +++ b/src/conf.c @@ -478,29 +478,52 @@ void conf_reload_dynamic(void) num = scandir(dir, &e, NULL, alphasort); if (num < 0) { - _d("No files found in %s, skipping ...", dir); + _d("Skipping %s, no files found ...", dir); return; } for (i = 0; i < num; i++) { char *name = e[i]->d_name; char path[CMD_SIZE]; + size_t len; struct stat st; struct timeval mtime; snprintf(path, sizeof(path), "%s/%s", dir, name); - /* Check that it's an actual file ... */ - if (stat(path, &st)) { - _d("Cannot even read .conf file %s, skipping ...", path); + /* Check that it's an actual file ... beyond any symlinks */ + if (lstat(path, &st)) { + _d("Skipping %s, cannot access: %s", path, strerror(errno)); continue; } - if (S_ISEXEC(st.st_mode) || S_ISDIR(st.st_mode)) + + /* Skip directories */ + if (S_ISDIR(st.st_mode)) { + _d("Skipping directory %s", path); continue; + } + + /* Check for dangling symlinks */ + if (S_ISLNK(st.st_mode)) { + char *rp; + + rp = realpath(path, NULL); + if (!rp) { + /* + * XXX: Prune from rcsd? + * XXX: Possibly temporary service from last boot + */ + logit(LOG_WARNING, "Skipping %s, dangling symlink: %s", path, strerror(errno)); + continue; + } + + free(rp); + } /* Check that file ends with '.conf' */ - if (strcmp(&path[strlen(path) - 5], ".conf")) { - _d("File %s is not a .conf, skipping ... ", path); + len = strlen(path); + if (len < 6 || strcmp(&path[len - 5], ".conf")) { + _d("Skipping %s, not a valid .conf ... ", path); continue; }