Initial getopt in the project

This commit is contained in:
2019-01-06 01:42:05 +02:00
parent 0382ead79b
commit 31a9156add
3 changed files with 119 additions and 78 deletions

View File

@@ -25,61 +25,101 @@
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include "confparser.h"
#include "log.h"
#include "rmps.h"
static void usage(char *argv)
{
log(ERROR,
"Usage:\n%s start|stop|restart [--daemonize=yes|no]", argv);
fprintf(stderr, "Usage:\n%s TASK [-c CONFIG] [-d]\n\n"
"Tasks:\n"
"\t--start\t\tStart the RMPS server.\n"
"\t--stop\t\tStop the RMPS server.\n"
"\t--restart\tRestart the RMPS server.\n\n"
"Options:\n"
"\t-c, --config\n"
"\t\tSpecify the configuration file path. Default is set "
"to /etc/rmps/rmps.conf\n"
"\t-d, --daemonize\n"
"\t\tSetting this will make the parent fork in the "
"backgroud. Default is not to fork.\n"
"\t-h, --help\n"
"\t\tShow this message.\n", argv);
}
int main(int argc, char *argv[])
{
int fork_flag = 1, task;
enum tasks {
START = 1,
STOP,
RESTART
};
static int task;
const char opts_short[] = "dc:h";
static struct option opts_long[] =
{
{"start", no_argument, &task, START},
{"stop", no_argument, &task, STOP},
{"restart", no_argument, &task, RESTART},
{"daemonize", no_argument, 0, 'd'},
{"config", required_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{NULL, 0, NULL, 0}
};
int opt = 0, opt_index = 0, fork_flag = 0;
char *config = NULL;
if (argc < 2 || argc > 3) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
if (!strcmp(argv[1], "start"))
task = 1;
else if (!strcmp(argv[1], "stop"))
task = 2;
else if (!strcmp(argv[1], "restart"))
task = 3;
else {
usage(argv[0]);
exit(EXIT_FAILURE);
}
if (argc == 3) {
if (!strcmp("--daemonize=yes", argv[2]))
fork_flag = 1;
else if (!strcmp("--daemonize=no", argv[2]))
fork_flag = 0;
else {
usage(argv[0]);
exit(EXIT_FAILURE);
while ((opt = getopt_long(argc, argv, opts_short, opts_long, &opt_index)) != -1) {
switch (opt) {
case 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);
break;
case 'd':
fork_flag = 1;
break;
case 'c':
config = optarg;
break;
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (confparse() != 0) {
if (!config) {
char path[] = "/etc/rmps/rmps.conf";
int path_size = strlen(path) + 1;
config = calloc(sizeof(char), path_size);
if (config == NULL) {
log(ERROR, "malloc() failed for config");
exit(EXIT_FAILURE);
}
memcpy(config, path, path_size);
}
if (confparse(config) != 0) {
log(ERROR, "Failed to parse the conf!");
exit(EXIT_FAILURE);
}
log(VERBOSE, "Conf parser finished successfully");
//confexport();
if (task == 2 || task == 3) {
if (task == STOP|| task == RESTART) {
char buf[10];
int pid;
FILE *fp;
if (task == 2)
if (task == STOP)
log(VERBOSE, "We got a stop signal!");
else if (task == 3)
else if (task == RESTART)
log(VERBOSE, "We got a restart signal!");
fp = fopen(conf.rmps.pidfile, "r");
@@ -99,7 +139,7 @@ int main(int argc, char *argv[])
log(ERROR, "Permission denied to read PID. Exiting!");
exit(EXIT_FAILURE);
case ENOENT:
if (task == 2)
if (task == STOP)
exit(EXIT_FAILURE);
break;
default:
@@ -109,7 +149,7 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
}
if (task == 1 || task == 3)
if (task == START || task == RESTART)
launch_rmps(&conf, fork_flag);
return 0;