Please explain in detail (including tty related stuff) how is a sudo
foreground process on a X terminal emulator actually killed on the Ctrl-C
.
See the following example please:
$ sudo -u test_no_pw sleep 999 &
[1] 16657
$ ps -o comm,pid,ppid,ruid,rgid,euid,egid,suid,sgid,sid,pgid -t
COMMAND PID PPID RUID RGID EUID EGID SUID SGID SID PGID
zsh 15254 15253 1000 1000 1000 1000 1000 1000 15254 15254
sudo 16657 15254 0 1000 0 1000 0 1000 15254 16657
sleep 16658 16657 1002 1002 1002 1002 1002 1002 15254 16657
ps 16660 15254 1000 1000 1000 1000 1000 1000 15254 16660
$ fg
[1] + running sudo -u test_no_pw sleep 999
^C
$ # it was killed
Before I interrupted the sudo
I started the strace
on it in another terminal:
# strace -p 16657
Process 16657 attached
restart_syscall(<... resuming interrupted call ...>) = ? ERESTART_RESTARTBLOCK (Interrupted by signal)
--- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL, si_value={int=809122100, ptr=0x54552036303a3934}} ---
[...SNIP...]
So the sender is SI_KERNEL
, interesting. I've asked yesterday in IRC channels and Googled but got only hazy or incorrect answers. Most people said that terminal or shell will send the SINGINT
to sudo but it seems to me that it cannot happpen according to kill(2)
:
For a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of the sending process must equal the real or saved set-user-ID of the target process. In the case of SIGCONT it suffices when the sending and receiving processes belong to the same session. (Historically, the rules were different; see NOTES.)
I predict that it'll have something to do with sending some escape sequence with ASCII ETX
(3) to a pseudo-terminal but I'm far from understanding it. (Why is does the signal originate from the kernel?)
Related but hazy/incorrect:
I'm mostly interested in how it works on Linux.
^C
but thanks, it looks like a nice guide to pty stuff. – woky Dec 26 '15 at 23:10