From c4449ead50d764894763ba9c7486332af6472b3c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 23 Sep 2022 09:18:34 +0800 Subject: [PATCH] update: server exit process --- include/utils/process.h | 2 -- src/utils/process.c | 21 ++++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/utils/process.h b/include/utils/process.h index df265e6..cf19362 100644 --- a/include/utils/process.h +++ b/include/utils/process.h @@ -11,8 +11,6 @@ typedef struct { char *cwd; // working directory } process; -//extern process **process_list; - void process_list_run(); void process_list_init(); void process_list_daemon(); diff --git a/src/utils/process.c b/src/utils/process.c index 3bc5a36..95d096c 100644 --- a/src/utils/process.c +++ b/src/utils/process.c @@ -86,8 +86,8 @@ void process_list_run() { // start process list signal(SIGINT, get_exit_signal); // catch Ctrl + C (2) signal(SIGTERM, get_exit_signal); // catch exit signal (15) signal(SIGCHLD, get_sub_exit); // callback when child process die - for (process **p = process_list; *p != NULL; ++p) { - process_exec(*p); + for (process **proc = process_list; *proc != NULL; ++proc) { + process_exec(*proc); usleep(100 * 1000); // delay 100ms } log_info("Process start complete"); @@ -104,11 +104,17 @@ void server_exit(int exit_code) { // kill sub process and exit pause(); } EXITING = TRUE; - - log_info("ClearDNS exiting"); - - // TODO: kill subprocess and exit cleardns - + log_info("ClearDNS start exit process"); + for (process **proc = process_list; *proc != NULL; ++proc) { // send SIGTERM to all subprocess + log_info("%s send kill signal -> PID = %d", (*proc)->name, (*proc)->pid); + kill((*proc)->pid, SIGTERM); + } + for (process **proc = process_list; *proc != NULL; ++proc) { // ensure all subprocess exited + int status; + int ret = waitpid((*proc)->pid, &status, 0); // blocking wait + log_info("%s exit -> PID = %d", (*proc)->name, ret); + } + log_info("All sub-process exited"); exit(exit_code); } @@ -121,4 +127,5 @@ void get_sub_exit() { // catch child process exit log_debug("Get SIGCHLD signal"); // TODO: check exit subprocess and restart it + }