/* Unix only provides a single ALRM signal, so we use the same handler for * frequent updates (every 5 seconds) and differential updates (every 30 * seconds). We trigger an ALRM every 5 seconds and only write differential * updates every 6th ALRM. */ static void handle_signals(int sig) { if (sig == SIGINT || sig == SIGTERM) { write_update(); #ifdef ENABLE_FREQUENT_UPDATES write_frequent_update(); #endif exit(0); } else if (sig == SIGALRM) { alarm_count += 1; if (alarm_count % ALARMS_PER_UPDATE == 0) { #ifndef DISABLE_FLOW_THRESHOLDING write_flow_log(); #endif write_update(); } #ifdef ENABLE_FREQUENT_UPDATES write_frequent_update(); #endif if (alarm_count % ALARMS_PER_UPDATE == 0 && upload_failures_check(&upload_failures) > 0) { exit(0); } set_next_alarm(); } }
static void* frequent_updater(void* arg) { while (1) { sleep(FREQUENT_UPDATE_PERIOD_SECONDS); if (pthread_mutex_lock(&update_lock)) { perror("Error acquiring mutex for update"); exit(1); } write_frequent_update(); if (pthread_mutex_unlock(&update_lock)) { perror("Error unlocking update mutex"); exit(1); } } }
static void* handle_signals(void* arg) { sigset_t* signal_set = (sigset_t*)arg; int signal_handled; while (1) { if (sigwait(signal_set, &signal_handled)) { perror("Error handling signal"); continue; } if (pthread_mutex_lock(&update_lock)) { perror("Error acquiring mutex for update"); exit(1); } write_update(); #ifdef ENABLE_FREQUENT_UPDATES write_frequent_update(); #endif exit(0); } }