1fded4
From 27909acb584aed231d757d1d63c6c62b57c8e152 Mon Sep 17 00:00:00 2001
1fded4
From: Vincent Mihalkovic <vmihalko@redhat.com>
1fded4
Date: Mon, 3 Jan 2022 14:03:20 +0100
1fded4
Subject: [PATCH] Fix race conditions running external commands with job
1fded4
 control on
1fded4
1fded4
When ksh is compiled with SHOPT_SPAWN (the default), which uses
1fded4
posix_spawn(3) or vfork(2) (via sh_ntfork()) to launch external
1fded4
commands, at least two race conditions occur when launching
1fded4
external commands while job control is active. See:
1fded4
https://bugs.launchpad.net/ubuntu/+source/ksh/+bug/1887863/comments/3
1fded4
https://www.mail-archive.com/ast-developers@research.att.com/msg00717.html
1fded4
1fded4
The basic issue is that this performance optimisation is
1fded4
incompatible with job control, because it uses a spawning mechanism
1fded4
that doesn't copy the parent process' memory pages into the child
1fded4
process, therefore no state that involves memory can be set before
1fded4
exec-ing the external program. This makes it impossible to
1fded4
correctly set the terminal's process group ID in the child process,
1fded4
something that is essential for job control to work.
1fded4
1fded4
src/cmd/ksh93/sh/xec.c:
1fded4
- Use sh_fork() instead of sh_ntfork() if job control is active.
1fded4
  This uses fork(2), which is 30%-ish slower on most sytems, but
1fded4
  allows for correctly setting the terminal process group.
1fded4
1fded4
Fixes: https://github.com/ksh93/ksh/issues/79
1fded4
---
1fded4
 src/cmd/ksh93/sh/xec.c | 2 +-
1fded4
 1 file changed, 1 insertion(+), 1 deletion(-)
1fded4
1fded4
diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c
1fded4
index ae13178..be57a6b 100644
1fded4
--- a/src/cmd/ksh93/sh/xec.c
1fded4
+++ b/src/cmd/ksh93/sh/xec.c
1fded4
@@ -1737,7 +1737,7 @@ int sh_exec(register const Shnode_t *t, int flags)
1fded4
 #else
1fded4
 #if SHOPT_SPAWN
1fded4
 #   ifdef _lib_fork
1fded4
-				if(com)
1fded4
+				if(com && !job.jobcontrol)
1fded4
 					parent = sh_ntfork(shp,t,com,&jobid,ntflag);
1fded4
 				else
1fded4
 					parent = sh_fork(shp,type,&jobid);
1fded4
-- 
1fded4
2.31.1
1fded4