diff --git a/finit.c b/finit.c index 13750690..a405078c 100644 --- a/finit.c +++ b/finit.c @@ -26,6 +26,7 @@ #include #include #include /* umask(), mkdir() */ +#include #include "finit.h" #include "helpers.h" @@ -65,31 +66,63 @@ static void parse_kernel_cmdline(void) } } +static int usage(int rc) +{ + fprintf(stderr, "Usage: %s [OPTIONS] [RUNLEVEL]\n\n" + " -d, --debug Enable/Disable debug\n" + " -h, --help This help text\n\n", __progname); + + return rc; +} + static int client(int argc, char *argv[]) { int fd; + int c; + struct option long_options[] = { + {"debug", 0, NULL, 'd'}, + {"help", 0, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; struct init_request rq = { .magic = INIT_MAGIC, - .cmd = INIT_CMD_RUNLVL, + .cmd = 0 }; + while ((c = getopt_long (argc, argv, "h?d", long_options, NULL)) != EOF) { + switch(c) { + case 'd': + rq.cmd = INIT_CMD_DEBUG; + break; + + case 'h': + case '?': + return usage(0); + default: + return usage(1); + } + } + + if (!rq.cmd) { + if (argc < 2) { + fprintf(stderr, "Missing runlevel.\n"); + return 1; + } + + rq.cmd = INIT_CMD_RUNLVL; + rq.runlevel = (int)argv[1][0]; + } + if (!fexist(FINIT_FIFO)) { fprintf(stderr, "/sbin/init does not support %s!\n", FINIT_FIFO); return 1; } - if (argc < 2) { - fprintf(stderr, "Missing argument.\n"); - return 1; - } - fd = open(FINIT_FIFO, O_WRONLY); if (-1 == fd) { perror("Failed opening " FINIT_FIFO); return 1; } - - rq.runlevel = (int)argv[1][0]; write(fd, &rq, sizeof(rq)); close(fd); diff --git a/finit.h b/finit.h index 4bd8349c..efde7e7a 100644 --- a/finit.h +++ b/finit.h @@ -64,6 +64,7 @@ #define INIT_MAGIC 0x03091969 #define INIT_CMD_RUNLVL 1 +#define INIT_CMD_DEBUG 2 struct init_request { int magic; /* Magic number */ @@ -81,6 +82,7 @@ extern char *hostname; extern char *username; extern char *rcsd; extern char *console; +extern char *__progname; /* conf.c */ void parse_finit_conf(char *file); diff --git a/plugins/initctl.c b/plugins/initctl.c index c0e7bd6d..96dec18b 100644 --- a/plugins/initctl.c +++ b/plugins/initctl.c @@ -107,6 +107,8 @@ static void parse(void *UNUSED(arg), int fd, int UNUSED(events)) _d("Unsupported runlevel: %d", rq.runlevel); break; } + } else if (rq.cmd == INIT_CMD_DEBUG) { + debug = !debug; } else { _d("Unsupported cmd: %d", rq.cmd); }