From a8d45eff28e7e186d0749e3e9cf980a23d93231e Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Fri, 24 May 2019 17:07:22 +0300 Subject: Implement shutdown components * src/pies.h: Update comment. * src/progman.c (register_prog0): Register shutdown components in disabled state. (program_shutdown): New function. (progman_stop): Call program_shutdown. * tests/shutdown.at: New test. * tests/Makefile.am: Add new test. * tests/testsuite.at: Likewise. * tests/aux/startup: Rename to tests/aux/touchfile * tests/startup.at: Reflect the change. --- src/pies.h | 5 +++- src/progman.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/pies.h b/src/pies.h index bdc406b..74cb346 100644 --- a/src/pies.h +++ b/src/pies.h @@ -142,7 +142,10 @@ enum pies_comp_mode components is delayed until the last startup component finishes. */ pies_comp_startup, - /* FIXME: Runs before program termination */ + /* Components of this type are run right before program termination. + They have shutdown_timeout seconds to finish their job and terminate + gracefully, othervise they will be terminated forcefully via SIGTERM + (and SIGKILL, for persisting ones). */ pies_comp_shutdown, /* diff --git a/src/progman.c b/src/progman.c index a625885..bae82ba 100644 --- a/src/progman.c +++ b/src/progman.c @@ -287,7 +287,9 @@ register_prog0 (struct component *comp) else newp->v.p.status = status_stopped; - if ((comp->flags & CF_DISABLED) || comp->mode == pies_comp_ondemand) + if ((comp->flags & CF_DISABLED) + || comp->mode == pies_comp_ondemand + || comp->mode == pies_comp_shutdown) newp->active = 0; else newp->active = 1; @@ -1795,21 +1797,6 @@ prog_stop (struct prog *prog, int sig) kill (prog->pid, sig); } -static int -mark_for_stopping (struct prog *prog, void *data) -{ - if (IS_COMPONENT (prog)) - prog->stop = 1; - return 0; -} - -void -progman_stop (void) -{ - progman_foreach (mark_for_stopping, NULL); - progman_gc (); -} - static void print_status (const char *tag, pid_t pid, int status, int expect_term) { @@ -2582,4 +2569,59 @@ progman_gc (void) } +static int +start_shutdown (struct prog *prog, void *data) +{ + if (IS_COMPONENT (prog) && prog->v.p.comp->mode == pies_comp_shutdown) + { + int *p = data; + if (!*p) + { + diagmsg (DIAG_CON, LOG_INFO, "Starting shutdown components"); + *p = 1; + } + prog->active = 1; + prog_start (prog); + prog->stop = 1; + } + return 0; +} + +void +program_shutdown (void) +{ + time_t start = time (NULL); + int sd = 0; + + /* Activate shutdown components. */ + progman_foreach (start_shutdown, &sd); + while (prog_to_stop (proghead)) + { + progman_cleanup (1); + if (time (NULL) - start < shutdown_timeout) + sleep (1); + else + { + progman_gc (); + break; + } + } +} + +static int +mark_for_stopping (struct prog *prog, void *data) +{ + if (IS_COMPONENT (prog)) + prog->stop = 1; + return 0; +} + +void +progman_stop (void) +{ + progman_foreach (mark_for_stopping, NULL); + progman_gc (); + program_shutdown (); +} + /* EOF */ -- cgit v1.2.1