问题描述
我用C编写了一个简单的守护程序,该守护程序在Linux上运行。我试图了解如何正确设置信号屏蔽。我的守护进程中包含以下代码:
I have written a simple daemon in C, running on Linux. I'm trying to understand how to correctly setup signal masks. I have the following code in my daemon:
struct sigaction new_sig_action;
sigset_t new_sig_set;
/* Set signal mask - signals we want to block */
sigemptyset(&new_sig_set);
sigaddset(&new_sig_set, SIGCHLD); /* ignore child - i.e. we don't need to wait for it */
sigaddset(&new_sig_set, SIGTSTP); /* ignore Tty stop signals */
sigaddset(&new_sig_set, SIGTTOU); /* ignore Tty background writes */
sigaddset(&new_sig_set, SIGTTIN); /* ignore Tty background reads */
sigprocmask(SIG_BLOCK, &new_sig_set, NULL); /* Block the above specified signals */
/* Set up a signal handler */
new_sig_action.sa_handler = signal_handler;
sigemptyset(&new_sig_action.sa_mask);
new_sig_action.sa_flags = 0;
/* Signals to handle */
sigaction(SIGHUP, &new_sig_action, NULL); /* catch hangup signal */
sigaction(SIGTERM, &new_sig_action, NULL); /* catch term signal */
sigaction(SIGINT, &new_sig_action, NULL); /* catch interrupt signal */
其中signal_handler是已定义的函数。我在停止守护程序时遇到问题,如以下。
where signal_handler is a defined function. I have a problem stopping the daemon, described in this thread.
我现在已经跟踪到在不同环境(同一用户,同一系统)中启动守护程序时块掩码不同的问题。从命令行启动守护程序会产生以下 ps输出:
I have now tracked the problem to the block mask being different when the daemon is started in different environments (same user, same system). Starting the daemon from the command line yields the following 'ps' output:
> ps -C powid -o pid,ppid,command,blocked,caught,ignored
PID PPID COMMAND BLOCKED CAUGHT IGNORED
11406 1 ./powid 0000000000390000 0000000180004003 0000000000000000
,并通过PHP脚本启动完全相同的守护程序时,会产生:
and when starting the exact same daemon via a PHP script, yields:
> ps -C powid -o pid,ppid,command,blocked,caught,ignored
PID PPID COMMAND BLOCKED CAUGHT IGNORED
11491 1 ./powid fffffffe3bfbe207 0000000180004003 00000000010010
我的问题是,为什么被屏蔽的面具与众不同。我的理解表明,给定的C代码将在所有情况下都强制被屏蔽的掩码相同?
My question is, why is the blocked mask different. My understanding suggests that the given C code would force the blocked mask to be the same under all conditions?
推荐答案
状态:
因此,为什么被阻止了掩码不同是因为父母以前使用不同的掩码...
So the answer to why is the blocked mask different is because the parents used to have different masks...
自您使用以来,您给定的C代码向阻止列表添加了一些信号 sigprocmask
,第一个参数设置为 SIG_BLOCK
。如果要覆盖信号掩码,请使用 SIG_SETMASK
。这样,您应该得到一个独立于父代的信号块掩码。
Your given C code adds some signals to the block list since you use sigprocmask
with the first parameter set to SIG_BLOCK
. If you want to overwrite the signal mask, use SIG_SETMASK
. With that, you should end up with a parent independent signal block mask.
这篇关于在Linux C守护程序中设置信号掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!