Browse Source

feat: subprocess manage demo

dev
dnomd343 3 years ago
parent
commit
bbe334110b
  1. 2
      .gitignore
  2. 2
      CMakeLists.txt
  3. 1
      include/common.h
  4. 6
      include/process.h
  5. 48
      src/cleardns.c
  6. 7
      src/common.c
  7. 44
      src/process.c

2
.gitignore

@ -1,2 +1,4 @@
/.idea/
/bin/
/build/
/cmake-build-debug/

2
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)

1
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;

6
include/process.h

@ -0,0 +1,6 @@
#ifndef _PROCESS_H_
#define _PROCESS_H_
void init_server(char *init_script, char *custom_script);
#endif

48
src/cleardns.c

@ -1,18 +1,24 @@
#include <stdio.h>
#include <string.h>
#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;
}

7
src/common.c

@ -3,7 +3,6 @@
#include <string.h>
#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";

44
src/process.c

@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/prctl.h>
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() {
}
Loading…
Cancel
Save