Skip to content

Commit 9c63172

Browse files
committed
manager: support custom kill command.
1 parent 7c3853a commit 9c63172

File tree

11 files changed

+479
-113
lines changed

11 files changed

+479
-113
lines changed

src/modelbox/manager/src/conf.c

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
#include "conf.h"
1918

2019
#include <errno.h>
2120
#include <stdio.h>
2221
#include <stdlib.h>
2322
#include <string.h>
2423

25-
#define CONF_LINE_MAX 512
24+
#define CONF_LINE_MAX 8192
2625
#define DEFAULT_LOG_NUM 48
2726
#define DEFAULT_LOG_SIZE (1024 * 1024 * 64)
2827
#define BUF_LEN_64 64
@@ -176,6 +175,10 @@ static int _parse_args(char *key, char *value, int max_argv, int *argc,
176175
}
177176
}
178177

178+
if (is_in_args == 0 && *value == '\t') {
179+
*value = ' ';
180+
}
181+
179182
/* 如果遇到结束字符 */
180183
if (*value == end_char) {
181184
/* 如果未处理任何参数,则跳过 */
@@ -289,11 +292,15 @@ static int parse_conf(struct config_map config_map[], char *key, char *value) {
289292

290293
static int load_conf_from_file(struct config_map config_map[],
291294
const char *conf_file) {
292-
char file_line[CONF_LINE_MAX];
295+
char read_line[CONF_LINE_MAX];
296+
char conf_line[CONF_LINE_MAX];
297+
char *line = NULL;
293298
char conf_key[BUF_LEN_64];
294299
char conf_value[CONF_LINE_MAX];
295300
int filed_num = 0;
296301
int line_no = 0;
302+
int line_len = 0;
303+
int read_len = 0;
297304
FILE *fp;
298305

299306
if (conf_file == NULL) {
@@ -304,13 +311,55 @@ static int load_conf_from_file(struct config_map config_map[],
304311
fp = fopen(conf_file, "r");
305312
if (fp == NULL) {
306313
manager_log(MANAGER_LOG_ERR, "open %s failed, %s", conf_file,
307-
strerror(errno));
314+
strerror(errno));
308315
return -1;
309316
}
310317

311-
while (fgets(file_line, sizeof(file_line), fp) != NULL) {
318+
while (fgets(read_line, sizeof(read_line), fp) != NULL) {
312319
line_no++;
313-
filed_num = sscanf(file_line, "%63s %1023[^\n]s", conf_key, conf_value);
320+
read_len = strnlen(read_line, sizeof(read_line));
321+
if (read_len == 0) {
322+
continue;
323+
}
324+
325+
if (read_line[0] == '#') {
326+
continue;
327+
}
328+
329+
if (read_line[read_len - 1] == '\\' ||
330+
(read_len >= 2 && read_line[read_len - 2] == '\\')) {
331+
if (line == NULL) {
332+
line = conf_line;
333+
line[0] = '\0';
334+
}
335+
336+
if (line_len + read_len - 1 >= CONF_LINE_MAX) {
337+
goto errout;
338+
}
339+
340+
if (read_len >= 2 && read_line[read_len - 2] == '\\') {
341+
read_len -= 1;
342+
}
343+
344+
strncpy(line + line_len, read_line, read_len - 1);
345+
line_len += read_len - 1;
346+
line[line_len] = '\0';
347+
continue;
348+
} else if (line != NULL) {
349+
if (line_len + read_len >= CONF_LINE_MAX) {
350+
goto errout;
351+
}
352+
353+
strncpy(line + line_len, read_line, sizeof(conf_line) - line_len);
354+
line_len += read_len;
355+
line[line_len] = '\0';
356+
} else {
357+
line = read_line;
358+
}
359+
360+
filed_num = sscanf(line, "%63s %1023[^\n]s", conf_key, conf_value);
361+
line = NULL;
362+
line_len = 0;
314363
if (filed_num <= 0) {
315364
continue;
316365
}
@@ -336,7 +385,7 @@ static int load_conf_from_file(struct config_map config_map[],
336385
if (fp) {
337386
if (line_no > 0) {
338387
manager_log(MANAGER_LOG_ERR, "invalid config at line %s:%d %s", conf_file,
339-
line_no, file_line);
388+
line_no, line);
340389
}
341390
fclose(fp);
342391
}

src/modelbox/manager/src/log.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ int manager_log_ext(MANAGER_LOG_LEVEL level, const char *file, int line,
3333
va_list ap;
3434

3535
if (log_func == NULL) {
36+
va_start(ap, format);
37+
vprintf(format, ap);
38+
va_end(ap);
39+
printf("\n");
3640
return 0;
3741
}
3842

src/modelbox/manager/src/manager.c

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,21 @@ static int manager_sig_register(void) {
104104
int manager_add_apps(void) {
105105
int i = 0;
106106
for (i = 0; i < conf_apps_num; i++) {
107-
manager_log(MANAGER_LOG_INFO, "add %s, %s, %s, check alive %d\n", conf_apps[i].name,
108-
conf_apps[i].cmd, conf_apps[i].pidfile, conf_apps[i].check_alive);
109-
if (app_start(conf_apps[i].name, conf_apps[i].cmd, PATH_MAX, conf_apps[i].pidfile,
110-
conf_apps[i].check_alive, conf_apps[i].check_alive_time,
111-
conf_apps[i].heartbeat_interval) != 0) {
107+
struct app_start_info info;
108+
memset_s(&info, sizeof(info), 0, sizeof(info));
109+
info.name = conf_apps[i].name;
110+
info.cmdline = conf_apps[i].cmd;
111+
info.cmd_max_len = sizeof(conf_apps[i].cmd);
112+
info.killcmd = conf_apps[i].killcmd;
113+
info.killcmd_max_len = sizeof(conf_apps[i].killcmd);
114+
info.pidfile = conf_apps[i].pidfile;
115+
info.check_alive = conf_apps[i].check_alive;
116+
info.keepalive_time = conf_apps[i].check_alive_time;
117+
info.heartbeat_interval = conf_apps[i].heartbeat_interval;
118+
119+
manager_log(MANAGER_LOG_INFO, "add %s, cmd: '%s', check alive %d\n",
120+
conf_apps[i].name, strcmds(conf_apps[i].cmd, PATH_MAX), conf_apps[i].check_alive);
121+
if (app_start(&info) != 0) {
112122
manager_log(MANAGER_LOG_ERR, "add app %s failed.", conf_apps[i].name);
113123
return -1;
114124
}
@@ -151,9 +161,19 @@ void manager_reload_apps(struct conf_app oldapps[CONF_MAX_APPS]) {
151161
}
152162

153163
if (j == CONF_MAX_APPS) {
154-
if (app_start(conf_apps[i].name, conf_apps[i].cmd, PATH_MAX, conf_apps[i].pidfile,
155-
conf_apps[i].check_alive, conf_apps[i].check_alive_time,
156-
conf_apps[i].heartbeat_interval) == 0) {
164+
struct app_start_info info;
165+
memset_s(&info, sizeof(info), 0, sizeof(info));
166+
info.name = conf_apps[i].name;
167+
info.cmdline = conf_apps[i].cmd;
168+
info.cmd_max_len = sizeof(conf_apps[i].cmd);
169+
info.killcmd = conf_apps[i].killcmd;
170+
info.killcmd_max_len = sizeof(conf_apps[i].killcmd);
171+
info.pidfile = conf_apps[i].pidfile;
172+
info.check_alive = conf_apps[i].check_alive;
173+
info.keepalive_time = conf_apps[i].check_alive_time;
174+
info.heartbeat_interval = conf_apps[i].heartbeat_interval;
175+
176+
if (app_start(&info) == 0) {
157177
manager_log(MANAGER_LOG_INFO, "start app %s success.",
158178
conf_apps[i].name);
159179
} else {
@@ -180,7 +200,7 @@ int manager_reload(void) {
180200
return 0;
181201
}
182202

183-
int manager_init_server(void) {
203+
int manager_init_server(int lockpage) {
184204
/* init monitor*/
185205
if (manager_monitor_init() != 0) {
186206
manager_log(MANAGER_LOG_ERR, "init monitor failed.\n");
@@ -193,7 +213,7 @@ int manager_init_server(void) {
193213
return -1;
194214
}
195215

196-
if (mlockall(MCL_FUTURE) != 0) {
216+
if (lockpage != 0 && mlockall(MCL_FUTURE) != 0) {
197217
manager_log(MANAGER_LOG_WARN, "lock memory failed.");
198218
}
199219
return 0;
@@ -287,9 +307,8 @@ static void _manager_default_conf_file(char *path, int max_len) {
287307
return;
288308
}
289309

290-
int manager_init(const char *conf_file, char *name) {
310+
int manager_init(char *name) {
291311
char log_file[PATH_MAX];
292-
char default_conf_file[PATH_MAX] = {0};
293312
char piddir[PATH_MAX];
294313

295314
if (manager_sig_register()) {
@@ -341,18 +360,6 @@ int manager_init(const char *conf_file, char *name) {
341360
snprintf(conf_log_file, sizeof(log_file), "%s/%s.log", MANAGER_LOG_PATH,
342361
MANAGER_NAME);
343362

344-
if (conf_file == NULL) {
345-
_manager_default_conf_file(default_conf_file, PATH_MAX);
346-
if (default_conf_file[0]) {
347-
conf_file = default_conf_file;
348-
}
349-
}
350-
351-
if (manager_load_conf(conf_file) != 0) {
352-
fprintf(stderr, "load master config file %s failed.\n", conf_file);
353-
return -1;
354-
}
355-
356363
if (tlog_init(get_modelbox_full_path(conf_log_file), conf_log_size,
357364
conf_log_num, 0, 0) != 0) {
358365
fprintf(stderr, "init master log failed.\n");
@@ -365,7 +372,7 @@ int manager_init(const char *conf_file, char *name) {
365372
manager_log(MANAGER_LOG_INFO, "%s starting... (Build : %s %s)",
366373
program_invocation_short_name, __DATE__, __TIME__);
367374

368-
if (manager_init_server() != 0) {
375+
if (manager_init_server(conf_lockpage) != 0) {
369376
manager_log(MANAGER_LOG_ERR, "init master server failed.");
370377
return -1;
371378
}
@@ -413,6 +420,21 @@ int main(int argc, char *argv[])
413420
}
414421
}
415422

423+
char default_conf_file[PATH_MAX] = {0};
424+
const char *load_conf = conf_file;
425+
if (conf_file == NULL) {
426+
_manager_default_conf_file(default_conf_file, PATH_MAX);
427+
if (default_conf_file[0]) {
428+
load_conf = default_conf_file;
429+
}
430+
}
431+
432+
if (manager_load_conf(load_conf) != 0) {
433+
fprintf(stderr, "load master config file %s failed.\n", load_conf);
434+
return -1;
435+
}
436+
437+
416438
if (is_forground == 0) {
417439
if (daemon(0, 0) < 0) {
418440
fprintf(stderr, "run daemon process failed, %s\n", strerror(errno));
@@ -425,7 +447,7 @@ int main(int argc, char *argv[])
425447
signal(SIGHUP, manager_sighup);
426448
g_reload_config = 0;
427449

428-
if (manager_init(conf_file, name) != 0) {
450+
if (manager_init(name) != 0) {
429451
fprintf(stderr, "master init failed.\n");
430452
return 1;
431453
}

src/modelbox/manager/src/manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extern "C" {
5151
#define MANAGER_LOG_SIZE (1024 * 1024 * 64)
5252
#define MANAGER_LOG_NUM (48)
5353

54-
extern int manager_init_server(void);
54+
extern int manager_init_server(int lockpage);
5555

5656
extern int manager_run(void);
5757

src/modelbox/manager/src/manager_conf.c

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ char pid_file_path[PATH_MAX];
2828
char key_file_path[PATH_MAX];
2929
int conf_watchdog_timeout = DEFAULT_WATCHDOG_TIMEOUT;
3030
int conf_force_kill_time = DEFAULT_FORCE_KILL_TIMEOUT;
31+
int conf_lockpage = 1;
3132

3233
struct conf_app conf_apps[CONF_MAX_APPS];
3334
int conf_apps_num;
@@ -49,12 +50,60 @@ int _manager_is_app_exists(char *name) {
4950
return -1;
5051
}
5152

53+
int _manager_conf_process_cmds(char *cmd, int max_cmdlen, const char *argline) {
54+
const char *ptr = argline;
55+
const char *arg_begin_ptr = argline;
56+
int cmd_len = 0;
57+
char arg[PATH_MAX];
58+
59+
while (1) {
60+
if (ptr == arg_begin_ptr) {
61+
ptr++;
62+
continue;
63+
}
64+
65+
if (*ptr != ' ' && *ptr != '\0') {
66+
ptr++;
67+
continue;
68+
}
69+
70+
if (ptr - 1 == arg_begin_ptr && *arg_begin_ptr == ' ') {
71+
arg_begin_ptr = ptr + 1;
72+
continue;
73+
}
74+
75+
strncpy(arg, arg_begin_ptr, ptr - arg_begin_ptr);
76+
arg[ptr - arg_begin_ptr] = '\0';
77+
78+
if (cmd_len + ptr - arg_begin_ptr + 1 > max_cmdlen - 2) {
79+
manager_log(MANAGER_LOG_ERR, "cmd is too long");
80+
return -1;
81+
}
82+
83+
strncpy(cmd + cmd_len, get_modelbox_full_path(arg), ptr - arg_begin_ptr);
84+
cmd_len += ptr - arg_begin_ptr;
85+
arg_begin_ptr = ptr;
86+
cmd[cmd_len] = '\0';
87+
if (*ptr == '\0') {
88+
cmd[cmd_len + 1] = '\0';
89+
break;
90+
}
91+
92+
ptr++;
93+
cmd_len++;
94+
arg_begin_ptr = ptr;
95+
}
96+
97+
return 0;
98+
}
99+
52100
int manager_load_app(void *item, int argc, char *argv[]) {
53101
static struct option options[] = {{"name", 1, 0, 'n'},
54102
{"pidfile", 1, 0, 'p'},
55103
{"check-alive", 0, 0, 'k'},
56104
{"check-alive-time", 1, 0, 't'},
57105
{"heartbeat-interval", 1, 0, 'i'},
106+
{"kill-cmd", 1, 0, 'K'},
58107
{0, 0, 0, 0}};
59108

60109
int cmdtype;
@@ -93,6 +142,15 @@ int manager_load_app(void *item, int argc, char *argv[]) {
93142
strncpy(conf_app->pidfile, get_modelbox_full_path(optarg),
94143
PATH_MAX - 1);
95144
break;
145+
case 'K': {
146+
char path_buf[PATH_MAX];
147+
if (_manager_conf_process_cmds(path_buf, PATH_MAX, optarg) != 0) {
148+
manager_log(MANAGER_LOG_ERR, "process kill cmd failed.");
149+
return -1;
150+
}
151+
strncpy(conf_app->killcmd, get_modelbox_full_path(path_buf),
152+
PATH_MAX - 1);
153+
} break;
96154
default:
97155
break;
98156
}
@@ -102,6 +160,13 @@ int manager_load_app(void *item, int argc, char *argv[]) {
102160
conf_app->heartbeat_interval = DEFAULT_HEARTBEAT_INTERVAL;
103161
}
104162

163+
if (conf_app->heartbeat_interval > conf_watchdog_timeout) {
164+
manager_log(MANAGER_LOG_ERR,
165+
"heartbeat interval is too large, watch dog timeout is %d",
166+
conf_watchdog_timeout);
167+
return -1;
168+
}
169+
105170
if (conf_app->check_alive_time <= 0) {
106171
conf_app->check_alive_time = conf_watchdog_timeout;
107172
}
@@ -132,11 +197,14 @@ static struct config_map conf_parse_map[] = {
132197
{CONF_WATCHDOG_TIMEOUT, conf_parse_int,
133198
.item =
134199
&(struct CONF_PARSE_INT){
135-
.value = &conf_watchdog_timeout, .min = 3, .max = 60 * 5}},
200+
.value = &conf_watchdog_timeout, .min = 3, .max = 600 * 5}},
136201
{CONF_FORCE_KILLTIME, conf_parse_int,
137202
.item =
138203
&(struct CONF_PARSE_INT){
139204
.value = &conf_force_kill_time, .min = 3, .max = 60 * 3}},
205+
{CONF_LOCK_PAGE, conf_parse_int,
206+
.item =
207+
&(struct CONF_PARSE_INT){.value = &conf_lockpage, .min = 0, .max = 1}},
140208
{CONF_APP, manager_load_app, 0},
141209
{CONF_KEY_FILE, conf_parse_string,
142210
.item =

0 commit comments

Comments
 (0)