/* arm the timer ( start it) * returns -1 on error * WARNING: use it in the same process as the timer * (the one using pause(); timer_handler()) or * change run_timer to a pointer in shared mem */ int arm_timer() { struct itimerval it; /* init signal generation */ it.it_interval.tv_sec=0; it.it_interval.tv_usec=1000000/TIMER_TICKS_HZ; it.it_value=it.it_interval; /* install the signal handler */ if (set_sig_h(SIGALRM, sig_timer) == SIG_ERR ){ LM_CRIT("SIGALRM signal handler cannot be installed: %s [%d]\n", strerror(errno), errno); return -1; } if (setitimer(ITIMER_REAL, &it, 0) == -1){ LM_CRIT("setitimer failed: %s [%d]\n", strerror(errno), errno); return -1; } if (gettimeofday(&last_time, 0)<0){ LM_ERR("gettimeofday failed: %s [%d]\n", strerror(errno), errno); return -1; } /* initialize the config framework */ if (cfg_child_init()) return -1; return 0; }
/* arm the timer ( start it) * returns -1 on error * WARNING: use it in the same process as the timer * (the one using pause(); timer_handler()) or * change run_timer to a pointer in shared mem */ int arm_timer() { struct itimerval it; /* init signal generation */ it.it_interval.tv_sec=0; it.it_interval.tv_usec=1000000/TIMER_TICKS_HZ; it.it_value=it.it_interval; /* install the signal handler */ if (set_sig_h(SIGALRM, sig_timer) == SIG_ERR ){ LOG(L_CRIT, "ERROR: init_timer: the SIGALRM signal handler cannot" " be installed: %s [%d]\n", strerror(errno), errno); return -1; } if (setitimer(ITIMER_REAL, &it, 0) == -1){ LOG(L_CRIT, "ERROR: init_timer: setitimer failed: %s [%d]\n", strerror(errno), errno); return -1; } if (gettimeofday(&last_time, 0)<0){ LOG(L_ERR, "ERROR: arm_timer: gettimeofday failed: %s [%d]\n", strerror(errno), errno); return -1; } return 0; }
void destroy_timer() { struct itimerval it; /* disable timer */ memset(&it, 0, sizeof(it)); setitimer(ITIMER_REAL, &it, 0); set_sig_h(SIGALRM, SIG_IGN); if (timer_lock){ lock_destroy(timer_lock); lock_dealloc(timer_lock); timer_lock=0; } if (ticks){ #ifdef SHM_MEM shm_free(ticks); #else pkg_free(ticks); #endif ticks=0; } if (timer_lst){ #ifdef SHM_MEM shm_free(timer_lst); #else pkg_free(timer_lst); #endif timer_lst=0; } if (running_timer){ shm_free((void*)running_timer); running_timer=0; } #ifdef USE_SLOW_TIMER if (slow_timer_lock){ lock_destroy(slow_timer_lock); lock_dealloc(slow_timer_lock); slow_timer_lock=0; } if (slow_timer_lists){ shm_free((void*)slow_timer_lists); slow_timer_lists=0; } if (t_idx){ shm_free((void*)t_idx); t_idx=0; } if (s_idx){ shm_free((void*)s_idx); s_idx=0; } if(running_timer2){ shm_free((void*)running_timer2); running_timer2=0; } #endif }
/* install the signal handlers, returns 0 on success, -1 on error */ int install_sigs() { /* added by jku: add exit handler */ if (set_sig_h(SIGINT, sig_usr) == SIG_ERR ) { DPrint("ERROR: no SIGINT signal handler can be installed\n"); goto error; } /* if we debug and write to a pipe, we want to exit nicely too */ if (set_sig_h(SIGPIPE, sig_usr) == SIG_ERR ) { DPrint("ERROR: no SIGINT signal handler can be installed\n"); goto error; } if (set_sig_h(SIGUSR1, sig_usr) == SIG_ERR ) { DPrint("ERROR: no SIGUSR1 signal handler can be installed\n"); goto error; } if (set_sig_h(SIGCHLD , sig_usr) == SIG_ERR ) { DPrint("ERROR: no SIGCHLD signal handler can be installed\n"); goto error; } if (set_sig_h(SIGTERM , sig_usr) == SIG_ERR ) { DPrint("ERROR: no SIGTERM signal handler can be installed\n"); goto error; } if (set_sig_h(SIGHUP , sig_usr) == SIG_ERR ) { DPrint("ERROR: no SIGHUP signal handler can be installed\n"); goto error; } if (set_sig_h(SIGUSR2 , sig_usr) == SIG_ERR ) { DPrint("ERROR: no SIGUSR2 signal handler can be installed\n"); goto error; } return 0; error: return -1; }
void handle_sigs() { pid_t chld; int chld_status; switch(sig_flag){ case 0: break; /* do nothing*/ case SIGPIPE: /* SIGPIPE might be rarely received on use of exec module; simply ignore it */ LOG(L_WARN, "WARNING: SIGPIPE received and ignored\n"); break; case SIGINT: case SIGTERM: /* we end the program in all these cases */ if (sig_flag==SIGINT) DBG("INT received, program terminates\n"); else DBG("SIGTERM received, program terminates\n"); /* first of all, kill the children also */ kill_all_children(SIGTERM); /* Wait for all the children to die */ while(wait(0) > 0); cleanup(1); /* cleanup & show status*/ dprint("Thank you for flying " NAME "\n"); exit(0); break; case SIGUSR1: #ifdef STATS dump_all_statistic(); #endif #ifdef PKG_MALLOC LOG(memlog, "Memory status (pkg):\n"); pkg_status(); #endif #ifdef SHM_MEM LOG(memlog, "Memory status (shm):\n"); shm_status(); #endif break; case SIGCHLD: while ((chld=waitpid( -1, &chld_status, WNOHANG ))>0) { if (WIFEXITED(chld_status)) LOG(L_INFO, "child process %d exited normally," " status=%d\n", chld, WEXITSTATUS(chld_status)); else if (WIFSIGNALED(chld_status)) { LOG(L_INFO, "child process %d exited by a signal" " %d\n", chld, WTERMSIG(chld_status)); #ifdef WCOREDUMP LOG(L_INFO, "core was %sgenerated\n", WCOREDUMP(chld_status) ? "" : "not " ); #endif }else if (WIFSTOPPED(chld_status)) LOG(L_INFO, "child process %d stopped by a" " signal %d\n", chld, WSTOPSIG(chld_status)); } #ifndef STOP_JIRIS_CHANGES if (dont_fork) { LOG(L_INFO, "INFO: dont_fork turned on, living on\n"); break; } LOG(L_INFO, "INFO: terminating due to SIGCHLD\n"); #endif /* exit */ kill_all_children(SIGTERM); if (set_sig_h(SIGALRM, sig_alarm_kill) == SIG_ERR ) { LOG(L_ERR, "ERROR: could not install SIGALARM handler\n"); /* continue, the process will die anyway if no * alarm is installed which is exactly what we want */ } alarm(60); /* 1 minute close timeout */ while(wait(0) > 0); /* wait for all the children to terminate*/ set_sig_h(SIGALRM, sig_alarm_abort); cleanup(1); /* cleanup & show status*/ alarm(0); set_sig_h(SIGALRM, SIG_IGN); DBG("terminating due to SIGCHLD\n"); exit(0); break; case SIGHUP: /* ignoring it*/ DBG("SIGHUP received, ignoring it\n"); break; default: LOG(L_CRIT, "WARNING: unhandled signal %d\n", sig_flag); } sig_flag=0; }