From bbe334110b23b792047a593509bb68f398ac04c2 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 8 Mar 2022 15:49:33 +0800 Subject: [PATCH] feat: subprocess manage demo --- .gitignore | 2 ++ CMakeLists.txt | 2 ++ include/common.h | 1 - include/process.h | 6 ++++++ src/cleardns.c | 48 ++++++++++++++++++++--------------------------- src/common.c | 7 ------- src/process.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 include/process.h create mode 100644 src/process.c diff --git a/.gitignore b/.gitignore index 5ac6c48..2234789 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /.idea/ +/bin/ +/build/ /cmake-build-debug/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 42b9b02..e793597 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,6 @@ cmake_minimum_required(VERSION 2.8.12) project(cleardns) +set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) + add_subdirectory(src) diff --git a/include/common.h b/include/common.h index 24a656a..3668eba 100644 --- a/include/common.h +++ b/include/common.h @@ -1,7 +1,6 @@ #ifndef _COMMON_H_ #define _COMMON_H_ -extern char **init_command; extern char **crond_command; extern char **adguard_command; extern char **overture_command; diff --git a/include/process.h b/include/process.h new file mode 100644 index 0000000..f3117bb --- /dev/null +++ b/include/process.h @@ -0,0 +1,6 @@ +#ifndef _PROCESS_H_ +#define _PROCESS_H_ + +void init_server(char *init_script, char *custom_script); + +#endif diff --git a/src/cleardns.c b/src/cleardns.c index a30ffd2..821e302 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -1,18 +1,24 @@ #include #include #include "common.h" +#include "process.h" int debug_mode = 0; +char *init_script = "/usr/bin/load"; +char *custom_script = "/etc/cleardns/custom.sh"; + char *adguard_workdir = "/etc/cleardns/AdGuardHome"; char *overture_config = "/etc/overture/config.yml"; char *upstream_config = "/etc/cleardns/upstream.json"; -void show_command(char **command) { +void show_command(char *title, char **command) { int num = 0; + fprintf(stderr, "\n%s => ", title); while(command[num] != NULL) { - printf("%s\n", command[num++]); + fprintf(stderr, "%s ", command[num++]); } + fprintf(stderr, "\n"); } int main(int argc, char *argv[]) { @@ -22,33 +28,19 @@ int main(int argc, char *argv[]) { } } - load_start_command(adguard_workdir, overture_config, upstream_config, debug_mode); - - printf("[init]\n"); - show_command(init_command); - printf("\n"); - - printf("[crond]\n"); - show_command(crond_command); - printf("\n"); - - printf("[AdGuardHome]\n"); - show_command(adguard_command); - printf("\n"); - - printf("[overture]\n"); - show_command(overture_command); - printf("\n"); - - printf("[domestic dnsproxy]\n"); - show_command(domestic_dnsproxy_command); - printf("\n"); + load_start_command(adguard_workdir, overture_config, upstream_config, debug_mode); // generate commands + if (debug_mode) { // show exec command + char *line = "----------------------------------------------------------------"; + fprintf(stderr, "\n%s%s\n", line, line); + show_command("crond", crond_command); + show_command("AdGuard", adguard_command); + show_command("overture", overture_command); + show_command("dnsproxy (domestic)", domestic_dnsproxy_command); + show_command("dnsproxy (foreign)", foreign_dnsproxy_command); + fprintf(stderr, "\n%s%s\n", line, line); + } - printf("[foreign dnsproxy]\n"); - show_command(foreign_dnsproxy_command); - printf("\n"); + init_server(init_script, custom_script); - printf("done.\n"); return 0; } - diff --git a/src/common.c b/src/common.c index a0325c2..521fddf 100644 --- a/src/common.c +++ b/src/common.c @@ -3,7 +3,6 @@ #include #include "cJSON.h" -char **init_command = NULL; char **crond_command = NULL; char **adguard_command = NULL; char **overture_command = NULL; @@ -121,12 +120,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) { - // init command - init_command = (char**)malloc(sizeof(char*) * 3); - init_command[0] = "sh"; - init_command[1] = "/usr/bin/load"; - init_command[2] = NULL; - // crond command crond_command = (char**)malloc(sizeof(char*) * 2); crond_command[0] = "crond"; diff --git a/src/process.c b/src/process.c new file mode 100644 index 0000000..17eaf8b --- /dev/null +++ b/src/process.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +void process_exec() { + +} + +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; + + if ((init_pid = fork()) < 0) { + perror("Fork error"); + exit(2); + } 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("Init error"); + exit(3); + } + } + wait(&status); // blocking wait + fprintf(stderr, "Init complete\n"); + + if (access(custom_script, F_OK) >= 0) { // custom script exist + if ((custom_pid = fork()) < 0) { + perror("Fork error"); + exit(2); + } 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("Custom script error"); + exit(3); + } + } + } +} + +void server_daemon() { + +}