From aa144b5981c29d0f04e3a9629dd7650ccf77ebbe Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 19 Feb 2025 15:07:48 +0800 Subject: [PATCH] process_stop_ex add parameter: force --- src/process_ctrl.c | 18 ++++++++++++------ src/process_ctrl.h | 10 ++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/process_ctrl.c b/src/process_ctrl.c index 4b47169..92b6971 100644 --- a/src/process_ctrl.c +++ b/src/process_ctrl.c @@ -112,7 +112,8 @@ static int do_stop(const char *pidFilename, const bool bShowError, pid_t *pid) } } -int process_stop_ex(const char *pidFilename, const bool bShowError) +int process_stop_ex(const char *pidFilename, + const bool bShowError, bool *force) { #define MAX_WAIT_COUNT 300 pid_t pid = 0; @@ -120,6 +121,7 @@ int process_stop_ex(const char *pidFilename, const bool bShowError) int sig; int i; + *force = false; if ((result=do_stop(pidFilename, bShowError, &pid)) != 0) { return result; } @@ -131,14 +133,15 @@ int process_stop_ex(const char *pidFilename, const bool bShowError) break; } - usleep(100 * 1000); + fc_sleep_ms(100); } if (i == MAX_WAIT_COUNT) { if (kill(pid, SIGKILL) == 0) { fprintf(stderr, "waiting for pid [%d] exit timeout, " "force kill!\n", (int)pid); - usleep(100 * 1000); + *force = true; + fc_sleep_ms(100); } } @@ -149,12 +152,16 @@ int process_stop_ex(const char *pidFilename, const bool bShowError) int process_restart(const char *pidFilename) { const bool bShowError = false; + bool force; int result; - result = process_stop_ex(pidFilename, bShowError); + result = process_stop_ex(pidFilename, bShowError, &force); if (result == ENOENT || result == ESRCH) { result = 0; } else if (result == 0) { + if (force) { + sleep(1); + } fprintf(stderr, "starting ...\n"); } @@ -332,7 +339,6 @@ int get_base_path_from_conf_file_ex(const char *filename, char *base_path, int process_action(const char *pidFilename, const char *action, bool *stop) { - const bool bShowError = true; int result; pid_t pid; @@ -345,7 +351,7 @@ int process_action(const char *pidFilename, const char *action, bool *stop) if (strcmp(action, "stop") == 0) { *stop = true; - return process_stop_ex(pidFilename, bShowError); + return process_stop(pidFilename); } else if (strcmp(action, "status") == 0) { diff --git a/src/process_ctrl.h b/src/process_ctrl.h index d38c446..2f1da41 100644 --- a/src/process_ctrl.h +++ b/src/process_ctrl.h @@ -39,9 +39,15 @@ int write_to_pid_file(const char *pidFilename); int delete_pid_file(const char *pidFilename); -int process_stop_ex(const char *pidFilename, const bool bShowError); +int process_stop_ex(const char *pidFilename, + const bool bShowError, bool *force); -#define process_stop(pidFilename) process_stop_ex(pidFilename, true) +static inline int process_stop(const char *pidFilename) +{ + const bool bShowError = true; + bool force; + return process_stop_ex(pidFilename, bShowError, &force); +} int process_restart(const char *pidFilename);