From a904856a812929b9fa3d2d33a9861934f16c9e92 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 9 Mar 2022 14:57:18 +0800 Subject: [PATCH] update: crond start in init process --- include/common.h | 1 - src/cleardns.c | 1 - src/common.c | 13 -------- src/process.c | 86 +++++++++++++++++------------------------------- 4 files changed, 31 insertions(+), 70 deletions(-) diff --git a/include/common.h b/include/common.h index 3668eba..18699c2 100644 --- a/include/common.h +++ b/include/common.h @@ -1,7 +1,6 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -extern char **crond_command; extern char **adguard_command; extern char **overture_command; extern char **domestic_dnsproxy_command; diff --git a/src/cleardns.c b/src/cleardns.c index 34dce71..cdbb994 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -34,7 +34,6 @@ int main(int argc, char *argv[]) { // ClearDNS server load_start_command(adguard_workdir, overture_config, upstream_config, debug_mode); // generate commands if (debug_mode) { // show exec command fprintf(stderr, "[ClearDNS] Debug mode.\n"); - show_command("[ClearDNS] crond", crond_command); show_command("[ClearDNS] adguard", adguard_command); show_command("[ClearDNS] overture", overture_command); show_command("[ClearDNS] dnsproxy (domestic)", domestic_dnsproxy_command); diff --git a/src/common.c b/src/common.c index 5724875..9dceb22 100644 --- a/src/common.c +++ b/src/common.c @@ -4,7 +4,6 @@ #include "cJSON.h" #include "exit_code.h" -char **crond_command = NULL; char **adguard_command = NULL; char **overture_command = NULL; char **domestic_dnsproxy_command = NULL; @@ -117,18 +116,6 @@ char** dnsproxy_config(char *port, cJSON *json, int is_debug) { // generate dnsp } void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug) { - // crond command - crond_command = (char**)malloc(sizeof(char*) * 2); - crond_command[0] = "crond"; - 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"); - } - // AdGuardHome command adguard_command = (char**)malloc(sizeof(char*) * 7); adguard_command[0] = "AdGuardHome"; diff --git a/src/process.c b/src/process.c index dcaec3e..f0fac89 100644 --- a/src/process.c +++ b/src/process.c @@ -11,7 +11,6 @@ int exiting = 0; -pid_t crond_pid = 0; pid_t adguard_pid = 0; pid_t overture_pid = 0; pid_t domestic_dnsproxy_pid = 0; @@ -33,10 +32,6 @@ void server_exit(int exit_code) { // kill sub process and exit int status, ret; exiting = 1; // set a exit flag - 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); @@ -54,9 +49,18 @@ void server_exit(int exit_code) { // kill sub process and exit kill(foreign_dnsproxy_pid, SIGTERM); } - while ((ret = wait(&status)) != -1) { - fprintf(stderr, "[ClearDNS] Subprocess exit. (pid = %d)\n", ret); - } + ret = waitpid(adguard_pid, &status, 0); // blocking + fprintf(stderr, "[ClearDNS] AdGuardHome exit. (pid = %d)\n", ret); + + ret = waitpid(overture_pid, &status, 0); // blocking + fprintf(stderr, "[ClearDNS] overture exit. (pid = %d)\n", ret); + + ret = waitpid(domestic_dnsproxy_pid, &status, 0); // blocking + fprintf(stderr, "[ClearDNS] domestic dnsproxy exit. (pid = %d)\n", ret); + + ret = waitpid(foreign_dnsproxy_pid, &status, 0); // blocking + fprintf(stderr, "[ClearDNS] foreign dnsproxy exit. (pid = %d)\n", ret); + fprintf(stderr, "[ClearDNS] Exit successfully.\n"); exit(exit_code); } @@ -66,19 +70,6 @@ void get_sub_exit() { // catch child process exit 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(EXIT_WAIT_ERROR); - } else if (ret) { // process exit - crond_pid = process_exec(crond_command); - fprintf(stderr, "[ClearDNS] Catch crond exit and restart it. (pid = %d -> %d)\n", ret, 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) { @@ -136,7 +127,7 @@ void get_sub_exit() { // catch child process exit perror("[ClearDNS] Waitpid error"); server_exit(EXIT_WAIT_ERROR); } else if (ret) { // process exit - fprintf(stderr, "[ClearDNS] Catch subprocess exit. (pid = %d)\n", ret); + fprintf(stderr, "[ClearDNS] Catch subprocess detach or exit. (pid = %d)\n", ret); return; } fprintf(stderr, "[ClearDNS] Subprocess not found.\n"); // get SIGCHLD signal but not exited subprocess found @@ -159,47 +150,32 @@ pid_t process_exec(char **command) { } void init_server(char *init_script, char *custom_script) { // run init script (blocking) / custom script (non-blocking) - int status; - pid_t init_pid, custom_pid; + int pid, status; + char *crond_command[] = {"crond", NULL}; + char *init_command[] = {"sh", init_script, NULL}; + char *custom_command[] = {"sh", custom_script, NULL}; - if ((init_pid = fork()) < 0) { - perror("[ClearDNS] Fork error"); - server_exit(EXIT_FORK_ERROR); - } else if (init_pid == 0) { // child process - prctl(PR_SET_PDEATHSIG, SIGKILL); // child process die with father process - if (execlp("/bin/sh", "/bin/sh", init_script, NULL)) { - perror("[ClearDNS] Init error"); - server_exit(EXIT_EXEC_ERROR); - } - } + fprintf(stderr, "[ClearDNS] Start init process.\n"); + pid = process_exec(init_command); wait(&status); // blocking wait - fprintf(stderr, "[ClearDNS] Init complete.\n"); + fprintf(stderr, "[ClearDNS] Init complete. (pid = %d)\n", pid); // init process + + 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 + + pid = process_exec(crond_command); + fprintf(stderr, "[ClearDNS] Crond load success. (pid = %d)\n", pid); // crond process if (access(custom_script, F_OK) >= 0) { // custom script exist - fprintf(stderr, "[ClearDNS] Running custom script.\n"); - if ((custom_pid = fork()) < 0) { - perror("[ClearDNS] Fork error"); - server_exit(EXIT_FORK_ERROR); - } else if (custom_pid == 0) { // child process - prctl(PR_SET_PDEATHSIG, SIGKILL); // child process die with father process - if (execlp("/bin/sh", "/bin/sh", custom_script, NULL)) { - perror("[ClearDNS] Custom script error"); - server_exit(EXIT_EXEC_ERROR); - } - } + pid = process_exec(custom_command); + fprintf(stderr, "[ClearDNS] Custom script is working. (pid = %d)\n", pid); // custom process sleep(1); // wait a moment } } -void server_daemon() { // run as a daemon of cleardns - int wait_us_time = 200 * 1000; // 200ms - 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 - - crond_pid = process_exec(crond_command); - fprintf(stderr, "[ClearDNS] Exec crond. (pid = %d)\n", crond_pid); - usleep(wait_us_time); +void server_daemon() { // run as a daemon of ClearDNS + int wait_us_time = 100000; // 100ms adguard_pid = process_exec(adguard_command); fprintf(stderr, "[ClearDNS] Exec AdGuardHome. (pid = %d)\n", adguard_pid);