Changeset 1339:e70ce64ce689 in freeDiameter for freeDiameterd
- Timestamp:
- Apr 9, 2019, 10:49:17 PM (5 years ago)
- Branch:
- default
- Phase:
- public
- histedit_source:
- 0ba425523160eed16c7d7b64279d053a70caf1de
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
freeDiameterd/main.c
r1305 r1339 34 34 *********************************************************************************************************/ 35 35 36 #if defined(__GLIBC__) 37 #define _BSD_SOURCE /* for vsyslog */ 38 #endif 39 36 40 #include <freeDiameter/freeDiameter-host.h> 37 41 #include <freeDiameter/libfdcore.h> … … 40 44 #include <getopt.h> 41 45 #include <locale.h> 42 46 #include <syslog.h> 47 #include <stdarg.h> 43 48 44 49 /* forward declarations */ … … 56 61 57 62 63 static void syslog_logger(int loglevel, const char * format, va_list args) 64 { 65 int level; 66 67 switch (loglevel) { 68 case FD_LOG_NOTICE: 69 level = LOG_NOTICE; 70 break; 71 case FD_LOG_ERROR: 72 level = LOG_ERR; 73 break; 74 case FD_LOG_FATAL: 75 level = LOG_CRIT; 76 break; 77 default: 78 /* fallthrough */ 79 case FD_LOG_DEBUG: 80 /* some systems log LOG_DEBUG to a file; but 81 * freeDiameter debug output is too verbose */ 82 return; 83 #if 0 84 level = LOG_DEBUG; 85 break; 86 #endif 87 } 88 89 vsyslog(level, format, args); 90 } 91 92 58 93 /* freeDiameter starting point */ 59 94 int main(int argc, char * argv[]) … … 61 96 int ret; 62 97 sigset_t sig_all; 63 98 64 99 /* Block all signals from the current thread and all its future children -- we will catch everything in catch_signals */ 65 100 sigfillset(&sig_all); 66 101 ret = pthread_sigmask(SIG_BLOCK, &sig_all, NULL); 67 102 ASSERT(ret == 0); 68 103 69 104 /* Parse the command-line */ 70 105 ret = main_cmdline(argc, argv); … … 72 107 return ret; 73 108 } 74 109 75 110 /* Initialize the core library */ 76 111 ret = fd_core_initialize(); … … 79 114 return ret; 80 115 } 81 116 82 117 /* Set gnutls debug level ? */ 83 118 if (gnutls_debug) { … … 86 121 TRACE_DEBUG(INFO, "Enabled GNUTLS debug at level %d", gnutls_debug); 87 122 } 88 123 89 124 /* Parse the configuration file */ 90 125 CHECK_FCT_DO( fd_core_parseconf(conffile), goto error ); 91 126 92 127 /* Start the servers */ 93 128 CHECK_FCT_DO( fd_core_start(), goto error ); 94 129 95 130 /* Allow SIGINT and SIGTERM from this point to terminate the application */ 96 131 CHECK_POSIX_DO( pthread_create(&signals_thr, NULL, catch_signals, NULL), goto error ); 97 132 98 133 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized."); 99 134 100 135 /* Now, just wait for termination */ 101 136 CHECK_FCT( fd_core_wait_shutdown_complete() ); 102 137 103 138 /* Just in case it was not the result of a signal, we cancel signals_thr */ 104 139 fd_thr_term(&signals_thr); 105 140 106 141 return 0; 107 error: 142 error: 108 143 CHECK_FCT_DO( fd_core_shutdown(), ); 109 144 CHECK_FCT( fd_core_wait_shutdown_complete() ); … … 138 173 " -V, --version Print version and exit\n" 139 174 " -c, --config=filename Read configuration from this file instead of the \n" 140 " default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ").\n"); 175 " default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ").\n" 176 " -s, --syslog Write log output to syslog (instead of stdout)\n"); 141 177 printf( "\nDebug:\n" 142 178 " These options are mostly useful for developers\n" … … 156 192 int option_index = 0; 157 193 char * locale; 158 194 159 195 struct option long_options[] = { 160 196 { "help", no_argument, NULL, 'h' }, 161 197 { "version", no_argument, NULL, 'V' }, 162 198 { "config", required_argument, NULL, 'c' }, 199 { "syslog", no_argument, NULL, 's' }, 163 200 { "debug", no_argument, NULL, 'd' }, 164 201 { "quiet", no_argument, NULL, 'q' }, … … 169 206 { NULL, 0, NULL, 0 } 170 207 }; 171 208 172 209 /* Loop on arguments */ 173 210 while (1) { 174 c = getopt_long (argc, argv, "hVc:dql:f:F:g: ", long_options, &option_index);175 if (c == -1) 211 c = getopt_long (argc, argv, "hVc:dql:f:F:g:s", long_options, &option_index); 212 if (c == -1) 176 213 break; /* Exit from the loop. */ 177 214 178 215 switch (c) { 179 216 case 'h': /* Print help and exit. */ … … 200 237 } 201 238 break; 202 239 203 240 case 'd': /* Increase verbosity of debug messages. */ 204 241 fd_g_debug_lvl--; 205 242 break; 206 243 207 244 case 'f': /* Full debug for the function with this name. */ 208 245 #ifdef DEBUG … … 214 251 #endif /* DEBUG */ 215 252 break; 216 253 217 254 case 'F': /* Full debug for the file with this name. */ 218 255 #ifdef DEBUG … … 224 261 #endif /* DEBUG */ 225 262 break; 226 263 227 264 case 'g': /* Set a debug level and function for GNU TLS calls. */ 228 265 gnutls_debug = (int)atoi(optarg); 229 266 break; 230 267 231 268 case 'q': /* Decrease verbosity then remove debug messages. */ 232 269 fd_g_debug_lvl++; 270 break; 271 272 case 's': /* Write log data using syslog(3) */ 273 if (fd_log_handler_register(syslog_logger) != 0) { 274 fprintf(stderr, "Cannot initialize syslog logger\n"); 275 return EINVAL; 276 } 233 277 break; 234 278 … … 244 288 } 245 289 } 246 290 247 291 return 0; 248 292 } … … 253 297 sigset_t ss; 254 298 fd_log_threadname ( "signals catcher" ); 255 299 256 300 sigemptyset(&ss); 257 301 258 302 /* Signals that terminate the daemon */ 259 303 sigaddset(&ss, SIGTERM); 260 304 sigaddset(&ss, SIGINT); 261 305 262 306 /* Signals that send an event */ 263 307 sigaddset(&ss, SIGUSR1); 264 308 sigaddset(&ss, SIGUSR2); 265 309 266 310 /* We unblock all other signals, so that their default handler is used (such as SIGTSTP) */ 267 311 CHECK_SYS_DO( pthread_sigmask( SIG_SETMASK, &ss, NULL ), goto out ); 268 312 269 313 /* Now loop on the reception of the signal */ 270 314 while (1) { 271 315 int sig, *ps; 272 316 273 317 /* Wait to receive the next signal */ 274 318 CHECK_POSIX_DO( sigwait(&ss, &sig), break ); 275 319 276 320 TRACE_DEBUG(FULL, "Signal %d caught", sig); 277 321 278 322 switch (sig) { 279 323 case SIGUSR1: … … 283 327 CHECK_FCT_DO( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TRIGGER, sizeof(int), ps), goto out ); 284 328 break; 285 329 286 330 case SIGINT: 287 331 case SIGTERM: … … 290 334 } 291 335 } 292 out: 336 out: 293 337 /* Better way to handle this ? */ 294 338 ASSERT(0);
Note: See TracChangeset
for help on using the changeset viewer.