1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

do not sh_close() a -1 file descriptor (re: 80767b1f, 411481eb)

Researching #483 revealed several instances in coprocess cleanup
where sh_close() is called with a file descriptor of -1 (which
flags that the pipe is already closed). As of feeb62d1, this sets
errno to EBADF. While the race condition triggered by this was
fixed in 411481eb, it's still better to avoid it.

This re-applies most of the changes reverted in 80767b1f.
This commit is contained in:
Martijn Dekker 2022-06-20 16:09:35 +01:00
parent 411481eb04
commit 7a06d911e0
3 changed files with 14 additions and 8 deletions

View file

@ -395,10 +395,11 @@ int job_reap(register int sig)
/* check for coprocess completion */
if(pid==sh.cpid)
{
if(sh.coutpipe > -1)
sh_close(sh.coutpipe);
if(sh.cpipe[1] > -1)
sh_close(sh.cpipe[1]);
sh.cpipe[1] = -1;
sh.coutpipe = -1;
sh.coutpipe = sh.cpipe[1] = -1;
}
else if(sh.subshell)
sh_subjobcheck(pid);

View file

@ -1275,10 +1275,13 @@ static noreturn void exscript(register char *path,register char *argv[],char **e
sh.bckpid = 0;
sh.st.ioset=0;
/* clean up any cooperating processes */
if(sh.cpipe[0]>0)
if(sh.cpipe[0] > -1)
sh_pclose(sh.cpipe);
if(sh.cpid && sh.outpipe)
if(sh.cpid && sh.outpipe && *sh.outpipe > -1)
{
sh_close(*sh.outpipe);
*sh.outpipe = -1;
}
sh.cpid = 0;
if(sp=fcfile())
while(sfstack(sp,SF_POPSTACK));

View file

@ -455,7 +455,9 @@ void sh_subjobcheck(pid_t pid)
{
if(sp->cpid==pid)
{
if(sp->coutpipe > -1)
sh_close(sp->coutpipe);
if(sp->cpipe > -1)
sh_close(sp->cpipe);
sp->coutpipe = sp->cpipe = -1;
return;