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

@@ -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)