reset sigaction to default
Asked Answered
F

3

2

In Android the bionic loader sets a default signal handler for every process on statrtup:

void debugger_init()
{
    struct sigaction act;
    memset(&act, 0, sizeof(act));
    act.sa_sigaction = debugger_signal_handler;
    act.sa_flags = SA_RESTART | SA_SIGINFO;
    sigemptyset(&act.sa_mask);

    sigaction(SIGILL, &act, NULL);
    sigaction(SIGABRT, &act, NULL);
    sigaction(SIGBUS, &act, NULL);
    sigaction(SIGFPE, &act, NULL);
    sigaction(SIGSEGV, &act, NULL);
    sigaction(SIGSTKFLT, &act, NULL);
    sigaction(SIGPIPE, &act, NULL);
}

I would like to set it back to its default, meaning I want to ignore these signal and that the default handler will take place (CORE DUMP)

How do I revert the action performed ? I want to ignore all these as if the above function never was called

Flawy answered 17/7, 2014 at 12:18 Comment(0)
G
4

Read signal(7), sigaction(2) and perhaps signal(2).

You could call

signal(SIGILL, SIG_DFL);
signal(SIGABRT, SIG_DFL);

and so on early in your main (which is entered after dynamic loading)

You could also use sigaction with sa_handler set to SIG_DFL

Of course, things are more tricky if you want to default handle these signals before your main, e.g. in some static constructor!

Gump answered 17/7, 2014 at 12:46 Comment(0)
A
0

I found it could lead unexpected behavior when mixed using sigaction and signal to set for one process.

Ambrosine answered 13/6, 2018 at 20:54 Comment(0)
A
0

From signal(2) posted above(wouldn't surprise me if this warning wasn't there 8 years ago):

WARNING: the behavior of signal() varies across UNIX versions, and has also varied historically across different versions of Linux. Avoid its use: use sigaction(2) instead.

Looking at https://docs.oracle.com/cd/E19455-01/806-5257/tlib-49639/index.html

int pthread_sigmask(int how, const sigset_t *new, sigset_t *old);

When the value of new is NULL, the value of how is not significant and the signal mask of the thread is unchanged. So, to inquire about currently blocked signals, assign a NULL value to the new argument.

So I guess you could use that to get the current sigmask and just wipe each one

sigset_t tempSet;
pthread_sigmask(SIG_SETMASK, NULL, &tempSet);
sigdelset(&tempSet, /*Signal you don't want to handle*/);
sigdelset(&tempSet, /*repeat for each signal*/);
pthread_sigmask(SIG_SETMASK, &tempSet, NULL);

It's pretty much the same thing with sigact to query the current action for a signal, from sigaction(2)

sigaction() can be called with a NULL second argument to query the current signal handler.

It's not clear to me the ramifications of, in my case, having SIGKILL in the first call to sigaction

struct sigaction sigAct;
sigaction(SIGKILL, NULL, &sigAct);
sigAct.sa_handler = SIG_DFL;    // Ensure default handling of Kill signal
sigaction(/*Signal you don't want to handle*/, &sigAct, NULL);
sigaction(/*repeat for each signal*/, &sigAct, NULL);

Using siggetmask is obsolete by sigprocmask, and sigprocmask is only for single threaded environments.

Amok answered 21/6, 2022 at 22:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.