msc_main: close SMS db on startup error

When the SMS sqlite db is opened and not closed properly, sqlite will
print a trace on the next OsmoMSC startup while restoring the database.
This happens when e.g. attempting to bind OsmoMSC on an IP that is not
available (yet) and then restarting OsmoMSC.

db.c:521 Init database connection to 'sms.db' using SQLite3 lib version 3.34.1
db.c:318 SQLITE3: (283) recovered 37 frames from WAL file /var/lib/osmocom/sms.db-wal
backtrace.c:42 backtrace() returned 22 addresses
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0x36a56) [0x7f1518c00a56]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(sqlite3_log+0x9e) [0x7f1518c00b3e]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0x5f4f4) [0x7f1518c294f4]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0x5fbb3) [0x7f1518c29bb3]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0x7ee02) [0x7f1518c48e02]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0x7f908) [0x7f1518c49908]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xb9a5f) [0x7f1518c83a5f]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xcddac) [0x7f1518c97dac]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xcddef) [0x7f1518c97def]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xf537d) [0x7f1518cbf37d]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xb479e) [0x7f1518c7e79e]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xb79b6) [0x7f1518c819b6]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xb8116) [0x7f1518c82116]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(+0xb853f) [0x7f1518c8253f]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(sqlite3_prepare_v2+0x16) [0x7f1518c826a6]
backtrace.c:53         /lib/x86_64-linux-gnu/libsqlite3.so.0(sqlite3_exec+0xb4) [0x7f1518c8fce4]
backtrace.c:53         /usr/bin/osmo-msc(+0x1bf13) [0x564f81946f13]
backtrace.c:53         /usr/bin/osmo-msc(+0x524c0) [0x564f8197d4c0]
backtrace.c:53         /usr/bin/osmo-msc(+0x1324e) [0x564f8193e24e]
backtrace.c:53         /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xea) [0x7f1518991d0a]
backtrace.c:53         /usr/bin/osmo-msc(+0x13fea) [0x564f8193efea]

Related: SYS#6360
Change-Id: I9bb799048db5fcdb2a2520107bd75d5f7a865459
diff --git a/src/osmo-msc/msc_main.c b/src/osmo-msc/msc_main.c
index 7f60ea8..a742771 100644
--- a/src/osmo-msc/msc_main.c
+++ b/src/osmo-msc/msc_main.c
@@ -594,6 +594,7 @@
 int main(int argc, char **argv)
 {
 	int rc;
+	int ret = 0;
 
 	struct osmo_sccp_instance *sccp_a;
 	struct osmo_sccp_instance *sccp_iu;
@@ -745,20 +746,26 @@
 	osmo_init_ignore_signals();
 
 	/* start the SMS queue */
-	if (sms_queue_start(msc_network) != 0)
-		return -1;
+	if (sms_queue_start(msc_network) != 0) {
+		ret = -1;
+		goto error;
+	}
 
-	if (msc_mgw_setup() != 0)
-		return 7;
+	if (msc_mgw_setup() != 0) {
+		ret = 7;
+		goto error;
+	}
 
 	if (ss7_setup(tall_msc_ctx, &sccp_a, &sccp_iu)) {
 		fprintf(stderr, "Setting up SCCP client failed.\n");
-		return 8;
+		ret = 8;
+		goto error;
 	}
 
 	if (sgs_server_open(g_sgs)) {
 		fprintf(stderr, "Starting SGs server failed\n");
-		return 9;
+		ret = 9;
+		goto error;
 	}
 
 	msc_network->a.sri = sccp_ran_init(msc_network, sccp_a, OSMO_SCCP_SSN_BSSAP,
@@ -766,7 +773,8 @@
 					   msc_network);
 	if (!msc_network->a.sri) {
 		fprintf(stderr, "Setting up A receiver failed\n");
-		return 10;
+		ret = 10;
+		goto error;
 	}
 	LOGP(DMSC, LOGL_NOTICE, "A-interface: SCCP user %s, cs7-instance %u (%s)\n",
 	     osmo_sccp_user_name(msc_network->a.sri->scu),
@@ -781,7 +789,8 @@
 					   msc_network);
 	if (!msc_network->iu.sri) {
 		fprintf(stderr, "Setting up IuCS receiver failed\n");
-		return 11;
+		ret = 11;
+		goto error;
 	}
 
 	/* Compatibility with legacy osmo-hnbgw that was unable to properly handle RESET messages. */
@@ -800,7 +809,8 @@
 		rc = osmo_daemonize();
 		if (rc < 0) {
 			perror("Error during daemonize");
-			return 6;
+			ret = 6;
+			goto error;
 		}
 	}
 
@@ -823,6 +833,7 @@
 		}
 	} while (!osmo_select_shutdown_done());
 
+error:
 	db_fini();
 	log_fini();
 
@@ -842,5 +853,5 @@
 	 */
 	talloc_report_full(NULL, stderr);
 	talloc_disable_null_tracking();
-	return 0;
+	return ret;
 }