Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIGKILL init(PID=1) without sudo? Bug in linux?

I tried to run the following command kill -9 1 and it says bash: kill: (1) - Operation not permitted.

It was pretty obvious to me that you should not be able to signal the init process without sudo.

But while writing the code for c-shell i encountered a bug(i think it really is). I compiled the following program and ran it. Now it has confused me and all my OS concepts.

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

Please save all your programs and run the code yourself.

Can anyone give me a reason and clarify my confusion.

UPDATE
Man page of killpg() read as...

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

Man page of kill() read as...

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

Now the question is, what is the use of such a call that literally kills everything. It has many many dangerous applications rather than useful ones. But still since it has been kept in the linux kernel since so many years then it must have its own usefulness. But i can't figure out any. Does anyone know anything about it?

like image 286
Pinkesh Badjatiya Avatar asked Oct 29 '25 22:10

Pinkesh Badjatiya


1 Answers

From the Linux manual page for killpg:

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

From the Linux manual page for kill:

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)

So you're running into a special case, where killpg(1, 9) doesn't in fact mean to send SIGKILL to pgrp 1, but instead it sends SIGKILL to everything it has permission to, due to a quirk of implementation. As others have pointed out, POSIX doesn't specify the behavior of killpg when its first argument is 1, so this is arguably not a bug.

like image 153
hobbs Avatar answered Oct 31 '25 11:10

hobbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!