1303 files changed, 32187 insertions, 57138 deletions
@@ -151,26 +151,38 @@ Exit Codes */ /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */ -/* TODO: depends on runit_lib.c - review and reduce/eliminate */ -//usage:#define sv_trivial_usage -//usage: "[-v] [-w SEC] CMD SERVICE_DIR..." -//usage:#define sv_full_usage "\n\n" -//usage: "Control services monitored by runsv supervisor.\n" -//usage: "Commands (only first character is enough):\n" -//usage: "\n" -//usage: "status: query service status\n" -//usage: "up: if service isn't running, start it. If service stops, restart it\n" -//usage: "once: like 'up', but if service stops, don't restart it\n" -//usage: "down: send TERM and CONT signals. If ./run exits, start ./finish\n" -//usage: " if it exists. After it stops, don't restart service\n" -//usage: "exit: send TERM and CONT signals to service and log service. If they exit,\n" -//usage: " runsv exits too\n" -//usage: "pause, cont, hup, alarm, interrupt, quit, 1, 2, term, kill: send\n" -//usage: "STOP, CONT, HUP, ALRM, INT, QUIT, USR1, USR2, TERM, KILL signal to service" +//config:config SV +//config: bool "sv" +//config: default y +//config: help +//config: sv reports the current status and controls the state of services +//config: monitored by the runsv supervisor. +//config: +//config:config SV_DEFAULT_SERVICE_DIR +//config: string "Default directory for services" +//config: default "/var/service" +//config: depends on SV +//config: help +//config: Default directory for services. +//config: Defaults to "/var/service" +//config: +//config:config SVC +//config: bool "svc" +//config: default y +//config: help +//config: svc controls the state of services monitored by the runsv supervisor. +//config: It is comaptible with daemontools command with the same name. + +//applet:IF_SV(APPLET(sv, BB_DIR_USR_BIN, BB_SUID_DROP)) +//applet:IF_SVC(APPLET(svc, BB_DIR_USR_BIN, BB_SUID_DROP)) + +//kbuild:lib-$(CONFIG_SV) += sv.o +//kbuild:lib-$(CONFIG_SVC) += sv.o #include <sys/file.h> #include "libbb.h" +#include "common_bufsiz.h" #include "runit_lib.h" struct globals { @@ -181,17 +193,17 @@ struct globals { uint64_t tstart, tnow; svstatus_t svstatus; } FIX_ALIASING; -#define G (*(struct globals*)&bb_common_bufsiz1) +#define G (*(struct globals*)bb_common_bufsiz1) #define acts (G.acts ) #define service (G.service ) #define rc (G.rc ) #define tstart (G.tstart ) #define tnow (G.tnow ) #define svstatus (G.svstatus ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) -#define str_equal(s,t) (!strcmp((s), (t))) +#define str_equal(s,t) (strcmp((s), (t)) == 0) static void fatal_cannot(const char *m1) NORETURN; @@ -433,8 +445,22 @@ static int control(const char *a) return 1; } -int sv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; -int sv_main(int argc UNUSED_PARAM, char **argv) +//usage:#define sv_trivial_usage +//usage: "[-v] [-w SEC] CMD SERVICE_DIR..." +//usage:#define sv_full_usage "\n\n" +//usage: "Control services monitored by runsv supervisor.\n" +//usage: "Commands (only first character is enough):\n" +//usage: "\n" +//usage: "status: query service status\n" +//usage: "up: if service isn't running, start it. If service stops, restart it\n" +//usage: "once: like 'up', but if service stops, don't restart it\n" +//usage: "down: send TERM and CONT signals. If ./run exits, start ./finish\n" +//usage: " if it exists. After it stops, don't restart service\n" +//usage: "exit: send TERM and CONT signals to service and log service. If they exit,\n" +//usage: " runsv exits too\n" +//usage: "pause, cont, hup, alarm, interrupt, quit, 1, 2, term, kill: send\n" +//usage: "STOP, CONT, HUP, ALRM, INT, QUIT, USR1, USR2, TERM, KILL signal to service" +static int sv(char **argv) { char *x; char *action; @@ -455,8 +481,8 @@ int sv_main(int argc UNUSED_PARAM, char **argv) x = getenv("SVWAIT"); if (x) waitsec = xatou(x); - opt_complementary = "w+:vv"; /* -w N, -v is a counter */ - getopt32(argv, "w:v", &waitsec, &verbose); + opt_complementary = "vv"; /* -w N, -v is a counter */ + getopt32(argv, "w:+v", &waitsec, &verbose); argv += optind; action = *argv++; if (!action || !*argv) bb_show_usage(); @@ -615,3 +641,72 @@ int sv_main(int argc UNUSED_PARAM, char **argv) } return rc > 99 ? 99 : rc; } + +#if ENABLE_SV +int sv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int sv_main(int argc UNUSED_PARAM, char **argv) +{ + return sv(argv); +} +#endif + +//usage:#define svc_trivial_usage +//usage: "[-udopchaitkx] SERVICE_DIR..." +//usage:#define svc_full_usage "\n\n" +//usage: "Control services monitored by runsv supervisor" +//usage: "\n" +//usage: "\n"" -u If service is not running, start it; restart if it stops" +//usage: "\n"" -d If service is running, send TERM+CONT signals; do not restart it" +//usage: "\n"" -o Once: if service is not running, start it; do not restart it" +//usage: "\n"" -pchaitk Send STOP, CONT, HUP, ALRM, INT, TERM, KILL signal to service" +//usage: "\n"" -x Exit: runsv will exit as soon as the service is down" +#if ENABLE_SVC +int svc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int svc_main(int argc UNUSED_PARAM, char **argv) +{ + char command[2]; + const char *optstring; + unsigned opts; + + INIT_G(); + + optstring = "udopchaitkx"; + opts = getopt32(argv, optstring); + argv += optind; + if (!argv[0] || !opts) + bb_show_usage(); + + argv -= 2; + if (optind > 2) { + argv--; + argv[2] = (char*)"--"; + } + argv[0] = (char*)"sv"; + argv[1] = command; + command[1] = '\0'; + + /* getopt32() was already called: + * reset the libc getopt() function, which keeps internal state. + */ +#ifdef __GLIBC__ + optind = 0; +#else /* BSD style */ + optind = 1; + /* optreset = 1; */ +#endif + + do { + if (opts & 1) { + int r; + command[0] = *optstring; + r = sv(argv); + if (r) + return 1; + } + optstring++; + opts >>= 1; + } while (opts); + + return 0; +} +#endif |