Skip to content

Commit

Permalink
handle shutdown in docker
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhangui committed Feb 26, 2024
1 parent 8eac7cd commit af94601
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
7 changes: 7 additions & 0 deletions daemontools-x/supervise.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static int flagwantup = 1;
static int flagpaused; /*- defined if (pid) */
static int fddir;
static int verbose = 0, silent = 0;
static int logger = 0;
static char flagfailed;
static unsigned long scan_interval = 60;
static char is_subreaper = 0, do_setpgid = 0;
Expand Down Expand Up @@ -155,8 +156,13 @@ sigterm()
{
int siglist[] = {-1, -1, -1};
char *signame[] = {0, 0};
static int got_sigterm;

if (childpid) {
if (getppid() == 1 && logger) {
if (!got_sigterm++)
return;
}
flagwant = 0;
flagwantup = 1;
siglist[0] = SIGTERM;
Expand Down Expand Up @@ -1000,6 +1006,7 @@ main(int argc, char **argv)
strerr_die1x(111, "supervise: out of memory");
} else
if (argc == 3) {
logger = 1;
if (!stralloc_copys(&fatal, argv[2]) || !stralloc_catb(&fatal, "/log: supervise: fatal: ", 24))
strerr_die1x(111, "supervise: out of memory");
if (!stralloc_copys(&warn, argv[2]) || !stralloc_catb(&warn, "/log: supervise: warning: ", 26))
Expand Down
41 changes: 29 additions & 12 deletions daemontools-x/svscan.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* $Id: svscan.c,v 1.34 2024-02-26 16:59:01+05:30 Cprogrammer Exp mbhangui $
* $Id: svscan.c,v 1.33 2023-06-05 19:00:51+05:30 Cprogrammer Exp mbhangui $
*/
#include <unistd.h>
#include <signal.h>
Expand Down Expand Up @@ -91,8 +91,10 @@ init_cmd(char *cmmd, int shutdown)
char strnum[FMT_ULONG];
time_t starttime, t;

#if 0
if (shutdown)
sig_block(sig_child);
#endif
cpath = shutdown ? SVSCANINFO"/shutdown" : cmmd && *cmmd ? cmmd : SVSCANINFO"/run";
if (access(cpath, X_OK))
return;
Expand Down Expand Up @@ -145,8 +147,12 @@ init_cmd(char *cmmd, int shutdown)
continue; /*- impossible for wait_nohang */
break;
}
if (!r)
break;
if (r)
continue;
if (!r) {
if (pid != 1)
break;
}
if (pid == 1 && shutdown) { /* wait_nohang was called */
if (!did_kill) {
did_kill = 1;
Expand All @@ -155,16 +161,28 @@ init_cmd(char *cmmd, int shutdown)
}
if (did_kill == 1) {
t = time((time_t *) NULL) - starttime;
if (t > w) {
if (t > w/2) {
did_kill = 2;
strerr_warn4(INFO, "pid: ", strnum, ": terminating session with TERM...", 0);
kill(0, sig_term);
}
}
if (did_kill == 2) {
t = time((time_t *) NULL) - starttime;
if (t > w) {
did_kill = 3;
strerr_warn4(INFO, "pid: ", strnum, ": terminating session with KILL...", 0);
kill(0, SIGKILL);
if (!kill(child, 0))
kill(child, SIGKILL);
}
}
strerr_warn4(INFO, "pid: ", strnum, ": waiting for processes to shutdown...", 0);
sleep(did_kill == 1 ? w : 5);
if (did_kill == 1 || did_kill == 2) {
sleep(w/2);
continue;
}
if (!r) {
strerr_warn4(INFO, "pid: ", strnum, ": waiting for processes to shutdown...", 0);
usleep(100);
}
continue;
} else /*- wait_pid on child was called */
break;
Expand Down Expand Up @@ -613,11 +631,13 @@ sigterm(int i)
{
char strnum[FMT_ULONG];

scannow = 0;
unlink(pidfile);
signal(SIGTERM, SIG_IGN);
strnum[fmt_ulong(strnum, getpid())] = 0;
strerr_warn4(INFO, "pid: ", strnum, ": shutdown...", 0);
init_cmd(0, 1); /*- run .svscan/shutdown */
strerr_warn4(INFO, "pid: ", strnum, ": exiting...", 0);
_exit(0);
}

Expand Down Expand Up @@ -906,16 +926,13 @@ main(int argc, char **argv)
void
getversion_svscan_c()
{
static char *y = "$Id: svscan.c,v 1.34 2024-02-26 16:59:01+05:30 Cprogrammer Exp mbhangui $";
static char *y = "$Id: svscan.c,v 1.33 2023-06-05 19:00:51+05:30 Cprogrammer Exp mbhangui $";

y++;
}

/*
* $Log: svscan.c,v $
* Revision 1.34 2024-02-26 16:59:01+05:30 Cprogrammer
* when running as init issue SIGKILL after KILLWAIT secs
*
* Revision 1.33 2023-06-05 19:00:51+05:30 Cprogrammer
* turn on verbose, silent using env variables VERBOSE, SILENT
*
Expand Down

0 comments on commit af94601

Please sign in to comment.