本文介绍了SIGKILL初始化过程(PID 1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着一个奇怪的问题,即将信号9(SIGKILL)发送到初始化进程(PID 1).如您所知,不能通过信号处理程序忽略SIGKILL.当我尝试将SIGKILL发送到init时,我发现没有任何反应. init不会终止.为了弄清楚这种行为,我决定通过strace将自己附加到初始化过程中,以便更清楚地了解正在发生的情况.现在来了怪异的部分.如果我正在用strace查看"初始化过程并将其发送给SIGKILL,则系统崩溃.

I'm facing a weird issue regarding sending signal 9 (SIGKILL) to the init process (PID 1).As you may know, SIGKILL can't be ignored via signal handlers. As I tried sending SIGKILL to init, I noticed that nothing was happening; init would not get terminated. Trying to figure out this behaviour, I decided to attach myself to the init process with strace too see more clearly what was happening. Now comes the weird part. If I'm "looking" at the init process with strace and send it SIGKILL, the system crashes.

我的问题是为什么会这样?为什么当我查看该过程时系统崩溃,为什么当我不查看该过程时却不崩溃?正如我所说,在两种情况下,我都将SIGKILL发送给init.在CentOS 6.5,Debian 7和Arch上进行了测试.

My question is why is this happening? Why does the system crash when I look at the process and why does it not crash when I'm not? As I said, in both cases I send SIGKILL to init. Tested on CentOS 6.5, Debian 7 and Arch.

谢谢!

推荐答案

如果init终止,Linux内核会故意强制系统崩溃(请参见 http://lxr.free-electrons.com/source/kernel/exit.c?v=3.12#L501 尤其是其中的panic调用).因此,作为一种安全措施,内核不会向init传递 any 致命信号,并且不排除SIGKILL(请参阅 http://lxr.free-electrons.com/ident?v=3.12&i=SIGNAL_UNKILLABLE )(但是,代码流很复杂,我不确定,但是我怀疑会通过内核生成的SIGSEGV或类似的代码.)

The Linux kernel deliberately forces a system crash if init terminates (see http://lxr.free-electrons.com/source/kernel/exit.c?v=3.12#L501 and particularly the call to panic therein). Therefore, as a safeguard, the kernel will not deliver any fatal signal to init, and SIGKILL is not excepted (see http://lxr.free-electrons.com/ident?v=3.12&i=SIGNAL_UNKILLABLE) (however, the code flow is convoluted enough that I'm not sure, but I suspect a kernel-generated SIGSEGV or similar would go through).

应用 ptrace(2) (strace使用的系统调用)进行处理1显然会禁用此保护.可以说这是内核中的错误.我没有足够的技巧来挖掘代码来查找此错误.

Applying ptrace(2) (the system call that strace uses) to process 1 apparently disables this protection. This could be said to be a bug in the kernel. I am insufficiently skilled at digging around in the code to find this bug.

我不知道其他Unix变体是否将相同的退出时崩溃语义或信号保护应用于init.如果init终止(至少是通过调用_exit终止),则让操作系统执行干净的关机或重新启动而不是恐慌是合理的,但据我所知,所有现代Unix变体可以通过专门的系统调用来请求此请求( reboot(2) ).

I do not know if other Unix variants apply the same crash-on-exit semantics or signal protection to init. It would be reasonable to have the OS perform a clean shutdown or reboot, rather than a panic, if init terminates (at least, if it does so by calling _exit) but as far as I know, all modern Unix variants have a dedicated system call to request this, instead (reboot(2)).

这篇关于SIGKILL初始化过程(PID 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:22