From 9df44cc960a484f70b59924e4a3122c89a8c36fa Mon Sep 17 00:00:00 2001 From: Bogomil Vasilev Date: Sun, 13 Jan 2019 12:05:10 +0200 Subject: [PATCH] improve rmps shutdown, cleanup main() --- src/main.c | 45 +++------------------------------------------ src/rmps.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/rmps.h | 4 ++-- 3 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/main.c b/src/main.c index 8e22057..25d6cfc 100644 --- a/src/main.c +++ b/src/main.c @@ -19,12 +19,7 @@ * along with RMPS. If not, see . */ -#include -#include #include -#include -#include -#include #include #include "confparser.h" #include "log.h" @@ -131,46 +126,12 @@ int main(int argc, char *argv[]) if (task == STOP || task == RESTART) { - char buf[10]; - int pid; - FILE *fp; - - if (task == STOP) - log(VERBOSE, "We got a stop signal!"); - else /* RESTART */ - log(VERBOSE, "We got a restart signal!"); - - fp = fopen(conf.rmps.pidfile, "r"); - if (fp) { - if (!fgets(buf, 10, fp)) { - log(ERROR, "Failed to read %s!", - conf.rmps.pidfile); - exit(EXIT_FAILURE); - } - pid = strtol(buf, NULL, 10); - log(VERBOSE, "Killing RMPS pid - %d", pid); - kill(pid, SIGTERM); - //waitpid(TODO); - } else { - switch (errno) { - case EACCES: - log(ERROR, "Permission denied to read PID. Exiting!"); - exit(EXIT_FAILURE); - case ENOENT: - log(VERBOSE, "PID file %s does not exist. Nothing to kill...", - conf.rmps.pidfile); - if (task == STOP) - exit(EXIT_FAILURE); - break; - default: - log(ERROR, "Failed to open PID file %s (errno: %d). Exiting!", - conf.rmps.pidfile, errno); - exit(EXIT_FAILURE); - } + if (rmps_die() != 0) { + exit(EXIT_FAILURE); } } if (task == START || task == RESTART) - launch_rmps(&conf, fork_flag); + rmps_launch(&conf, fork_flag); return 0; } diff --git a/src/rmps.c b/src/rmps.c index 27502c2..8d1813d 100644 --- a/src/rmps.c +++ b/src/rmps.c @@ -30,6 +30,7 @@ #include #include #include +#include /* included for openssl */ #include @@ -286,7 +287,47 @@ void load_certificates(SSL_CTX *ctx, const char *certfile, } } -void launch_rmps(struct conf_table *conf, int fork_flag) +int rmps_die(void) +{ + char buf[10]; + int pid, status; + FILE *fp; + + fp = fopen(conf.rmps.pidfile, "r"); + if (fp) { + if (!fgets(buf, 10, fp)) { + log(ERROR, "Failed to read %s!", conf.rmps.pidfile); + return 1; + } + fclose(fp); + pid = strtol(buf, NULL, 10); + kill(pid, SIGTERM); + //waitpid(TODO); + } else { + switch (errno) { + case EACCES: + log(ERROR, "Permission denied to read PID file %s. Exiting!", + conf.rmps.pidfile); + return 1; + case ENOENT: + log(VERBOSE, "PID file %s does not exist. Nothing to kill...", + conf.rmps.pidfile); + return 1; + default: + log(ERROR, "Failed to open PID file %s (errno: %d). Exiting!", + conf.rmps.pidfile, errno); + return 1; + } + } + status = kill(pid, SIGTERM); + if (status != 0) { + log(ERROR, "Failed to kill RMPS PID %s (errno %s). Exiting!"); + return 1; + } + return 0; +} + +void rmps_launch(struct conf_table *conf, int fork_flag) { pthread_t pool[2]; struct pool_data pool_args[2]; @@ -337,4 +378,3 @@ void launch_rmps(struct conf_table *conf, int fork_flag) pthread_join(pool[0], NULL); pthread_join(pool[1], NULL); } - diff --git a/src/rmps.h b/src/rmps.h index dce9062..c5f3642 100644 --- a/src/rmps.h +++ b/src/rmps.h @@ -31,7 +31,7 @@ struct pool_data { int size; }; -extern void launch_rmps(struct conf_table *conf, int fork_flag); +extern void rmps_launch(struct conf_table *conf, int fork_flag); +extern int rmps_die(void); #endif /* RMPS_H */ -