Browse Source

update: process manager module

dev
Dnomd343 2 years ago
parent
commit
1cbd7e8def
  1. 2
      Dockerfile
  2. 17
      include/utils/process.h
  3. 8
      src/applet/adguard.c
  4. 14
      src/cleardns.c
  5. 125
      src/utils/process.c

2
Dockerfile

@ -64,4 +64,4 @@ COPY --from=cleardns /tmp/toJSON /asset/usr/bin/
FROM ${ALPINE_IMG} FROM ${ALPINE_IMG}
COPY --from=asset /asset/ / COPY --from=asset /asset/ /
#ENTRYPOINT ["cleardns"] ENTRYPOINT ["cleardns"]

17
include/utils/process.h

@ -4,21 +4,20 @@
#include <stdint.h> #include <stdint.h>
typedef struct { typedef struct {
int pid; int pid; // process id
char *name; char *name; // caption
char **cmd; char **cmd; // process command
char **env; char **env; // environment variable
char *cwd; char *cwd; // working directory
} process; } process;
extern process **process_list;
void process_list_run(); void process_list_run();
void process_list_init(); void process_list_init();
void process_list_daemon();
void process_list_append(process *proc); void process_list_append(process *proc);
void process_add_arg(process *proc, const char *arg); void process_add_arg(process *proc, const char *arg);
process* process_init(const char *caption, const char *bin); process* process_init(const char *caption, const char *bin);
#include <unistd.h>
void process_exec(process *proc);
void process_list_daemon();
#endif #endif

8
src/applet/adguard.c

@ -88,13 +88,11 @@ process* adguard_load(adguard *info, const char *dir) { // load adguard options
char *adguard_config_ret; char *adguard_config_ret;
char *adguard_config_file = string_join(dir, "AdGuardHome.yaml"); char *adguard_config_file = string_join(dir, "AdGuardHome.yaml");
// TODO: skip to-json if AdGuardHome configure not exist if (!is_file_exist(adguard_config_file)) { // AdGuardHome configure not exist
char *adguard_config_raw = to_json(adguard_config_file);
if (adguard_config_raw == NULL) { // configure not exist
log_info("Create AdGuardHome configure"); log_info("Create AdGuardHome configure");
adguard_config_ret = adguard_config(info, "{}"); // begin with empty json adguard_config_ret = adguard_config(info, "{}"); // begin with empty json
} else { } else { // configure exist -> modify
char *adguard_config_raw = to_json(adguard_config_file);
adguard_config_ret = adguard_config(info, adguard_config_raw); adguard_config_ret = adguard_config(info, adguard_config_raw);
free(adguard_config_raw); free(adguard_config_raw);
} }

14
src/cleardns.c

