Skip to content

Commit

Permalink
Log when we receive SIGTERM
Browse files Browse the repository at this point in the history
Log reception of SIGTERM and exit relatively gracefully
  • Loading branch information
wez committed Nov 25, 2015
1 parent fbab997 commit ae8e197
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
19 changes: 12 additions & 7 deletions listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,17 @@ w_root_t *resolve_root_or_err(
return root;
}

void w_request_shutdown(void) {
stopping = true;
// Knock listener thread out of poll/accept
#ifndef _WIN32
pthread_kill(listener_thread, SIGUSR1);
pthread_kill(reaper_thread, SIGUSR1);
#else
SetEvent(listener_thread_event);
#endif
}

static void cmd_shutdown(
struct watchman_client *client,
json_t *args)
Expand All @@ -415,13 +426,7 @@ static void cmd_shutdown(
w_log(W_LOG_ERR, "shutdown-server was requested, exiting!\n");
stopping = true;

// Knock listener thread out of poll/accept
#ifndef _WIN32
pthread_kill(listener_thread, SIGUSR1);
pthread_kill(reaper_thread, SIGUSR1);
#else
SetEvent(listener_thread_event);
#endif
w_request_shutdown();

set_prop(resp, "shutdown-server", json_true());
send_and_dispose_response(client, resp);
Expand Down
17 changes: 15 additions & 2 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,16 @@ static void crash_handler(int signo, siginfo_t *si, void *ucontext) {
}
}

dprintf(STDERR_FILENO, "Terminating due to signal %d %s. %s (%p)\n",
signo, sys_siglist[signo], reason, si ? si->si_value.sival_ptr : NULL);
if (si) {
dprintf(STDERR_FILENO,
"Terminating due to signal %d %s "
"generated by pid=%d uid=%d. %s (%p)\n",
signo, sys_siglist[signo], si->si_pid, si->si_uid,
reason, si->si_value.sival_ptr);
} else {
dprintf(STDERR_FILENO, "Terminating due to signal %d %s. %s\n",
signo, sys_siglist[signo], reason);
}

#if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS_FD)
{
Expand All @@ -84,6 +92,10 @@ static void crash_handler(int signo, siginfo_t *si, void *ucontext) {
backtrace_symbols_fd(array, size, STDERR_FILENO);
}
#endif
if (signo == SIGTERM) {
w_request_shutdown();
return;
}
abort();
}
#endif
Expand All @@ -102,6 +114,7 @@ void w_setup_signal_handlers(void) {
#endif
sigaction(SIGFPE, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion tests/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "watchman.h"
#include "thirdparty/tap.h"

void w_request_shutdown(void) {}

bool w_should_log_to_clients(int level)
{
unused_parameter(level);
Expand Down Expand Up @@ -38,4 +40,3 @@ int main(int argc, char **argv)

/* vim:ts=2:sw=2:et:
*/

1 change: 1 addition & 0 deletions watchman.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ void w_log(int level, WATCHMAN_FMT_STRING(const char *fmt), ...)
__attribute__((format(printf, 2, 3)))
#endif
;
void w_request_shutdown(void);

bool w_should_log_to_clients(int level);
void w_log_to_clients(int level, const char *buf);
Expand Down

0 comments on commit ae8e197

Please sign in to comment.