From 701d5fefc6dd3fc98ad4d19a22a1b4abc820da49 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 8 Mar 2022 18:35:35 +0800 Subject: [PATCH] update: daemon of crond, overture and AdGuardHome --- src/common.c | 4 +++- src/process.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/common.c b/src/common.c index d2106de..11d2cac 100644 --- a/src/common.c +++ b/src/common.c @@ -126,6 +126,8 @@ void load_start_command(char *adguard_workdir, char *overture_config, char *upst crond_command[1] = NULL; // end sign if (is_debug) { // debug mode crond_command = command_add_field(crond_command, "-f"); // run in foreground + crond_command = command_add_field(crond_command, "-l"); // log-level + crond_command = command_add_field(crond_command, "0"); // 0 -> verbose crond_command = command_add_field(crond_command, "-L"); crond_command = command_add_field(crond_command, "/dev/stdout"); } @@ -181,5 +183,5 @@ void load_start_command(char *adguard_workdir, char *overture_config, char *upst if (foreign_dnsproxy_command == NULL) { error_exit("Miss foreign DNS settings."); } - cJSON_free(json_root); + cJSON_free(json_root); // free cJSON object } diff --git a/src/process.c b/src/process.c index 065e212..225900b 100644 --- a/src/process.c +++ b/src/process.c @@ -25,6 +25,18 @@ void server_exit() { // kill sub-process and exit exiting = 1; fprintf(stderr, "[ClearDNS] Get exit signal.\n"); + if (crond_pid != 0) { + fprintf(stderr, "[ClearDNS] Kill crond. (pid = %d)\n", crond_pid); + kill(crond_pid, SIGTERM); + } + if (adguard_pid != 0) { + fprintf(stderr, "[ClearDNS] Kill AdGuardHome. (pid = %d)\n", adguard_pid); + kill(adguard_pid, SIGTERM); + } + if (overture_pid != 0) { + fprintf(stderr, "[ClearDNS] Kill overture. (pid = %d)\n", overture_pid); + kill(overture_pid, SIGTERM); + } if (domestic_dnsproxy_pid != 0) { fprintf(stderr, "[ClearDNS] Kill domestic dnsproxy. (pid = %d)\n", domestic_dnsproxy_pid); kill(domestic_dnsproxy_pid, SIGTERM); @@ -46,6 +58,45 @@ void get_sub_exit() { // catch child process die int wait_time = 3; // seconds for wait before restart if (exiting) { return; } // server daemon is exiting + if (crond_pid != 0) { // check crond + ret = waitpid(crond_pid, &status, WNOHANG); // non-blocking + if (ret == -1) { + perror("[ClearDNS] Waitpid error"); + server_exit(); + } else if (ret) { // process exit + crond_pid = process_exec(crond_command); + fprintf(stderr, "[ClearDNS] Catch crond exit and restart it. (pid = %d)\n", crond_pid); + sleep(wait_time); // reduce restart frequency + return; + } + } + + if (adguard_pid != 0) { // check AdGuardHome + ret = waitpid(adguard_pid, &status, WNOHANG); // non-blocking + if (ret == -1) { + perror("[ClearDNS] Waitpid error"); + server_exit(); + } else if (ret) { // process exit + adguard_pid = process_exec(adguard_command); + fprintf(stderr, "[ClearDNS] Catch AdGuardHome exit and restart it. (pid = %d)\n", adguard_pid); + sleep(wait_time); // reduce restart frequency + return; + } + } + + if (overture_pid != 0) { // check overture + ret = waitpid(overture_pid, &status, WNOHANG); // non-blocking + if (ret == -1) { + perror("[ClearDNS] Waitpid error"); + server_exit(); + } else if (ret) { // process exit + overture_pid = process_exec(overture_command); + fprintf(stderr, "[ClearDNS] Catch overture exit and restart it. (pid = %d)\n", overture_pid); + sleep(wait_time); // reduce restart frequency + return; + } + } + if (domestic_dnsproxy_pid != 0) { // check domestic dnsproxy ret = waitpid(domestic_dnsproxy_pid, &status, WNOHANG); // non-blocking if (ret == -1) { @@ -138,6 +189,18 @@ void server_daemon() { // run as a daemon of cleardns signal(SIGTERM, server_exit); // catch exit signal (15) signal(SIGCHLD, get_sub_exit); // callback when child process die + crond_pid = process_exec(crond_command); + fprintf(stderr, "[ClearDNS] Exec crond. (pid = %d)\n", crond_pid); + usleep(wait_us_time); + + adguard_pid = process_exec(adguard_command); + fprintf(stderr, "[ClearDNS] Exec AdGuardHome. (pid = %d)\n", adguard_pid); + usleep(wait_us_time); + + overture_pid = process_exec(overture_command); + fprintf(stderr, "[ClearDNS] Exec overture. (pid = %d)\n", overture_pid); + usleep(wait_us_time); + domestic_dnsproxy_pid = process_exec(domestic_dnsproxy_command); fprintf(stderr, "[ClearDNS] Exec domestic dnsproxy. (pid = %d)\n", domestic_dnsproxy_pid); usleep(wait_us_time);