From b8fdf5a1c2240fe1e6aaffead9be830ee9460e41 Mon Sep 17 00:00:00 2001 From: "xiang.zhou" Date: Wed, 26 Jul 2023 14:12:01 +0800 Subject: [PATCH] feat:add cmd about stopping running neuron fix:close file fd --- src/argparse.c | 5 +++++ src/argparse.h | 1 + src/daemon.c | 31 +++++++++++++++++++++++++++++++ src/daemon.h | 2 ++ src/main.c | 18 ++++++++++++++++-- 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/argparse.c b/src/argparse.c index 7477cece..2aa68d2c 100644 --- a/src/argparse.c +++ b/src/argparse.c @@ -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 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(); diff --git a/src/argparse.h b/src/argparse.h index d87bba1d..14b045f1 100644 --- a/src/argparse.h +++ b/src/argparse.h @@ -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. diff --git a/src/daemon.c b/src/daemon.c index 8e75d349..afbe6840 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -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; +} \ No newline at end of file diff --git a/src/daemon.h b/src/daemon.h index 6fa5596a..5402a6af 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -34,6 +34,8 @@ void daemonize(); */ int neuron_already_running(); +int neuron_stop(); + #ifdef __cplusplus } #endif diff --git a/src/main.c b/src/main.c index f2727693..74942fd0 100644 --- a/src/main.c +++ b/src/main.c @@ -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) {