Add support for finit <stop|start|reload|restart> JOB

This patch further extends the capabilities of the client with the
ability to control the running state of services:

    finit <stop|start|reload|restart> JOB

Since services can now exist with multiple instances the concept of
"jobs" is introduced in this patch.  A job is a numeric identifier of
the form `NUM[:NUM]`, the latter `:NUM` is the new instance syntax
introduced earlier.  See the output of `finit status` for the JOB id.

Also, with the introduction of multiple instances we broke support for
multiple related inetd services.  This became obvious when the shiny new
`finit status` was tested ... hence, this patch also contains fixes to
the inetd support.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2015-06-08 08:32:49 +02:00
1 parent 720324b256
commit 24e1d284ad
7 files changed
+296 -93

No files matched your search

+75 -46
View File
@@ -34,7 +34,7 @@
typedef struct {
char *cmd;
int (*cb)(void);
int (*cb)(char *arg);
} command_t;
@@ -67,7 +67,7 @@ static int do_send(struct init_request *rq, ssize_t len)
return result;
}
static int toggle_debug(void)
static int toggle_debug(char *UNUSED(arg))
{
struct init_request rq = {
.magic = INIT_MAGIC,
@@ -77,53 +77,69 @@ static int toggle_debug(void)
return do_send(&rq, sizeof(rq));
}
static int reload_conf(void)
{
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = INIT_CMD_RELOAD,
};
return do_send(&rq, sizeof(rq));
}
static int set_runlevel(int lvl)
static int set_runlevel(char *arg)
{
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = INIT_CMD_RUNLVL,
.runlevel = lvl,
.runlevel = (int)arg[0],
};
return do_send(&rq, sizeof(rq));
}
int show_version(void)
static int do_svc(int cmd, char *jobid)
{
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = cmd,
};
if (!jobid) {
if (cmd == INIT_CMD_RELOAD_SVC) {
rq.cmd = INIT_CMD_RELOAD;
goto exit;
}
return 1;
}
strlcpy(rq.data, jobid, sizeof(rq.data));
exit:
return do_send(&rq, sizeof(rq));
}
static int do_start (char *arg) { return do_svc(INIT_CMD_START_SVC, arg); }
static int do_stop (char *arg) { return do_svc(INIT_CMD_STOP_SVC, arg); }
static int do_reload (char *arg) { return do_svc(INIT_CMD_RELOAD_SVC, arg); }
static int do_restart(char *arg) { return do_svc(INIT_CMD_RESTART_SVC, arg); }
static int show_version(char *UNUSED(arg))
{
puts("v" VERSION);
return 0;
}
static char *svc_status(svc_t *svc)
{
if (svc->pid)
return "running";
if (svc_is_inetd(svc))
return "inetd";
return "waiting";
}
static int show_status(void)
/* In verbose mode we skip the header and each service description.
* This in favor of having all info on one line so a machine can more
* easily parse it. */
static int show_status(char *UNUSED(arg))
{
svc_t *svc;
printf("Status PID Runlevels Service Description\n");
printf("==============================================================================\n");
if (!verbose) {
printf("# Status PID Runlevels Service Description\n");
printf("====================================================================================\n");
}
for (svc = svc_iterator(1); svc; svc = svc_iterator(0)) {
printf("%7s %-6d %-9s %-20s %s\n", svc_status(svc), svc->pid,
runlevel_string(svc->runlevels), svc->cmd, svc->desc);
char jobid[10];
if (svc_is_unique(svc))
snprintf(jobid, sizeof(jobid), "%d", svc->job);
else
snprintf(jobid, sizeof(jobid), "%d:%d", svc->job, svc->id);
if (verbose) {
int i;
char args[80] = "";
@@ -132,7 +148,11 @@ static int show_status(void)
strlcat(args, svc->args[i], sizeof(args));
strlcat(args, " ", sizeof(args));
}
printf(" %s\n", args);
printf("%-6s %7s %-6d %-9s %s %s\n", jobid, svc_status(svc), svc->pid,
runlevel_string(svc->runlevels), svc->cmd, args);
} else {
printf("%-6s %7s %-6d %-9s %-20s %s\n", jobid, svc_status(svc), svc->pid,
runlevel_string(svc->runlevels), svc->cmd, svc->desc);
}
}
@@ -143,14 +163,19 @@ static int usage(int rc)
{
fprintf(stderr, "Usage: %s [OPTIONS] [<COMMAND> | <q | 1-6>]\n\n"
"Options:\n"
" -d, --debug Toggle Finit debug\n"
" -v, --verbose Verbose output\n"
" -h, --help This help text\n\n"
" -d, --debug Toggle Finit debug\n"
" -v, --verbose Verbose output\n"
" -h, --help This help text\n\n"
"Commands:\n"
" debug Toggle Finit debug\n"
" reload Reload *.conf in /etc/finit.d/\n"
" status Show status of services\n"
" version Show Finit version\n\n", __progname);
" debug Toggle Finit debug\n"
" q | reload Reload *.conf in /etc/finit.d/\n"
" runlevel <1-6> Set new runlevel\n"
" status Show status of services\n"
" start <JOB#> Start stopped service\n"
" stop <JOB#> Stop running service\n"
" restart <JOB#> Restart (stop/start) running service\n"
" reload <JOB#> Reload (SIGHUP) running service\n"
" version Show Finit version\n\n", __progname);
return rc;
}
@@ -160,10 +185,13 @@ int client(int argc, char *argv[])
int c;
command_t command[] = {
{ "debug", toggle_debug },
{ "q", reload_conf }, /* SysV init compat. */
{ "reload", reload_conf },
// { "runlevel", set_runlevel },
{ "q", do_reload }, /* SysV init compat. */
{ "reload", do_reload },
{ "runlevel", set_runlevel },
{ "status", show_status },
{ "start", do_start },
{ "stop", do_stop },
{ "restart", do_restart },
{ "version", show_version },
{ NULL, NULL }
};
@@ -178,7 +206,7 @@ int client(int argc, char *argv[])
while ((c = getopt_long(argc, argv, "h?dv", long_options, NULL)) != EOF) {
switch(c) {
case 'd':
toggle_debug();
toggle_debug(NULL);
break;
case 'h':
@@ -192,15 +220,16 @@ int client(int argc, char *argv[])
}
if (optind < argc) {
char *cmd = argv[optind];
char *cmd = argv[optind++];
char *job = optind < argc ? argv[optind] : NULL;
/* Compat: 'init <1-9>' */
if (strlen(cmd) == 1 && isdigit((int)cmd[0]))
return set_runlevel((int)cmd[0]);
return set_runlevel(cmd);
for (c = 0; command[c].cmd; c++) {
if (!strcmp(command[c].cmd, cmd))
return command[c].cb();
return command[c].cb(job);
}
}