Skip to content

Commit

Permalink
job_kill(): fix bogus assumption that login shell has PPID 1
Browse files Browse the repository at this point in the history
Normally, suspending a login shell with SIGSTOP (e.g., kill -STOP
$$) will irrecoverably freeze the terminal session, so it is a good
idea to prohibit that.

But job_kill() (called via the 'kill' built-in, b_kill()) assumes
that any login shell has a parent PID of 1, a.k.a. init. This may
have been valid on some systems back in the 1980s, but when people
started using graphical terminals (if not before), that assumption
became universally bogus.

Some shells (at least yash) prohibit issuing SIGSTOP to self if
their process is the session leader. But even that doesn't always
hold. For instance, on macOS, the terminal creates a login(1)
process (the session leader), and the login shell is its child.

So really the only thing we can reliably do is check for the
-l/--login/SH_LOGIN_SHELL shell option (as the 'suspend' built-in
already does; see 3ba4900).
  • Loading branch information
McDutchie committed Sep 16, 2023
1 parent 0492a06 commit b046142
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ This documents significant changes in the 1.0 branch of ksh 93u+m.
For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.

2023-09-16:

- The 'kill' built-in command was meant to refuse to issue SIGSTOP to the
shell's process if the shell is a login shell (because this normally
causes an unrecoverable freeze of the terminal session). The code
prohibiting this did not work on modern systems and is now fixed.

2023-09-15:

- Release 1.0.7.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/data/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ const char sh_opthist[] =
#endif /* !SHOPT_SCRIPTONLY */

const char sh_optkill[] =
"[-1c?\n@(#)$Id: kill (ksh 93u+m) 2022-08-30 $\n]"
"[-1c?\n@(#)$Id: kill (ksh 93u+m) 2023-09-16 $\n]"
"[--catalog?" SH_DICT "]"
"[+NAME?kill - terminate or signal process]"
"[+DESCRIPTION?With the first form in which \b-l\b is not specified, "
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#include <releaseflags.h>

#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.0.7" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2023-09-15" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_SVER "1.0.7-beta" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2023-09-16" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2023 Contributors to ksh " SH_RELEASE_FORK

/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ int job_kill(struct process *pw,int sig)
{
if(pid==0 && job.jobcontrol)
r = job_walk(outfile, job_kill,sig, NULL);
if(sig==SIGSTOP && pid==sh.pid && sh.ppid==1)
if(sig==SIGSTOP && pid==sh.pid && sh_isoption(SH_LOGIN_SHELL))
{
/* can't stop login shell */
errno = EPERM;
Expand Down

0 comments on commit b046142

Please sign in to comment.