* kill %jobspec tries to kill dead processes
@ 2018-01-05 12:46 Stephane Chazelas
2018-01-05 23:21 ` Stephane Chazelas
0 siblings, 1 reply; 2+ messages in thread
From: Stephane Chazelas @ 2018-01-05 12:46 UTC (permalink / raw)
To: Zsh hackers list
Hi,
kill %jobspec
usually avoids the problem when kill $pid may kill the wrong
process when it may have been reused. However, in:
sleep 1 | sleep 3 & sleep 2
kill %"sleep 1"
kill still tries to kill the process that was running sleep 1
even though the shell knows it has died:
$ strace -e kill zsh -c 'sleep 1 | sleep 3 & ps -f; sleep 2; printf "%s => %s\n" "${(@kv)jobstates}"; kill %"sleep 1"'
UID PID PPID C STIME TTY TIME CMD
chazelas 8175 14816 0 12:37 pts/4 00:00:00 strace -e kill zsh -c sleep 1 | sleep 3 & ps -f; sleep 2; printf "%s => %s\n" "${(@kv)jobstates}"; kill %"sleep 1"
chazelas 8177 8175 0 12:37 pts/4 00:00:00 zsh -c sleep 1 | sleep 3 & ps -f; sleep 2; printf "%s => %s\n" "${(@kv)jobstates}"; kill %"sleep 1"
chazelas 8178 8177 0 12:37 pts/4 00:00:00 sleep 1
chazelas 8179 8177 0 12:37 pts/4 00:00:00 sleep 3
chazelas 8180 8177 0 12:37 pts/4 00:00:00 ps -f
chazelas 14816 14812 0 Jan03 pts/4 00:00:01 /bin/zsh
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=8180, si_uid=1000, si_status=0, si_utime=0, si_stime=2} ---
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=8178, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=8181, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
1 => running:+:8178=done:8179=running
kill(8178, SIGTERM) = -1 ESRCH (No such process)
kill(8179, SIGTERM) = 0
+++ exited with 0 +++
See how 8178 was known to be "done", but is still killed.
mksh has the same issue. bash is worse in that it doesn't
attempt to kill the sleep 3 process. ksh93 segfaults when I try
the same thing there.
--
Stephane
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: kill %jobspec tries to kill dead processes
2018-01-05 12:46 kill %jobspec tries to kill dead processes Stephane Chazelas
@ 2018-01-05 23:21 ` Stephane Chazelas
0 siblings, 0 replies; 2+ messages in thread
From: Stephane Chazelas @ 2018-01-05 23:21 UTC (permalink / raw)
To: Zsh hackers list
2018-01-05 12:46:02 +0000, Stephane Chazelas:
[...]
> sleep 1 | sleep 3 & sleep 2
> kill %"sleep 1"
>
> kill still tries to kill the process that was running sleep 1
> even though the shell knows it has died:
[...]
I beleive the patch below should do it. I added an unrelated
comment about the "sig != 0". I guess we could skip the killing
altogether for sig == 0, but I left it in on the basis of "who
are we do deny the user's request even if it's pointless".
diff --git a/Src/signals.c b/Src/signals.c
index 94f379e..e06fc05 100644
--- a/Src/signals.c
+++ b/Src/signals.c
@@ -779,8 +779,17 @@ killjb(Job jn, int sig)
return killpg(jn->gleader, sig);
}
for (pn = jn->procs; pn; pn = pn->next)
- if ((err = kill(pn->pid, sig)) == -1 && errno != ESRCH && sig != 0)
- return -1;
+ /*
+ * Do not kill this job's process if it's already dead as its
+ * pid could have been reused by the system.
+ */
+ if (pn->status == SP_RUNNING || WIFSTOPPED(pn->status))
+ /*
+ * kill -0 on a job is pointless. We still call kill() for each process
+ * in case the user cares about it but we ignore its outcome.
+ */
+ if ((err = kill(pn->pid, sig)) == -1 && errno != ESRCH && sig != 0)
+ return -1;
return err;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-01-05 23:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-05 12:46 kill %jobspec tries to kill dead processes Stephane Chazelas
2018-01-05 23:21 ` Stephane Chazelas
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).