+-
在Linux上显式调用SIG_DFL / SIG_IGN处理程序
我已阻止,然后通过以下代码等待信号:

sigset_t set;
sigfillset(&set); // all signals
sigprocmask(SIG_SETMASK, &set, NULL); // block all signals
siginfo_t info;
int signum = sigwaitinfo(&set, &info); // wait for next signal
struct sigaction act;
sigaction(signum, NULL, &act); // get the current handler for the signal
act.sa_handler(signum); // invoke it

最后一行生成分段错误,因为处理程序设置为SIG_DFL(定义为0).如果设置为SIG_DFL或SIG_IGN,如何手动调用默认处理程序?另请注意,SIG_IGN定义为1.

最佳答案
正如您所发现的那样,您本身无法调用SIG_DFL和SIG_IGN.但是,您可以或多或少地模仿他们的行为.

简而言之,模仿正常的信号配置将是:

>用户定义的sa_handler非常容易
>对于SIG_IGN来说已经足够了,需要注意的是在CHLD的情况下你需要waitpid()
>直截了当但令人不愉快的SIG_DFL,重新提升让内核发挥其魔力.

这样做你想要的吗?

#include <signal.h>
#include <stdlib.h>

/* Manually dispose of a signal, mimicking the behavior of current
 * signal dispositions as best we can.  We won't cause EINTR, for
 * instance.
 *
 * FIXME:  save and restore errno around the SIG_DFL logic and
 *         SIG_IGN/CHLD logic.
 */
void dispatch_signal(const int signo) {
    int stop = 0;
    sigset_t oset;
    struct sigaction curact;

    sigaction(signo, NULL, &curact);

    /* SIG_IGN => noop or soak up child term/stop signals (for CHLD) */
    if (SIG_IGN == curact.sa_handler) {
        if (SIGCHLD == signo) {
          int status;
          while (waitpid(-1, &status, WNOHANG|WUNTRACED) > 0) {;}
        } 
        return;
    }

    /* user defined => invoke it */
    if (SIG_DFL != curact.sa_handler) {
        curact.sa_handler(signo);
        return;
    }

    /* SIG_DFL => let kernel handle it (mostly).
     *
     *  We handle noop signals ourselves -- "Ign" and "Cont", which we
     *  can never intercept while stopped.
     */
    if (SIGURG == signo || SIGWINCH == signo || SIGCONT == signo) return;

    /*  Unblock CONT if this is a "Stop" signal, so that we may later be
     *  woken up.
     */
    stop = (SIGTSTP == signo || SIGTTIN == signo || SIGTTOU == signo);
    if (stop) {
        sigset_t sig_cont;

        sigemptyset(&sig_cont);
        sigaddset(&sig_cont, SIGCONT);
        sigprocmask(SIG_UNBLOCK, &sig_cont, &oset);
    }

    /*  Re-raise, letting the kernel do the work:
     *     - Set exit codes and corefiles for "Term" and "Core"
     *     - Halt us and signal WUNTRACED'ing parents for "Stop"
     *     - Do the right thing if we forgot to handle any special
     *       signals or signals yet to be introduced
     */
    kill(getpid(), signo);

    /* Re-block CONT, if needed */
    if (stop) sigprocmask(SIG_SETMASK, &oset, NULL);
}

UPDATE
(回应OP的优秀问题)

1: does this slot in after the sigwaitinfo?

是.就像是:

... block signals ...
signo = sigwaitinfo(&set, &info);
dispatch_signal(signo);

2: Why not raise those signals handled by SIG_IGN, they’ll be ignored anyway

用户空间中的noop比三个系统调用(重新提升,取消屏蔽,重新屏蔽)更有效.此外,CHLD在SIG_IGNored时具有特殊的语义.

3: Why treat SIGCHLD specially?

最初(检查答案编辑)我没有 – 在SIG_IGN案例中重新提出它,
因为IGNored CHLD signals tell the kernel to automatically reap children

但是,因为“natural” CHLD signals carry information about
the terminated process(至少是PID,状态和真实的UID)我改变了它.
用户生成的CHLD信号不具有相同的语义,在我的测试中,
IGNoring它们不会导致2.6自动恢复其SIGCHLD的排队僵尸
被“错过了”.所以,我自己做.

4: Why are “stop” related signals unblocking CONT. Will not invoking the default handler for CONT unstop the process?

如果我们停止(不执行)并且CONT被阻止,我们将永远不会收到
发信号唤醒我们!

5: Why not call raise instead of the kill line you’ve given?

个人喜好; raise()也会起作用.

点击查看更多相关文章

转载注明原文:在Linux上显式调用SIG_DFL / SIG_IGN处理程序 - 乐贴网