From ed5d561a722d6ab8ae85dfe14bd2a288c371f040 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Fri, 10 Jun 2022 13:29:29 +0100 Subject: [PATCH] sh_exec(): do not simulate close-on-exec for (very few) built-ins The question-everything department is productive today. When executing a built-in, ksh checks execflg, which is set if the command is run in a location where an exec optimisation would take place if the command were an external command (i.e. if it's the last command in the script). In that case, and if there are no traps, and we're not running a function (which we wouldn't anyway if execflg is set), and the built-in does not have the BLT_ENV attribute, ksh simulates close-on-exec by manually closing the file descriptors with the IOCLEX attribute before running the built-in. This is yet another thing that makes no sense: - The command is a built-in. There is no exec, so close-on-exec should not happen. - Why only for the last command in the script? For external commands, close-on-exec happens regardless of the command's location, because external commands fork, then exec. So if this was meant to introduce behavioural consistency between externals and built-ins: FAIL - Why only for built-ins without the BLT_ENV attribute? There are only a few of them (see data/builtins.c). Researching ksh93-history shows that this code was introduced in 2005-03-19 ksh93q+. The changes in ksh93/RELEASE in that commit have a couple of entries that could be superficially related, but nothing directly relevant. It also introduced several regression tests that are still present, and none of them fail after we delete this. So, this looks like yet more undocumented nonsense. --- src/cmd/ksh93/sh/xec.c | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 0b981d7ea..1c1380b95 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -1319,15 +1319,6 @@ int sh_exec(register const Shnode_t *t, int flags) bp->flags = (OPTIMIZE!=0); if(sh.subshell && nv_isattr(np,BLT_NOSFIO)) sh_subtmpfile(); - if(execflg && !sh.subshell && - !sh.st.trapcom[0] && !sh.st.trap[SH_ERRTRAP] && sh.fn_depth==0 && !nv_isattr(np,BLT_ENV)) - { - /* do close-on-exec */ - int fd; - for(fd=0; fd < sh.lim.open_max; fd++) - if((sh.fdstatus[fd]&IOCLEX)&&fd!=sh.infd) - sh_close(fd); - } if(argn) sh.exitval = (*sh.bltinfun)(argn,com,(void*)bp); if(error_info.flags&ERROR_INTERACTIVE)