守护进程保活
# 04.守护进程保活
# 目录介绍
- 01.守护保活概念
- 1.1 需求背景介绍
- 02.M4守护保活思路
- 2.1 保活思路介绍
- 2.2 核心实现代码
- 03.守护保活设计
- 3.1 设计整体思路
- 3.2 实现方式思路
- 3.3 具体实践步骤
- 04.方案设计和选择
- 4.1 进程ID监控
- 4.2 心跳检测
- 4.3 双进程互守护
# 01.守护保活概念
# 1.1 需求背景介绍
守护进程监控主应用,当主应用崩溃或者不在时,守护进程将主应用打开。
# 02.M4守护保活思路
# 2.1 保活思路介绍
守护进程核心设计思路:
- M4设备没有去捕获应用是否发生崩溃,M4设备有个系统进程,该进程定期查询应用程序的进程是否存在,如果不存在就再次启动应用;
- M4再次按照步骤1去,查看系统中是否有应用程序的进程,如果还是没有,说明应用不能正常启动,提升用户重启设备。如果重启设备应用进程还是不能起来,会把备份在系统区的应用拷贝并启动;
- 该系统进程是个后台的守护进程;
- 目前M4设备依赖应用持续喂狗,来保证应用没有阻塞;
# 2.2 核心实现代码
------------------------main.cpp------------------------
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <gdbus.h>
#include <glib.h>
#include <getopt.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "minilogger/backtrace.h"
#include "minilogger/log.h"
#include "daemon_service.h"
#include "daemon_manager.h"
int main(int argc, char **argv) {
GMainLoop *main_loop;
__minilog_log_init(argv[0], NULL, false, true, "dae_server", "1.0");
daemon_services_start();
deamon_manager_init();
main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(main_loop);
g_main_loop_unref(main_loop);
return 0;
}
关键代码
------------------------daemon_service.c------------------------
// Copyright 2020 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "daemon_service.h"
#include "common.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <glib.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef PROJIECT_PALM_TXY
#define FACIAL_PROCESS_NUM 2
static const struct process palm_process_list[FACIAL_PROCESS_NUM] = {
{"weston","weston","--tty=2 --idle-time=0"},
{"sys_server","sys_server",""},
};
static struct process coustmer_app = {
"/oem/yt-palm/run.sh",
"yt-palm",
"",
};
#else
#define FACIAL_PROCESS_NUM 3
static const struct process palm_process_list[FACIAL_PROCESS_NUM] = {
{"weston","weston","--tty=2 --idle-time=0"},
{"sys_server","sys_server",""},
{"QLauncher","QLauncher", ""},
};
#endif
int watchdog_fb;
bool watchdog_feed = true;
int daemon_app = 1;
static int PROCESS_NUM = FACIAL_PROCESS_NUM;
static struct process *process_list = palm_process_list;
bool program_running(const struct process *service) {
assert(service);
assert(service->ps_name);
return program_running_by_psname(service->ps_name);
}
bool program_running_by_psname(const char* ps_name) {
FILE *fp;
char cmd[128]={0};
snprintf(cmd, sizeof(cmd), "/bin/pgrep -c %s",ps_name);
fp = popen(cmd, "r");
if (NULL == fp) {
LOG("%s %s false \n",__func__,ps_name);
return false;
}
char buf[2];
fgets(buf, sizeof(buf), fp);
pclose(fp);
int state = g_str_equal(buf, "0");
if(state==1){
LOG("%s %s %s \n",__func__,ps_name,buf);
return false;
}
return true;
}
void program_run(const struct process *service) {
char cmd[64] = {0};
assert(service);
assert(service->name && service->args);
snprintf(cmd, sizeof(cmd), "%s %s &", service->name, service->args);
LOG("program_run start: %s\n", cmd);
system(cmd);
if (g_str_equal(service->name, "weston"))
{
FILE *fp;
while(1){
char buf[19]={0};
FILE *fp;
fp = popen("ls /var/run/wayland-0", "r");
if (NULL != fp) {
fgets(buf, sizeof(buf), fp);
}
pclose(fp);
if(g_str_equal(buf, "/var/run/wayland-0")){
return;
}
LOG("wait weston start.... %s \n",buf);
usleep(100*1000);
}
}
}
void program_kill(const struct process *service) {
char cmd[64] = {0};
assert(service);
assert(service->ps_name);
snprintf(cmd, sizeof(cmd), "killall %s", service->ps_name);
system(cmd);
LOG("%s %s\n",__func__, cmd);
}
void program_force_kill(const struct process *service) {
char cmd[64] = {0};
assert(service);
assert(service->ps_name);
program_force_kill_by_psname(service->ps_name);
}
void program_force_kill_by_psname(const char* psname) {
char cmd[64] = {0};
snprintf(cmd, sizeof(cmd), "killall -9 %s", psname);
system(cmd);
LOG("%s %s\n",__func__, cmd);
}
#ifdef PROJIECT_PALM_TXY
static gboolean program_check_app(gpointer user_data) {
if(!program_running(&coustmer_app)){
LOG("%s restore app from system.\n",__func__);
daemon_app = 0;
char cmd[256] = {0};
snprintf(cmd, sizeof(cmd), "palm_fota system install %s &",coustmer_app.ps_name);
system(cmd);
}
return FALSE;
}
#endif
static gboolean program_check_and_run(gpointer user_data) {
for (int i = 0; i < PROCESS_NUM; i++) {
if (!program_running(&process_list[i])){
program_run(&process_list[i]);
}
}
#ifdef PROJIECT_PALM_TXY
if (!program_running(&coustmer_app)){
program_run(&coustmer_app);
}
g_timeout_add(3000, program_check_app, NULL);
#endif
return TRUE;
}
/*
static bool program_check(void) {
bool running = true;
for (int i = 0; i < PROCESS_NUM; i++) {
if (!program_running(&process_list[i])){
printf("%s %s is not running. \n",__FUNCTION__,process_list[i].name);
running = false;
}
}
return running;
}
*/
static gint timeout_tag;
static gboolean daemon_running(gpointer user_data) {
if(watchdog_feed){
write(watchdog_fb, "\0", 1);
}
system("/usr/share/rm_coredump.sh");
for (int i = 0; i < PROCESS_NUM; i++) {
if (!program_running(&process_list[i])){
LOG("%s %s is not running. restarting \n",__FUNCTION__,process_list[i].ps_name);
program_run(&process_list[i]);
}
}
#ifdef PROJIECT_PALM_TXY
if(daemon_app){
if (!program_running(&coustmer_app)){
LOG("%s app:%s is not running. restarting \n",__FUNCTION__,coustmer_app.ps_name);
program_run(&coustmer_app);
}
}
#endif
return TRUE;
}
int daemon_services_start() {
program_check_and_run(NULL);
timeout_tag =g_timeout_add(SERVICE_CHECK_PERIOD_MS, daemon_running, NULL);
watchdog_fb = open("/dev/watchdog", O_WRONLY);
return 0;
}
int daemon_services_stop(void) {
const int wait_time_ms = 100;
int wait_count = 10;
close(watchdog_fb);
if (timeout_tag) {
g_source_remove(timeout_tag);
timeout_tag = 0;
}
while (--wait_count > 0) {
/* killall process */
for (int i = 0; i < PROCESS_NUM; i++) {
if (program_running(&process_list[i]))
program_kill(&process_list[i]);
}
usleep(wait_time_ms * 1000);
/* check process */
bool succeed = true;
int i;
for (i = 0; i < PROCESS_NUM; i++)
if (program_running(&process_list[i]))
break;
if (i == PROCESS_NUM)
break;
}
/* if failed to killall process, force killall proces */
if (wait_count == 0) {
for (int i = 0; i < PROCESS_NUM; i++) {
if (program_running(&process_list[i]))
program_force_kill(&process_list[i]);
}
usleep(100 * 1000);
}
return 0;
}
------------------------daemon_service.h------------------------
// Copyright 2020 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef __DAEMON_SEVICE_H__
#define __DAEMON_SEVICE_H__
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
struct process {
char *name;
char *ps_name;
char *args;
};
#define SERVICE_CHECK_PERIOD_MS 10000
int daemon_services_start(void);
int daemon_services_stop(void);
bool program_running(const struct process *service);
bool program_running_by_psname(const char* ps_name);
void program_run(const struct process *service) ;
void program_kill(const struct process *service);
void program_force_kill(const struct process *service);
void program_force_kill_by_psname(const char* psname);
#ifdef __cplusplus
}
#endif
#endif
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# 03.守护保活设计
# 3.1 设计整体思路
- 守护进程和主应用是两个独立的进程。
- 守护进程负责监控主应用的状态。
- 如果主应用崩溃或退出,守护进程需要重新启动主应用。
# 3.2 实现方式思路
- 第一种,守护进程可以通过定期检查主进程是否存在(例如通过进程ID或进程名)来实现监控。
- 第二种,常见的方式是主应用在运行时定期向守护进程发送“心跳”信号,如果守护进程在指定时间内没有收到心跳,则认为主应用已经崩溃并重启它。
# 3.3 具体实践步骤
- 守护进程启动主应用,并记录主应用的进程ID。
- 守护进程定期检查该进程ID是否还存在,如果不存在则重新启动主应用。注意:主应用可能正常退出,这时可能不需要重启,所以需要根据退出码判断是否是正常退出。但通常守护进程用于保证服务持续运行,所以即使正常退出也可能需要重启。
- 或者,主应用启动后,定期向守护进程发送心跳(可以通过本地Socket、共享内存、信号等方式),守护进程监控心跳,超时则重启。
# 04.方案设计和选择
# 4.1 进程ID监控
设计思想:守护进程记录主应用的进程ID,定期检查该进程是否存在。
实现要点:
- 使用QProcess启动主应用并获取进程ID
- 定期使用系统命令(如ps)检查进程是否存在
- 发现进程不存在时重新启动应用
优点:实现简单,资源消耗低
缺点:无法检测应用无响应但进程仍在的情况
# 4.2 心跳检测
设计思想:主应用定期向守护进程发送心跳信号,守护进程超时未收到心跳则重启应用。
实现要点:
- 使用本地Socket(QLocalServer/QLocalSocket)进行进程间通信
- 主应用定期发送心跳信号
- 守护进程监控心跳超时情况
优点:能检测应用无响应情况
缺点:实现较复杂,需要应用配合发送心跳
# 4.3 双进程互守护
设计思想:主应用和守护进程相互监控,任一进程退出时另一方负责重启它。
实现要点:
- 两个进程都实现监控逻辑
- 相互检查对方进程状态
- 发现对方进程不存在时负责重启
优点:高可靠性,避免单点故障
缺点:实现复杂,资源消耗较高
上次更新: 2026/06/10, 11:13:41