Skip to content

Commit

Permalink
job_init(): don't call setpgid() if non-interactive (re: 41ebb55)
Browse files Browse the repository at this point in the history
This returns job_init() to a version quite close to the original
93u+ version. Upon further code analysis, really everything after
the init_savelist() call is only relevant to interactive shells, so
we can just return at that point for non-interactive shells as 93u+
did. All the script-only job control regression tests still pass.

Resolves: #320
  • Loading branch information
McDutchie committed Aug 25, 2022
1 parent 58233b4 commit 7132603
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/cmd/ksh93/sh/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,29 +540,27 @@ void job_init(int lflag)
# endif
if(njob_savelist < NJOB_SAVELIST)
init_savelist();
if(!sh_isoption(SH_INTERACTIVE))
return;
/* use new line discipline when available */
if(sh_isoption(SH_INTERACTIVE))
{
#ifdef NTTYDISC
# ifdef FIOLOOKLD
if((job.linedisc = ioctl(JOBTTY, FIOLOOKLD, 0)) <0)
if((job.linedisc = ioctl(JOBTTY, FIOLOOKLD, 0)) <0)
# else
if(ioctl(JOBTTY,TIOCGETD,&job.linedisc) !=0)
if(ioctl(JOBTTY,TIOCGETD,&job.linedisc) !=0)
# endif /* FIOLOOKLD */
return;
if(job.linedisc!=NTTYDISC && job.linedisc!=OTTYDISC)
{
/* no job control when running with MPX */
return;
if(job.linedisc!=NTTYDISC && job.linedisc!=OTTYDISC)
{
/* no job control when running with MPX */
# if SHOPT_VSH
sh_onoption(SH_VIRAW);
sh_onoption(SH_VIRAW);
# endif /* SHOPT_VSH */
return;
}
if(job.linedisc==NTTYDISC)
job.linedisc = -1;
return;
}
if(job.linedisc==NTTYDISC)
job.linedisc = -1;
#endif /* NTTYDISC */

job.mypgid = getpgrp();
/* some systems have job control, but not initialized */
if(job.mypgid<=0)
Expand All @@ -574,25 +572,22 @@ void job_init(int lflag)
#ifndef SIGTSTP
setpgid(0,sh.pid);
#endif /*SIGTSTP */
if(sh_isoption(SH_INTERACTIVE))
{
if(job.mypgid<0 || !(ttynam=ttyname(JOBTTY)))
return;
while(close(JOBTTY)<0 && errno==EINTR)
;
if((fd = open(ttynam,O_RDWR)) <0)
return;
if(fd!=JOBTTY)
sh_iorenumber(fd,JOBTTY);
if(job.mypgid<0 || !(ttynam=ttyname(JOBTTY)))
return;
while(close(JOBTTY)<0 && errno==EINTR)
;
if((fd = open(ttynam,O_RDWR)) <0)
return;
if(fd!=JOBTTY)
sh_iorenumber(fd,JOBTTY);
#ifdef SIGTSTP
tcsetpgrp(JOBTTY,sh.pid);
tcsetpgrp(JOBTTY,sh.pid);
#endif /* SIGTSTP */
}
job.mypgid = sh.pid;
}
#ifdef SIGTSTP
possible = (setpgid(0,job.mypgid) >= 0) || errno==EPERM;
if(sh_isoption(SH_INTERACTIVE) && possible)
if(possible)
{
/* wait until we are in the foreground */
while((job.mytgid=tcgetpgrp(JOBTTY)) != job.mypgid)
Expand All @@ -611,10 +606,9 @@ void job_init(int lflag)
}
}
#endif /* SIGTTIN */

#ifdef NTTYDISC
/* set the line discipline */
if(sh_isoption(SH_INTERACTIVE) && job.linedisc>=0)
if(job.linedisc>=0)
{
int linedisc = NTTYDISC;
# ifdef FIOPUSHLD
Expand All @@ -635,17 +629,14 @@ void job_init(int lflag)
errormsg(SH_DICT,0,e_newtty);
else
job.linedisc = -1;
#endif /* NTTYDISC */
}
#endif /* NTTYDISC */
if(!possible)
return;

#ifdef SIGTSTP
/* make sure that we are a process group leader */
setpgid(0,sh.pid);
job.mypid = sh.pid;
if(!sh_isoption(SH_INTERACTIVE))
return;
# if defined(SA_NOCLDSTOP) || defined(SA_NOCLDWAIT)
# if !defined(SA_NOCLDSTOP)
# define SA_NOCLDSTOP 0
Expand Down

0 comments on commit 7132603

Please sign in to comment.