Navigation



Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • freeDiameterd/main.c

    r1339 r1305  
    3434*********************************************************************************************************/
    3535
    36 #if defined(__GLIBC__)
    37 #define _BSD_SOURCE /* for vsyslog */
    38 #endif
    39 
    4036#include <freeDiameter/freeDiameter-host.h>
    4137#include <freeDiameter/libfdcore.h>
     
    4440#include <getopt.h>
    4541#include <locale.h>
    46 #include <syslog.h>
    47 #include <stdarg.h>
     42
    4843
    4944/* forward declarations */
     
    6156
    6257
    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 
    9358/* freeDiameter starting point */
    9459int main(int argc, char * argv[])
     
    9661        int ret;
    9762        sigset_t sig_all;
    98 
     63       
    9964        /* Block all signals from the current thread and all its future children -- we will catch everything in catch_signals */
    10065        sigfillset(&sig_all);
    10166        ret = pthread_sigmask(SIG_BLOCK, &sig_all, NULL);
    10267        ASSERT(ret == 0);
    103 
     68       
    10469        /* Parse the command-line */
    10570        ret = main_cmdline(argc, argv);
     
    10772                return ret;
    10873        }
    109 
     74       
    11075        /* Initialize the core library */
    11176        ret = fd_core_initialize();
     
    11479                return ret;
    11580        }
    116 
     81       
    11782        /* Set gnutls debug level ? */
    11883        if (gnutls_debug) {
     
    12186                TRACE_DEBUG(INFO, "Enabled GNUTLS debug at level %d", gnutls_debug);
    12287        }
    123 
     88       
    12489        /* Parse the configuration file */
    12590        CHECK_FCT_DO( fd_core_parseconf(conffile), goto error );
    126 
     91       
    12792        /* Start the servers */
    12893        CHECK_FCT_DO( fd_core_start(), goto error );
    129 
     94       
    13095        /* Allow SIGINT and SIGTERM from this point to terminate the application */
    13196        CHECK_POSIX_DO( pthread_create(&signals_thr, NULL, catch_signals, NULL), goto error );
    132 
     97       
    13398        TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized.");
    13499
    135100        /* Now, just wait for termination */
    136101        CHECK_FCT( fd_core_wait_shutdown_complete() );
    137 
     102       
    138103        /* Just in case it was not the result of a signal, we cancel signals_thr */
    139104        fd_thr_term(&signals_thr);
    140 
     105       
    141106        return 0;
    142 error:
     107error: 
    143108        CHECK_FCT_DO( fd_core_shutdown(),  );
    144109        CHECK_FCT( fd_core_wait_shutdown_complete() );
     
    173138                "  -V, --version          Print version and exit\n"
    174139                "  -c, --config=filename  Read configuration from this file instead of the \n"
    175                 "                           default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ").\n"
    176                 "  -s, --syslog            Write log output to syslog (instead of stdout)\n");
     140                "                           default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ").\n");
    177141        printf( "\nDebug:\n"
    178142                "  These options are mostly useful for developers\n"
     
    192156        int option_index = 0;
    193157        char * locale;
    194 
     158       
    195159        struct option long_options[] = {
    196160                { "help",       no_argument,            NULL, 'h' },
    197161                { "version",    no_argument,            NULL, 'V' },
    198162                { "config",     required_argument,      NULL, 'c' },
    199                 { "syslog",     no_argument,            NULL, 's' },
    200163                { "debug",      no_argument,            NULL, 'd' },
    201164                { "quiet",      no_argument,            NULL, 'q' },
     
    206169                { NULL,         0,                      NULL, 0 }
    207170        };
    208 
     171       
    209172        /* Loop on arguments */
    210173        while (1) {
    211                 c = getopt_long (argc, argv, "hVc:dql:f:F:g:s", long_options, &option_index);
    212                 if (c == -1)
     174                c = getopt_long (argc, argv, "hVc:dql:f:F:g:", long_options, &option_index);
     175                if (c == -1) 
    213176                        break;  /* Exit from the loop.  */
    214 
     177               
    215178                switch (c) {
    216179                        case 'h':       /* Print help and exit.  */
     
    237200                                }
    238201                                break;
    239 
     202                               
    240203                        case 'd':       /* Increase verbosity of debug messages.  */
    241204                                fd_g_debug_lvl--;
    242205                                break;
    243 
     206                               
    244207                        case 'f':       /* Full debug for the function with this name.  */
    245208                                #ifdef DEBUG
     
    251214                                #endif /* DEBUG */
    252215                                break;
    253 
     216                               
    254217                        case 'F':       /* Full debug for the file with this name.  */
    255218                                #ifdef DEBUG
     
    261224                                #endif /* DEBUG */
    262225                                break;
    263 
     226                               
    264227                        case 'g':       /* Set a debug level and function for GNU TLS calls.  */
    265228                                gnutls_debug = (int)atoi(optarg);
    266229                                break;
    267 
     230                               
    268231                        case 'q':       /* Decrease verbosity then remove debug messages.  */
    269232                                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                                 }
    277233                                break;
    278234
     
    288244                }
    289245        }
    290 
     246               
    291247        return 0;
    292248}
     
    297253        sigset_t ss;
    298254        fd_log_threadname ( "signals catcher" );
    299 
     255       
    300256        sigemptyset(&ss);
    301 
     257       
    302258        /* Signals that terminate the daemon */
    303259        sigaddset(&ss, SIGTERM);
    304260        sigaddset(&ss, SIGINT);
    305 
     261       
    306262        /* Signals that send an event */
    307263        sigaddset(&ss, SIGUSR1);
    308264        sigaddset(&ss, SIGUSR2);
    309 
     265       
    310266        /* We unblock all other signals, so that their default handler is used (such as SIGTSTP) */
    311267        CHECK_SYS_DO( pthread_sigmask( SIG_SETMASK, &ss, NULL ), goto out );
    312 
     268       
    313269        /* Now loop on the reception of the signal */
    314270        while (1) {
    315271                int sig, *ps;
    316 
     272               
    317273                /* Wait to receive the next signal */
    318274                CHECK_POSIX_DO( sigwait(&ss, &sig), break );
    319 
     275               
    320276                TRACE_DEBUG(FULL, "Signal %d caught", sig);
    321 
     277               
    322278                switch (sig) {
    323279                        case SIGUSR1:
     
    327283                                CHECK_FCT_DO( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TRIGGER, sizeof(int), ps), goto out );
    328284                                break;
    329 
     285                               
    330286                        case SIGINT:
    331287                        case SIGTERM:
     
    334290                }
    335291        }
    336 out:
     292out:   
    337293        /* Better way to handle this ? */
    338294        ASSERT(0);
Note: See TracChangeset for help on using the changeset viewer.