Home | History | Annotate | Line # | Download | only in csh
exec.c revision 1.2
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1980, 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)exec.c	5.17 (Berkeley) 6/17/91";
     36  1.2  cgd static char rcsid[] = "$Id: exec.c,v 1.2 1993/03/22 08:04:00 cgd Exp $";
     37  1.1  cgd #endif /* not lint */
     38  1.1  cgd 
     39  1.1  cgd #include <sys/types.h>
     40  1.1  cgd #include <dirent.h>
     41  1.1  cgd #include <fcntl.h>
     42  1.1  cgd #include <errno.h>
     43  1.1  cgd #include <stdlib.h>
     44  1.1  cgd #include <string.h>
     45  1.1  cgd #include <unistd.h>
     46  1.1  cgd #if __STDC__
     47  1.1  cgd # include <stdarg.h>
     48  1.1  cgd #else
     49  1.1  cgd # include <varargs.h>
     50  1.1  cgd #endif
     51  1.1  cgd 
     52  1.1  cgd #include "csh.h"
     53  1.1  cgd #include "extern.h"
     54  1.1  cgd 
     55  1.1  cgd /*
     56  1.1  cgd  * System level search and execute of a command.  We look in each directory
     57  1.1  cgd  * for the specified command name.  If the name contains a '/' then we
     58  1.1  cgd  * execute only the full path name.  If there is no search path then we
     59  1.1  cgd  * execute only full path names.
     60  1.1  cgd  */
     61  1.1  cgd extern char **environ;
     62  1.1  cgd 
     63  1.1  cgd /*
     64  1.1  cgd  * As we search for the command we note the first non-trivial error
     65  1.1  cgd  * message for presentation to the user.  This allows us often
     66  1.1  cgd  * to show that a file has the wrong mode/no access when the file
     67  1.1  cgd  * is not in the last component of the search path, so we must
     68  1.1  cgd  * go on after first detecting the error.
     69  1.1  cgd  */
     70  1.1  cgd static char *exerr;		/* Execution error message */
     71  1.1  cgd static Char *expath;		/* Path for exerr */
     72  1.1  cgd 
     73  1.1  cgd /*
     74  1.1  cgd  * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
     75  1.1  cgd  * to hash execs.  If it is allocated (havhash true), then to tell
     76  1.1  cgd  * whether ``name'' is (possibly) present in the i'th component
     77  1.1  cgd  * of the variable path, you look at the bit in xhash indexed by
     78  1.1  cgd  * hash(hashname("name"), i).  This is setup automatically
     79  1.1  cgd  * after .login is executed, and recomputed whenever ``path'' is
     80  1.1  cgd  * changed.
     81  1.1  cgd  * The two part hash function is designed to let texec() call the
     82  1.1  cgd  * more expensive hashname() only once and the simple hash() several
     83  1.1  cgd  * times (once for each path component checked).
     84  1.1  cgd  * Byte size is assumed to be 8.
     85  1.1  cgd  */
     86  1.1  cgd #define	HSHSIZ		8192	/* 1k bytes */
     87  1.1  cgd #define HSHMASK		(HSHSIZ - 1)
     88  1.1  cgd #define HSHMUL		243
     89  1.1  cgd static char xhash[HSHSIZ / 8];
     90  1.1  cgd 
     91  1.1  cgd #define hash(a, b)	((a) * HSHMUL + (b) & HSHMASK)
     92  1.1  cgd #define bit(h, b)	((h)[(b) >> 3] & 1 << ((b) & 7))	/* bit test */
     93  1.1  cgd #define bis(h, b)	((h)[(b) >> 3] |= 1 << ((b) & 7))	/* bit set */
     94  1.1  cgd static int hits, misses;
     95  1.1  cgd 
     96  1.1  cgd /* Dummy search path for just absolute search when no path */
     97  1.1  cgd static Char *justabs[] = {STRNULL, 0};
     98  1.1  cgd 
     99  1.1  cgd static void	pexerr __P((void));
    100  1.1  cgd static void	texec __P((Char *, Char **));
    101  1.1  cgd static int	hashname __P((Char *));
    102  1.1  cgd 
    103  1.1  cgd void
    104  1.1  cgd doexec(t)
    105  1.1  cgd     register struct command *t;
    106  1.1  cgd {
    107  1.1  cgd     register Char *dp, **pv, **av, *sav;
    108  1.1  cgd     register struct varent *v;
    109  1.1  cgd     register bool slash;
    110  1.1  cgd     register int hashval = 0, hashval1, i;
    111  1.1  cgd     Char   *blk[2];
    112  1.1  cgd 
    113  1.1  cgd     /*
    114  1.1  cgd      * Glob the command name. We will search $path even if this does something,
    115  1.1  cgd      * as in sh but not in csh.  One special case: if there is no PATH, then we
    116  1.1  cgd      * execute only commands which start with '/'.
    117  1.1  cgd      */
    118  1.1  cgd     blk[0] = t->t_dcom[0];
    119  1.1  cgd     blk[1] = 0;
    120  1.1  cgd     gflag = 0, tglob(blk);
    121  1.1  cgd     if (gflag) {
    122  1.1  cgd 	pv = globall(blk);
    123  1.1  cgd 	if (pv == 0) {
    124  1.1  cgd 	    setname(short2str(blk[0]));
    125  1.1  cgd 	    stderror(ERR_NAME | ERR_NOMATCH);
    126  1.1  cgd 	}
    127  1.1  cgd 	gargv = 0;
    128  1.1  cgd     }
    129  1.1  cgd     else
    130  1.1  cgd 	pv = saveblk(blk);
    131  1.1  cgd 
    132  1.1  cgd     trim(pv);
    133  1.1  cgd 
    134  1.1  cgd     exerr = 0;
    135  1.1  cgd     expath = Strsave(pv[0]);
    136  1.1  cgd     Vexpath = expath;
    137  1.1  cgd 
    138  1.1  cgd     v = adrof(STRpath);
    139  1.1  cgd     if (v == 0 && expath[0] != '/') {
    140  1.1  cgd 	blkfree(pv);
    141  1.1  cgd 	pexerr();
    142  1.1  cgd     }
    143  1.1  cgd     slash = any(short2str(expath), '/');
    144  1.1  cgd 
    145  1.1  cgd     /*
    146  1.1  cgd      * Glob the argument list, if necessary. Otherwise trim off the quote bits.
    147  1.1  cgd      */
    148  1.1  cgd     gflag = 0;
    149  1.1  cgd     av = &t->t_dcom[1];
    150  1.1  cgd     tglob(av);
    151  1.1  cgd     if (gflag) {
    152  1.1  cgd 	av = globall(av);
    153  1.1  cgd 	if (av == 0) {
    154  1.1  cgd 	    blkfree(pv);
    155  1.1  cgd 	    setname(short2str(expath));
    156  1.1  cgd 	    stderror(ERR_NAME | ERR_NOMATCH);
    157  1.1  cgd 	}
    158  1.1  cgd 	gargv = 0;
    159  1.1  cgd     }
    160  1.1  cgd     else
    161  1.1  cgd 	av = saveblk(av);
    162  1.1  cgd 
    163  1.1  cgd     blkfree(t->t_dcom);
    164  1.1  cgd     t->t_dcom = blkspl(pv, av);
    165  1.1  cgd     xfree((ptr_t) pv);
    166  1.1  cgd     xfree((ptr_t) av);
    167  1.1  cgd     av = t->t_dcom;
    168  1.1  cgd     trim(av);
    169  1.1  cgd 
    170  1.1  cgd     if (*av == NULL || **av == '\0')
    171  1.1  cgd 	pexerr();
    172  1.1  cgd 
    173  1.1  cgd     xechoit(av);		/* Echo command if -x */
    174  1.1  cgd     /*
    175  1.1  cgd      * Since all internal file descriptors are set to close on exec, we don't
    176  1.1  cgd      * need to close them explicitly here.  Just reorient ourselves for error
    177  1.1  cgd      * messages.
    178  1.1  cgd      */
    179  1.1  cgd     SHIN = 0;
    180  1.1  cgd     SHOUT = 1;
    181  1.1  cgd     SHDIAG = 2;
    182  1.1  cgd     OLDSTD = 0;
    183  1.1  cgd     /*
    184  1.1  cgd      * We must do this AFTER any possible forking (like `foo` in glob) so that
    185  1.1  cgd      * this shell can still do subprocesses.
    186  1.1  cgd      */
    187  1.1  cgd     (void) sigsetmask((sigset_t) 0);
    188  1.1  cgd     /*
    189  1.1  cgd      * If no path, no words in path, or a / in the filename then restrict the
    190  1.1  cgd      * command search.
    191  1.1  cgd      */
    192  1.1  cgd     if (v == 0 || v->vec[0] == 0 || slash)
    193  1.1  cgd 	pv = justabs;
    194  1.1  cgd     else
    195  1.1  cgd 	pv = v->vec;
    196  1.1  cgd     sav = Strspl(STRslash, *av);/* / command name for postpending */
    197  1.1  cgd     Vsav = sav;
    198  1.1  cgd     if (havhash)
    199  1.1  cgd 	hashval = hashname(*av);
    200  1.1  cgd     i = 0;
    201  1.1  cgd     hits++;
    202  1.1  cgd     do {
    203  1.1  cgd 	/*
    204  1.1  cgd 	 * Try to save time by looking at the hash table for where this command
    205  1.1  cgd 	 * could be.  If we are doing delayed hashing, then we put the names in
    206  1.1  cgd 	 * one at a time, as the user enters them.  This is kinda like Korn
    207  1.1  cgd 	 * Shell's "tracked aliases".
    208  1.1  cgd 	 */
    209  1.1  cgd 	if (!slash && pv[0][0] == '/' && havhash) {
    210  1.1  cgd 	    hashval1 = hash(hashval, i);
    211  1.1  cgd 	    if (!bit(xhash, hashval1))
    212  1.1  cgd 		goto cont;
    213  1.1  cgd 	}
    214  1.1  cgd 	if (pv[0][0] == 0 || eq(pv[0], STRdot))	/* don't make ./xxx */
    215  1.1  cgd 	    texec(*av, av);
    216  1.1  cgd 	else {
    217  1.1  cgd 	    dp = Strspl(*pv, sav);
    218  1.1  cgd 	    Vdp = dp;
    219  1.1  cgd 	    texec(dp, av);
    220  1.1  cgd 	    Vdp = 0;
    221  1.1  cgd 	    xfree((ptr_t) dp);
    222  1.1  cgd 	}
    223  1.1  cgd 	misses++;
    224  1.1  cgd cont:
    225  1.1  cgd 	pv++;
    226  1.1  cgd 	i++;
    227  1.1  cgd     } while (*pv);
    228  1.1  cgd     hits--;
    229  1.1  cgd     Vsav = 0;
    230  1.1  cgd     xfree((ptr_t) sav);
    231  1.1  cgd     pexerr();
    232  1.1  cgd }
    233  1.1  cgd 
    234  1.1  cgd static void
    235  1.1  cgd pexerr()
    236  1.1  cgd {
    237  1.1  cgd     /* Couldn't find the damn thing */
    238  1.1  cgd     if (expath) {
    239  1.1  cgd 	setname(short2str(expath));
    240  1.1  cgd 	Vexpath = 0;
    241  1.1  cgd 	xfree((ptr_t) expath);
    242  1.1  cgd 	expath = 0;
    243  1.1  cgd     }
    244  1.1  cgd     else
    245  1.1  cgd 	setname("");
    246  1.1  cgd     if (exerr)
    247  1.1  cgd 	stderror(ERR_NAME | ERR_STRING, exerr);
    248  1.1  cgd     stderror(ERR_NAME | ERR_COMMAND);
    249  1.1  cgd }
    250  1.1  cgd 
    251  1.1  cgd /*
    252  1.1  cgd  * Execute command f, arg list t.
    253  1.1  cgd  * Record error message if not found.
    254  1.1  cgd  * Also do shell scripts here.
    255  1.1  cgd  */
    256  1.1  cgd static void
    257  1.1  cgd texec(sf, st)
    258  1.1  cgd     Char   *sf;
    259  1.1  cgd     register Char **st;
    260  1.1  cgd {
    261  1.1  cgd     register char **t;
    262  1.1  cgd     register char *f;
    263  1.1  cgd     register struct varent *v;
    264  1.1  cgd     register Char **vp;
    265  1.1  cgd     Char   *lastsh[2];
    266  1.1  cgd     int     fd;
    267  1.1  cgd     unsigned char c;
    268  1.1  cgd     Char   *st0, **ost;
    269  1.1  cgd 
    270  1.1  cgd     /* The order for the conversions is significant */
    271  1.1  cgd     t = short2blk(st);
    272  1.1  cgd     f = short2str(sf);
    273  1.1  cgd     Vt = t;
    274  1.1  cgd     errno = 0;			/* don't use a previous error */
    275  1.1  cgd     (void) execve(f, t, environ);
    276  1.1  cgd     Vt = 0;
    277  1.1  cgd     blkfree((Char **) t);
    278  1.1  cgd     switch (errno) {
    279  1.1  cgd 
    280  1.1  cgd     case ENOEXEC:
    281  1.1  cgd 	/*
    282  1.1  cgd 	 * From: casper (at) fwi.uva.nl (Casper H.S. Dik) If we could not execute
    283  1.1  cgd 	 * it, don't feed it to the shell if it looks like a binary!
    284  1.1  cgd 	 */
    285  1.1  cgd 	if ((fd = open(f, O_RDONLY)) != -1) {
    286  1.1  cgd 	    if (read(fd, (char *) &c, 1) == 1) {
    287  1.1  cgd 		if (!Isprint(c) && (c != '\n' && c != '\t')) {
    288  1.1  cgd 		    (void) close(fd);
    289  1.1  cgd 		    /*
    290  1.1  cgd 		     * We *know* what ENOEXEC means.
    291  1.1  cgd 		     */
    292  1.1  cgd 		    stderror(ERR_ARCH, f, strerror(errno));
    293  1.1  cgd 		}
    294  1.1  cgd 	    }
    295  1.1  cgd #ifdef _PATH_BSHELL
    296  1.1  cgd 	    else
    297  1.1  cgd 		c = '#';
    298  1.1  cgd #endif
    299  1.1  cgd 	    (void) close(fd);
    300  1.1  cgd 	}
    301  1.1  cgd 	/*
    302  1.1  cgd 	 * If there is an alias for shell, then put the words of the alias in
    303  1.1  cgd 	 * front of the argument list replacing the command name. Note no
    304  1.1  cgd 	 * interpretation of the words at this point.
    305  1.1  cgd 	 */
    306  1.1  cgd 	v = adrof1(STRshell, &aliases);
    307  1.1  cgd 	if (v == 0) {
    308  1.1  cgd 	    vp = lastsh;
    309  1.1  cgd 	    vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
    310  1.1  cgd 	    vp[1] = NULL;
    311  1.1  cgd #ifdef _PATH_BSHELL
    312  1.1  cgd 	    if (fd != -1 && c != '#')
    313  1.1  cgd 		vp[0] = STR_BSHELL;
    314  1.1  cgd #endif
    315  1.1  cgd 	}
    316  1.1  cgd 	else
    317  1.1  cgd 	    vp = v->vec;
    318  1.1  cgd 	st0 = st[0];
    319  1.1  cgd 	st[0] = sf;
    320  1.1  cgd 	ost = st;
    321  1.1  cgd 	st = blkspl(vp, st);	/* Splice up the new arglst */
    322  1.1  cgd 	ost[0] = st0;
    323  1.1  cgd 	sf = *st;
    324  1.1  cgd 	/* The order for the conversions is significant */
    325  1.1  cgd 	t = short2blk(st);
    326  1.1  cgd 	f = short2str(sf);
    327  1.1  cgd 	xfree((ptr_t) st);
    328  1.1  cgd 	Vt = t;
    329  1.1  cgd 	(void) execve(f, t, environ);
    330  1.1  cgd 	Vt = 0;
    331  1.1  cgd 	blkfree((Char **) t);
    332  1.1  cgd 	/* The sky is falling, the sky is falling! */
    333  1.1  cgd 
    334  1.1  cgd     case ENOMEM:
    335  1.1  cgd 	stderror(ERR_SYSTEM, f, strerror(errno));
    336  1.1  cgd 
    337  1.1  cgd     case ENOENT:
    338  1.1  cgd 	break;
    339  1.1  cgd 
    340  1.1  cgd     default:
    341  1.1  cgd 	if (exerr == 0) {
    342  1.1  cgd 	    exerr = strerror(errno);
    343  1.1  cgd 	    if (expath)
    344  1.1  cgd 		xfree((ptr_t) expath);
    345  1.1  cgd 	    expath = Strsave(sf);
    346  1.1  cgd 	    Vexpath = expath;
    347  1.1  cgd 	}
    348  1.1  cgd     }
    349  1.1  cgd }
    350  1.1  cgd 
    351  1.1  cgd /*ARGSUSED*/
    352  1.1  cgd void
    353  1.1  cgd execash(t, kp)
    354  1.1  cgd     char  **t;
    355  1.1  cgd     register struct command *kp;
    356  1.1  cgd {
    357  1.1  cgd     if (chkstop == 0 && setintr)
    358  1.1  cgd 	panystop(0);
    359  1.1  cgd     rechist();
    360  1.1  cgd     (void) signal(SIGINT, parintr);
    361  1.1  cgd     (void) signal(SIGQUIT, parintr);
    362  1.1  cgd     (void) signal(SIGTERM, parterm);	/* if doexec loses, screw */
    363  1.1  cgd     lshift(kp->t_dcom, 1);
    364  1.1  cgd     exiterr = 1;
    365  1.1  cgd     doexec(kp);
    366  1.1  cgd     /* NOTREACHED */
    367  1.1  cgd }
    368  1.1  cgd 
    369  1.1  cgd void
    370  1.1  cgd xechoit(t)
    371  1.1  cgd     Char  **t;
    372  1.1  cgd {
    373  1.1  cgd     if (adrof(STRecho)) {
    374  1.1  cgd 	flush();
    375  1.1  cgd 	haderr = 1;
    376  1.1  cgd 	blkpr(t), xputchar('\n');
    377  1.1  cgd 	haderr = 0;
    378  1.1  cgd     }
    379  1.1  cgd }
    380  1.1  cgd 
    381  1.1  cgd /*VARARGS0*/
    382  1.1  cgd void
    383  1.1  cgd dohash()
    384  1.1  cgd {
    385  1.1  cgd     DIR    *dirp;
    386  1.1  cgd     register struct dirent *dp;
    387  1.1  cgd     register int cnt;
    388  1.1  cgd     int     i = 0;
    389  1.1  cgd     struct varent *v = adrof(STRpath);
    390  1.1  cgd     Char  **pv;
    391  1.1  cgd     int     hashval;
    392  1.1  cgd 
    393  1.1  cgd     havhash = 1;
    394  1.1  cgd     for (cnt = 0; cnt < sizeof xhash; cnt++)
    395  1.1  cgd 	xhash[cnt] = 0;
    396  1.1  cgd     if (v == 0)
    397  1.1  cgd 	return;
    398  1.1  cgd     for (pv = v->vec; *pv; pv++, i++) {
    399  1.1  cgd 	if (pv[0][0] != '/')
    400  1.1  cgd 	    continue;
    401  1.1  cgd 	dirp = opendir(short2str(*pv));
    402  1.1  cgd 	if (dirp == NULL)
    403  1.1  cgd 	    continue;
    404  1.1  cgd 	while ((dp = readdir(dirp)) != NULL) {
    405  1.1  cgd 	    if (dp->d_ino == 0)
    406  1.1  cgd 		continue;
    407  1.1  cgd 	    if (dp->d_name[0] == '.' &&
    408  1.1  cgd 		(dp->d_name[1] == '\0' ||
    409  1.1  cgd 		 dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
    410  1.1  cgd 		continue;
    411  1.1  cgd 	    hashval = hash(hashname(str2short(dp->d_name)), i);
    412  1.1  cgd 	    bis(xhash, hashval);
    413  1.1  cgd 	    /* tw_add_comm_name (dp->d_name); */
    414  1.1  cgd 	}
    415  1.1  cgd 	(void) closedir(dirp);
    416  1.1  cgd     }
    417  1.1  cgd }
    418  1.1  cgd 
    419  1.1  cgd void
    420  1.1  cgd dounhash()
    421  1.1  cgd {
    422  1.1  cgd     havhash = 0;
    423  1.1  cgd }
    424  1.1  cgd 
    425  1.1  cgd void
    426  1.1  cgd hashstat()
    427  1.1  cgd {
    428  1.1  cgd     if (hits + misses)
    429  1.1  cgd 	xprintf("%d hits, %d misses, %d%%\n",
    430  1.1  cgd 		hits, misses, 100 * hits / (hits + misses));
    431  1.1  cgd }
    432  1.1  cgd 
    433  1.1  cgd /*
    434  1.1  cgd  * Hash a command name.
    435  1.1  cgd  */
    436  1.1  cgd static int
    437  1.1  cgd hashname(cp)
    438  1.1  cgd     register Char *cp;
    439  1.1  cgd {
    440  1.1  cgd     register long h = 0;
    441  1.1  cgd 
    442  1.1  cgd     while (*cp)
    443  1.1  cgd 	h = hash(h, *cp++);
    444  1.1  cgd     return ((int) h);
    445  1.1  cgd }
    446