From cc8bd2c97271f922a4a54b98202b7c6fd58ec4e0 Mon Sep 17 00:00:00 2001 From: aixiao Date: Thu, 13 Jan 2022 19:18:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++---- conf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++- conf.h | 5 +++++ daemon.conf | 5 +++-- main.c | 43 +++++++++++------------------------------- 5 files changed, 77 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 1481448..6e8e0b8 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,11 @@ 配置文件示列: global { - PROCESS="ls->ls -al"; // 要执行的命令和进程名, 依'->'为分界, '->'之前的字符为进程名字, '->'之后的字符串为要执行的命令.(PROCESS配置理论可以有无数个, 通过链表存储) - PROCESS="id->id"; // 同上 - TIME = "10"; // 循环时间, 单位秒. 大于等于1 - LOGFILE = "log_daemon.txt"; // 日志文件 + PROCESS="ls" -> "ls -al"; // 要执行的命令和进程名, 依'" -> "'为分界, '" -> "'之前的字符为进程名字, '" -> "'之后的字符串为要执行的命令.(PROCESS配置理论可以有无数个, 通过链表存储) + PROCESS="ls1" -> "ls -al"; // 同上 + PROCESS="id" -> "id"; // 同上 + TIME = "10"; // 循环时间, 单位秒. 大于等于1 + LOGFILE = "log_daemon.txt"; // 日志文件 } diff --git a/conf.c b/conf.c index 9fac36f..c74cf25 100644 --- a/conf.c +++ b/conf.c @@ -115,12 +115,34 @@ static void parse_global_module(char *content) { char *var, *val_begin, *val_end, *lineEnd; conf *conf_node = NULL; + char *p1 = NULL, *s = NULL, *t = NULL; + char *p2 = NULL; while ((lineEnd = set_var_val_lineEnd(content, &var, &val_begin, &val_end)) != NULL) { if (strcasecmp(var, "PROCESS") == 0) { conf_node = (conf *)malloc(sizeof(*conf_node)); conf_node->PROCESS = strdup(val_begin); + conf_node->PROCESS_len = val_end - val_begin; + + p1 = strstr(val_begin, "->"); + for (t = p1; *t != '"'; ++t) ; + conf_node->PROCESS_command = strdup(t + 1); + p2 = strchr(t+1, '\0'); + conf_node->PROCESS_command_len = p2 - (t + 1); + + for (s = p1 - 1; *s == ' '; s--) { + if (s == val_begin) + return; + } + if (*s == '"') + s--; + + conf_node->PROCESS_name = strndup(val_begin, s - val_begin + 1); + conf_node->PROCESS_name_len = s - val_begin + 1; + + + conf_node->next = NULL; if (conf_head == NULL) { conf_head = conf_node; @@ -135,6 +157,34 @@ static void parse_global_module(char *content) } } +void print_conf(conf * p) { + conf *temp = p; //将temp指针重新指向头结点 + while (temp) { //只要temp指针指向的结点的next不是Null就执行输出 + if (temp->PROCESS) + printf("%s %d\n", temp->PROCESS, temp->PROCESS_len); + if (temp->PROCESS_name) + printf("%s %d\n", temp->PROCESS_name, temp->PROCESS_name_len); + if (temp->PROCESS_command) + printf("%s %d\n", temp->PROCESS_command, temp->PROCESS_command_len); + + temp = temp->next; + } +} + +void free_conf(conf *p) { + conf *temp = p; + while (temp) { + if (temp->PROCESS) + free(temp->PROCESS); + if (temp->PROCESS_name) + free(temp->PROCESS_name); + if (temp->PROCESS_command) + free(temp->PROCESS_command); + + temp = temp->next; + } +} + void read_conf_link(char *path) { char *buff, *global_content; @@ -143,8 +193,10 @@ void read_conf_link(char *path) /* 读取配置文件到缓冲区 */ file = fopen(path, "r"); - if (file == NULL) + if (file == NULL) { printf("cannot open config file.\n"); + exit(1); + } fseek(file, 0, SEEK_END); file_size = ftell(file); buff = (char *)alloca(file_size + 1); diff --git a/conf.h b/conf.h index e8826ea..f502f22 100644 --- a/conf.h +++ b/conf.h @@ -7,11 +7,16 @@ typedef struct conf { char *PROCESS; + char *PROCESS_name, *PROCESS_command; + + int PROCESS_len, PROCESS_name_len, PROCESS_command_len; struct conf *next; } conf; extern conf *conf_head; extern void read_conf_link(char *path); +extern void free_conf(conf *p); +extern void print_conf(conf *p); #endif diff --git a/daemon.conf b/daemon.conf index bad8dc9..36489a9 100644 --- a/daemon.conf +++ b/daemon.conf @@ -1,6 +1,7 @@ global { - PROCESS="ls->ls -al"; - PROCESS="init->id"; + PROCESS="ls" -> "ls -al"; + PROCESS="ls1" -> "ls -al"; + PROCESS="init" -> "id"; TIME = "10"; LOGFILE = "log_daemon.txt"; } \ No newline at end of file diff --git a/main.c b/main.c index bb6e5be..665d45d 100644 --- a/main.c +++ b/main.c @@ -4,24 +4,6 @@ conf *conf_head = NULL; -void print_conf(conf * p) { - conf *temp = p; //将temp指针重新指向头结点 - while (temp) { //只要temp指针指向的结点的next不是Null就执行输出 - if (temp->PROCESS) - printf("%s\n", temp->PROCESS); - temp = temp->next; - } -} - -void free_conf(conf *p) { - conf *temp = p; - while (temp) { - if (temp->PROCESS) - free(temp->PROCESS); - temp = temp->next; - } -} - int get_process_pid(char *proces_name) { char bufer[PATH_SIZE]; char comm[PATH_SIZE]; @@ -94,24 +76,18 @@ int loop(char *configfile, conf *p) FILE *fp; char buffer[CACHE_SIZE]; - char *key; //键 - char *val; //值 char log_content[CACHE_SIZE]; memset(log_content, 0, CACHE_SIZE); while (1) { while (conf) { if (conf->PROCESS) { - val = strstr(conf->PROCESS, "->"); - copy_new_mem(conf->PROCESS, val - conf->PROCESS, &key); - - if (get_process_pid(key) <= 0) { + if (get_process_pid(conf->PROCESS_name) <= 0) { strcpy(log_content, times()); - strcat(log_content, key); + strcat(log_content, conf->PROCESS_name); strcat(log_content, " Not running\n"); logs(log_content, configfile); - fp = _popen(val+2, "r"); - val = NULL; + fp = _popen(conf->PROCESS_command, "r"); memset(buffer, 0, CACHE_SIZE); while (fgets(buffer, sizeof(buffer), fp)) { //printf("%s", buffer); @@ -120,12 +96,10 @@ int loop(char *configfile, conf *p) _pclose(fp); } else { strcpy(log_content, times()); - strcat(log_content, key); + strcat(log_content, conf->PROCESS_name); strcat(log_content, " Running\n"); logs(log_content, configfile); } - - free(key); } conf = conf->next; } @@ -170,8 +144,13 @@ int main(int argc, char *argv[], char **env) { if (daemon(1, 1)) { // 守护, 脱离终端 perror("daemon"); } - - read_conf_link(configfile); + + read_conf_link(configfile); + /* Test Code + print_conf(conf_head); + free_conf(conf_head); + exit(0); + */ loop(configfile, conf_head); free_conf(conf_head);