daemon/main.c

160 lines
4.2 KiB
C
Raw Permalink 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;
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 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) {
2022-01-13 19:18:43 +08:00
if (get_process_pid(conf->PROCESS_name) <= 0) {
2021-12-11 13:20:06 +08:00
strcpy(log_content, times());
2022-01-13 19:18:43 +08:00
strcat(log_content, conf->PROCESS_name);
2021-12-11 13:20:06 +08:00
strcat(log_content, " Not running\n");
logs(log_content, configfile);
2022-01-13 19:18:43 +08:00
fp = _popen(conf->PROCESS_command, "r");
2021-12-11 13:20:06 +08:00
memset(buffer, 0, CACHE_SIZE);
while (fgets(buffer, sizeof(buffer), fp)) {
//printf("%s", buffer);
logs(buffer, configfile);
}
_pclose(fp);
} else {
strcpy(log_content, times());
2022-01-13 19:18:43 +08:00
strcat(log_content, conf->PROCESS_name);
2021-12-11 13:20:06 +08:00
strcat(log_content, " Running\n");
logs(log_content, configfile);
}
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");
}
2022-01-13 19:18:43 +08:00
read_conf_link(configfile);
/* Test Code
print_conf(conf_head);
free_conf(conf_head);
exit(0);
*/
2021-12-11 13:20:06 +08:00
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