initctl: Add SysV compat support for setenv

- Fix compat INIT_CMD_ defines so Finit clients can talk to SysV init
- Add setenv support, only for INIT_HALT={POWERDOWN,HALT} to runlevel 6

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2016-07-30 15:54:37 +02:00
parent 72a9832662
commit 4cf60b7491
3 changed files with 75 additions and 16 deletions
+46 -5
View File
@@ -39,6 +39,14 @@
#include "../sig.h"
#include "../service.h"
/*
* Old-style SysV shutdown sends a setenv cmd INIT_HALT with "=HALT",
* "=POWERDOWN", or "" to cancel shutdown, before requesting change to
* runlevel 6 over the /dev/initctl FIFO.
*/
shutop_t halt = SHUT_DEFAULT;
static void parse(void *arg, int fd, int events);
static plugin_t plugin = {
@@ -58,7 +66,36 @@ static void fifo_open(void)
}
}
/* Standard reboot/shutdown utilities talk to init using /dev/initctl.
/*
* We only handle INIT_HALT for differentiating between halt/poweroff
*/
static void set_env(char *data)
{
const char *var = "INIT_HALT";
if (strncmp(data, var, strlen(var)))
return;
data += strlen(var);
/*
* No value means unset env, in this case cancel shutdown.
*/
if (data[0] == 0) {
halt = SHUT_DEFAULT;
/* XXX: Cancel timer here, when we get support for timed shutdown */
return;
}
if (string_match(data, "=POWERDOWN"))
halt = SHUT_OFF;
if (string_match(data, "=HALT"))
halt = SHUT_HALT;
/* Now we wait for runlevel change, or cancelled shutdown */
}
/*
* Standard reboot/shutdown utilities talk to init using /dev/initctl.
* We should check if the fifo was recreated and reopen it.
*/
static void parse(void *UNUSED(arg), int fd, int UNUSED(events))
@@ -92,8 +129,8 @@ static void parse(void *UNUSED(arg), int fd, int UNUSED(events))
case INIT_CMD_RUNLVL:
switch (rq.runlevel) {
case '0':
_d("Halting system (SIGUSR2)");
do_shutdown(SIGUSR2);
_d("Halting system (SIGUSR1 or SIGUSR2)");
do_shutdown(halt);
break;
case 's':
@@ -109,8 +146,8 @@ static void parse(void *UNUSED(arg), int fd, int UNUSED(events))
break;
case '6':
_d("Rebooting system (SIGUSR1)");
do_shutdown(SIGUSR1);
_d("Rebooting system (SIGTERM)");
do_shutdown(SHUT_REBOOT);
break;
default:
@@ -123,6 +160,10 @@ static void parse(void *UNUSED(arg), int fd, int UNUSED(events))
service_reload_dynamic();
break;
case INIT_CMD_SETENV:
set_env(rq.data);
break;
default:
_d("Unsupported cmd: %d", rq.cmd);
break;