ctx cleanup: use non-NULL talloc ctx for osmo_init_logging2()

Fix various sanitizer complaints about memory leaks using a sanitizer build
with gcc (Debian 7.3.0-12) 7.3.0.

Also fix deprecation warnings on osmo_init_logging().

Depends: I216837780e9405fdaec8059c63d10699c695b360 (libosmocore)
Change-Id: I970c6f8a0e36a8b63e42349dbc92baff649e5cef
diff --git a/tests/abis/abis_test.c b/tests/abis/abis_test.c
index 7deb0f0..faf9ea5 100644
--- a/tests/abis/abis_test.c
+++ b/tests/abis/abis_test.c
@@ -175,7 +175,7 @@
 
 int main(int argc, char **argv)
 {
-	osmo_init_logging(&log_info);
+	osmo_init_logging2(NULL, &log_info);
 
 	test_sw_selection();
 	test_abis_nm_ipaccess_cgi();
diff --git a/tests/bsc/bsc_test.c b/tests/bsc/bsc_test.c
index 744b9e1..106b08b 100644
--- a/tests/bsc/bsc_test.c
+++ b/tests/bsc/bsc_test.c
@@ -39,6 +39,8 @@
 #include <stdio.h>
 #include <search.h>
 
+void *ctx = NULL;
+
 enum test {
 	TEST_SCAN_TO_BTS,
 	TEST_SCAN_TO_MSC,
@@ -122,7 +124,7 @@
 {
 	int i;
 
-	struct gsm_network *net = bsc_network_init(NULL);
+	struct gsm_network *net = bsc_network_init(ctx);
 	struct gsm_bts *bts = gsm_bts_alloc(net, 0);
 	struct bsc_msc_data *msc;
 	struct gsm_subscriber_connection *conn;
@@ -227,12 +229,14 @@
 
 int main(int argc, char **argv)
 {
-	msgb_talloc_ctx_init(NULL, 0);
-	osmo_init_logging(&log_info);
+	ctx = talloc_named_const(NULL, 0, "bsc-test");
+	msgb_talloc_ctx_init(ctx, 0);
+	osmo_init_logging2(ctx, &log_info);
 
 	test_scan();
 
 	printf("Testing execution completed.\n");
+	talloc_free(ctx);
 	return 0;
 }
 
diff --git a/tests/channel/channel_test.c b/tests/channel/channel_test.c
index 78db1d4..e8f6cd9 100644
--- a/tests/channel/channel_test.c
+++ b/tests/channel/channel_test.c
@@ -98,7 +98,7 @@
 
 int main(int argc, char **argv)
 {
-	osmo_init_logging(&log_info);
+	osmo_init_logging2(NULL, &log_info);
 
 	test_dyn_ts_subslots();
 	test_bts_debug_print();
diff --git a/tests/gsm0408/gsm0408_test.c b/tests/gsm0408/gsm0408_test.c
index 67840f0..a934806 100644
--- a/tests/gsm0408/gsm0408_test.c
+++ b/tests/gsm0408/gsm0408_test.c
@@ -812,7 +812,9 @@
 {
 	struct gsm_network *net;
 
-	osmo_init_logging(&log_info);
+	tall_bsc_ctx = talloc_named_const(NULL, 0, "gsm0408_test");
+
+	osmo_init_logging2(tall_bsc_ctx, &log_info);
 	log_set_log_level(osmo_stderr_target, LOGL_INFO);
 
 	net = bsc_network_init(tall_bsc_ctx);
diff --git a/tests/handover/handover_test.c b/tests/handover/handover_test.c
index 280861c..ab32a29 100644
--- a/tests/handover/handover_test.c
+++ b/tests/handover/handover_test.c
@@ -43,6 +43,8 @@
 #include <osmocom/bsc/osmo_bsc.h>
 #include <osmocom/bsc/bsc_subscr_conn_fsm.h>
 
+void *ctx;
+
 struct gsm_network *bsc_gsmnet;
 
 /* override, requires '-Wl,--wrap=mgcp_conn_modify'.
@@ -213,7 +215,7 @@
 	bts->codec.hr = 1;
 	bts->codec.amr = 1;
 
-	rsl_link = talloc_zero(0, struct e1inp_sign_link);
+	rsl_link = talloc_zero(ctx, struct e1inp_sign_link);
 	rsl_link->trx = bts->c0;
 	bts->c0->rsl_link = rsl_link;
 
@@ -1350,6 +1352,9 @@
 	int test_case_i;
 	int last_test_i;
 
+	ctx = talloc_named_const(NULL, 0, "handover_test");
+	msgb_talloc_ctx_init(ctx, 0);
+
 	test_case_i = argc > 1? atoi(argv[1]) : -1;
 	last_test_i = ARRAY_SIZE(test_cases) - 1;
 
@@ -1362,14 +1367,14 @@
 		return EXIT_FAILURE;
 	}
 
-	osmo_init_logging(&log_info);
+	osmo_init_logging2(ctx, &log_info);
 
 	log_set_print_category(osmo_stderr_target, 1);
 	log_set_print_category_hex(osmo_stderr_target, 0);
 	log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
 
 	/* Create a dummy network */
-	bsc_gsmnet = bsc_network_init(NULL);
+	bsc_gsmnet = bsc_network_init(ctx);
 	if (!bsc_gsmnet)
 		exit(1);
 
@@ -1676,6 +1681,7 @@
 
 	fprintf(stderr, "--------------------\n");
 
+	talloc_free(ctx);
 	return EXIT_SUCCESS;
 }
 
diff --git a/tests/nanobts_omlattr/nanobts_omlattr_test.c b/tests/nanobts_omlattr/nanobts_omlattr_test.c
index 663594c..8e8626d 100644
--- a/tests/nanobts_omlattr/nanobts_omlattr_test.c
+++ b/tests/nanobts_omlattr/nanobts_omlattr_test.c
@@ -199,11 +199,11 @@
 	struct gsm_network *net;
 	struct gsm_bts_trx *trx;
 
-	osmo_init_logging(&log_info);
-	log_set_log_level(osmo_stderr_target, LOGL_INFO);
-
 	ctx = talloc_named_const(NULL, 0, "ctx");
 
+	osmo_init_logging2(ctx, &log_info);
+	log_set_log_level(osmo_stderr_target, LOGL_INFO);
+
 	/* Allocate environmental structs (bts, net, trx) */
 	net = talloc_zero(ctx, struct gsm_network);
 	INIT_LLIST_HEAD(&net->bts_list);
@@ -282,7 +282,16 @@
 	talloc_free(net);
 	talloc_free(trx);
 	talloc_report_full(ctx, stderr);
-	OSMO_ASSERT(talloc_total_blocks(ctx) == 1);
+	/* Expecting something like:
+	 * full talloc report on 'ctx' (total    813 bytes in   6 blocks)
+	 *     logging                        contains    813 bytes in   5 blocks (ref 0) 0x60b0000000a0
+	 * 	struct log_target              contains    196 bytes in   2 blocks (ref 0) 0x6110000000a0
+	 * 	    struct log_category            contains     36 bytes in   1 blocks (ref 0) 0x60d0000003e0
+	 * 	struct log_info                contains    616 bytes in   2 blocks (ref 0) 0x60d000000310
+	 * 	    struct log_info_cat            contains    576 bytes in   1 blocks (ref 0) 0x6170000000e0
+	 * That's the root ctx + 5x logging: */
+	OSMO_ASSERT(talloc_total_blocks(ctx) == 6);
+	talloc_free(ctx);
 	return 0;
 }
 
diff --git a/tests/subscr/bsc_subscr_test.c b/tests/subscr/bsc_subscr_test.c
index d15c114..3c94b86 100644
--- a/tests/subscr/bsc_subscr_test.c
+++ b/tests/subscr/bsc_subscr_test.c
@@ -125,14 +125,15 @@
 
 int main()
 {
+	void *ctx = talloc_named_const(NULL, 0, "bsc_subscr_test");
 	printf("Testing BSC subscriber core code.\n");
-	osmo_init_logging(&log_info);
+	osmo_init_logging2(ctx, &log_info);
 	log_set_print_filename(osmo_stderr_target, 0);
 	log_set_print_timestamp(osmo_stderr_target, 0);
 	log_set_use_color(osmo_stderr_target, 0);
 	log_set_print_category(osmo_stderr_target, 1);
 
-	bsc_subscribers = talloc_zero(NULL, struct llist_head);
+	bsc_subscribers = talloc_zero(ctx, struct llist_head);
 	INIT_LLIST_HEAD(bsc_subscribers);
 
 	test_bsc_subscr();