# HG changeset patch # User Thomas Klausner # Date 1571149583 -7200 # Node ID de90cf7f381e77501c1f4607a19c06f1bb212c70 # Parent 61e693afccc68422b7c4c6c868453d3cea760eba freeDiameterd: add background (daemon) mode diff -r 61e693afccc6 -r de90cf7f381e freeDiameterd/main.c --- 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 #include #include +#include +#include /* 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 Enable GNU TLS debug at level \n" " -f, --dbg_func Enable all traces within the function \n" " -F, --dbg_file Enable all traces within the file (basename match)\n" - " --dbg_gnutls Enable GNU TLS debug at level \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) {