生成.sig linux,在Linux上显式调用SIG

编程入门 行业动态 更新时间:2024-10-08 10:36:11

生成.sig <a href=https://www.elefans.com/category/jswz/34/1770067.html style=linux,在Linux上显式调用SIG"/>

生成.sig linux,在Linux上显式调用SIG

提问

我已阻止,然后通过以下代码等待信号:

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

#include

/* 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?

但是,因为“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()也会起作用.

更多推荐

生成.sig linux,在Linux上显式调用SIG

本文发布于:2024-02-24 21:08:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1696657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:linux   sig   Linux   SIG   上显式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!