feat:add cmd about stopping running neuron

fix:close file fd
This commit is contained in:
xiang.zhou
2023-07-26 14:12:01 +08:00
parent d7f1ae1d9b
commit b8fdf5a1c2
5 changed files with 55 additions and 2 deletions
+5
View File
@@ -60,6 +60,7 @@ const char *usage_text =
"OPTIONS:\n"
" -d, --daemon run as daemon process\n"
" -h, --help show this help message\n"
" --stop stop running neuron\n"
" --log log to the stdout\n"
" --log_level <LEVEL> default log level(DEBUG,NOTICE)\n"
" --reset-password reset dashboard to use default password\n"
@@ -151,6 +152,7 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[])
{ "disable_auth", no_argument, NULL, 'a' },
{ "config_dir", required_argument, NULL, 'c' },
{ "plugin_dir", required_argument, NULL, 'p' },
{ "stop", no_argument, NULL, 's' },
{ NULL, 0, NULL, 0 },
};
@@ -200,6 +202,9 @@ void neu_cli_args_init(neu_cli_args_t *args, int argc, char *argv[])
case 'p':
plugin_dir = strdup(optarg);
break;
case 's':
args->stop = true;
break;
case '?':
default:
usage();
+1
View File
@@ -46,6 +46,7 @@ typedef struct {
char * log_init_file;
char * config_dir;
char * plugin_dir;
bool stop;
} neu_cli_args_t;
/** Parse command line arguments.
+31
View File
@@ -135,3 +135,34 @@ int neuron_already_running()
return 0;
}
int neuron_stop()
{
int ret = -1;
int fd = -1;
char buf[16] = { 0 };
fd = open(NEURON_DAEMON_LOCK_FNAME, O_RDONLY);
if (fd < 0) {
nlog_error("cannot open %s reason: %s\n", NEURON_DAEMON_LOCK_FNAME,
strerror(errno));
return ret;
}
int size = read(fd, buf, sizeof(buf) - 1);
if (size <= 0) {
nlog_error("cannot read %s reason: %s\n", NEURON_DAEMON_LOCK_FNAME,
strerror(errno));
ret = -1;
} else {
pid_t pid = -1;
if (sscanf(buf, "%d", &pid) == 1) {
if (kill(pid, SIGINT) == -1) {
ret = -1;
} else {
ret = 0;
}
}
}
close(fd);
return ret;
}
+2
View File
@@ -34,6 +34,8 @@ void daemonize();
*/
int neuron_already_running();
int neuron_stop();
#ifdef __cplusplus
}
#endif
+16 -2
View File
@@ -110,9 +110,23 @@ int main(int argc, char *argv[])
zlog_level_switch(neuron, default_log_level);
if (neuron_already_running()) {
nlog_fatal("neuron process already running, exit.");
rv = -1;
if (args.stop) {
rv = neuron_stop();
nlog_notice("neuron stop ret=%d", rv);
if (rv == 0) {
printf("neuron stop successfully.\n");
}
} else {
nlog_fatal("neuron process already running, exit.");
rv = -1;
}
goto main_end;
} else {
if (args.stop) {
rv = -1;
printf("neuron no running.\n");
goto main_end;
}
}
for (size_t i = 0; i < args.restart; ++i) {