修改配置文件格式

This commit is contained in:
aixiao 2022-01-13 19:18:43 +08:00
parent 90e7e58cb5
commit cc8bd2c972
5 changed files with 77 additions and 39 deletions

View File

@ -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"; // 日志文件
}

54
conf.c
View File

@ -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);

5
conf.h
View File

@ -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

View File

@ -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";
}

39
main.c
View File

@ -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;
}
@ -172,6 +146,11 @@ int main(int argc, char *argv[], char **env) {
}
read_conf_link(configfile);
/* Test Code
print_conf(conf_head);
free_conf(conf_head);
exit(0);
*/
loop(configfile, conf_head);
free_conf(conf_head);