Fix #302: add support for running scripts at shutdown

This change adds support for running hook scripts at two new hook points
during the shutdown process:

 - HOOK_SVC_DN: after all services and non-reserved processes have been
   killed (and collected)
 - HOOK_SYS_DN: after all file systems have been unmounted, *just prior*
   to Finit calling reboot() to shut down or reboot the system

See doc/plugins.md for details and an example.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2022-11-07 07:01:21 +01:00
parent 5f74adad06
commit d810ac00bc
5 changed files with 74 additions and 11 deletions
+29
View File
@@ -195,11 +195,40 @@ int plugin_exists(hook_point_t no)
return 0;
}
#ifdef HAVE_HOOK_SCRIPTS_PLUGIN
#define CHOOSE(x, y) y
static const char *hscript_paths[] = HOOK_TYPES;
#undef CHOOSE
void plugin_script_run(hook_point_t no)
{
char path[CMD_SIZE] = "";
strlcat(path, PLUGIN_HOOK_SCRIPTS_PATH, sizeof(path));
strlcat(path, hscript_paths[no] + 4, sizeof(path));
run_parts(path, NULL);
}
#else
void plugin_script_run(hook_point_t no)
{
(void)no;
}
#endif
/* Some hooks are called with a fixed argument */
void plugin_run_hook(hook_point_t no, void *arg)
{
plugin_t *p, *tmp;
if (!cond_is_available()) {
#ifdef HAVE_HOOK_SCRIPTS_PLUGIN
warnx("conditions not available, calling script script based hooks only!");
plugin_script_run(no);
#endif
return;
}
PLUGIN_ITERATOR(p, tmp) {
if (p->hook[no].cb) {
dbg("Calling %s hook n:o %d (arg: %p) ...", basename(p->name), no, arg ?: "NIL");
+2
View File
@@ -82,6 +82,8 @@
\
/* Shutdown hooks, runlevel [06] */ \
CHOOSE(HOOK_SHUTDOWN, "hook/sys/shutdown"), \
CHOOSE(HOOK_SVC_DN, "hook/svc/down"), \
CHOOSE(HOOK_SYS_DN, "hook/sys/down"), \
CHOOSE(HOOK_MAX_NUM, "nop") \
}
+6
View File
@@ -329,6 +329,9 @@ void do_shutdown(shutop_t op)
while (waitpid(-1, NULL, WNOHANG) > 0)
;
/* All services and (non-critical) processes have stopped. */
plugin_run_hooks(HOOK_SVC_DN);
if (in_cont) {
if (osheading)
logit(LOG_CONSOLE | LOG_NOTICE, "%s, shutting down container.", osheading);
@@ -374,6 +377,9 @@ void do_shutdown(shutop_t op)
/* Call mdadm to mark any RAID array(s) as clean before halting. */
mdadm_wait();
/* Last chance to run scripts (all .so plugins have exited already) */
plugin_run_hooks(HOOK_SYS_DN);
/* Reboot via watchdog or kernel, or shutdown? */
if (op == SHUT_REBOOT) {
if (wdog && wdog->pid > 1) {