Navigation


Changeset 235:8773740401a5 in freeDiameter


Ignore:
Timestamp:
Mar 5, 2010, 7:01:48 PM (14 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Centralized signal handlers management in the library

Files:
2 added
1 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • extensions/test_app/CMakeLists.txt

    r120 r235  
    1414        ta_conf.tab.c
    1515        ta_conf.tab.h
    16         ta_sig.c
    1716        ta_dict.c
    1817        ta_cli.c
  • extensions/test_app/ta_cli.c

    r203 r235  
    136136
    137137/* Create a test message */
    138 static void ta_cli_test_message(void)
     138static void ta_cli_test_message(int sig)
    139139{
    140140        struct msg * req = NULL;
     
    236236        CHECK_FCT( fd_sess_handler_create(&ta_cli_reg, free) );
    237237       
    238         CHECK_FCT( ta_sig_init(ta_cli_test_message) );
     238        CHECK_FCT( fd_sig_register(ta_conf->signal, "test_app.cli", ta_cli_test_message ) );
    239239       
    240240        return 0;
     
    243243void ta_cli_fini(void)
    244244{
    245         ta_sig_fini();
    246        
    247         (void) fd_sess_handler_destroy(&ta_cli_reg);
     245        CHECK_FCT_DO( fd_sig_unregister(ta_conf->signal), /* continue */ );
     246       
     247        CHECK_FCT_DO( fd_sess_handler_destroy(&ta_cli_reg), /* continue */ );
    248248       
    249249        return;
  • extensions/test_app/test_app.c

    r120 r235  
    3939
    4040#include "test_app.h"
    41 #include <signal.h>
    4241
    4342/* Initialize the configuration */
  • extensions/test_app/test_app.h

    r127 r235  
    4242 
    4343#include <freeDiameter/extension.h>
     44#include <signal.h>
    4445
    4546#ifndef TEST_APP_DEFAULT_SIGNAL
     
    6970int ta_conf_handle(char * conffile);
    7071
    71 /* Start or stop the signal handler */
    72 int ta_sig_init(void (*cb)(void));
    73 void ta_sig_fini(void);
    74 
    7572/* Handle incoming messages (server) */
    7673int ta_serv_init(void);
  • freeDiameter/main.c

    r218 r235  
    4242
    4343/* forward declarations */
    44 static void * sig_hdl(void * arg);
     44static void fd_shutdown(int signal);
    4545static int main_cmdline(int argc, char *argv[]);
    4646static void main_version(void);
     
    5858{
    5959        int ret;
    60         pthread_t sig_th;
    61         sigset_t sig_all;
    6260       
    6361        memset(fd_g_config, 0, sizeof(struct fd_config));
    6462       
    65         /* Block all signals */
    66         sigfillset(&sig_all);
    67         CHECK_POSIX(  pthread_sigmask(SIG_BLOCK, &sig_all, NULL)  );
    68        
    6963        /* Initialize the library -- must come first since it initializes the debug facility */
    70         CHECK_FCT( fd_lib_init() );
     64        CHECK_FCT( fd_lib_init(1) );
    7165        TRACE_DEBUG(INFO, "libfreeDiameter initialized.");
    7266       
     
    9185        CHECK_FCT(  main_cmdline(argc, argv)  );
    9286       
    93         /* Allow SIGINT and SIGTERM from this point */
    94         CHECK_POSIX(  pthread_create(&sig_th, NULL, sig_hdl, NULL)  );
     87        /* Allow SIGINT and SIGTERM from this point to terminate the application */
     88        CHECK_FCT( fd_sig_register(SIGINT,  "freeDiameter.main", fd_shutdown) );
     89        CHECK_FCT( fd_sig_register(SIGTERM, "freeDiameter.main", fd_shutdown) );
    9590       
    9691        /* Add definitions of the base protocol */
     
    110105       
    111106        fd_conf_dump();
     107        fd_sig_dump(FULL, 1);
    112108       
    113109        /* Start the servers */
     
    170166        CHECK_FCT_DO( fd_rtdisp_cleanup(), /* destroy remaining handlers */ );
    171167       
    172         CHECK_FCT_DO( fd_thr_term(&sig_th), /* reclaim resources of the signal thread */ );
    173        
    174168        GNUTLS_TRACE( gnutls_global_deinit() );
    175169       
    176170        fd_log_debug(FD_PROJECT_BINARY " daemon is terminated.\n");
     171       
     172        fd_lib_fini();
     173       
    177174        return ret;
    178175}
     
    319316}
    320317
    321 #ifdef HAVE_SIGNALENT_H
    322 const char *const signalstr[] = {
    323 # include "signalent.h"
    324 };
    325 const int nsignalstr = sizeof signalstr / sizeof signalstr[0];
    326 # define SIGNALSTR(sig) (((sig) < nsignalstr) ? signalstr[(sig)] : "unknown")
    327 #else /* HAVE_SIGNALENT_H */
    328 # define SIGNALSTR(sig) ("")
    329 #endif /* HAVE_SIGNALENT_H */
    330 
    331 /* signal handler */
    332 static void * sig_hdl(void * arg)
    333 {
    334         sigset_t sig_main;
    335         int sig = 0;
    336        
    337         TRACE_ENTRY();
    338         fd_log_threadname("Main signal handler");
    339        
    340         sigemptyset(&sig_main);
    341         sigaddset(&sig_main, SIGINT);
    342         sigaddset(&sig_main, SIGTERM);
    343        
    344         CHECK_SYS_DO(  sigwait(&sig_main, &sig), TRACE_DEBUG(INFO, "Error in sigwait function") );
    345        
    346         TRACE_DEBUG(INFO, "Received signal %s (%d), exiting", SIGNALSTR(sig), sig);
     318/* Terminate the application */
     319static void fd_shutdown(int signal)
     320{
     321        TRACE_ENTRY("%d", signal);
     322       
     323        TRACE_DEBUG(INFO, "Received signal %s (%d), exiting", SIGNALSTR(signal), signal);
     324
    347325        CHECK_FCT_DO( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL), exit(2) );
    348         return NULL;
    349 }
    350        
     326       
     327        return;
     328}
  • freeDiameter/tests/tests.h

    r31 r235  
    106106#define INIT_FD() {                                                             \
    107107        memset(fd_g_config, 0, sizeof(struct fd_config));                       \
    108         CHECK( 0, fd_lib_init() );                                              \
     108        CHECK( 0, fd_lib_init(1) );                                             \
    109109        fd_log_threadname(basename(__FILE__));                                  \
    110110        (void) gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);    \
  • include/freeDiameter/libfreeDiameter.h

    r232 r235  
    7575#include <libgen.h>     /* for basename if --dbg_file is specified */
    7676#endif /* DEBUG */
     77
     78
     79/*============================================================*/
     80/*                          INIT                              */
     81/*============================================================*/
     82
     83/* This function must be called first, before any call to another library function */
     84/* If the parameter is not 0, the support for signals (fd_sig_register) is enabled, otherwise it is disabled */
     85/* The function must be called while the application is single-threaded to enable support for signals */
     86int fd_lib_init(int support_signals);
     87
     88/* Call this one when the application terminates, to destroy internal threads */
     89void fd_lib_fini(void);
     90
     91
     92
    7793
    7894/*============================================================*/
     
    559575
    560576/*============================================================*/
     577/*                          SIGNALS                           */
     578/*============================================================*/
     579
     580/* Register a new callback to be called on reception of a given signal (it receives the signal as parameter) */
     581/* EALREADY will be returned if there is already a callback registered on this signal */
     582/* NOTE: the signal handler will be called from a new detached thread */
     583int fd_sig_register(int signal, char * modname, void (*callback)(int signal));
     584
     585/* Remove the handler for a given signal */
     586int fd_sig_unregister(int signal);
     587
     588/* Dump list of handlers */
     589void fd_sig_dump(int level, int indent);
     590
     591/* Name of signals */
     592#ifdef HAVE_SIGNALENT_H
     593extern const char *const fd_sig_str[];
     594extern const int fd_sig_nstr;
     595# define SIGNALSTR(sig) (((sig) < fd_sig_nstr) ? fd_sig_str[(sig)] : "[unknown signal]")
     596#else /* HAVE_SIGNALENT_H */
     597# define SIGNALSTR(sig) ("[no sig names]")
     598#endif /* HAVE_SIGNALENT_H */
     599
     600
     601/*============================================================*/
    561602/*                          LISTS                             */
    562603/*============================================================*/
  • libfreeDiameter/CMakeLists.txt

    r83 r235  
    1414        rt_data.c
    1515        sessions.c
     16        signal.c
    1617        )
    1718
  • libfreeDiameter/init.c

    r14 r235  
    3636#include "libfD.h"
    3737
    38 int fd_lib_init(void)
     38/* Initialize library variables and threads */
     39int fd_lib_init(int support_signals)
    3940{
    4041        int ret = 0;
     
    4748        }
    4849       
     50        /* Initialize signals if requested */
     51        if (support_signals) {
     52                CHECK_FCT( fd_sig_init() );
     53        }
     54       
    4955        /* Initialize the modules that need it */
    5056        fd_msg_eteid_init();
     
    5359        return 0;
    5460}
     61
     62/* Stop all threads created in the library */
     63void fd_lib_fini(void)
     64{
     65        fd_sess_fini();
     66        fd_sig_fini();
     67}
  • libfreeDiameter/libfD.h

    r156 r235  
    4646void fd_msg_eteid_init(void);
    4747int fd_sess_init(void);
     48void fd_sess_fini(void);
     49int fd_sig_init(void);
     50void fd_sig_fini(void);
    4851
    4952/* Iterator on the rules of a parent object */
  • libfreeDiameter/sessions.c

    r203 r235  
    238238}
    239239
     240/* Terminate */
     241void fd_sess_fini(void)
     242{
     243        TRACE_ENTRY("");
     244        CHECK_FCT_DO( fd_thr_term(&exp_thr), /* continue */ );
     245        return;
     246}
     247
    240248/* Create a new handler */
    241249int fd_sess_handler_create_internal ( struct session_handler ** handler, void (*cleanup)(char * sid, session_state * state) )
Note: See TracChangeset for help on using the changeset viewer.