4

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?

4
  • 1
    If pgrp is 0, killpg() sends the signal to the calling process's process group. (POSIX says: If pgrp is less than or equal to 1, the behavior is undefined. Commented Oct 5, 2015 at 17:09
  • i missed that line on the man page. This reminds me that i should read man pages twice before concluding anything. :) Commented Oct 5, 2015 at 17:21
  • You do not test around as root, don't you? Commented Oct 5, 2015 at 18:51
  • No, absolutely not. I even ran it on a virtual machine and it kill every process and also logs out the current user. Commented Oct 5, 2015 at 20:40

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

12 Comments

but would not it be better if you just cross-check before sending the signal. This would prevent the system to suffer, as running the code logs out the user account.
@PinkeshBadjatiya Who is "you"?
by "you" i mean the actual code for the system call kill. What i mean is if we verify in the code itself and do not permit the signalling if PID==1, then this would prevent system crash.
@PinkeshBadjatiya Please don't do that. If you have a new question, post a new question. Although in this case I don't believe your followup question is good material for SO.
@PinkeshBadjatiya What you describe isn't happening anywhere I have tried so far. Only the users own processes gets killed. Not everything. Sure, you'll get logged out, as most of the GUI stuff is running as your user and gets killed. I can give you a program that does far, far worse things than just kill processes. I don't think this is a bad feature at all, but quite handy for wiping out all processes for a user. So we can agree to disagree on that.
|
3

From the man page:

(POSIX says: If pgrp is less than or equal to 1, the behaviour is undefined.)

So you can't depend on any specific behavior if you do this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.