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/bsc/osmo_bsc_main.c b/openbsc/src/bsc/osmo_bsc_main.c
index 60b5225..7ddf91d 100644
--- a/openbsc/src/bsc/osmo_bsc_main.c
+++ b/openbsc/src/bsc/osmo_bsc_main.c
@@ -25,6 +25,7 @@
 
 #include <osmocom/vty/command.h>
 #include <osmocore/talloc.h>
+#include <osmocore/process.h>
 
 #include <osmocom/sccp/sccp.h>
 
@@ -41,6 +42,7 @@
 struct gsm_network *bsc_gsmnet = 0;
 static const char *config_file = "openbsc.cfg";
 static const char *rf_ctl = NULL;
+static int daemonize = 0;
 
 extern void bsc_vty_init(void);
 extern int bsc_bootstrap_network(int (*layer4)(struct gsm_network *, int, void *), const char *cfg_file);
@@ -54,6 +56,7 @@
 {
 	printf("  Some useful help...\n");
 	printf("  -h --help this text\n");
+	printf("  -D --daemonize Fork the process into a background daemon\n");
 	printf("  -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
 	printf("  -s --disable-color\n");
 	printf("  -T --timestamp. Print a timestamp in the debug output.\n");
@@ -71,6 +74,7 @@
 		static 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'},
@@ -82,7 +86,7 @@
 			{0, 0, 0, 0}
 		};
 
-		c = getopt_long(argc, argv, "hd:sTc:e:r:t",
+		c = getopt_long(argc, argv, "hd:DsTc:e:r:t",
 				long_options, &option_index);
 		if (c == -1)
 			break;
@@ -98,6 +102,9 @@
 		case 'd':
 			log_parse_category_mask(stderr_target, optarg);
 			break;
+		case 'D':
+			daemonize = 1;
+			break;
 		case 'c':
 			config_file = strdup(optarg);
 			break;
@@ -179,6 +186,13 @@
 		}
 	}
 
+	if (daemonize) {
+		rc = osmo_daemonize();
+		if (rc < 0) {
+			perror("Error during daemonize");
+			exit(1);
+		}
+	}
 
 	while (1) {
 		bsc_select_main(0);
diff --git a/openbsc/src/bsc_hack.c b/openbsc/src/bsc_hack.c
index 45156f3..4e8309b 100644
--- a/openbsc/src/bsc_hack.c
+++ b/openbsc/src/bsc_hack.c
@@ -32,6 +32,7 @@
 
 #include <openbsc/db.h>
 #include <osmocore/select.h>
+#include <osmocore/process.h>
 #include <openbsc/debug.h>
 #include <openbsc/e1_input.h>
 #include <osmocore/talloc.h>
@@ -48,6 +49,7 @@
 static const char *database_name = "hlr.sqlite3";
 static const char *config_file = "openbsc.cfg";
 extern const char *openbsc_copyright;
+static int daemonize = 0;
 
 /* timer to store statistics */
 #define DB_SYNC_INTERVAL	60, 0
@@ -80,6 +82,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("  -c --config-file filename The config file to use.\n");
 	printf("  -s --disable-color\n");
 	printf("  -l --database db-name The database to use\n");
@@ -98,6 +101,7 @@
 		static 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'},
 			{"database", 1, 0, 'l'},
@@ -110,7 +114,7 @@
 			{0, 0, 0, 0}
 		};
 
-		c = getopt_long(argc, argv, "hd:sl:ar:p:TPVc:e:",
+		c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:",
 				long_options, &option_index);
 		if (c == -1)
 			break;
@@ -126,6 +130,9 @@
 		case 'd':
 			log_parse_category_mask(stderr_target, optarg);
 			break;
+		case 'D':
+			daemonize = 1;
+			break;
 		case 'l':
 			database_name = strdup(optarg);
 			break;
@@ -267,6 +274,14 @@
 	signal(SIGUSR2, &signal_handler);
 	signal(SIGPIPE, SIG_IGN);
 
