changeset 1389:de90cf7f381e

freeDiameterd: add background (daemon) mode
author Thomas Klausner <tk@giga.or.at>
date Tue, 15 Oct 2019 16:26:23 +0200
parents 61e693afccc6
children 46656b52ae97
files freeDiameterd/main.c
diffstat 1 files changed, 22 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/freeDiameterd/main.c	Tue Oct 15 16:26:03 2019 +0200
+++ b/freeDiameterd/main.c	Tue Oct 15 16:26:23 2019 +0200
@@ -45,6 +45,8 @@
 #include <locale.h>
 #include <syslog.h>
 #include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
 
 /* forward declarations */
 static int main_cmdline(int argc, char *argv[]);
@@ -52,6 +54,7 @@
 static pthread_t signals_thr;
 
 static char *conffile = NULL;
+static int daemon_mode = 0;
 static int gnutls_debug = 0;
 
 /* gnutls debug */
@@ -107,6 +110,11 @@
 		return ret;
 	}
 
+	if (daemon_mode) {
+		TRACE_DEBUG(INFO, "entering background mode");
+		CHECK_POSIX_DO( daemon(1, 0), goto error );
+	}
+
 	/* Initialize the core library */
 	ret = fd_core_initialize();
 	if (ret != 0) {
@@ -169,19 +177,20 @@
 	printf(	"  This daemon is an implementation of the Diameter protocol\n"
 		"  used for Authentication, Authorization, and Accounting (AAA).\n");
 	printf("\nUsage:  " FD_PROJECT_BINARY " [OPTIONS]...\n");
-	printf( "  -h, --help             Print help and exit\n"
-  		"  -V, --version          Print version and exit\n"
-  		"  -c, --config=filename  Read configuration from this file instead of the \n"
-		"                           default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ").\n"
+	printf( "  -h, --help              Print help and exit\n"
+  		"  -V, --version           Print version and exit\n"
+  		"  -c, --config=filename   Read configuration from this file instead of the \n"
+		"                          default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ")\n"
+		"  -D, --daemon            Start program in background\n"
 		"  -s, --syslog            Write log output to syslog (instead of stdout)\n");
  	printf( "\nDebug:\n"
   		"  These options are mostly useful for developers\n"
-  		"  -l, --dbglocale         Set the locale for error messages\n"
   		"  -d, --debug             Increase verbosity of debug messages if default logger is used\n"
-  		"  -q, --quiet             Decrease verbosity if default logger is used\n"
+  		"  --dbg_gnutls <int>      Enable GNU TLS debug at level <int>\n"
   		"  -f, --dbg_func <func>   Enable all traces within the function <func>\n"
   		"  -F, --dbg_file <file.c> Enable all traces within the file <file.c> (basename match)\n"
-  		"  --dbg_gnutls <int>      Enable GNU TLS debug at level <int>\n"
+  		"  -l, --dbglocale         Set the locale for error messages\n"
+  		"  -q, --quiet             Decrease verbosity if default logger is used\n"
 	);
 }
 
@@ -197,6 +206,7 @@
 		{ "version",	no_argument, 		NULL, 'V' },
 		{ "config",	required_argument, 	NULL, 'c' },
 		{ "syslog",     no_argument,            NULL, 's' },
+		{ "daemon",	no_argument, 		NULL, 'D' },
 		{ "debug",	no_argument, 		NULL, 'd' },
 		{ "quiet",	no_argument, 		NULL, 'q' },
 		{ "dbglocale",	optional_argument, 	NULL, 'l' },
@@ -208,7 +218,7 @@
 
 	/* Loop on arguments */
 	while (1) {
-		c = getopt_long (argc, argv, "hVc:dql:f:F:g:s", long_options, &option_index);
+		c = getopt_long (argc, argv, "hVc:Ddql:f:F:g:s", long_options, &option_index);
 		if (c == -1)
 			break;	/* Exit from the loop.  */
 
@@ -229,6 +239,10 @@
 				conffile = optarg;
 				break;
 
+			case 'D':
+				daemon_mode = 1;
+				break;
+
 			case 'l':	/* Change the locale.  */
 				locale = setlocale(LC_ALL, optarg?:"");
 				if (!locale) {
"Welcome to our mercurial repository"