/* * main.c * * Copyright (C) 2018 by Bogomil Vasilev * * This file is part of Remote Management and Provisioning System (RMPS). * * RMPS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * RMPS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with RMPS. If not, see . */ #include #include #include #include #include #include #include #include "confparser.h" #include "log.h" #include "rmps.h" static void usage(char *argv) { fprintf(stderr, "Usage:\n%s TASK [-c CONFIG] [-d]\n\n" "Tasks:\n" "\t--start\t\tStart the RMPS server. This is by default. \n" "\t--stop\t\tStop the RMPS server.\n" "\t--restart\tRestart the RMPS server.\n" "\t-t, --test\tTest the configuration file and exit,\n" "\t-T\t\tTest the configuration file, dump it to stdout and exit.\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[]) { enum tasks { START = 1, STOP, RESTART, TEST, TEST_AND_EXPORT }; static int task = START; const char opts_short[] = "dc:htT"; static struct option opts_long[] = { {"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'}, {NULL, 0, NULL, 0} }; 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) { 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); case 't': task = TEST; break; case 'T': task = TEST_AND_EXPORT; break; default: usage(argv[0]); exit(EXIT_FAILURE); } } 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"); 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]; int pid; FILE *fp; if (task == STOP) log(VERBOSE, "We got a stop signal!"); else /* RESTART */ log(VERBOSE, "We got a restart signal!"); fp = fopen(conf.rmps.pidfile, "r"); 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); } else { switch (errno) { case EACCES: log(ERROR, "Permission denied to read PID. Exiting!"); 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) launch_rmps(&conf, fork_flag); return 0; }