@ -4,15 +4,15 @@
#include <sys/wait.h> #include <sys/wait.h>
#include "loader.h" #include "loader.h"
#include "logger.h" #include "logger.h"
#include "sundry.h"
#include "system.h"
#include "assets.h"
#include "adguard.h"
#include "crontab.h"
#include "constant.h" #include "constant.h"
#include "dnsproxy.h" #include "dnsproxy.h"
#include "overture.h" #include "overture.h"
#include "adguard.h"
#include "system.h"
#include "sundry.h"
#include "structure.h" #include "structure.h"
#include "assets.h"
#include "crontab.h"
char* init(int argc, char *argv[]) { // return config file char* init(int argc, char *argv[]) { // return config file
char *config = string_init(CONFIG_FILE); char *config = string_init(CONFIG_FILE);
@ -43,10 +43,8 @@ char* init(int argc, char *argv[]) { // return config file
int main(int argc, char *argv[]) { // ClearDNS service int main(int argc, char *argv[]) { // ClearDNS service
char *config_file = init(argc, argv); char *config_file = init(argc, argv);
LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION); log_info("ClearDNS server start (%s)", VERSION);
create_folder(EXPOSE_DIR);
create_folder(WORK_DIR); create_folder(WORK_DIR);
extract_assets(); extract_assets();
load_config(config_file); load_config(config_file);

125
src/utils/process.c

@ -1,17 +1,22 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/types.h>
#include <sys/prctl.h> #include <sys/prctl.h>
//#include <stdio.h> #include <sys/types.h>
#include "logger.h"
#include "sundry.h" #include "sundry.h"
#include "process.h" #include "process.h"
#include "constant.h" #include "constant.h"
#include "structure.h" #include "structure.h"
#include "logger.h"
process **process_list; process **process_list;
void get_sub_exit();
void get_exit_signal();
void server_exit(int exit_code);
void process_dump(process *proc);
void process_exec(process *proc);
process* process_init(const char *caption, const char *bin) { // init process struct process* process_init(const char *caption, const char *bin) { // init process struct
process *proc = (process *)malloc(sizeof(process)); process *proc = (process *)malloc(sizeof(process));
proc->pid = 0; // process not running proc->pid = 0; // process not running
@ -23,104 +28,66 @@ process* process_init(const char *caption, const char *bin) { // init process st
return proc; return proc;
} }
void process_add_arg(process *proc, const char *arg) { // add argument for process void process_dump(process *proc) { // output process options into debug log
string_list_append(&proc->cmd, arg);
}
void process_list_init() { // init process list
process_list = (process **)malloc(sizeof(process *));
*process_list = NULL;
}
void process_list_append(process *proc) { // add new process into process list
int proc_num = 0;
for (process **p = process_list; *p != NULL; ++p, ++proc_num); // get process number
process_list = (process **)realloc(process_list, sizeof(process *) * (proc_num + 2));
process_list[proc_num++] = proc; // add into process list
process_list[proc_num] = NULL;
log_debug("%s process added", proc->name);
}
void server_exit(int exit_code) { // kill sub process and exit
log_info("Kill subprocess");
// TODO: kill subprocess and exit cleardns
exit(exit_code);
}
void get_exit_signal() { // get SIGINT or SIGTERM signal
log_info("Get exit signal");
server_exit(EXIT_NORMAL);
}
void get_sub_exit() { // catch child process exit
log_debug("Get SIGCHLD signal");
// TODO: check exit subprocess and restart it
}
void process_list_run() {
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
for (process **p = process_list; *p != NULL; ++p) {
process_exec(*p);
}
log_warn("Process list start complete");
}
void process_dump(process *proc) {
char *process_cmd = string_list_dump(proc->cmd); char *process_cmd = string_list_dump(proc->cmd);
char *process_env = string_list_dump(proc->env); char *process_env = string_list_dump(proc->env);
log_debug("%s cwd -> %s", proc->name, proc->cwd); log_debug("%s cwd -> %s", proc->name, proc->cwd);
log_debug("%s command -> %s", proc->name, process_cmd); log_debug("%s command -> %s", proc->name, process_cmd);
log_debug("%s env variable -> %s", proc->name, process_env); log_debug("%s env variable -> %s", proc->name, process_env);
// log_debug("%s new group -> %s", proc->name, show_bool(proc->is_group));
free(process_env); free(process_env);
free(process_cmd); free(process_cmd);
} }
void process_exec(process *proc) { void process_exec(process *proc) {
pid_t pid; pid_t pid;
process_dump(proc); process_dump(proc);
log_info("%s start", proc->name); log_info("%s start", proc->name);
if ((pid = fork()) < 0) { // fork error if ((pid = fork()) < 0) { // fork error
log_perror("%s fork error", proc->name); log_perror("%s fork error", proc->name);
server_exit(EXIT_FORK_ERROR); server_exit(EXIT_FORK_ERROR);
} else if (pid == 0) { // child process } else if (pid == 0) { // child process
// TODO: new process group
// if (proc->is_group) {
// setpgrp(); // new process group
// }
if (chdir(proc->cwd)) { // change working directory if (chdir(proc->cwd)) { // change working directory
log_perror("%s with invalid cwd `%s` -> ", proc->name, proc->cwd); log_perror("%s with invalid cwd `%s` -> ", proc->name, proc->cwd);
exit(2); exit(2);
} }
prctl(PR_SET_PDEATHSIG, SIGKILL); // child process die with father process prctl(PR_SET_PDEATHSIG, SIGKILL); // child process die with father process
if (execvpe(*(proc->cmd), proc->cmd, proc->env) < 0) { if (execvpe(*(proc->cmd), proc->cmd, proc->env) < 0) {
log_perror("%s exec error -> ", proc->name); log_perror("%s exec error -> ", proc->name);
exit(1); exit(1);
} }
} }
proc->pid = pid; proc->pid = pid;
log_info("%s running success -> PID = %d", proc->name, proc->pid); log_info("%s running success -> PID = %d", proc->name, proc->pid);
}
void process_add_arg(process *proc, const char *arg) { // add argument for process
string_list_append(&proc->cmd, arg);
}
void process_list_init() { // init process list
process_list = (process **)malloc(sizeof(process *));
*process_list = NULL;
}
void process_list_append(process *proc) { // add new process into process list
int proc_num = 0;
for (process **p = process_list; *p != NULL; ++p, ++proc_num); // get process number
process_list = (process **)realloc(process_list, sizeof(process *) * (proc_num + 2));
process_list[proc_num++] = proc; // add into process list
process_list[proc_num] = NULL;
log_debug("%s process added", proc->name);
}
void process_list_run() { // start process list
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
for (process **p = process_list; *p != NULL; ++p) {
process_exec(*p);
usleep(100 * 1000); // delay 100ms
}
log_info("Process start complete");
} }
void process_list_daemon() { void process_list_daemon() {
@ -128,3 +95,21 @@ void process_list_daemon() {
wait(&status); wait(&status);
} }
void server_exit(int exit_code) { // kill sub process and exit
log_info("Kill subprocess");
// TODO: kill subprocess and exit cleardns
exit(exit_code);
}
void get_exit_signal() { // get SIGINT or SIGTERM signal
log_info("Get exit signal");
server_exit(EXIT_NORMAL);
}
void get_sub_exit() { // catch child process exit
log_debug("Get SIGCHLD signal");
// TODO: check exit subprocess and restart it
}

Loading…
Cancel
Save