[PATCH] osmo-hlr[master]: main: add option parsing with db file and default options

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Wed Feb 1 03:44:56 UTC 2017


Hello Jenkins Builder, Holger Freyther,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/1706

to look at the new patch set (#3).

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
---
M src/db.c
M src/hlr.c
2 files changed, 91 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/06/1706/3

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 @@
 	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..04ca0a6 100644
--- a/src/hlr.c
+++ b/src/hlr.c
@@ -20,6 +20,9 @@
 #include <signal.h>
 #include <errno.h>
 
+#define _GNU_SOURCE
+#include <getopt.h>
+
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/logging.h>
 #include <osmocom/core/application.h>
@@ -516,6 +519,82 @@
 	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 +628,9 @@
 		fprintf(stderr, "Error initializing logging\n");
 		exit(1);
 	}
+
+	handle_options(argc, argv);
+
 	LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
 
 	rc = rand_init();
@@ -557,7 +639,7 @@
 		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 +655,13 @@
 	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);

-- 
To view, visit https://gerrit.osmocom.org/1706
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I279d517e1310e398b0a2382349e62be8e65364c1
Gerrit-PatchSet: 3
Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Holger Freyther <holger at freyther.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list