我的守护进程说明
名称:daemon.c
作用:守护目标程序,当目标程序停止执行将其唤醒;
局限:需要系统支持 killall 和 pidof 命令,仅能守护二进制程序,无法守护脚本程序;
使用方法
编译方法:
1
<xxx->gcc daemon.c -o daemon
运行方法:
daemon 程序本身会转入后台且不会有任何输出;
/root/test_app para1 para2 &
是实际守护执行的命令;
1
./daemon /root/test_app para1 para2
源文件
守护进程会在被守护的程序的同一目录下生成日志文件;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*****************************************************************
* 守护进程(仅能守护二进制程序,不能守护脚本)
* 需要系统支持 killall 和 pidof 指令
* 编译方法:xxx-gcc daemon.c -o daemon
* 运行方法:/xxx/daemon /mnt/xxxxxxxxx para1 para2(自动转入后台)
*****************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdarg.h>
#include <libgen.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#define LOG_FILE "daemon_event.txt" //日志文件
#define LOG_NUM 30 //日志条数
#define LOG_LINE_SIZE 128 //日志每条最大字节
static char log_file_path[256] = {0,};
static char log_buf[LOG_NUM][LOG_LINE_SIZE] = {0,};
static char log_index = 0;
static void log2buf(void)
{
char buf[LOG_LINE_SIZE]; /*缓冲区*/
FILE *fp; /*文件指针*/
if(!(fp = fopen(log_file_path, "r"))) {
perror("log2buf fopen");
return;
}
log_index = 0;
while(fgets(buf, LOG_LINE_SIZE -1,fp) != NULL) {
if(log_index < LOG_NUM) {
memcpy(log_buf[log_index], buf, strlen(buf));
log_index++;
}
}
fclose(fp);
}
static void init_log(void)
{
int fd;
if(-1 == access(log_file_path, F_OK)){
fd = open(log_file_path, O_RDWR | O_CREAT, 0644);
log_index = 0;
}
else{
fd = open(log_file_path, O_RDWR, 0644);
}
close(fd);
log2buf();
}
static void write_log(const char *fmt, ...)
{
#if LOG_LINE_SIZE > 32
char i;
int fd;
time_t timer;
struct tm *p;
char info_buf[LOG_LINE_SIZE] = {0,};
va_list args;
char fmt_out[LOG_LINE_SIZE - 26] = {0,};
va_start(args, fmt);
vsnprintf(fmt_out, LOG_LINE_SIZE - 26 -1, fmt, args);
va_end(args);
time(&timer);
p = localtime(&timer);
snprintf(info_buf, LOG_LINE_SIZE -1, "[ %04d/%02d/%02d %02d:%02d:%02d ] %s\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, fmt_out);
if(-1 == access(log_file_path, F_OK)){
init_log();
}
fd = open(log_file_path, O_RDWR, 0644);
if (log_index++ < LOG_NUM) {
lseek(fd, 0, SEEK_END); //重新设置文件偏移量到末尾
memcpy(log_buf[log_index], info_buf, LOG_LINE_SIZE -1);
write(fd, info_buf, strlen(info_buf));
} else {
log_index = LOG_NUM;
ftruncate(fd, 0); //清空文件
lseek(fd, 0, SEEK_SET); //重新设置文件偏移量到开头
for (i = 0; i < LOG_NUM -1; i++){
memcpy(log_buf[i], log_buf[i + 1], LOG_LINE_SIZE -1);
write(fd, log_buf[i], strlen(log_buf[i]));
}
memcpy(log_buf[log_index - 1], info_buf, LOG_LINE_SIZE -1);
write(fd, info_buf, strlen(info_buf));
}
close(fd);
#endif
}
static int get_pidof(const char *pid_of_cmd){
FILE *fp;
char pid[16] = {0,};
int i;
fp = popen(pid_of_cmd, "r");
fgets(pid, 16, fp);
for (i = 0; i < strlen(pid); i++){
if(pid[i] == ' ')
pid[i] = '\0';
}
pclose(fp);
if (strlen(pid) == 0)
return 0;
else
return atoi(pid);
}
int main(int argc, char *argv[]){
char abspath[128] = {0,};
char abspath_dir[128] = {0,};
char *abspath_base = NULL;
char daemon_content[256] = {0,};
char killall_cmd[128] = "killall ";
char pidof_cmd[128] = "pidof ";
pid_t pid;
int i;
if(argc <= 1){
printf("Usage:\n %s <prog_full_path>\nEg:\n %s /mnt/app/xxxxxx\n", argv[0], argv[0]);
return 1;
}
/* 获取守护执行的程序的绝对地址 */
if (realpath(argv[1], abspath) != abspath){
perror("realpath");
return 1;
}
/* 判断守护执行的程序是否存在 */
if(-1 == access(abspath, F_OK)){
printf("%s: No such file or directory\n", abspath);
return 1;
}
/* 赋予守护执行的程序的执行权限 */
chmod(abspath, 0744);
/* 创建守护进程后台执行,进程目录变更为/,不打印任何输出 */
daemon(0, 0);
/* 得到写入日志完整路径 */
memcpy(abspath_dir, abspath, strlen(abspath));
dirname(abspath_dir); //此操作会裁剪输入的字符串
memcpy(log_file_path, abspath_dir, strlen(abspath_dir));
memcpy(log_file_path + strlen(log_file_path), "/" LOG_FILE, strlen(LOG_FILE) + 1);
/* 得到守护命令 */
for (i = 1; i < argc; i++){
if(i == 1)
memcpy(daemon_content + strlen(daemon_content), abspath, strlen(abspath));
else
memcpy(daemon_content + strlen(daemon_content), argv[i], strlen(argv[i]));
daemon_content[strlen(daemon_content)] = ' ';
}
daemon_content[strlen(daemon_content)] = '&';
/* 得到获取进程、杀死程序命令 */
abspath_base = basename(abspath);
memcpy(killall_cmd + strlen(killall_cmd), abspath_base, strlen(abspath_base));
memcpy(killall_cmd + strlen(killall_cmd), " 2>/dev/null", 12);
memcpy(pidof_cmd + strlen(pidof_cmd), abspath_base, strlen(abspath_base));
//初始化日志
init_log();
write_log("Daemon content: \"%s\"", daemon_content);
//重启程序
system(killall_cmd);
system(daemon_content);
while(1){
sleep(3);
if(0 == get_pidof(pidof_cmd)){
write_log("Wake-up program: %s", argv[1]);
system(killall_cmd);
system(daemon_content);
}
}
return 0;
}