implement conf test option and improve systemd service
This commit is contained in:
@@ -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]
|
||||||
|
|||||||
51
src/main.c
51
src/main.c
@@ -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,30 +140,32 @@ 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 {
|
||||||
case EACCES:
|
switch (errno) {
|
||||||
log(ERROR, "Permission denied to read PID. Exiting!");
|
case EACCES:
|
||||||
exit(EXIT_FAILURE);
|
log(ERROR, "Permission denied to read PID. Exiting!");
|
||||||
case ENOENT:
|
|
||||||
if (task == STOP)
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
break;
|
case ENOENT:
|
||||||
default:
|
log(VERBOSE, "PID file %s does not exist. Nothing to kill...",
|
||||||
log(ERROR,
|
conf.rmps.pidfile);
|
||||||
"Failed to open PID file (errno: %d. Exiting!",
|
if (task == STOP)
|
||||||
errno);
|
exit(EXIT_FAILURE);
|
||||||
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)
|
if (task == START || task == RESTART)
|
||||||
|
|||||||
Reference in New Issue
Block a user