Commit ff34bb0f authored by Francisco Blas Izquierdo Riera's avatar Francisco Blas Izquierdo Riera
Browse files

Prevent race conditions involving SIGINT handling

Until now if the user pressed Control+C whilst the program was cleaning up (for example waiting for pending packets) the program would die inmediately. This patch renames  the globals control_c_pressed and control_c_pressed_again to much clearer tx_done and rx_done. It also adds propper handling of control_c count handling in the handler itself as a result the wait will now only be cancelled when pressing control+c a second time during the program life instead of on the first control+c if the user pressed it when we are already doing the scan.
parent 32dfd866
Loading
Loading
Loading
Loading
+2 −1
Changes for src/main-globals.h: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@
#define MAIN_GLOBALS_H
#include <time.h>

extern unsigned control_c_pressed;
extern unsigned volatile tx_done;
extern unsigned volatile rx_done;
extern time_t global_now;


+1 −1
Changes for src/main-status.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -139,7 +139,7 @@ status_print(
                        total_tcbs
                        );
    } else {
        if (control_c_pressed) {
        if (tx_done) {
            fprintf(stderr,
                "rate:%6.2f-kpps, %5.2f%% done, waiting %d-secs, found=%" PRIu64 "       \r",
                        x/1000.0,
+19 −14
Changes for src/main.c: 19 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -77,8 +77,8 @@
/*
 * yea I know globals suck
 */
unsigned control_c_pressed = 0;
static unsigned control_c_pressed_again = 0;
unsigned volatile tx_done = 0;
unsigned volatile rx_done = 0;
time_t global_now;


@@ -441,7 +441,7 @@ infinite:
        /* If the user pressed <ctrl-c>, then we need to exit. but, in case
         * the user wants to --resume the scan later, we save the current
         * state in a file */
        if (control_c_pressed) {
        if (tx_done) {
            break;
        }
    }
@@ -450,7 +450,7 @@ infinite:
     * --infinite
     *  For load testing, go around and do this again
     */
    if (masscan->is_infinite && !control_c_pressed) {
    if (masscan->is_infinite && !tx_done) {
        seed++;
        repeats++;
        goto infinite;
@@ -468,7 +468,7 @@ infinite:
     * Wait until the receive thread realizes the scan is over
     */
    LOG(1, "Transmit thread done, waiting for receive thread to realize this\n");
    while (!control_c_pressed)
    while (!tx_done)
        pixie_usleep(1000);

    /*
@@ -477,7 +477,7 @@ infinite:
     * packets to arrive. Pressing <ctrl-c> a second time will exit this
     * prematurely.
     */
    while (!control_c_pressed_again) {
    while (!rx_done) {
        unsigned k;
        uint64_t batch_size;

@@ -661,7 +661,7 @@ receive_thread(void *v)
     * wait until transmitter thread is done then go to the end
     */
    if (masscan->is_offline) {
        while (!control_c_pressed_again)
        while (!rx_done)
            pixie_usleep(10000);
        parms->done_receiving = 1;
        goto end;
@@ -672,7 +672,7 @@ receive_thread(void *v)
     * them to the terminal.
     */
    LOG(1, "begin receive thread\n");
    while (!control_c_pressed_again) {
    while (!rx_done) {
        int status;
        unsigned length;
        unsigned secs;
@@ -1005,6 +1005,8 @@ end:
 ***************************************************************************/
static void control_c_handler(int x)
{
    static unsigned control_c_pressed = 0;
    static unsigned control_c_pressed_again = 0;
    if (control_c_pressed == 0) {
        fprintf(stderr,
                "waiting several seconds to exit..."
@@ -1012,8 +1014,11 @@ static void control_c_handler(int x)
                );
        fflush(stderr);
        control_c_pressed = 1+x;
    } else
        tx_done = control_c_pressed;
    } else {
        control_c_pressed_again = 1;
        rx_done = control_c_pressed_again;
    }

}

@@ -1256,7 +1261,7 @@ main_scan(struct Masscan *masscan)
     */
    status_start(&status);
    status.is_infinite = masscan->is_infinite;
    while (!control_c_pressed) {
    while (!tx_done) {
        unsigned i;
        double rate = 0;
        uint64_t total_tcbs = 0;
@@ -1284,7 +1289,7 @@ main_scan(struct Masscan *masscan)

        if (min_index >= range && !masscan->is_infinite) {
            /* Note: This is how we can tell the scan has ended */
            control_c_pressed = 1;
            tx_done = 1;
        }

        /*
@@ -1352,7 +1357,7 @@ main_scan(struct Masscan *masscan)
                masscan->wait - (time(0) - now));

        if (time(0) - now >= masscan->wait)
            control_c_pressed_again = 1;
            rx_done = 1;

        for (i=0; i<masscan->nic_count; i++) {
            struct ThreadPair *parms = &parms_array[i];
@@ -1366,8 +1371,8 @@ main_scan(struct Masscan *masscan)

        if (transmit_count < masscan->nic_count)
            continue;
        control_c_pressed = 1;
        control_c_pressed_again = 1;
        tx_done = 1;
        rx_done = 1;
        if (receive_count < masscan->nic_count)
            continue;
        break;
+1 −1
Changes for src/output.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -206,7 +206,7 @@ open_rotate(struct Output *out, const char *filename)
            fprintf(stderr, "out: could not open file for %s\n",
                    is_append?"appending":"writing");
            perror(filename);
            control_c_pressed = 1;
            tx_done = 1;
            return NULL;
        }
    }
+1 −1
Changes for src/rawsock.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -367,7 +367,7 @@ int rawsock_recv_packet(
                        );
        if (err == PF_RING_ERROR_NO_PKT_AVAILABLE || hdr.caplen == 0) {
            PFRING.poll(adapter->ring, 1);
            if (control_c_pressed)
            if (tx_done)
                return 1;
            goto again;
        }