Change in osmo-hnodeb[master]: More work on main.c to add osmocom functionalitites

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/.

pespin gerrit-no-reply at lists.osmocom.org
Tue Nov 2 12:00:36 UTC 2021


pespin has submitted this change. ( https://gerrit.osmocom.org/c/osmo-hnodeb/+/26013 )

Change subject: More work on main.c to add osmocom functionalitites
......................................................................

More work on main.c to add osmocom functionalitites

Change-Id: I61a5ee2e100f79cef7e42dbd981e308597c30900
---
M src/osmo-hnodeb/main.c
1 file changed, 205 insertions(+), 15 deletions(-)

Approvals:
  pespin: Looks good to me, approved; Verified
  laforge: Looks good to me, but someone else must approve
  fixeria: Looks good to me, but someone else must approve



diff --git a/src/osmo-hnodeb/main.c b/src/osmo-hnodeb/main.c
index b2ce276..cbf606d 100644
--- a/src/osmo-hnodeb/main.c
+++ b/src/osmo-hnodeb/main.c
@@ -37,6 +37,9 @@
 #include <osmocom/vty/telnet_interface.h>
 #include <osmocom/vty/logging.h>
 #include <osmocom/vty/command.h>
+#include <osmocom/vty/misc.h>
+#include <osmocom/vty/cpu_sched_vty.h>
+#include <osmocom/vty/ports.h>
 
 #include <osmocom/ranap/ranap_common.h> /* ranap_set_log_area() */
 
@@ -46,44 +49,191 @@
 #include <osmocom/hnodeb/vty.h>
 #include <osmocom/hnodeb/hnodeb.h>
 
+static const char * const osmohnodeb_copyright =
+	"OsmoHNodeB - Osmocom 3G Home NodeB implementation\r\n"
+	"Copyright (C) 2015 by Daniel Willmann <dwillmann at sysmocom.de\r\n"
+	"Copyright (C) 2021 by sysmocom s.f.m.c. GmbH <info at sysmocom.de>\r\n"
+	"Based on initial work (hnb-test) by Daniel Willmann\r\n"
+	"Contributions by Pau Espin Pedrol\r\n\r\n"
+	"License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
+	"This is free software: you are free to change and redistribute it.\r\n"
+	"There is NO WARRANTY, to the extent permitted by law.\r\n";
+
 void *tall_hnb_ctx;
 struct hnb *g_hnb;
 
+static struct {
+	const char *database_name;
+	const char *config_file;
+	int daemonize;
+	const char *mncc_sock_path;
+} hnb_cmdline_config = {
+	.database_name = NULL,
+	.config_file = "osmo-hnodeb.cfg",
+};
+
 static struct vty_app_info vty_info = {
 	.name		= "OsmoHNodeB",
-	.version	= "0",
+	.version	= PACKAGE_VERSION,
 };
 
+static void print_usage()
+{
+	printf("Usage: osmo-hnodeb\n");
+}
+
+static void print_help()
+{
+	printf("Some useful options:\n");
+	printf("  -h --help                  This text.\n");
+	printf("  -d option --debug=DCC:DMM:DRR:  Enable debugging.\n");
+	printf("  -D --daemonize             Fork the process into a background daemon.\n");
+	printf("  -c --config-file filename  The config file to use.\n");
+	printf("  -s --disable-color\n");
+	printf("  -T --timestamp             Prefix every log line with a timestamp.\n");
+	printf("  -V --version               Print the version of OsmoHNodeB.\n");
+	printf("  -e --log-level number      Set a global loglevel.\n");
+
+	printf("\nVTY reference generation:\n");
+	printf("     --vty-ref-mode MODE     VTY reference generation mode (e.g. 'expert').\n");
+	printf("     --vty-ref-xml           Generate the VTY reference XML output and exit.\n");
+}
+
+static void handle_long_options(const char *prog_name, const int long_option)
+{
+	static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
+
+	switch (long_option) {
+	case 1:
+		vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
+		if (vty_ref_mode < 0) {
+			fprintf(stderr, "%s: Unknown VTY reference generation "
+				"mode '%s'\n", prog_name, optarg);
+			exit(2);
+		}
+		break;
+	case 2:
+		fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
+			get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
+			get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
+		vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
+		exit(0);
+	default:
+		fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
+		exit(2);
+	}
+}
+
 static void handle_options(int argc, char **argv)
 {
 	while (1) {
 		int idx = 0, c;
+		static int long_option = 0;
 		static const struct option long_options[] = {
+			{"help", 0, 0, 'h'},
+			{"debug", 1, 0, 'd'},
+			{"daemonize", 0, 0, 'D'},
+			{"config-file", 1, 0, 'c'},
+			{"disable-color", 0, 0, 's'},
+			{"timestamp", 0, 0, 'T'},
+			{"version", 0, 0, 'V' },
+			{"log-level", 1, 0, 'e'},
+			{"vty-ref-mode", 1, &long_option, 1},
+			{"vty-ref-xml", 0, &long_option, 2},
 			{ "ues", 1, 0, 'u' },
 			{ "gw-addr", 1, 0, 'g' },
 			{ 0, 0, 0, 0 },
 		};
 
-		c = getopt_long(argc, argv, "u:g:", long_options, &idx);
+		c = getopt_long(argc, argv, "hd:Dc:sTVe:u:g:", long_options, &idx);
 
 		if (c == -1)
 			break;
 
 		switch (c) {
+		case 'h':
+			print_usage();
+			print_help();
+			exit(0);
+		case 0:
+			handle_long_options(argv[0], long_option);
+			break;
+		case 'd':
+			log_parse_category_mask(osmo_stderr_target, optarg);
+			break;
+		case 'D':
+			hnb_cmdline_config.daemonize = 1;
+			break;
+		case 'c':
+			hnb_cmdline_config.config_file = optarg;
+			break;
+		case 's':
+			log_set_use_color(osmo_stderr_target, 0);
+			break;
+		case 'T':
+			log_set_print_timestamp(osmo_stderr_target, 1);
+			break;
+		case 'V':
+			print_version(1);
+			exit(0);
+			break;
+		case 'e':
+			log_set_log_level(osmo_stderr_target, atoi(optarg));
+			break;
 		case 'u':
 			g_hnb->ues = atoi(optarg);
 			break;
 		case 'g':
 			g_hnb->gw_addr = optarg;
 			break;
+		default:
+			/* catch unknown options *as well as* missing arguments. */
+			fprintf(stderr, "Error in command line options. Exiting.\n");
+			exit(-1);
 		}
 	}
 }
 
+static void signal_handler(int signum)
+{
+	fprintf(stdout, "signal %u received\n", signum);
+
+	switch (signum) {
+	case SIGINT:
+	case SIGTERM:
+		/* If SIGTERM was already sent before, just terminate immediately. */
+		if (osmo_select_shutdown_requested())
+			exit(-1);
+		osmo_select_shutdown_request();
+		break;
+	case SIGABRT:
+		/* in case of abort, we want to obtain a talloc report and
+		 * then run default SIGABRT handler, who will generate coredump
+		 * and abort the process. abort() should do this for us after we
+		 * return, but program wouldn't exit if an external SIGABRT is
+		 * received.
+		 */
+		talloc_report(tall_vty_ctx, stderr);
+		talloc_report_full(tall_hnb_ctx, stderr);
+		signal(SIGABRT, SIG_DFL);
+		raise(SIGABRT);
+		break;
+	case SIGUSR1:
+		talloc_report(tall_vty_ctx, stderr);
+		talloc_report_full(tall_hnb_ctx, stderr);
+		break;
+	default:
+		break;
+	}
+}
+
 int main(int argc, char **argv)
 {
 	int rc;
 
+	/* Track the use of talloc NULL memory contexts */
+	talloc_enable_null_tracking();
+
 	tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
 
 	msgb_talloc_ctx_init(tall_hnb_ctx, 0);
@@ -95,36 +245,76 @@
 
 	ranap_set_log_area(DRANAP);
 
-	log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
-	log_set_use_color(osmo_stderr_target, 0);
-	log_set_print_category(osmo_stderr_target, 0);
-	log_set_print_category_hex(osmo_stderr_target, 0);
+	vty_info.copyright = osmohnodeb_copyright;
+	vty_info.tall_ctx = tall_hnb_ctx;
+	vty_init(&vty_info);
+	logging_vty_add_cmds();
+	osmo_talloc_vty_add_cmds();
+	osmo_cpu_sched_vty_init(tall_hnb_ctx);
 
 	g_hnb = hnb_alloc(tall_hnb_ctx);
-
-	vty_init(&vty_info);
 	hnb_vty_init();
 
-	rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
+	handle_options(argc, argv);
+
+
+	rc = vty_read_config_file(hnb_cmdline_config.config_file, NULL);
+	if (rc < 0) {
+		fprintf(stderr, "Failed to parse the config file: '%s'\n",
+			hnb_cmdline_config.config_file);
+		exit(1);
+	}
+
+	rc = telnet_init_dynif(tall_hnb_ctx, g_hnb, vty_get_bind_addr(), OSMO_VTY_PORT_HNODEB);
 	if (rc < 0) {
 		perror("Error binding VTY port");
 		exit(1);
 	}
 
-	handle_options(argc, argv);
-
 	rc = hnb_connect(g_hnb);
 	if (rc < 0) {
 		perror("Error connecting to Iuh port");
 		exit(1);
 	}
 
-	while (1) {
-		rc = osmo_select_main(0);
-		if (rc < 0)
-			exit(3);
+	signal(SIGINT, &signal_handler);
+	signal(SIGTERM, &signal_handler);
+	signal(SIGABRT, &signal_handler);
+	signal(SIGUSR1, &signal_handler);
+	signal(SIGUSR2, &signal_handler);
+	osmo_init_ignore_signals();
+
+	if (hnb_cmdline_config.daemonize) {
+		rc = osmo_daemonize();
+		if (rc < 0) {
+			perror("Error during daemonize");
+			return 6;
+		}
 	}
 
+	while (!osmo_select_shutdown_done()) {
+		osmo_select_main_ctx(0);
+	}
+
+	log_fini();
+
+	/**
+	 * Report the heap state of root context, then free,
+	 * so both ASAN and Valgrind are happy...
+	 */
+	talloc_report_full(tall_hnb_ctx, stderr);
+	talloc_free(tall_hnb_ctx);
+
+	/* FIXME: VTY code still uses NULL-context */
+	talloc_free(tall_vty_ctx);
+
+	/**
+	 * Report the heap state of NULL context, then free,
+	 * so both ASAN and Valgrind are happy...
+	 */
+	talloc_report_full(NULL, stderr);
+	talloc_disable_null_tracking();
+
 	/* not reached */
 	exit(0);
 }

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-hnodeb/+/26013
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-hnodeb
Gerrit-Branch: master
Gerrit-Change-Id: I61a5ee2e100f79cef7e42dbd981e308597c30900
Gerrit-Change-Number: 26013
Gerrit-PatchSet: 3
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20211102/e79d9f8c/attachment.htm>


More information about the gerrit-log mailing list