aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-01-30 13:30:47 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-02-01 05:07:36 +0100
commitfced66cdd32e55c63f0512ea60c1eae8d6179c38 (patch)
tree064f785303bbdfcc8b89dce85c745175f3f7e275
parentca43e30be3a27d573e24f85452415b0b6bcefa67 (diff)
main: add option parsing with db file and default options
Parse commandline options, supporting general Osmocom options as copied from osmo-nitb (bsc_hack.c): version, logging and daemonize options. Set the HLR database file from cmdline option, log the filename in db_open(). (VTY config file in next patch.) Change-Id: I279d517e1310e398b0a2382349e62be8e65364c1
-rw-r--r--src/db.c1
-rw-r--r--src/hlr.c91
2 files changed, 90 insertions, 2 deletions
diff --git a/src/db.c b/src/db.c
index 9d4e99e..1385502 100644
--- a/src/db.c
+++ b/src/db.c
@@ -75,6 +75,7 @@ struct db_context *db_open(void *ctx, const char *fname)
unsigned int i;
int rc;
+ LOGP(DDB, LOGL_NOTICE, "using database: %s\n", fname);
LOGP(DDB, LOGL_INFO, "Compiled against SQLite3 lib version %s\n", SQLITE_VERSION);
LOGP(DDB, LOGL_INFO, "Running with SQLite3 lib version %s\n", sqlite3_libversion());
diff --git a/src/hlr.c b/src/hlr.c
index bf0655e..e9d53cf 100644
--- a/src/hlr.c
+++ b/src/hlr.c
@@ -20,6 +20,8 @@
#include <signal.h>
#include <errno.h>
+#include <getopt.h>
+
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/application.h>
@@ -516,6 +518,82 @@ static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
return 0;
}
+static void print_usage()
+{
+ printf("Usage: osmo-hlr\n");
+}
+
+static void print_help()
+{
+ printf(" -h --help This text.\n");
+ printf(" -l --database db-name The database 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");
+}
+
+struct {
+ const char *db_file;
+ bool daemonize;
+} cmdline_opts = {
+ .db_file = "hlr.db",
+ .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'},
+ {"database", 1, 0, 'l'},
+ {"debug", 1, 0, 'd'},
+ {"daemonize", 0, 0, 'D'},
+ {"disable-color", 0, 0, 's'},
+ {"log-level", 1, 0, 'e'},
+ {"timestamp", 0, 0, 'T'},
+ {0, 0, 0, 0}
+ };
+
+ c = getopt_long(argc, argv, "hl:d:Dse:T",
+ long_options, &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ print_usage();
+ print_help();
+ exit(0);
+ case 'l':
+ cmdline_opts.db_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;
+ default:
+ /* catch unknown options *as well as* missing arguments. */
+ fprintf(stderr, "Error in command line options. Exiting.\n");
+ exit(-1);
+ break;
+ }
+ }
+}
+
static void *hlr_ctx = NULL;
static struct osmo_gsup_server *gs;
@@ -549,6 +627,9 @@ int main(int argc, char **argv)
fprintf(stderr, "Error initializing logging\n");
exit(1);
}
+
+ handle_options(argc, argv);
+
LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
rc = rand_init();
@@ -557,7 +638,7 @@ int main(int argc, char **argv)
exit(1);
}
- g_dbc = db_open(hlr_ctx, "hlr.db");
+ g_dbc = db_open(hlr_ctx, cmdline_opts.db_file);
if (!g_dbc) {
LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
exit(1);
@@ -573,7 +654,13 @@ int main(int argc, char **argv)
signal(SIGINT, &signal_hdlr);
signal(SIGUSR1, &signal_hdlr);
- //osmo_daemonize();
+ if (cmdline_opts.daemonize) {
+ rc = osmo_daemonize();
+ if (rc < 0) {
+ perror("Error during daemonize");
+ exit(1);
+ }
+ }
while (1) {
osmo_select_main(0);