daemon/main.c

181 lines
4.7 KiB
C
Raw Normal View History

2020-09-11 16:40:25 +08:00
#include "main.h"
2021-12-11 13:20:06 +08:00
#include "conf.h"
2020-09-11 16:40:25 +08:00
#include "libconf.h"
2021-12-11 13:20:06 +08:00
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) {
2020-09-11 16:40:25 +08:00
char bufer[PATH_SIZE];
char comm[PATH_SIZE];
char proc_comm_name[PATH_SIZE];
int num[PATH_SIZE] = { 0 };
int n = 0;
FILE *fp;
DIR *dir;
struct dirent *ptr;
dir = opendir("/proc");
while ((ptr = readdir(dir)) != NULL) {
if (ptr->d_type == 4 && strcasecmp(ptr->d_name, ".") && strcasecmp(ptr->d_name, "..")) {
bzero(bufer, 0);
sprintf(comm, "/proc/%s/comm", ptr->d_name);
if (access(comm, F_OK) == 0) {
fp = fopen(comm, "r");
if (fgets(bufer, PATH_SIZE - 1, fp) == NULL) {
fclose(fp);
continue;
}
sscanf(bufer, "%s", proc_comm_name);
if (!strcmp(proces_name, proc_comm_name)) {
num[n] = atoi(ptr->d_name);
n += 1;
}
fclose(fp);
}
}
}
n -= 1; // 去除最后一个搜索时的本身进程
closedir(dir);
if (num[0] > 0)
return num[0];
else
return 0;
}
2021-12-11 13:20:06 +08:00
char *times() {
2020-09-11 16:40:25 +08:00
time_t t;
2020-09-11 18:14:13 +08:00
struct tm *timeinfo;
2021-12-11 13:20:06 +08:00
2020-09-11 16:40:25 +08:00
time(&t);
timeinfo = localtime(&t);
2020-09-11 18:14:13 +08:00
return asctime(timeinfo);
2020-09-11 16:40:25 +08:00
}
2021-12-11 13:20:06 +08:00
int logs(char *str, char *configfile) {
2020-09-11 16:40:25 +08:00
FILE *fp = NULL;
2021-12-11 13:20:06 +08:00
2020-10-22 14:20:05 +08:00
fp = fopen(read_conf(configfile, "global", "LOGFILE"), "a+");
2020-09-11 16:40:25 +08:00
fprintf(fp, str);
return fclose(fp);
}
2021-12-11 13:20:06 +08:00
int8_t copy_new_mem(char *src, int src_len, char **dest) {
*dest = (char *)malloc(src_len + 1);
if (*dest == NULL)
return 1;
memcpy(*dest, src, src_len);
*((*dest)+src_len) = '\0';
return 0;
}
int loop(char *configfile, conf *p)
2020-09-11 16:40:25 +08:00
{
2021-12-11 13:20:06 +08:00
conf *conf = p;
2020-09-11 16:40:25 +08:00
FILE *fp;
2021-12-11 13:20:06 +08:00
char buffer[CACHE_SIZE];
char *key; //键
char *val; //值
char log_content[CACHE_SIZE];
memset(log_content, 0, CACHE_SIZE);
2020-09-11 16:40:25 +08:00
while (1) {
2021-12-11 13:20:06 +08:00
while (conf) {
if (conf->PROCESS) {
val = strstr(conf->PROCESS, "->");
copy_new_mem(conf->PROCESS, val - conf->PROCESS, &key);
if (get_process_pid(key) <= 0) {
strcpy(log_content, times());
strcat(log_content, key);
strcat(log_content, " Not running\n");
logs(log_content, configfile);
fp = _popen(val+2, "r");
val = NULL;
memset(buffer, 0, CACHE_SIZE);
while (fgets(buffer, sizeof(buffer), fp)) {
//printf("%s", buffer);
logs(buffer, configfile);
}
_pclose(fp);
} else {
strcpy(log_content, times());
strcat(log_content, key);
strcat(log_content, " Running\n");
logs(log_content, configfile);
}
free(key);
2020-09-11 16:40:25 +08:00
}
2021-12-11 13:20:06 +08:00
conf = conf->next;
2020-09-11 16:40:25 +08:00
}
2021-12-11 13:20:06 +08:00
conf = p;
2020-09-11 18:14:13 +08:00
sleep(atoi(read_conf(configfile, "global", "TIME")));
2020-09-11 16:40:25 +08:00
}
2021-12-11 13:20:06 +08:00
return 0;
2020-09-11 16:40:25 +08:00
}
2021-12-11 13:20:06 +08:00
int main(int argc, char *argv[], char **env) {
2020-09-11 18:14:13 +08:00
char configfile[PATH_SIZE];
int opt;
memset(configfile, 0, PATH_SIZE);
2021-12-11 13:20:06 +08:00
while ((opt = getopt(argc, argv, "c:h?")) != -1) {
2020-09-11 18:14:13 +08:00
switch (opt) {
case 'c':
strcpy(configfile, optarg);
break;
2021-12-11 13:20:06 +08:00
case 'h':
case '?':
printf("%s\n",
"Process daemon\n"\
"Author: AIXIAO@AIXIAO.ME\n"\
"Usage: [-?h] [-c filename]\n"\
"\n"\
"Options:\n"\
"-c : set configuration file, (default: daemon.conf)\n"\
"-? -h : help information\n");
exit(1);
break;
2020-09-11 18:14:13 +08:00
default:
break;
}
}
if (strlen(configfile) == 0) {
strcpy(configfile, "daemon.conf");
}
2021-12-11 13:20:06 +08:00
if (daemon(1, 1)) { // 守护, 脱离终端
2020-09-11 16:40:25 +08:00
perror("daemon");
}
2021-12-11 13:20:06 +08:00
read_conf_link(configfile);
loop(configfile, conf_head);
free_conf(conf_head);
2020-09-11 16:40:25 +08:00
return 0;
}
2021-12-11 13:20:06 +08:00