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: + */