implement conf test option and improve systemd service

This commit is contained in:
2019-01-07 00:50:35 +02:00
parent 1bad24582e
commit f144838049
2 changed files with 37 additions and 21 deletions

View File

@@ -5,9 +5,10 @@ After=network.target
[Service] [Service]
Type=simple Type=simple
#PIDFile=/run/rmps/rmpsd.pid #PIDFile=/run/rmps/rmpsd.pid
ExecStart=/usr/bin/rmpsd start --daemonize=no ConfFile=/etc/rmps/rmps.conf
ExecStop=/usr/bin/rmpsd stop ExecStart=/usr/bin/rmpsd --start -c $ConfFile
ExecReload=/usr/bin/rmpsd reload ExecStop=/usr/bin/rmpsd --stop -c $ConfFile
ExecReload=/usr/bin/rmpsd --restart -c $ConfFile
Restart=always Restart=always
[Install] [Install]

View File

@@ -66,6 +66,7 @@ int main(int argc, char *argv[])
{"start", no_argument, &task, START}, {"start", no_argument, &task, START},
{"stop", no_argument, &task, STOP}, {"stop", no_argument, &task, STOP},
{"restart", no_argument, &task, RESTART}, {"restart", no_argument, &task, RESTART},
{"test", no_argument, 0, 't'},
{"daemonize", no_argument, 0, 'd'}, {"daemonize", no_argument, 0, 'd'},
{"config", required_argument, 0, 'c'}, {"config", required_argument, 0, 'c'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
@@ -73,12 +74,12 @@ int main(int argc, char *argv[])
}; };
int opt = 0, opt_index = 0, fork_flag = 0; int opt = 0, opt_index = 0, fork_flag = 0;
char *config = NULL; char *config = NULL;
while ((opt = getopt_long(argc, argv, opts_short, opts_long, &opt_index)) != -1) { while ((opt = getopt_long(argc, argv, opts_short, opts_long, &opt_index)) != -1) {
switch (opt) { switch (opt) {
case 0: case 0:
if (opts_long[opt_index].flag != 0) if (opts_long[opt_index].flag != 0) {
break; break;
}
log(INFO, "option %s", opts_long[opt_index].name); log(INFO, "option %s", opts_long[opt_index].name);
if (optarg) if (optarg)
log(INFO, "with arg %s\n", optarg); log(INFO, "with arg %s\n", optarg);
@@ -92,6 +93,12 @@ int main(int argc, char *argv[])
case 'h': case 'h':
usage(argv[0]); usage(argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
case 't':
task = TEST;
break;
case 'T':
task = TEST_AND_EXPORT;
break;
default: default:
usage(argv[0]); usage(argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -114,7 +121,13 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
log(VERBOSE, "Conf parser finished successfully"); 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) { if (task == STOP || task == RESTART) {
char buf[10]; char buf[10];
@@ -127,32 +140,34 @@ int main(int argc, char *argv[])
log(VERBOSE, "We got a restart signal!"); log(VERBOSE, "We got a restart signal!");
fp = fopen(conf.rmps.pidfile, "r"); fp = fopen(conf.rmps.pidfile, "r");
if (fp) {
switch (errno) {
case EEXIST:
if (!fgets(buf, 10, fp)) { if (!fgets(buf, 10, fp)) {
log(ERROR, "Failed to read %s!", log(ERROR, "Failed to read %s!",
conf.rmps.pidfile); conf.rmps.pidfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
pid = strtol(buf, NULL, 10); pid = strtol(buf, NULL, 10);
log(VERBOSE, "Killing RMPS pid - %d", pid);
kill(pid, SIGTERM); kill(pid, SIGTERM);
//waitpid(TODO); //waitpid(TODO);
break; } else {
switch (errno) {
case EACCES: case EACCES:
log(ERROR, "Permission denied to read PID. Exiting!"); log(ERROR, "Permission denied to read PID. Exiting!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case ENOENT: case ENOENT:
log(VERBOSE, "PID file %s does not exist. Nothing to kill...",
conf.rmps.pidfile);
if (task == STOP) if (task == STOP)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
default: default:
log(ERROR, log(ERROR, "Failed to open PID file %s (errno: %d). Exiting!",
"Failed to open PID file (errno: %d. Exiting!", conf.rmps.pidfile, errno);
errno);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
}
if (task == START || task == RESTART) if (task == START || task == RESTART)
launch_rmps(&conf, fork_flag); launch_rmps(&conf, fork_flag);