Browse Source

update: crond start in init process

dev
dnomd343 3 years ago
parent
commit
a904856a81
  1. 1
      include/common.h
  2. 1
      src/cleardns.c
  3. 13
      src/common.c
  4. 86
      src/process.c

1
include/common.h

@ -1,7 +1,6 @@
#ifndef _COMMON_H_ #ifndef _COMMON_H_
#define _COMMON_H_ #define _COMMON_H_
extern char **crond_command;
extern char **adguard_command; extern char **adguard_command;
extern char **overture_command; extern char **overture_command;
extern char **domestic_dnsproxy_command; extern char **domestic_dnsproxy_command;

1
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 load_start_command(adguard_workdir, overture_config, upstream_config, debug_mode); // generate commands
if (debug_mode) { // show exec command if (debug_mode) { // show exec command
fprintf(stderr, "[ClearDNS] Debug mode.\n"); fprintf(stderr, "[ClearDNS] Debug mode.\n");
show_command("[ClearDNS] crond", crond_command);
show_command("[ClearDNS] adguard", adguard_command); show_command("[ClearDNS] adguard", adguard_command);
show_command("[ClearDNS] overture", overture_command); show_command("[ClearDNS] overture", overture_command);
show_command("[ClearDNS] dnsproxy (domestic)", domestic_dnsproxy_command); show_command("[ClearDNS] dnsproxy (domestic)", domestic_dnsproxy_command);

13
src/common.c

@ -4,7 +4,6 @@
#include "cJSON.h" #include "cJSON.h"
#include "exit_code.h" #include "exit_code.h"
char **crond_command = NULL;
char **adguard_command = NULL; char **adguard_command = NULL;
char **overture_command = NULL; char **overture_command = NULL;
char **domestic_dnsproxy_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) { 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 // AdGuardHome command
adguard_command = (char**)malloc(sizeof(char*) * 7); adguard_command = (char**)malloc(sizeof(char*) * 7);
adguard_command[0] = "AdGuardHome"; adguard_command[0] = "AdGuardHome";

86
src/process.c

@ -11,7 +11,6 @@
int exiting = 0; int exiting = 0;
pid_t crond_pid = 0;
pid_t adguard_pid = 0; pid_t adguard_pid = 0;
pid_t overture_pid = 0; pid_t overture_pid = 0;
pid_t domestic_dnsproxy_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; int status, ret;
exiting = 1; // set a exit flag 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) { if (adguard_pid != 0) {
fprintf(stderr, "[ClearDNS] Kill AdGuardHome. (pid = %d)\n", adguard_pid); fprintf(stderr, "[ClearDNS] Kill AdGuardHome. (pid = %d)\n", adguard_pid);
kill(adguard_pid, SIGTERM); kill(adguard_pid, SIGTERM);
@ -54,9 +49,18 @@ void server_exit(int exit_code) { // kill sub process and exit
kill(foreign_dnsproxy_pid, SIGTERM); kill(foreign_dnsproxy_pid, SIGTERM);
} }
while ((ret = wait(&status)) != -1) { ret = waitpid(adguard_pid, &status, 0); // blocking
fprintf(stderr, "[ClearDNS] Subprocess exit. (pid = %d)\n", ret); 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"); fprintf(stderr, "[ClearDNS] Exit successfully.\n");
exit(exit_code); exit(exit_code);
} }
@ -66,19 +70,6 @@ void get_sub_exit() { // catch child process exit
int wait_time = 3; // seconds for wait before restart int wait_time = 3; // seconds for wait before restart
if (exiting) { return; } // server daemon is exiting 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 if (adguard_pid != 0) { // check AdGuardHome
ret = waitpid(adguard_pid, &status, WNOHANG); // non-blocking ret = waitpid(adguard_pid, &status, WNOHANG); // non-blocking
if (ret == -1) { if (ret == -1) {
@ -136,7 +127,7 @@ void get_sub_exit() { // catch child process exit
perror("[ClearDNS] Waitpid error"); perror("[ClearDNS] Waitpid error");
server_exit(EXIT_WAIT_ERROR); server_exit(EXIT_WAIT_ERROR);
} else if (ret) { // process exit } 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; return;
} }
fprintf(stderr, "[ClearDNS] Subprocess not found.\n"); // get SIGCHLD signal but not exited subprocess found 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) void init_server(char *init_script, char *custom_script) { // run init script (blocking) / custom script (non-blocking)
int status; int pid, status;
pid_t init_pid, custom_pid; char *crond_command[] = {"crond", NULL};
char *init_command[] = {"sh", init_script, NULL};
char *custom_command[] = {"sh", custom_script, NULL};
if ((init_pid = fork()) < 0) { fprintf(stderr, "[ClearDNS] Start init process.\n");
perror("[ClearDNS] Fork error"); pid = process_exec(init_command);
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);
}
}
wait(&status); // blocking wait 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 if (access(custom_script, F_OK) >= 0) { // custom script exist
fprintf(stderr, "[ClearDNS] Running custom script.\n"); pid = process_exec(custom_command);
if ((custom_pid = fork()) < 0) { fprintf(stderr, "[ClearDNS] Custom script is working. (pid = %d)\n", pid); // custom process
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);
}
}
sleep(1); // wait a moment sleep(1); // wait a moment
} }
} }
void server_daemon() { // run as a daemon of cleardns void server_daemon() { // run as a daemon of ClearDNS
int wait_us_time = 200 * 1000; // 200ms int wait_us_time = 100000; // 100ms
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);
adguard_pid = process_exec(adguard_command); adguard_pid = process_exec(adguard_command);
fprintf(stderr, "[ClearDNS] Exec AdGuardHome. (pid = %d)\n", adguard_pid); fprintf(stderr, "[ClearDNS] Exec AdGuardHome. (pid = %d)\n", adguard_pid);

Loading…
Cancel
Save