Navigation


Changeset 1339:e70ce64ce689 in freeDiameter for freeDiameterd


Ignore:
Timestamp:
Apr 9, 2019, 10:49:17 PM (5 years ago)
Author:
Thomas Klausner <tk@giga.or.at>
Branch:
default
Phase:
public
histedit_source:
0ba425523160eed16c7d7b64279d053a70caf1de
Message:

freeDiameterd: support syslog() logging.

Written for Effortel Technologies SA, published with their consent.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • freeDiameterd/main.c

    r1305 r1339  
    3434*********************************************************************************************************/
    3535
     36#if defined(__GLIBC__)
     37#define _BSD_SOURCE /* for vsyslog */
     38#endif
     39
    3640#include <freeDiameter/freeDiameter-host.h>
    3741#include <freeDiameter/libfdcore.h>
     
    4044#include <getopt.h>
    4145#include <locale.h>
    42 
     46#include <syslog.h>
     47#include <stdarg.h>
    4348
    4449/* forward declarations */
     
    5661
    5762
     63static 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
    5893/* freeDiameter starting point */
    5994int main(int argc, char * argv[])
     
    6196        int ret;
    6297        sigset_t sig_all;
    63        
     98
    6499        /* Block all signals from the current thread and all its future children -- we will catch everything in catch_signals */
    65100        sigfillset(&sig_all);
    66101        ret = pthread_sigmask(SIG_BLOCK, &sig_all, NULL);
    67102        ASSERT(ret == 0);
    68        
     103
    69104        /* Parse the command-line */
    70105        ret = main_cmdline(argc, argv);
     
    72107                return ret;
    73108        }
    74        
     109
    75110        /* Initialize the core library */
    76111        ret = fd_core_initialize();
     
    79114                return ret;
    80115        }
    81        
     116
    82117        /* Set gnutls debug level ? */
    83118        if (gnutls_debug) {
     
    86121                TRACE_DEBUG(INFO, "Enabled GNUTLS debug at level %d", gnutls_debug);
    87122        }
    88        
     123
    89124        /* Parse the configuration file */
    90125        CHECK_FCT_DO( fd_core_parseconf(conffile), goto error );
    91        
     126
    92127        /* Start the servers */
    93128        CHECK_FCT_DO( fd_core_start(), goto error );
    94        
     129
    95130        /* Allow SIGINT and SIGTERM from this point to terminate the application */
    96131        CHECK_POSIX_DO( pthread_create(&signals_thr, NULL, catch_signals, NULL), goto error );
    97        
     132
    98133        TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized.");
    99134
    100135        /* Now, just wait for termination */
    101136        CHECK_FCT( fd_core_wait_shutdown_complete() );
    102        
     137
    103138        /* Just in case it was not the result of a signal, we cancel signals_thr */
    104139        fd_thr_term(&signals_thr);
    105        
     140
    106141        return 0;
    107 error: 
     142error:
    108143        CHECK_FCT_DO( fd_core_shutdown(),  );
    109144        CHECK_FCT( fd_core_wait_shutdown_complete() );
     
    138173                "  -V, --version          Print version and exit\n"
    139174                "  -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");
    141177        printf( "\nDebug:\n"
    142178                "  These options are mostly useful for developers\n"
     
    156192        int option_index = 0;
    157193        char * locale;
    158        
     194
    159195        struct option long_options[] = {
    160196                { "help",       no_argument,            NULL, 'h' },
    161197                { "version",    no_argument,            NULL, 'V' },
    162198                { "config",     required_argument,      NULL, 'c' },
     199                { "syslog",     no_argument,            NULL, 's' },
    163200                { "debug",      no_argument,            NULL, 'd' },
    164201                { "quiet",      no_argument,            NULL, 'q' },
     
    169206                { NULL,         0,                      NULL, 0 }
    170207        };
    171        
     208
    172209        /* Loop on arguments */
    173210        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)
    176213                        break;  /* Exit from the loop.  */
    177                
     214
    178215                switch (c) {
    179216                        case 'h':       /* Print help and exit.  */
     
    200237                                }
    201238                                break;
    202                                
     239
    203240                        case 'd':       /* Increase verbosity of debug messages.  */
    204241                                fd_g_debug_lvl--;
    205242                                break;
    206                                
     243
    207244                        case 'f':       /* Full debug for the function with this name.  */
    208245                                #ifdef DEBUG
     
    214251                                #endif /* DEBUG */
    215252                                break;
    216                                
     253
    217254                        case 'F':       /* Full debug for the file with this name.  */
    218255                                #ifdef DEBUG
     
    224261                                #endif /* DEBUG */
    225262                                break;
    226                                
     263
    227264                        case 'g':       /* Set a debug level and function for GNU TLS calls.  */
    228265                                gnutls_debug = (int)atoi(optarg);
    229266                                break;
    230                                
     267
    231268                        case 'q':       /* Decrease verbosity then remove debug messages.  */
    232269                                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                                }
    233277                                break;
    234278
     
    244288                }
    245289        }
    246                
     290
    247291        return 0;
    248292}
     
    253297        sigset_t ss;
    254298        fd_log_threadname ( "signals catcher" );
    255        
     299
    256300        sigemptyset(&ss);
    257        
     301
    258302        /* Signals that terminate the daemon */
    259303        sigaddset(&ss, SIGTERM);
    260304        sigaddset(&ss, SIGINT);
    261        
     305
    262306        /* Signals that send an event */
    263307        sigaddset(&ss, SIGUSR1);
    264308        sigaddset(&ss, SIGUSR2);
    265        
     309
    266310        /* We unblock all other signals, so that their default handler is used (such as SIGTSTP) */
    267311        CHECK_SYS_DO( pthread_sigmask( SIG_SETMASK, &ss, NULL ), goto out );
    268        
     312
    269313        /* Now loop on the reception of the signal */
    270314        while (1) {
    271315                int sig, *ps;
    272                
     316
    273317                /* Wait to receive the next signal */
    274318                CHECK_POSIX_DO( sigwait(&ss, &sig), break );
    275                
     319
    276320                TRACE_DEBUG(FULL, "Signal %d caught", sig);
    277                
     321
    278322                switch (sig) {
    279323                        case SIGUSR1:
     
    283327                                CHECK_FCT_DO( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TRIGGER, sizeof(int), ps), goto out );
    284328                                break;
    285                                
     329
    286330                        case SIGINT:
    287331                        case SIGTERM:
     
    290334                }
    291335        }
    292 out:   
     336out:
    293337        /* Better way to handle this ? */
    294338        ASSERT(0);
Note: See TracChangeset for help on using the changeset viewer.