aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2018-12-13 14:33:22 +0000
committerGerrit Code Review <gerrit@jenkins.osmocom.org>2018-12-13 14:33:22 +0000
commit3b25774db5b584e926dab2534d93314f910f62b6 (patch)
treecf2c88630d40ea06e9b2c09bef8f1c55dbb11ace
parente649fb2d7523c0db39f34c4117ecfd6102807863 (diff)
parentae25399f759d69f78433297526505f5e98a55803 (diff)
Merge "Add cmdline option parsing support"
-rw-r--r--src/osysmon_main.c99
1 files changed, 95 insertions, 4 deletions
diff --git a/src/osysmon_main.c b/src/osysmon_main.c
index 5983212..486ee8f 100644
--- a/src/osysmon_main.c
+++ b/src/osysmon_main.c
@@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include <signal.h>
+#include <getopt.h>
#include "config.h"
#include "osysmon.h"
@@ -69,8 +70,6 @@ static struct vty_app_info vty_info = {
.is_config_node = osysmon_is_config_node,
};
-
-static const char *config_file = "osmo-sysmon.cfg";
struct osysmon_state *g_oss;
@@ -110,6 +109,88 @@ static void signal_handler(int signal)
}
}
+static void print_usage()
+{
+ printf("Usage: osmo-sysmon\n");
+}
+
+static void print_help()
+{
+ printf(" -h --help This text.\n");
+ printf(" -c --config-file filename The config file to use.\n");
+ printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
+ printf(" -D --daemonize Fork the process into a background daemon.\n");
+ printf(" -s --disable-color Do not print ANSI colors in the log\n");
+ printf(" -T --timestamp Prefix every log line with a timestamp.\n");
+ printf(" -e --log-level number Set a global loglevel.\n");
+ printf(" -V --version Print the version of OsmoHLR.\n");
+}
+
+static struct {
+ const char *config_file;
+ bool daemonize;
+} cmdline_opts = {
+ .config_file = "osmo-sysmon.cfg",
+ .daemonize = false,
+};
+
+static void handle_options(int argc, char **argv)
+{
+ while (1) {
+ int option_index = 0, c;
+ static struct option long_options[] = {
+ {"help", 0, 0, 'h'},
+ {"config-file", 1, 0, 'c'},
+ {"debug", 1, 0, 'd'},
+ {"daemonize", 0, 0, 'D'},
+ {"disable-color", 0, 0, 's'},
+ {"log-level", 1, 0, 'e'},
+ {"timestamp", 0, 0, 'T'},
+ {"version", 0, 0, 'V' },
+ {0, 0, 0, 0}
+ };
+
+ c = getopt_long(argc, argv, "hc:d:Dse:TV",
+ long_options, &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ print_usage();
+ print_help();
+ exit(0);
+ case 'c':
+ cmdline_opts.config_file = optarg;
+ break;
+ case 'd':
+ log_parse_category_mask(osmo_stderr_target, optarg);
+ break;
+ case 'D':
+ cmdline_opts.daemonize = 1;
+ break;
+ case 's':
+ log_set_use_color(osmo_stderr_target, 0);
+ break;
+ case 'e':
+ log_set_log_level(osmo_stderr_target, atoi(optarg));
+ break;
+ case 'T':
+ log_set_print_timestamp(osmo_stderr_target, 1);
+ break;
+ case 'V':
+ print_version(1);
+ exit(0);
+ break;
+ default:
+ /* catch unknown options *as well as* missing arguments. */
+ fprintf(stderr, "Error in command line options. Exiting.\n");
+ exit(-1);
+ break;
+ }
+ }
+}
+
int main(int argc, char **argv)
{
int rc;
@@ -122,14 +203,16 @@ int main(int argc, char **argv)
INIT_LLIST_HEAD(&g_oss->files);
vty_init(&vty_info);
+ handle_options(argc, argv);
osysmon_sysinfo_init();
osysmon_ctrl_init();
osysmon_rtnl_init();
osysmon_file_init();
- rc = vty_read_config_file(config_file, NULL);
+ rc = vty_read_config_file(cmdline_opts.config_file, NULL);
if (rc < 0) {
- fprintf(stderr, "Failed to parse the config file %s\n", config_file);
+ fprintf(stderr, "Failed to parse the config file %s\n",
+ cmdline_opts.config_file);
exit(2);
}
@@ -137,6 +220,14 @@ int main(int argc, char **argv)
signal(SIGUSR2, &signal_handler);
osmo_init_ignore_signals();
+ if (cmdline_opts.daemonize) {
+ rc = osmo_daemonize();
+ if (rc < 0) {
+ perror("Error during daemonize");
+ exit(1);
+ }
+ }
+
while (1) {
struct value_node *root = value_node_add(g_oss, NULL, "root", NULL);
osysmon_sysinfo_poll(root);