comparison freeDiameterd/main.c @ 1389:de90cf7f381e

freeDiameterd: add background (daemon) mode
author Thomas Klausner <tk@giga.or.at>
date Tue, 15 Oct 2019 16:26:23 +0200
parents e70ce64ce689
children d4a4ab5239c7
comparison
equal deleted inserted replaced
1388:61e693afccc6 1389:de90cf7f381e
43 #include <signal.h> 43 #include <signal.h>
44 #include <getopt.h> 44 #include <getopt.h>
45 #include <locale.h> 45 #include <locale.h>
46 #include <syslog.h> 46 #include <syslog.h>
47 #include <stdarg.h> 47 #include <stdarg.h>
48 #include <stdlib.h>
49 #include <unistd.h>
48 50
49 /* forward declarations */ 51 /* forward declarations */
50 static int main_cmdline(int argc, char *argv[]); 52 static int main_cmdline(int argc, char *argv[]);
51 static void * catch_signals(void * arg); 53 static void * catch_signals(void * arg);
52 static pthread_t signals_thr; 54 static pthread_t signals_thr;
53 55
54 static char *conffile = NULL; 56 static char *conffile = NULL;
57 static int daemon_mode = 0;
55 static int gnutls_debug = 0; 58 static int gnutls_debug = 0;
56 59
57 /* gnutls debug */ 60 /* gnutls debug */
58 static void fd_gnutls_debug(int level, const char * str) { 61 static void fd_gnutls_debug(int level, const char * str) {
59 fd_log_debug(" [gnutls:%d] %s", level, str); 62 fd_log_debug(" [gnutls:%d] %s", level, str);
105 ret = main_cmdline(argc, argv); 108 ret = main_cmdline(argc, argv);
106 if (ret != 0) { 109 if (ret != 0) {
107 return ret; 110 return ret;
108 } 111 }
109 112
113 if (daemon_mode) {
114 TRACE_DEBUG(INFO, "entering background mode");
115 CHECK_POSIX_DO( daemon(1, 0), goto error );
116 }
117
110 /* Initialize the core library */ 118 /* Initialize the core library */
111 ret = fd_core_initialize(); 119 ret = fd_core_initialize();
112 if (ret != 0) { 120 if (ret != 0) {
113 fprintf(stderr, "An error occurred during freeDiameter core library initialization.\n"); 121 fprintf(stderr, "An error occurred during freeDiameter core library initialization.\n");
114 return ret; 122 return ret;
167 { 175 {
168 main_version_core(); 176 main_version_core();
169 printf( " This daemon is an implementation of the Diameter protocol\n" 177 printf( " This daemon is an implementation of the Diameter protocol\n"
170 " used for Authentication, Authorization, and Accounting (AAA).\n"); 178 " used for Authentication, Authorization, and Accounting (AAA).\n");
171 printf("\nUsage: " FD_PROJECT_BINARY " [OPTIONS]...\n"); 179 printf("\nUsage: " FD_PROJECT_BINARY " [OPTIONS]...\n");
172 printf( " -h, --help Print help and exit\n" 180 printf( " -h, --help Print help and exit\n"
173 " -V, --version Print version and exit\n" 181 " -V, --version Print version and exit\n"
174 " -c, --config=filename Read configuration from this file instead of the \n" 182 " -c, --config=filename Read configuration from this file instead of the \n"
175 " default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ").\n" 183 " default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ")\n"
184 " -D, --daemon Start program in background\n"
176 " -s, --syslog Write log output to syslog (instead of stdout)\n"); 185 " -s, --syslog Write log output to syslog (instead of stdout)\n");
177 printf( "\nDebug:\n" 186 printf( "\nDebug:\n"
178 " These options are mostly useful for developers\n" 187 " These options are mostly useful for developers\n"
179 " -l, --dbglocale Set the locale for error messages\n"
180 " -d, --debug Increase verbosity of debug messages if default logger is used\n" 188 " -d, --debug Increase verbosity of debug messages if default logger is used\n"
181 " -q, --quiet Decrease verbosity if default logger is used\n" 189 " --dbg_gnutls <int> Enable GNU TLS debug at level <int>\n"
182 " -f, --dbg_func <func> Enable all traces within the function <func>\n" 190 " -f, --dbg_func <func> Enable all traces within the function <func>\n"
183 " -F, --dbg_file <file.c> Enable all traces within the file <file.c> (basename match)\n" 191 " -F, --dbg_file <file.c> Enable all traces within the file <file.c> (basename match)\n"
184 " --dbg_gnutls <int> Enable GNU TLS debug at level <int>\n" 192 " -l, --dbglocale Set the locale for error messages\n"
193 " -q, --quiet Decrease verbosity if default logger is used\n"
185 ); 194 );
186 } 195 }
187 196
188 /* Parse the command-line */ 197 /* Parse the command-line */
189 static int main_cmdline(int argc, char *argv[]) 198 static int main_cmdline(int argc, char *argv[])
195 struct option long_options[] = { 204 struct option long_options[] = {
196 { "help", no_argument, NULL, 'h' }, 205 { "help", no_argument, NULL, 'h' },
197 { "version", no_argument, NULL, 'V' }, 206 { "version", no_argument, NULL, 'V' },
198 { "config", required_argument, NULL, 'c' }, 207 { "config", required_argument, NULL, 'c' },
199 { "syslog", no_argument, NULL, 's' }, 208 { "syslog", no_argument, NULL, 's' },
209 { "daemon", no_argument, NULL, 'D' },
200 { "debug", no_argument, NULL, 'd' }, 210 { "debug", no_argument, NULL, 'd' },
201 { "quiet", no_argument, NULL, 'q' }, 211 { "quiet", no_argument, NULL, 'q' },
202 { "dbglocale", optional_argument, NULL, 'l' }, 212 { "dbglocale", optional_argument, NULL, 'l' },
203 { "dbg_func", required_argument, NULL, 'f' }, 213 { "dbg_func", required_argument, NULL, 'f' },
204 { "dbg_file", required_argument, NULL, 'F' }, 214 { "dbg_file", required_argument, NULL, 'F' },
206 { NULL, 0, NULL, 0 } 216 { NULL, 0, NULL, 0 }
207 }; 217 };
208 218
209 /* Loop on arguments */ 219 /* Loop on arguments */
210 while (1) { 220 while (1) {
211 c = getopt_long (argc, argv, "hVc:dql:f:F:g:s", long_options, &option_index); 221 c = getopt_long (argc, argv, "hVc:Ddql:f:F:g:s", long_options, &option_index);
212 if (c == -1) 222 if (c == -1)
213 break; /* Exit from the loop. */ 223 break; /* Exit from the loop. */
214 224
215 switch (c) { 225 switch (c) {
216 case 'h': /* Print help and exit. */ 226 case 'h': /* Print help and exit. */
225 if (optarg == NULL ) { 235 if (optarg == NULL ) {
226 fprintf(stderr, "Missing argument with --config directive\n"); 236 fprintf(stderr, "Missing argument with --config directive\n");
227 return EINVAL; 237 return EINVAL;
228 } 238 }
229 conffile = optarg; 239 conffile = optarg;
240 break;
241
242 case 'D':
243 daemon_mode = 1;
230 break; 244 break;
231 245
232 case 'l': /* Change the locale. */ 246 case 'l': /* Change the locale. */
233 locale = setlocale(LC_ALL, optarg?:""); 247 locale = setlocale(LC_ALL, optarg?:"");
234 if (!locale) { 248 if (!locale) {
"Welcome to our mercurial repository"