Skip to content

Commit

Permalink
fatal-signal: Handle SIGINT for Windows.
Browse files Browse the repository at this point in the history
Ctrl+C signals are a special case for Windows and can
be handled by registering a handle through
SetConsoleCtrlHandler() routine. This is only useful
when we run it directly on console and not as services in
the background.

Once we get a Ctrl+C signal, we call the cleanup functions
and then exit.

One thing to know here is that MinGW terminal handles
Ctrl+C signal differently (and looks a little buggy. I see
it exiting the handler midway with some sort of timeout).
So this implementation is only useful when run on Windows
terminal. Since we only use MinGW for compilation and
eventually to run unit tests, it should be okay. (The unit
tests would ideally use windows services and not expect
Ctrl+C)

Signed-off-by: Gurucharan Shetty <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
shettyg committed Feb 26, 2014
1 parent a19a3a9 commit 0c10054
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/fatal-signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ static struct ovs_mutex mutex;

static void atexit_handler(void);
static void call_hooks(int sig_nr);
#ifdef _WIN32
static BOOL WINAPI ConsoleHandlerRoutine(DWORD dwCtrlType);
#endif

/* Initializes the fatal signal handling module. Calling this function is
* optional, because calling any other function in the module will also
Expand All @@ -91,6 +94,9 @@ fatal_signal_init(void)
char *msg_buf = ovs_lasterror_to_string();
VLOG_FATAL("Failed to create a event (%s).", msg_buf);
}

/* Register a function to handle Ctrl+C. */
SetConsoleCtrlHandler(ConsoleHandlerRoutine, true);
#endif

for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
Expand Down Expand Up @@ -235,6 +241,15 @@ call_hooks(int sig_nr)
}
}
}

#ifdef _WIN32
BOOL WINAPI ConsoleHandlerRoutine(DWORD dwCtrlType)
{
stored_sig_nr = SIGINT;
SetEvent(wevent);
return true;
}
#endif

/* Files to delete on exit. */
static struct sset files = SSET_INITIALIZER(&files);
Expand Down

0 comments on commit 0c10054

Please sign in to comment.