/** Implements the end_interval function of the plugin API */ int bgpcorsaro_pfxmonitor_end_interval(bgpcorsaro_t *bgpcorsaro, bgpcorsaro_interval_t *int_end) { struct bgpcorsaro_pfxmonitor_state_t *state = STATE(bgpcorsaro); bgpcorsaro_io_write_interval_end(bgpcorsaro, state->outfile, int_end); /* if we are rotating, now is when we should do it */ if (bgpcorsaro_is_rotate_interval(bgpcorsaro)) { /* leave the current file to finish draining buffers */ assert(state->outfile != NULL); /* move on to the next output pointer */ state->outfile_n = (state->outfile_n + 1) % OUTFILE_POINTERS; if (state->outfile_p[state->outfile_n] != NULL) { /* we're gonna have to wait for this to close */ wandio_wdestroy(state->outfile_p[state->outfile_n]); state->outfile_p[state->outfile_n] = NULL; } state->outfile = NULL; } output_stats_and_reset(state, state->interval_start); return 0; }
static void lzo_wclose(iow_t *iow) { const uint32_t zero = 0; int i; /* Flush the last buffer */ pthread_mutex_lock(&get_next_thread(iow)->mutex); if (get_next_thread(iow)->state == EMPTY && get_next_thread(iow)->inbuf.offset != 0) { get_next_thread(iow)->state = WAITING; pthread_cond_signal(&get_next_thread(iow)->in_ready); } pthread_mutex_unlock(&get_next_thread(iow)->mutex); DATA(iow)->next_thread = (DATA(iow)->next_thread+1) % DATA(iow)->threads; /* Right, now we have to shutdown all our threads -- in order */ for(i=DATA(iow)->next_thread; i<DATA(iow)->threads; ++i) { shutdown_thread(iow,&DATA(iow)->thread[i]); } for(i=0; i<DATA(iow)->next_thread; ++i) { shutdown_thread(iow,&DATA(iow)->thread[i]); } /* Write out an end of file marker */ wandio_wwrite(DATA(iow)->child, &zero, sizeof(zero)); /* And clean everything up */ wandio_wdestroy(DATA(iow)->child); free(DATA(iow)->thread); free(iow->data); free(iow); }
void corsaro_file_close(corsaro_t *corsaro, corsaro_file_t *file) { switch(file->mode) { case CORSARO_FILE_MODE_ASCII: case CORSARO_FILE_MODE_BINARY: /* close the wandio object */ assert(file->wand_io != NULL); wandio_wdestroy(file->wand_io); file->wand_io = NULL; break; case CORSARO_FILE_MODE_TRACE: assert(file->trace_io != NULL); trace_destroy_output(file->trace_io); file->trace_io = NULL; break; default: assert(0); } free(file); return; }
/** Implements the close_output function of the plugin API */ int bgpcorsaro_pfxmonitor_close_output(bgpcorsaro_t *bgpcorsaro) { int i; struct bgpcorsaro_pfxmonitor_state_t *state = STATE(bgpcorsaro); khiter_t k; khash_t(peer_asn_map) * v; if (state == NULL) { return 0; } /* close all the outfile pointers */ for (i = 0; i < OUTFILE_POINTERS; i++) { if (state->outfile_p[i] != NULL) { wandio_wdestroy(state->outfile_p[i]); state->outfile_p[i] = NULL; } } state->outfile = NULL; if (state->poi != NULL) { bgpstream_ip_counter_destroy(state->poi); state->poi = NULL; } /* deallocate the dynamic memory in use */ if (state->overlapping_pfx_cache != NULL) { bgpstream_pfx_storage_set_destroy(state->overlapping_pfx_cache); state->overlapping_pfx_cache = NULL; } if (state->non_overlapping_pfx_cache != NULL) { bgpstream_pfx_storage_set_destroy(state->non_overlapping_pfx_cache); state->non_overlapping_pfx_cache = NULL; } if (state->peer_asns != NULL) { bgpstream_id_set_destroy(state->peer_asns); state->peer_asns = NULL; } if (state->pfx_info != NULL) { for (k = kh_begin(state->pfx_info); k != kh_end(state->pfx_info); ++k) { if (kh_exist(state->pfx_info, k)) { v = kh_val(state->pfx_info, k); kh_destroy(peer_asn_map, v); } } kh_destroy(pfx_info_map, state->pfx_info); state->pfx_info = NULL; } if (state->unique_origins != NULL) { bgpstream_id_set_destroy(state->unique_origins); state->unique_origins = NULL; } bgpcorsaro_plugin_free_state(bgpcorsaro->plugin_manager, PLUGIN(bgpcorsaro)); return 0; }
/** Implements the end_interval function of the plugin API */ int bgpcorsaro_pacifier_end_interval(bgpcorsaro_t *bgpcorsaro, bgpcorsaro_interval_t *int_end) { struct bgpcorsaro_pacifier_state_t *state = STATE(bgpcorsaro); bgpcorsaro_io_write_interval_end(bgpcorsaro, state->outfile, int_end); /* if we are rotating, now is when we should do it */ if(bgpcorsaro_is_rotate_interval(bgpcorsaro)) { /* leave the current file to finish draining buffers */ assert(state->outfile != NULL); /* move on to the next output pointer */ state->outfile_n = (state->outfile_n+1) % OUTFILE_POINTERS; if(state->outfile_p[state->outfile_n] != NULL) { /* we're gonna have to wait for this to close */ wandio_wdestroy(state->outfile_p[state->outfile_n]); state->outfile_p[state->outfile_n] = NULL; } state->outfile = NULL; } struct timeval tv; gettimeofday_wrap(&tv); int expected_time = 0; int diff = 0; if(state->adaptive == 0) { diff = state->wait - (tv.tv_sec - state->tv_start); } else { expected_time = state->tv_first_time + state->intervals * state->wait; diff = expected_time - tv.tv_sec ; } // if the end interval is faster than "the wait" time // then we wait for the remaining seconds if(diff > 0) { // fprintf(stderr, "\tWaiting: %d s\n", diff); sleep(diff); gettimeofday_wrap(&tv); } state->tv_start = tv.tv_sec; // fprintf(stderr, "END INTERVAL TIME: %d \n", state->tv_start); return 0; }
static void bz_wclose(iow_t *iow) { while (BZ2_bzCompress(&DATA(iow)->strm, BZ_FINISH) == BZ_OK) { /* Need to flush the output buffer */ wandio_wwrite(DATA(iow)->child, DATA(iow)->outbuff, sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); DATA(iow)->strm.next_out = DATA(iow)->outbuff; DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); } BZ2_bzCompressEnd(&DATA(iow)->strm); wandio_wwrite(DATA(iow)->child, DATA(iow)->outbuff, sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); wandio_wdestroy(DATA(iow)->child); free(iow->data); free(iow); }
/** Implements the close_output function of the plugin API */ int bgpcorsaro_pacifier_close_output(bgpcorsaro_t *bgpcorsaro) { int i; struct bgpcorsaro_pacifier_state_t *state = STATE(bgpcorsaro); if(state != NULL) { /* close all the outfile pointers */ for(i = 0; i < OUTFILE_POINTERS; i++) { if(state->outfile_p[i] != NULL) { wandio_wdestroy(state->outfile_p[i]); state->outfile_p[i] = NULL; } } state->outfile = NULL; bgpcorsaro_plugin_free_state(bgpcorsaro->plugin_manager, PLUGIN(bgpcorsaro)); } return 0; }
/* The writing thread */ static void *thread_consumer(void *userdata) { int buffer=0; bool running = true; iow_t *state = (iow_t *) userdata; #ifdef PR_SET_NAME char namebuf[17]; if (prctl(PR_GET_NAME, namebuf, 0,0,0) == 0) { namebuf[16] = '\0'; /* Make sure it's NUL terminated */ /* If the filename is too long, overwrite the last few bytes */ if (strlen(namebuf)>9) { strcpy(namebuf+10,"[iow]"); } else { strncat(namebuf," [iow]",16); } prctl(PR_SET_NAME, namebuf, 0,0,0); } #endif pthread_mutex_lock(&DATA(state)->mutex); do { /* Wait for data that we can write */ while (DATA(state)->buffer[buffer].state == EMPTY) { /* Unless, of course, the program is over! */ if (DATA(state)->closing) break; pthread_cond_wait(&DATA(state)->data_ready, &DATA(state)->mutex); } /* Empty the buffer using the child writer */ pthread_mutex_unlock(&DATA(state)->mutex); wandio_wwrite( DATA(state)->iow, DATA(state)->buffer[buffer].buffer, DATA(state)->buffer[buffer].len); pthread_mutex_lock(&DATA(state)->mutex); /* If we've not reached the end of the file keep going */ running = ( DATA(state)->buffer[buffer].len > 0 ); DATA(state)->buffer[buffer].len = 0; DATA(state)->buffer[buffer].state = EMPTY; /* Signal that we've freed up another buffer for the main * thread to copy data into */ pthread_cond_signal(&DATA(state)->space_avail); /* Move on to the next buffer */ buffer=(buffer+1) % BUFFERS; } while(running); /* If we reach here, it's all over so start tidying up */ wandio_wdestroy(DATA(state)->iow); pthread_mutex_unlock(&DATA(state)->mutex); return NULL; }