Introduce '-D' commandline option to daemonize processes

This uses the osmo_daemonize() function of libosmocore >= 0.1.18,
and is now implemented for bac_nat, osmo-bsc, bsc_hack, osmo-gbproxy
and bsc_mgcp.  This means only osmo-sgsn is missing, which currently
has no option parsing at all.
diff --git a/openbsc/src/nat/bsc_nat.c b/openbsc/src/nat/bsc_nat.c
index e029b11..997a57f 100644
--- a/openbsc/src/nat/bsc_nat.c
+++ b/openbsc/src/nat/bsc_nat.c
@@ -46,6 +46,7 @@
 
 #include <osmocore/gsm0808.h>
 #include <osmocore/talloc.h>
+#include <osmocore/process.h>
 
 #include <osmocore/protocol/gsm_08_08.h>
 
@@ -65,6 +66,7 @@
 static struct bsc_fd bsc_listen;
 static const char *msc_ip = NULL;
 static struct timer_list sccp_close;
+static int daemonize = 0;
 
 const char *openbsc_copyright =
 	"Copyright (C) 2010 Holger Hans Peter Freyther and On-Waves\n"
@@ -994,6 +996,7 @@
 	printf("  Some useful help...\n");
 	printf("  -h --help this text\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\n");
 	printf("  -c --config-file filename The config file to use.\n");
 	printf("  -m --msc=IP. The address of the MSC.\n");
@@ -1106,8 +1109,9 @@
 
 int main(int argc, char** argv)
 {
-	talloc_init_ctx();
+	int rc;
 
+	talloc_init_ctx();
 
 	log_init(&log_info);
 	stderr_target = log_target_create_stderr();
@@ -1182,6 +1186,14 @@
 	signal(SIGUSR1, &signal_handler);
 	signal(SIGPIPE, SIG_IGN);
 
+	if (daemonize) {
+		rc = osmo_daemonize();
+		if (rc < 0) {
+			perror("Error during daemonize");
+			exit(1);
+		}
+	}
+
 	/* recycle timer */
 	sccp_set_log_area(DSCCP);
 	sccp_close.cb = sccp_close_unconfirmed;