+	if (daemonize) {
+		rc = osmo_daemonize();
+		if (rc < 0) {
+			perror("Error during daemonize");
+			exit(1);
+		}
+	}
+
 	while (1) {
 		bsc_upqueue(bsc_gsmnet);
 		log_reset_context();
diff --git a/openbsc/src/gprs/gb_proxy_main.c b/openbsc/src/gprs/gb_proxy_main.c
index 32bbe38..8ce83f1 100644
--- a/openbsc/src/gprs/gb_proxy_main.c
+++ b/openbsc/src/gprs/gb_proxy_main.c
@@ -37,6 +37,7 @@
 #include <osmocore/talloc.h>
 #include <osmocore/select.h>
 #include <osmocore/rate_ctr.h>
+#include <osmocore/process.h>
 
 #include <openbsc/signal.h>
 #include <openbsc/debug.h>
@@ -67,6 +68,7 @@
 static struct log_target *stderr_target;
 static char *config_file = "osmo_gbproxy.cfg";
 struct gbproxy_config gbcfg;
+static int daemonize = 0;
 
 /* Pointer to the SGSN peer */
 extern struct gbprox_peer *gbprox_peer_sgsn;
@@ -126,6 +128,7 @@
 	printf("  Some useful help...\n");
 	printf("  -h --help this text\n");
 	printf("  -d option --debug=DNS:DGPRS,0:0 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");
@@ -140,6 +143,7 @@
 		static 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' },
@@ -148,7 +152,7 @@
 			{ 0, 0, 0, 0 }
 		};
 
-		c = getopt_long(argc, argv, "hd:c:sTVe:",
+		c = getopt_long(argc, argv, "hd:Dc:sTVe:",
 				long_options, &option_index);
 		if (c == -1)
 			break;
@@ -164,6 +168,9 @@
 		case 'd':
 			log_parse_category_mask(stderr_target, optarg);
 			break;
+		case 'D':
+			daemonize = 1;
+			break;
 		case 'c':
 			config_file = strdup(optarg);
 			break;
@@ -260,6 +267,14 @@
 		exit(2);
 	}
 
+	if (daemonize) {
+		rc = osmo_daemonize();
+		if (rc < 0) {
+			perror("Error during daemonize");
+			exit(1);
+		}
+	}
+
 	/* Reset all the persistent NS-VCs that we've read from the config */
 	gbprox_reset_persistent_nsvcs(bssgp_nsi);
 
diff --git a/openbsc/src/mgcp/mgcp_main.c b/openbsc/src/mgcp/mgcp_main.c
index 76f4667..21968aa 100644
--- a/openbsc/src/mgcp/mgcp_main.c
+++ b/openbsc/src/mgcp/mgcp_main.c
@@ -35,6 +35,7 @@
 #include <openbsc/debug.h>
 #include <osmocore/msgb.h>
 #include <osmocore/talloc.h>
+#include <osmocore/process.h>
 #include <openbsc/gsm_data.h>
 #include <osmocore/select.h>
 #include <openbsc/mgcp.h>
@@ -57,6 +58,7 @@
 static struct bsc_fd bfd;
 static struct mgcp_config *cfg;
 static int reset_endpoints = 0;
+static int daemonize = 0;
 
 const char *openbsc_copyright =
 	"Copyright (C) 2009-2010 Holger Freyther and On-Waves\n"
@@ -85,11 +87,12 @@
 		static struct option long_options[] = {
 			{"help", 0, 0, 'h'},
 			{"config-file", 1, 0, 'c'},
+			{"daemonize", 0, 0, 'D'},
 			{"version", 0, 0, 'V'},
 			{0, 0, 0, 0},
 		};
 
-		c = getopt_long(argc, argv, "hc:V", long_options, &option_index);
+		c = getopt_long(argc, argv, "hc:VD", long_options, &option_index);
 
 		if (c == -1)
 			break;
@@ -106,6 +109,9 @@
 			print_version(1);
 			exit(0);
 			break;
+		case 'D':
+			daemonize = 1;
+			break;
 		default:
 			/* ignore */
 			break;
@@ -259,6 +265,14 @@
 	/* initialisation */
 	srand(time(NULL));
 
+	if (daemonize) {
+		rc = osmo_daemonize();
+		if (rc < 0) {
+			perror("Error during daemonize");
+			exit(1);
+		}
+	}
+
 	/* main loop */
 	while (1) {
 		bsc_select_main(0);
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;