diff options
author | Roland Mainz <roland.mainz@nrubsig.org> | 2009-10-28 10:36:39 -0700 |
---|---|---|
committer | Roland Mainz <roland.mainz@nrubsig.org> | 2009-10-28 10:36:39 -0700 |
commit | 34f9b3eef6fdadbda0a846aa4d68691ac40eace5 (patch) | |
tree | 0b0fdfb35f8eb9324728de5a99e50e939aca650f /usr/src/lib/libshell/common/sh/subshell.c | |
parent | 14969419acb89bb74e6c95fa472119b710224440 (diff) | |
download | illumos-joyent-34f9b3eef6fdadbda0a846aa4d68691ac40eace5.tar.gz |
Portions contributed by Olga Kryzhanovska <olga.kryzhanovska@gmail.com>
PSARC/2009/063 ksh93 update 2
PSARC/2009/248 ksh93 update to 2009-03-10
PSARC/2009/249 more ksh93 command conversions
6888396 libast sources should not include localedef.h
6605478 ksh93 profile shell option does not work
6631006 ksh93 hangs in situations that ksh handles okay
6661487 logname reports nothing after running the script command
6705126 first call to read doesn't honor new setting of HISTFILE
6764665 *libpp* Array overrun in libpp
6765756 *libast* Array overruns in libast
6769332 Recursive function+command substitutions terminate shell after 257 iterations
6777491 *ksh93* lacks arithmetric function iszero()
6778077 *ksh93* does not understand "THAW" as a signal for use with trap
6789247 [ku1] libast/ksh93 1-digit hexfloat base conversion rounds incorrectly
6791838 *ksh93* unset of a variable which is not set should return 0
6793714 RFE: Update /usr/bin/comm to AT&T AST "comm"
6793719 RFE: Update /usr/bin/cut to AT&T AST "cut"
6793721 RFE: Update /usr/bin/paste to AT&T AST "paste"
6793722 RFE: Update /usr/bin/cmp to AT&T AST "cmp"
6793726 RFE: Update /usr/bin/uniq to AT&T AST "uniq"
6793735 RFE: Update /usr/bin/wc to AT&T AST "wc"
6793744 RFE: Add /usr/share/doc/ksh/ for ksh93 documentation
6793747 RFE: Provide "print" builtin as /usr/bin/print for external applications
6793763 RFE: Update /usr/bin/ksh93 to ast-ksh.2009-05-05
6794952 RFE: Enable "globstar" mode in /etc/ksh.kshrc (= interactive ksh93 shells)
6805792 [ku1] Moving local compound var into array does not work
6805794 [ku1] printf returns "invalid character constant" for $ printf "%d\n" "'<euro>"
6805795 [ku1] ksh93 does not differ between -0 and +0
6805797 [ku1]Can't append to nodes of an array of compound vars if addressing them via nameref
6805799 Indexed compound variable arrays do not work...
6805800 [ku1] Declaring associative compound array does not work
6805813 RFE: Update /usr/bin/join to AT&T AST "join".
6805819 RFE: Update /usr/bin/tee to AT&T AST "tee".
6809663 shlint missing ending newline on errors
6811916 ksh93 repeatedly segfaults when "tee" builtin is interrupted via <ctrl-c> in interactive mode
6821113 SUNWosdem package issues
6828644 RFE: Update /usr/bin/logname to AT&T AST "logname".
6828692 RFE: Update /usr/bin/cksum to AT&T AST "cksum".
6834184 ksh93 gets SIGSEGV if HISTFILE is changed in place.
6834207 ksh93 gets SIGSEGV on interactive function definition with HISTSIZE unset
6835835 ksh93 "cat" builtin does not handle "-n" correctly
6841442 Need exception list for OS/Net trees managed via Subversion
6848486 "echo ${test}" with test undefined crashes the shell
6850672 ksh93 (VISUAL=vi) crashes with memory fault while scrolling through history
6855875 typeset -X x ; print $x # does not print sufficient digits to restore value
6857344 /usr/bin/hash core dump with invalid arguments
6866676 Need test suite module to test the kernel support for compiled shell scripts
6881017 Subshell doesn't exit, holds pipe open preventing callers from exiting
6884409 fts functions in libast library can result in segv with deep dir trees (similar to CERT VU#590371)
Diffstat (limited to 'usr/src/lib/libshell/common/sh/subshell.c')
-rw-r--r-- | usr/src/lib/libshell/common/sh/subshell.c | 95 |
1 files changed, 71 insertions, 24 deletions
diff --git a/usr/src/lib/libshell/common/sh/subshell.c b/usr/src/lib/libshell/common/sh/subshell.c index 4d54449c27..9f6eeafdd9 100644 --- a/usr/src/lib/libshell/common/sh/subshell.c +++ b/usr/src/lib/libshell/common/sh/subshell.c @@ -1,7 +1,7 @@ /*********************************************************************** * * * This software is part of the ast package * -* Copyright (c) 1982-2008 AT&T Intellectual Property * +* Copyright (c) 1982-2009 AT&T Intellectual Property * * and is licensed under the * * Common Public License, Version 1.0 * * by AT&T Intellectual Property * @@ -102,38 +102,48 @@ void sh_subtmpfile(int pflag) Shell_t *shp = &sh; int fds[2]; Sfoff_t off; + register struct checkpt *pp = (struct checkpt*)shp->jmplist; + register struct subshell *sp = subshell_data->pipe; if(sfset(sfstdout,0,0)&SF_STRING) { register int fd; - register struct checkpt *pp = (struct checkpt*)shp->jmplist; - register struct subshell *sp = subshell_data->pipe; /* save file descriptor 1 if open */ if((sp->tmpfd = fd = fcntl(1,F_DUPFD,10)) >= 0) { fcntl(fd,F_SETFD,FD_CLOEXEC); shp->fdstatus[fd] = shp->fdstatus[1]|IOCLEX; close(1); + shp->fdstatus[1] = IOCLOSE; } else if(errno!=EBADF) + { + ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT; + shp->toomany = 1; errormsg(SH_DICT,ERROR_system(1),e_toomany); - if(!pflag) + } + if(shp->subshare || !pflag) { sfdisc(sfstdout,SF_POPDISC); if((fd=sffileno(sfstdout))>=0) { - sh.fdstatus[fd] = IOREAD|IOWRITE; + shp->fdstatus[fd] = IOREAD|IOWRITE; sfsync(sfstdout); if(fd==1) fcntl(1,F_SETFD,0); else { sfsetfd(sfstdout,1); - sh.fdstatus[1] = sh.fdstatus[fd]; - sh.fdstatus[fd] = IOCLOSE; + shp->fdstatus[1] = shp->fdstatus[fd]; + shp->fdstatus[fd] = IOCLOSE; } goto skip; } } + } + if(sp && (shp->fdstatus[1]==IOCLOSE || (!shp->subshare && !(shp->fdstatus[1]&IONOSEEK)))) + { + struct stat statb,statx; + int fd; sh_pipe(fds); sp->pipefd = fds[0]; sh_fcntl(sp->pipefd,F_SETFD,FD_CLOEXEC); @@ -143,10 +153,22 @@ void sh_subtmpfile(int pflag) write(fds[1],sfsetbuf(sfstdout,(Void_t*)sfstdout,0),(size_t)off); sfpurge(sfstdout); } + if((sfset(sfstdout,0,0)&SF_STRING) || fstat(1,&statb)<0) + statb.st_ino = 0; sfclose(sfstdout); if((sh_fcntl(fds[1],F_DUPFD, 1)) != 1) - errormsg(SH_DICT,ERROR_system(1),e_file+4); + errormsg(SH_DICT,ERROR_system(1),e_redirect); sh_close(fds[1]); + if(statb.st_ino) for(fd=0; fd < 10; fd++) + { + if(fd==1 || ((shp->fdstatus[fd]&(IONOSEEK|IOSEEK|IOWRITE))!=(IOSEEK|IOWRITE)) || fstat(fd,&statx)<0) + continue; + if(statb.st_ino==statx.st_ino && statb.st_dev==statx.st_dev) + { + sh_close(fd); + fcntl(1,F_DUPFD, fd); + } + } skip: sh_iostream(shp,1); sfset(sfstdout,SF_SHARE|SF_PUBLIC,1); @@ -156,6 +178,7 @@ void sh_subtmpfile(int pflag) } } + /* * This routine creates a temp file if necessary and creates a subshell. * The parent routine longjmps back to sh_subshell() @@ -167,16 +190,21 @@ void sh_subfork(void) Shell_t *shp = sp->shp; int curenv = shp->curenv; pid_t pid; + char *trap = shp->st.trapcom[0]; + if(trap) + trap = strdup(trap); /* see whether inside $(...) */ if(sp->pipe) sh_subtmpfile(1); shp->curenv = 0; - if(pid = sh_fork(0,NIL(int*))) + if(pid = sh_fork(FSHOWME,NIL(int*))) { shp->curenv = curenv; /* this is the parent part of the fork */ if(sp->subpid==0) sp->subpid = pid; + if(trap) + free((void*)trap); siglongjmp(*shp->jmplist,SH_JMPSUB); } else @@ -190,6 +218,7 @@ void sh_subfork(void) shp->subshell = 0; SH_SUBSHELLNOD->nvalue.s = 0; sp->subpid=0; + shp->st.trapcom[0] = trap; } } @@ -225,7 +254,7 @@ Namval_t *sh_assignok(register Namval_t *np,int add) Namarr_t *ap; int save; /* don't bother with this */ - if(!sp->shpwd || (nv_isnull(np) && !add)) + if(!sp->shpwd || (nv_isnull(np) && !add) || np==SH_LEVELNOD) return(np); /* don't bother to save if in newer scope */ if(!(rp=shp->st.real_fun) || !(dp=rp->sdict)) @@ -305,7 +334,7 @@ static void nv_restore(struct subshell *sp) continue; if(nv_isarray(mp)) nv_putsub(mp,NIL(char*),ARRAY_SCAN); - _nv_unset(mp,NV_RDONLY); + _nv_unset(mp,NV_RDONLY|NV_CLONE); if(nv_isarray(np)) { nv_clone(np,mp,NV_MOVE); @@ -317,7 +346,11 @@ static void nv_restore(struct subshell *sp) mp->nvfun = np->nvfun; mp->nvflag = np->nvflag; if(nv_cover(mp)) - nv_putval(mp, np->nvalue.cp,0); + { + nv_putval(mp, nv_getval(np),np->nvflag|NV_NOFREE); + if(!nv_isattr(np,NV_NOFREE)) + nv_offattr(mp,NV_NOFREE); + } else mp->nvalue.cp = np->nvalue.cp; np->nvfun = 0; @@ -386,7 +419,7 @@ static void table_unset(register Dt_t *root,int fun) { nq = (Namval_t*)dtnext(root,np); flag=0; - if(fun && np->nvalue.rp->fname && *np->nvalue.rp->fname=='/') + if(fun && np->nvalue.rp && np->nvalue.rp->fname && *np->nvalue.rp->fname=='/') { np->nvalue.rp->fdict = 0; flag = NV_NOFREE; @@ -437,7 +470,7 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) Shell_t *shp = &sh; struct subshell sub_data; register struct subshell *sp = &sub_data; - int jmpval,nsig=0; + int jmpval,nsig=0,duped=0; int savecurenv = shp->curenv; int savejobpgid = job.curpgid; int16_t subshell; @@ -498,7 +531,6 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) sp->cpid = shp->cpid; sp->coutpipe = shp->coutpipe; sp->cpipe = shp->cpipe[1]; - shp->coutpipe = shp->cpipe[1] = -1; shp->cpid = 0; sh_sigreset(0); } @@ -554,6 +586,7 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) subshell_data = sp->prev; if(jmpval==SH_JMPSCRIPT) siglongjmp(*shp->jmplist,jmpval); + shp->exitval &= SH_EXITMASK; sh_done(shp,0); } if(comsub) @@ -584,7 +617,11 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) { int fd=sfsetfd(iop,3); if(fd<0) + { + shp->toomany = 1; + ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT; errormsg(SH_DICT,ERROR_system(1),e_toomany); + } shp->sftable[fd] = iop; fcntl(fd,F_SETFD,FD_CLOEXEC); shp->fdstatus[fd] = (shp->fdstatus[1]|IOCLEX); @@ -597,7 +634,8 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) if(sp->tmpfd>=0) { close(1); - fcntl(sp->tmpfd,F_DUPFD,1); + if (fcntl(sp->tmpfd,F_DUPFD,1) != 1) + duped++; sh_close(sp->tmpfd); } shp->fdstatus[1] = sp->fdstatus; @@ -609,8 +647,6 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) shp->exitval = 0; if(comsub) shp->spid = sp->subpid; - else - job_wait(sp->subpid); } if(comsub && iop && sp->pipefd<0) sfseek(iop,(off_t)0,SEEK_SET); @@ -671,7 +707,7 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) free((void*)sp->pwd); if(sp->mask!=shp->mask) umask(shp->mask=sp->mask); - if(shp->coutpipe>=0) + if(shp->coutpipe!=sp->coutpipe) { sh_close(shp->coutpipe); sh_close(shp->cpipe[1]); @@ -683,6 +719,11 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) shp->subshare = sp->subshare; if(shp->subshell) SH_SUBSHELLNOD->nvalue.s = --shp->subshell; + subshell = shp->subshell; + subshell_data = sp->prev; + sh_argfree(shp,argsav,0); + if(shp->topfd != buff.topfd) + sh_iorestore(shp,buff.topfd|IOSUBSHELL,jmpval); if(sp->sig) { if(sp->prev) @@ -693,17 +734,23 @@ Sfio_t *sh_subshell(Shnode_t *t, int flags, int comsub) sh_chktrap(); } } - subshell = shp->subshell; - subshell_data = sp->prev; - sh_argfree(shp,argsav,0); + sh_sigcheck(); shp->trapnote = 0; - if(shp->topfd != buff.topfd) - sh_iorestore(shp,buff.topfd|IOSUBSHELL,jmpval); + if(sp->subpid && !comsub) + job_wait(sp->subpid); if(shp->exitval > SH_EXITSIG) { int sig = shp->exitval&SH_EXITMASK; if(sig==SIGINT || sig== SIGQUIT) sh_fault(sig); } + if(duped) + { + ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT; + shp->toomany = 1; + errormsg(SH_DICT,ERROR_system(1),e_redirect); + } + if(jmpval && shp->toomany) + siglongjmp(*shp->jmplist,jmpval); return(iop); } |