Browse Source

update: cleardns main process

dev
Dnomd343 2 years ago
parent
commit
b07bd59606
  1. 1
      include/applet/crontab.h
  2. 1
      include/utils/logger.h
  3. 3
      include/utils/process.h
  4. 37
      src/applet/crontab.c
  5. 49
      src/cleardns.c
  6. 2
      src/utils/assets.c
  7. 9
      src/utils/logger.c
  8. 16
      src/utils/process.c

1
include/applet/crontab.h

@ -4,6 +4,7 @@
#include "config.h"
#include "process.h"
void assets_free(assets_config *info);
process* assets_load(assets_config *info);
#endif

1
include/utils/logger.h

@ -18,7 +18,6 @@ enum {
#define log_fatal(...) log_printf(LOG_FATAL, __VA_ARGS__)
extern int LOG_LEVEL;
//void log_perror(char *prefix);
void log_perror(const char *fmt, ...);
void log_printf(int level, const char *fmt, ...);

3
include/utils/process.h

@ -9,8 +9,6 @@ typedef struct {
char **cmd;
char **env;
char *cwd;
// TODO: add is_daemon option
int8_t is_group; // bool value
} process;
void process_list_run();
@ -21,5 +19,6 @@ process* process_init(const char *caption, const char *bin);
#include <unistd.h>
void process_exec(process *proc);
void process_list_daemon();
#endif

37
src/applet/crontab.c

@ -1,23 +1,42 @@
#include <stdlib.h>
#include "process.h"
#include <signal.h>
#include "config.h"
#include "logger.h"
#include "sundry.h"
#include "system.h"
#include "process.h"
#include "structure.h"
process* assets_load(assets_config *info) { // load assets update options
log_info("assets test ok");
char **update_file;
char **update_url;
// TODO: update assets process
// TODO: save cron exp in `/var/spool/cron/crontabs/root`
void assets_update() { // update all assets
log_info("Start assets update");
for (char **file = update_file; *file != NULL; ++file) {
char **url = file - update_file + update_url;
log_info("Update asset `%s` -> %s", *file, *url);
}
}
char *cron = string_join(info->cron, " kill -14 1");
void assets_free(assets_config *info) {
string_list_free(info->update_file);
string_list_free(info->update_url);
free(info->cron);
free(info);
}
log_info("Crontab exp -> `%s`", cron);
process* assets_load(assets_config *info) { // load assets update options
update_url = string_list_init();
update_file = string_list_init();
string_list_update(&update_url, info->update_url);
string_list_update(&update_file, info->update_file);
signal(SIGALRM, assets_update); // receive SIGALRM signal
char *cron = string_join(info->cron, " kill -14 1");
save_file("/var/spool/cron/crontabs/root", cron);
free(cron);
return process_init("Crontab", "crond");
process *proc = process_init("Crontab", "crond");
process_add_arg(proc, "-f"); // foreground
return proc;
}

49
src/cleardns.c

@ -41,48 +41,17 @@ char* init(int argc, char *argv[]) { // return config file
return config;
}
void signal_test() {
log_info("Get alarm signal");
}
int main(int argc, char *argv[]) { // ClearDNS service
char *config_file = init(argc, argv);
LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION);
signal(SIGALRM, signal_test);
assets_config demo;
demo.cron = "* * * * *";
process *crontab = assets_load(&demo);
process_exec(crontab);
for (;;) {
pause();
}
return 0;
// process *test = process_init("TEST", "lls");
// process *test = process_init("TEST", "ls");
// process_add_arg(test, "-al");
// test->cwd = "/etc/cleardns/fuck";
// int pid = process_exec(test);
// log_info("PID = %d", pid);
// int status;
// wait(&status);
// return 0;
log_info("ClearDNS server start (%s)", VERSION);
create_folder(WORK_DIR);
extract_assets();
load_config(config_file);
free(config_file);
assets_load(loader.assets);
if (LOG_LEVEL == LOG_DEBUG) { // debug mode enabled
loader.filter->debug = TRUE;
loader.diverter->debug = TRUE;
@ -91,31 +60,27 @@ int main(int argc, char *argv[]) { // ClearDNS service
}
process_list_init();
// TODO: crontab of assets
// process_list_append(assets_load(loader.assets));
// TODO: free assets_config
process_list_append(assets_load(loader.assets));
process_list_append(dnsproxy_load("Domestic", loader.domestic, "domestic.json"));
process_list_append(dnsproxy_load("Foreign", loader.foreign, "foreign.json"));
process_list_append(overture_load(loader.diverter, "overture.json"));
overture_free(loader.diverter);
dnsproxy_free(loader.domestic);
dnsproxy_free(loader.foreign);
assets_free(loader.assets);
if (loader.filter != NULL) {
process_list_append(adguard_load(loader.filter, ADGUARD_DIR));
adguard_free(loader.filter);
}
for (char **script = loader.script; *script != NULL; ++script) {
for (char **script = loader.script; *script != NULL; ++script) { // run custom script
log_info("Run custom script -> `%s`", *script);
run_command(*script);
}
string_list_free(loader.script);
process_list_run();
int status;
wait(&status);
kill(1, SIGALRM); // send alarm signal to itself
process_list_daemon();
return 0;
}

2
src/utils/assets.c

@ -8,8 +8,6 @@
void extract_asset(const char *file);
// TODO: assets update -> crontab task
void extract_assets() {
log_info("Start loading assets");
create_folder(ASSETS_DIR);

9
src/utils/logger.c

@ -51,15 +51,6 @@ void log_printf(int level, const char *fmt, ...) {
}
}
//void log_perror(char *prefix) {
// fprint_prefix();
// fprintf(stderr, " %s%s\x1b[0m ", log_color[LOG_ERROR], log_string[LOG_ERROR]);
// fflush(stderr);
// perror(prefix);
//}
// TODO: update perror logger
void log_perror(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);

16
src/utils/process.c

@ -3,7 +3,7 @@
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <stdio.h>
//#include <stdio.h>
#include "sundry.h"
#include "process.h"
#include "constant.h"
@ -20,7 +20,6 @@ process* process_init(const char *caption, const char *bin) { // init process st
string_list_append(&proc->cmd, bin); // argv[0] normally be process file name
proc->env = string_list_init(); // empty environment variable
proc->cwd = WORK_DIR; // current working directory
proc->is_group = FALSE; // create new process group
return proc;
}
@ -85,7 +84,7 @@ void process_dump(process *proc) {
log_debug("%s cwd -> %s", proc->name, proc->cwd);
log_debug("%s command -> %s", proc->name, process_cmd);
log_debug("%s env variable -> %s", proc->name, process_env);
log_debug("%s new group -> %s", proc->name, show_bool(proc->is_group));
// log_debug("%s new group -> %s", proc->name, show_bool(proc->is_group));
free(process_env);
free(process_cmd);
}
@ -103,9 +102,9 @@ void process_exec(process *proc) {
} else if (pid == 0) { // child process
// TODO: new process group
if (proc->is_group) {
setpgrp(); // new process group
}
// if (proc->is_group) {
// setpgrp(); // new process group
// }
if (chdir(proc->cwd)) { // change working directory
log_perror("%s with invalid cwd `%s` -> ", proc->name, proc->cwd);
@ -124,3 +123,8 @@ void process_exec(process *proc) {
}
void process_list_daemon() {
int status;
wait(&status);
}

Loading…
Cancel
Save