Navigation


Changeset 8:3e143f047f78 in freeDiameter for freeDiameter/main.c


Ignore:
Timestamp:
Sep 18, 2009, 6:54:07 PM (15 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Backup for the week-end

File:
1 edited

Legend:

Unmodified
Added
Removed
  • freeDiameter/main.c

    r3 r8  
    3636#include "fD.h"
    3737
     38#include <signal.h>
     39#include <getopt.h>
     40
     41
     42/* Display package version */
     43static void main_version_core(void)
     44{
     45        printf("%s, version %d.%d.%d"
     46#ifdef HG_VERSION
     47                " (r%s"
     48# ifdef PACKAGE_HG_REVISION
     49                "/%s"
     50# endif /* PACKAGE_HG_VERSION */
     51                ")"
     52#endif /* HG_VERSION */
     53                "\n",
     54                FD_PROJECT_NAME, FD_PROJECT_VERSION_MAJOR, FD_PROJECT_VERSION_MINOR, FD_PROJECT_VERSION_REV
     55#ifdef HG_VERSION
     56                , HG_VERSION
     57# ifdef PACKAGE_HG_REVISION
     58                , PACKAGE_HG_REVISION
     59# endif /* PACKAGE_HG_VERSION */
     60#endif /* HG_VERSION */
     61                );
     62}
     63
     64/* Display package version and general info */
     65static void main_version(void)
     66{
     67        main_version_core();
     68        printf( "%s\n", FD_PROJECT_COPYRIGHT);
     69        printf( "\nSee " FD_PROJECT_NAME " homepage at http://aaa.koganei.wide.ad.jp/\n"
     70                " for information, updates and bug reports on this software.\n");
     71}
     72
     73/* Print command-line options */
     74static void main_help( void )
     75{
     76        main_version_core();
     77        printf( "  This daemon is an implementation of the Diameter protocol\n"
     78                "  used for Authentication, Authorization, and Accounting (AAA).\n");
     79        printf("\nUsage:  " FD_PROJECT_BINARY " [OPTIONS]...\n");
     80        printf( "  -h, --help             Print help and exit\n"
     81                "  -V, --version          Print version and exit\n"
     82                "  -c, --config=filename  Read configuration from this file instead of the \n"
     83                "                           default location (%s).\n", DEFAULT_CONF_FILE);
     84        printf( "\nDebug:\n"
     85                "  These options are mostly useful for developers\n"
     86                "  -d, --debug            Increase verbosity of debug messages\n"
     87                "  -q, --quiet            Decrease verbosity then remove debug messages\n");
     88}
     89
     90/* Parse the command-line */
     91static int main_cmdline(int argc, char *argv[])
     92{
     93        int c;
     94        int option_index = 0;
     95       
     96        struct option long_options[] = {
     97                { "help",       0, NULL, 'h' },
     98                { "version",    0, NULL, 'V' },
     99                { "config",     1, NULL, 'c' },
     100                { "debug",      0, NULL, 'd' },
     101                { "quiet",      0, NULL, 'q' },
     102                { NULL, 0, NULL, 0 }
     103        };
     104       
     105        TRACE_ENTRY("%d %p", argc, argv);
     106       
     107        /* Loop on arguments */
     108        while (1) {
     109                c = getopt_long (argc, argv, "hVc:dq", long_options, &option_index);
     110                if (c == -1)
     111                        break;  /* Exit from the loop.  */
     112               
     113                switch (c) {
     114                        case 'h':       /* Print help and exit.  */
     115                                main_help();
     116                                exit(0);
     117
     118                        case 'V':       /* Print version and exit.  */
     119                                main_version();
     120                                exit(0);
     121
     122                        case 'c':       /* Read configuration from this file instead of the default location..  */
     123                                CHECK_PARAMS( optarg );
     124                                fd_g_config->conf_file = optarg;
     125                                break;
     126
     127                        case 'd':       /* Increase verbosity of debug messages.  */
     128                                fd_g_debug_lvl++;
     129                                break;
     130                               
     131                        case 'q':       /* Decrease verbosity then remove debug messages.  */
     132                                fd_g_debug_lvl--;
     133                                break;
     134
     135                        case '?':       /* Invalid option.  */
     136                                /* `getopt_long' already printed an error message.  */
     137                                TRACE_DEBUG(INFO, "getopt_long found an invalid character\n");
     138                                return EINVAL;
     139
     140                        default:        /* bug: option not considered.  */
     141                                TRACE_DEBUG(INFO, "A command-line option is missing in parser: %c\n", c);
     142                                ASSERT(0);
     143                                return EINVAL;
     144                }
     145        }
     146               
     147        return 0;
     148       
     149}
     150
     151#ifdef HAVE_SIGNALENT_H
     152const char *const signalstr[] = {
     153# include "signalent.h"
     154};
     155const int nsignalstr = sizeof signalstr / sizeof signalstr[0];
     156# define SIGNALSTR(sig) (((sig) < nsignalstr) ? signalstr[(sig)] : "unknown")
     157#else /* HAVE_SIGNALENT_H */
     158# define SIGNALSTR(sig) ("")
     159#endif /* HAVE_SIGNALENT_H */
     160
     161
     162/* signal handler */
     163static void * sig_hdl(void * arg)
     164{
     165        sigset_t sig_main;
     166        int sig = 0;
     167       
     168        TRACE_ENTRY();
     169        fd_log_threadname("Main signal handler");
     170       
     171        sigemptyset(&sig_main);
     172        sigaddset(&sig_main, SIGINT);
     173        sigaddset(&sig_main, SIGTERM);
     174       
     175        CHECK_SYS_DO(  sigwait(&sig_main, &sig), TRACE_DEBUG(INFO, "Error in sigwait function") );
     176       
     177        TRACE_DEBUG(INFO, "Received signal %s (%d), exiting", SIGNALSTR(sig), sig);
     178        CHECK_FCT_DO( fd_event_send(fd_g_config->g_fifo_main, FM_TERMINATE, NULL), exit(2) );
     179        return NULL;
     180}
     181       
     182/* The static configuration structure */
     183static struct fd_config conf;
     184struct fd_config * fd_g_config = &conf;
     185
    38186/* Entry point */
    39187int main(int argc, char * argv[])
    40188{
     189        int ret;
     190        pthread_t sig_th;
     191        sigset_t sig_all;
     192       
     193        memset(fd_g_config, 0, sizeof(struct fd_config));
     194        sigfillset(&sig_all);
     195        CHECK_POSIX(  pthread_sigmask(SIG_BLOCK, &sig_all, NULL)  );
     196       
    41197        /* Initialize the library */
    42198        CHECK_FCT( fd_lib_init() );
     
    44200        /* Name this thread */
    45201        fd_log_threadname("Main");
    46 
    47         /* Initialize the dictionary */
    48         CHECK_FCT( fd_dict_init(&fd_g_dict) );
     202       
     203        /* Initialize the config */
     204        CHECK_FCT( fd_conf_init() );
     205
     206        /* Parse the command-line */
     207        CHECK_FCT(  main_cmdline(argc, argv)  );
     208       
     209        /* Allow SIGINT and SIGTERM from this point */
     210        CHECK_POSIX(  pthread_create(&sig_th, NULL, sig_hdl, NULL)  );
    49211       
    50212        /* Add definitions of the base protocol */
    51         CHECK_FCT( fd_dict_base_protocol(fd_g_dict) );
    52        
    53         TRACE_DEBUG(INFO, "freeDiameter daemon initialized.");
    54        
    55         return 0;
    56 }
     213        CHECK_FCT( fd_dict_base_protocol(fd_g_config->g_dict) );
     214       
     215        /* Initialize other modules */
     216        CHECK_FCT(  fd_ext_init()  );
     217       
     218        /* Parse the configuration file */
     219        CHECK_FCT( fd_conf_parse() );
     220       
     221        /* Load the dynamic extensions */
     222        CHECK_FCT(  fd_ext_load()  );
     223       
     224        /* Start the peer state machines */
     225       
     226       
     227        /* Now, just wait for events */
     228        TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized.");
     229        fd_conf_dump();
     230        while (1) {
     231                int code;
     232                CHECK_FCT_DO(  fd_event_get(fd_g_config->g_fifo_main, &code, NULL),  break  );
     233                switch (code) {
     234                        case FM_TERMINATE:
     235                                ret = 0;
     236                                goto end;
     237                       
     238                        default:
     239                                TRACE_DEBUG(INFO, "Unexpected event in the daemon (%d), terminating.\n", code);
     240                                ret = -1;
     241                                goto end;
     242                }
     243        }
     244       
     245end:
     246        TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon is stopping...");
     247       
     248        /* cleanups */
     249        CHECK_FCT( fd_thr_term(&sig_th) );
     250       
     251        return ret;
     252}
Note: See TracChangeset for help on using the changeset viewer.