Skip to content

Commit

Permalink
Improve signal handling
Browse files Browse the repository at this point in the history
Allows to call recorder_dump_on_signal again with the same signal,
the call will ignored as the signal is already set.
Do not read old signal handler if not needed.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Christophe de Dinechin <dinechin@redhat.com>
  • Loading branch information
Frediano Ziglio authored and c3d committed Jan 14, 2019
1 parent c647452 commit 03eb4b6
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions recorder.c
Expand Up @@ -1713,7 +1713,6 @@ RECORDER(signals, 32, "Information about signals");

// Saved old actions
#if HAVE_SIGACTION
typedef void (*sig_fn)(int, siginfo_t *, void *);
static struct sigaction old_action[NSIG] = { };

static void signal_handler(int sig, siginfo_t *info, void *ucontext)
Expand All @@ -1727,8 +1726,7 @@ static void signal_handler(int sig, siginfo_t *info, void *ucontext)
strsignal(sig), sig);

// Restore previous handler in case we crash during the dump
struct sigaction next;
sigaction(sig, &old_action[sig], &next);
sigaction(sig, &old_action[sig], NULL);
recorder_dump();
}

Expand All @@ -1742,14 +1740,19 @@ void recorder_dump_on_signal(int sig)
return;

struct sigaction action;

// Already set?
sigaction(sig, NULL, &action);
if ((action.sa_flags & SA_SIGINFO) != 0 && action.sa_sigaction == signal_handler)
return;
old_action[sig] = action;

action.sa_sigaction = signal_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO;
sigaction(sig, &action, &old_action[sig]);
sigaction(sig, &action, NULL);
record(signals, "Recorder dump handler %p for signal %u, old action=%p",
signal_handler, sig, old_action[sig].sa_sigaction);
if (old_action[sig].sa_sigaction == signal_handler)
old_action[sig].sa_sigaction = (sig_fn) SIG_DFL;
}

#else // !HAVE_SIGACTION
Expand Down

0 comments on commit 03eb4b6

Please sign in to comment.