Refactor: introduce telinit and a new initctl tool w/ API

- Reduce size of `helpers.c`, for linking against new `initctl` tool,
  by moving out functions to `pid.c` and `exec.c`
- Add `AF_UNIX` API to Finit, to complement old `/dev/initctl` FIFO
- Let old FIFO API be used by init/telinit: `init <q | 1-9>`
- Move all advanced initctl code from `client.c` to `initctl.c`, yes
  its a bit confusing to call the *new* tool the same as the old FIFO
  but this is more in line with what, e.g Upstart does.
- Move all advanced server side code from `plugins/initctl.c` to `api.c`
- Update TODO with upcoming inetd syntax change and dynamic events.
- Temporarily fix display of inetd services from `initctl status -v`

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2015-06-13 12:26:16 +02:00
1 parent 66edb42e06
commit 1a1f24721f
15 files changed
+934 -731

No files matched your search

+24 -154
View File
@@ -1,4 +1,4 @@
/* Finit client for querying status and control services
/* Simple telinit client
*
* Copyright (c) 2008-2010 Claudio Matsuoka <cmatsuoka@gmail.com>
* Copyright (c) 2008-2015 Joachim Nilsson <troglobit@gmail.com>
@@ -27,22 +27,18 @@
#include <stdio.h>
#include "finit.h"
#include "helpers.h"
#include "service.h"
#include "libite/lite.h"
typedef struct {
char *cmd;
int (*cb)(char *arg);
} command_t;
static int do_send(struct init_request *rq, ssize_t len)
static int do_send(int cmd, int runlevel)
{
int fd;
int result = -1;
ssize_t num;
int result = 255;
ssize_t len;
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = cmd,
.runlevel = runlevel,
};
if (!fexist(FINIT_FIFO)) {
fprintf(stderr, "%s does not support %s!\n", __progname, FINIT_FIFO);
@@ -55,127 +51,22 @@ static int do_send(struct init_request *rq, ssize_t len)
return 1;
}
num = write(fd, rq, len);
if (num == len) {
num = read(fd, rq, len);
if (num == len && rq->cmd == INIT_CMD_ACK)
result = 0;
}
len = write(fd, &rq, sizeof(rq));
if (sizeof(rq) == len)
result = 0;
close(fd);
return result;
}
static int toggle_debug(char *UNUSED(arg))
{
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = INIT_CMD_DEBUG,
};
return do_send(&rq, sizeof(rq));
}
static int set_runlevel(char *arg)
{
struct init_request rq = {
.magic = INIT_MAGIC,
.cmd = INIT_CMD_RUNLVL,
.runlevel = (int)arg[0],
};
return do_send(&rq, sizeof(rq));
}
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;
}
/* 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;
if (!verbose) {
printf("# Status PID Runlevels Service Description\n");
printf("====================================================================================\n");
}
for (svc = svc_iterator(1); svc; svc = svc_iterator(0)) {
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] = "";
for (i = 1; i < MAX_NUM_SVC_ARGS; i++) {
strlcat(args, svc->args[i], sizeof(args));
strlcat(args, " ", sizeof(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);
}
}
return 0;
}
/* telinit q | telinit <1-6> */
static int usage(int rc)
{
fprintf(stderr, "Usage: %s [OPTIONS] [<COMMAND> | <q | 1-6>]\n\n"
fprintf(stderr, "Usage: %s <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"
"Commands:\n"
" 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);
" -V, --version Show Finit version\n\n", __progname);
return rc;
}
@@ -183,54 +74,33 @@ static int usage(int rc)
int client(int argc, char *argv[])
{
int c;
command_t command[] = {
{ "debug", toggle_debug },
{ "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 }
};
struct option long_options[] = {
{"debug", 0, NULL, 'd'},
{"help", 0, NULL, 'h'},
{"verbose", 0, NULL, 'v'},
{"version", 0, NULL, 'v'},
{NULL, 0, NULL, 0}
};
verbose = 0;
while ((c = getopt_long(argc, argv, "h?dv", long_options, NULL)) != EOF) {
while ((c = getopt_long(argc, argv, "h?v", long_options, NULL)) != EOF) {
switch(c) {
case 'd':
toggle_debug(NULL);
break;
case 'v':
return puts("v" VERSION) == EOF;
case 'h':
case '?':
return usage(0);
case 'v':
verbose = 1;
break;
}
}
if (optind < argc) {
char *cmd = argv[optind++];
char *job = optind < argc ? argv[optind] : NULL;
int req = (int)argv[optind][0];
/* Compat: 'init <1-9>' */
if (strlen(cmd) == 1 && isdigit((int)cmd[0]))
return set_runlevel(cmd);
if (isdigit(req))
return do_send(INIT_CMD_RUNLVL, req);
for (c = 0; command[c].cmd; c++) {
if (!strcmp(command[c].cmd, cmd))
return command[c].cb(job);
}
if (req == 'q' || req == 'Q')
return do_send(INIT_CMD_RELOAD, 0);
}
return usage(1);