From f144838049934c963347e5271d8a619809137a6f Mon Sep 17 00:00:00 2001 From: Bogomil Vasilev Date: Mon, 7 Jan 2019 00:50:35 +0200 Subject: [PATCH] implement conf test option and improve systemd service --- docs/systemd/rmpsd.service | 7 +++--- src/main.c | 51 ++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/docs/systemd/rmpsd.service b/docs/systemd/rmpsd.service index e800bec..23bd3d1 100644 --- a/docs/systemd/rmpsd.service +++ b/docs/systemd/rmpsd.service @@ -5,9 +5,10 @@ After=network.target [Service] Type=simple #PIDFile=/run/rmps/rmpsd.pid -ExecStart=/usr/bin/rmpsd start --daemonize=no -ExecStop=/usr/bin/rmpsd stop -ExecReload=/usr/bin/rmpsd reload +ConfFile=/etc/rmps/rmps.conf +ExecStart=/usr/bin/rmpsd --start -c $ConfFile +ExecStop=/usr/bin/rmpsd --stop -c $ConfFile +ExecReload=/usr/bin/rmpsd --restart -c $ConfFile Restart=always [Install] diff --git a/src/main.c b/src/main.c index 48aa23d..1d58781 100644 --- a/src/main.c +++ b/src/main.c @@ -66,6 +66,7 @@ int main(int argc, char *argv[]) {"start", no_argument, &task, START}, {"stop", no_argument, &task, STOP}, {"restart", no_argument, &task, RESTART}, + {"test", no_argument, 0, 't'}, {"daemonize", no_argument, 0, 'd'}, {"config", required_argument, 0, 'c'}, {"help", no_argument, 0, 'h'}, @@ -73,12 +74,12 @@ int main(int argc, char *argv[]) }; int opt = 0, opt_index = 0, fork_flag = 0; char *config = NULL; - while ((opt = getopt_long(argc, argv, opts_short, opts_long, &opt_index)) != -1) { switch (opt) { case 0: - if (opts_long[opt_index].flag != 0) + if (opts_long[opt_index].flag != 0) { break; + } log(INFO, "option %s", opts_long[opt_index].name); if (optarg) log(INFO, "with arg %s\n", optarg); @@ -92,6 +93,12 @@ int main(int argc, char *argv[]) case 'h': usage(argv[0]); exit(EXIT_SUCCESS); + case 't': + task = TEST; + break; + case 'T': + task = TEST_AND_EXPORT; + break; default: usage(argv[0]); exit(EXIT_FAILURE); @@ -114,7 +121,13 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } log(VERBOSE, "Conf parser finished successfully"); - //confexport(); + if (task == TEST || task == TEST_AND_EXPORT) { + printf("RMPS: Configuration test is successful!\n"); + if (task == TEST_AND_EXPORT) + confexport(); + exit(EXIT_SUCCESS); + } + if (task == STOP || task == RESTART) { char buf[10]; @@ -127,30 +140,32 @@ int main(int argc, char *argv[]) log(VERBOSE, "We got a restart signal!"); fp = fopen(conf.rmps.pidfile, "r"); - - switch (errno) { - case EEXIST: + 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); - break; - case EACCES: - log(ERROR, "Permission denied to read PID. Exiting!"); - exit(EXIT_FAILURE); - case ENOENT: - if (task == STOP) + } else { + switch (errno) { + case EACCES: + log(ERROR, "Permission denied to read PID. Exiting!"); exit(EXIT_FAILURE); - break; - default: - log(ERROR, - "Failed to open PID file (errno: %d. Exiting!", - errno); - 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 (task == START || task == RESTART)