c - 没有 sudo 的 SIGKILL init(PID=1)? Linux 中的错误?

标签 c linux system-calls

我尝试运行以下命令 kill -9 1,它显示 bash: kill: (1) - Operation not permitted

对我来说很明显,如果没有 sudo,你不应该能够向 init 进程发出信号。

但是在为 c-shell 编写代码时,我遇到了一个错误(我认为确实如此)。我编译了以下程序并运行了它。现在它让我和我所有的操作系​​统概念都感到困惑。

#include <signal.h>
int main()
{
    killpg(1,9);
    return (0);
}

Please save all your programs and run the code yourself.

任何人都可以给我一个理由并澄清我的困惑。

更新
killpg() 的手册页读作...

On Linux, killpg() is implemented as a library function that makes the call kill(-pgrp, sig).

kill() 的手册页读作...

A PID of -1 is special; it indicates all processes except the kill process itself and init.

现在的问题是,这样一个字面上杀死一切的调用有什么用。它有许多危险的应用程序而不是有用的应用程序。但既然它已经保存在 linux 内核中那么多年了,那么它一定有它自己的用处。但我想不通。有人知道吗?

最佳答案

来自 killpg 的 Linux 手册页:

On Linux, killpg() is implemented as a library function that makes the call kill(-pgrp, sig).

来自 kill 的 Linux 手册页:

If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init)

所以你遇到了一个特殊情况,其中 killpg(1, 9) 实际上并不意味着发送 SIGKILL 到 pgrp 1,而是它由于实现的怪癖,将 SIGKILL 发送到它有权访问的所有内容。正如其他人指出的那样,POSIX 没有指定第一个参数为 1 时 killpg 的行为,因此可以说这不是错误。

关于c - 没有 sudo 的 SIGKILL init(PID=1)? Linux 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954016/

相关文章:

c - 为什么 free() 放置随机数据 block 而不是删除节点?

linux - 使用 DramSim2 运行 gem5

c - 使用 CUDA 生成排列

c++ - Windows 中的 gdb : different behaviour when debugging compiled C and C++ code

linux - 如何从所有查找命令结果中排除整数

c++ - C++ 中的 Linux execlp

sockets - 为什么 Sendto() 系统调用不返回发送的字节数?

c - fork() 系统调用和 while 循环

c++ - C/C++ 帮助在 C 中处理多个文件

Linux : How to LIMIT incoming (concurrent) connection COUNT?