exec.c revision 18018
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	$Id: exec.c,v 1.5 1996/09/01 10:20:02 peter Exp $
37 */
38
39#ifndef lint
40static char sccsid[] = "@(#)exec.c	8.4 (Berkeley) 6/8/95";
41#endif /* not lint */
42
43#include <sys/types.h>
44#include <sys/stat.h>
45#include <unistd.h>
46#include <fcntl.h>
47#include <errno.h>
48#include <stdlib.h>
49
50/*
51 * When commands are first encountered, they are entered in a hash table.
52 * This ensures that a full path search will not have to be done for them
53 * on each invocation.
54 *
55 * We should investigate converting to a linear search, even though that
56 * would make the command name "hash" a misnomer.
57 */
58
59#include "shell.h"
60#include "main.h"
61#include "nodes.h"
62#include "parser.h"
63#include "redir.h"
64#include "eval.h"
65#include "exec.h"
66#include "builtins.h"
67#include "var.h"
68#include "options.h"
69#include "input.h"
70#include "output.h"
71#include "syntax.h"
72#include "memalloc.h"
73#include "error.h"
74#include "init.h"
75#include "mystring.h"
76#include "show.h"
77#include "jobs.h"
78
79
80#define CMDTABLESIZE 31		/* should be prime */
81#define ARB 1			/* actual size determined at run time */
82
83
84
85struct tblentry {
86	struct tblentry *next;	/* next entry in hash chain */
87	union param param;	/* definition of builtin function */
88	short cmdtype;		/* index identifying command */
89	char rehash;		/* if set, cd done since entry created */
90	char cmdname[ARB];	/* name of command */
91};
92
93
94STATIC struct tblentry *cmdtable[CMDTABLESIZE];
95STATIC int builtinloc = -1;		/* index in path of %builtin, or -1 */
96
97
98STATIC void tryexec __P((char *, char **, char **));
99STATIC void execinterp __P((char **, char **));
100STATIC void printentry __P((struct tblentry *, int));
101STATIC void clearcmdentry __P((int));
102STATIC struct tblentry *cmdlookup __P((char *, int));
103STATIC void delete_cmd_entry __P((void));
104
105
106
107/*
108 * Exec a program.  Never returns.  If you change this routine, you may
109 * have to change the find_command routine as well.
110 */
111
112void
113shellexec(argv, envp, path, index)
114	char **argv, **envp;
115	char *path;
116	int index;
117{
118	char *cmdname;
119	int e;
120
121	if (strchr(argv[0], '/') != NULL) {
122		tryexec(argv[0], argv, envp);
123		e = errno;
124	} else {
125		e = ENOENT;
126		while ((cmdname = padvance(&path, argv[0])) != NULL) {
127			if (--index < 0 && pathopt == NULL) {
128				tryexec(cmdname, argv, envp);
129				if (errno != ENOENT && errno != ENOTDIR)
130					e = errno;
131			}
132			stunalloc(cmdname);
133		}
134	}
135	error("%s: %s", argv[0], errmsg(e, E_EXEC));
136}
137
138
139STATIC void
140tryexec(cmd, argv, envp)
141	char *cmd;
142	char **argv;
143	char **envp;
144	{
145	int e;
146#ifndef BSD
147	char *p;
148#endif
149
150#ifdef SYSV
151	do {
152		execve(cmd, argv, envp);
153	} while (errno == EINTR);
154#else
155	execve(cmd, argv, envp);
156#endif
157	e = errno;
158	if (e == ENOEXEC) {
159		initshellproc();
160		setinputfile(cmd, 0);
161		commandname = arg0 = savestr(argv[0]);
162#ifndef BSD
163		pgetc(); pungetc();		/* fill up input buffer */
164		p = parsenextc;
165		if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
166			argv[0] = cmd;
167			execinterp(argv, envp);
168		}
169#endif
170		setparam(argv + 1);
171		exraise(EXSHELLPROC);
172		/*NOTREACHED*/
173	}
174	errno = e;
175}
176
177
178#ifndef BSD
179/*
180 * Execute an interpreter introduced by "#!", for systems where this
181 * feature has not been built into the kernel.  If the interpreter is
182 * the shell, return (effectively ignoring the "#!").  If the execution
183 * of the interpreter fails, exit.
184 *
185 * This code peeks inside the input buffer in order to avoid actually
186 * reading any input.  It would benefit from a rewrite.
187 */
188
189#define NEWARGS 5
190
191STATIC void
192execinterp(argv, envp)
193	char **argv, **envp;
194	{
195	int n;
196	char *inp;
197	char *outp;
198	char c;
199	char *p;
200	char **ap;
201	char *newargs[NEWARGS];
202	int i;
203	char **ap2;
204	char **new;
205
206	n = parsenleft - 2;
207	inp = parsenextc + 2;
208	ap = newargs;
209	for (;;) {
210		while (--n >= 0 && (*inp == ' ' || *inp == '\t'))
211			inp++;
212		if (n < 0)
213			goto bad;
214		if ((c = *inp++) == '\n')
215			break;
216		if (ap == &newargs[NEWARGS])
217bad:		  error("Bad #! line");
218		STARTSTACKSTR(outp);
219		do {
220			STPUTC(c, outp);
221		} while (--n >= 0 && (c = *inp++) != ' ' && c != '\t' && c != '\n');
222		STPUTC('\0', outp);
223		n++, inp--;
224		*ap++ = grabstackstr(outp);
225	}
226	if (ap == newargs + 1) {	/* if no args, maybe no exec is needed */
227		p = newargs[0];
228		for (;;) {
229			if (equal(p, "sh") || equal(p, "ash")) {
230				return;
231			}
232			while (*p != '/') {
233				if (*p == '\0')
234					goto break2;
235				p++;
236			}
237			p++;
238		}
239break2:;
240	}
241	i = (char *)ap - (char *)newargs;		/* size in bytes */
242	if (i == 0)
243		error("Bad #! line");
244	for (ap2 = argv ; *ap2++ != NULL ; );
245	new = ckmalloc(i + ((char *)ap2 - (char *)argv));
246	ap = newargs, ap2 = new;
247	while ((i -= sizeof (char **)) >= 0)
248		*ap2++ = *ap++;
249	ap = argv;
250	while (*ap2++ = *ap++);
251	shellexec(new, envp, pathval(), 0);
252}
253#endif
254
255
256
257/*
258 * Do a path search.  The variable path (passed by reference) should be
259 * set to the start of the path before the first call; padvance will update
260 * this value as it proceeds.  Successive calls to padvance will return
261 * the possible path expansions in sequence.  If an option (indicated by
262 * a percent sign) appears in the path entry then the global variable
263 * pathopt will be set to point to it; otherwise pathopt will be set to
264 * NULL.
265 */
266
267char *pathopt;
268
269char *
270padvance(path, name)
271	char **path;
272	char *name;
273	{
274	register char *p, *q;
275	char *start;
276	int len;
277
278	if (*path == NULL)
279		return NULL;
280	start = *path;
281	for (p = start ; *p && *p != ':' && *p != '%' ; p++);
282	len = p - start + strlen(name) + 2;	/* "2" is for '/' and '\0' */
283	while (stackblocksize() < len)
284		growstackblock();
285	q = stackblock();
286	if (p != start) {
287		memcpy(q, start, p - start);
288		q += p - start;
289		*q++ = '/';
290	}
291	strcpy(q, name);
292	pathopt = NULL;
293	if (*p == '%') {
294		pathopt = ++p;
295		while (*p && *p != ':')  p++;
296	}
297	if (*p == ':')
298		*path = p + 1;
299	else
300		*path = NULL;
301	return stalloc(len);
302}
303
304
305
306/*** Command hashing code ***/
307
308
309int
310hashcmd(argc, argv)
311	int argc;
312	char **argv;
313{
314	struct tblentry **pp;
315	struct tblentry *cmdp;
316	int c;
317	int verbose;
318	struct cmdentry entry;
319	char *name;
320
321	verbose = 0;
322	while ((c = nextopt("rv")) != '\0') {
323		if (c == 'r') {
324			clearcmdentry(0);
325		} else if (c == 'v') {
326			verbose++;
327		}
328	}
329	if (*argptr == NULL) {
330		for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
331			for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
332				printentry(cmdp, verbose);
333			}
334		}
335		return 0;
336	}
337	while ((name = *argptr) != NULL) {
338		if ((cmdp = cmdlookup(name, 0)) != NULL
339		 && (cmdp->cmdtype == CMDNORMAL
340		     || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)))
341			delete_cmd_entry();
342		find_command(name, &entry, 1, pathval());
343		if (verbose) {
344			if (entry.cmdtype != CMDUNKNOWN) {	/* if no error msg */
345				cmdp = cmdlookup(name, 0);
346				printentry(cmdp, verbose);
347			}
348			flushall();
349		}
350		argptr++;
351	}
352	return 0;
353}
354
355
356STATIC void
357printentry(cmdp, verbose)
358	struct tblentry *cmdp;
359	int verbose;
360	{
361	int index;
362	char *path;
363	char *name;
364
365	if (cmdp->cmdtype == CMDNORMAL) {
366		index = cmdp->param.index;
367		path = pathval();
368		do {
369			name = padvance(&path, cmdp->cmdname);
370			stunalloc(name);
371		} while (--index >= 0);
372		out1str(name);
373	} else if (cmdp->cmdtype == CMDBUILTIN) {
374		out1fmt("builtin %s", cmdp->cmdname);
375	} else if (cmdp->cmdtype == CMDFUNCTION) {
376		out1fmt("function %s", cmdp->cmdname);
377		if (verbose) {
378			INTOFF;
379			name = commandtext(cmdp->param.func);
380			out1c(' ');
381			out1str(name);
382			ckfree(name);
383			INTON;
384		}
385#ifdef DEBUG
386	} else {
387		error("internal error: cmdtype %d", cmdp->cmdtype);
388#endif
389	}
390	if (cmdp->rehash)
391		out1c('*');
392	out1c('\n');
393}
394
395
396
397/*
398 * Resolve a command name.  If you change this routine, you may have to
399 * change the shellexec routine as well.
400 */
401
402void
403find_command(name, entry, printerr, path)
404	char *name;
405	struct cmdentry *entry;
406	int printerr;
407	char *path;
408{
409	struct tblentry *cmdp;
410	int index;
411	int prev;
412	char *fullname;
413	struct stat statb;
414	int e;
415	int i;
416
417	/* If name contains a slash, don't use the hash table */
418	if (strchr(name, '/') != NULL) {
419		entry->cmdtype = CMDNORMAL;
420		entry->u.index = 0;
421		return;
422	}
423
424	/* If name is in the table, and not invalidated by cd, we're done */
425	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->rehash == 0)
426		goto success;
427
428	/* If %builtin not in path, check for builtin next */
429	if (builtinloc < 0 && (i = find_builtin(name)) >= 0) {
430		INTOFF;
431		cmdp = cmdlookup(name, 1);
432		cmdp->cmdtype = CMDBUILTIN;
433		cmdp->param.index = i;
434		INTON;
435		goto success;
436	}
437
438	/* We have to search path. */
439	prev = -1;		/* where to start */
440	if (cmdp) {		/* doing a rehash */
441		if (cmdp->cmdtype == CMDBUILTIN)
442			prev = builtinloc;
443		else
444			prev = cmdp->param.index;
445	}
446
447	e = ENOENT;
448	index = -1;
449loop:
450	while ((fullname = padvance(&path, name)) != NULL) {
451		stunalloc(fullname);
452		index++;
453		if (pathopt) {
454			if (prefix("builtin", pathopt)) {
455				if ((i = find_builtin(name)) < 0)
456					goto loop;
457				INTOFF;
458				cmdp = cmdlookup(name, 1);
459				cmdp->cmdtype = CMDBUILTIN;
460				cmdp->param.index = i;
461				INTON;
462				goto success;
463			} else if (prefix("func", pathopt)) {
464				/* handled below */
465			} else {
466				goto loop;	/* ignore unimplemented options */
467			}
468		}
469		/* if rehash, don't redo absolute path names */
470		if (fullname[0] == '/' && index <= prev) {
471			if (index < prev)
472				goto loop;
473			TRACE(("searchexec \"%s\": no change\n", name));
474			goto success;
475		}
476		while (stat(fullname, &statb) < 0) {
477#ifdef SYSV
478			if (errno == EINTR)
479				continue;
480#endif
481			if (errno != ENOENT && errno != ENOTDIR)
482				e = errno;
483			goto loop;
484		}
485		e = EACCES;	/* if we fail, this will be the error */
486		if (!S_ISREG(statb.st_mode))
487			goto loop;
488		if (pathopt) {		/* this is a %func directory */
489			stalloc(strlen(fullname) + 1);
490			readcmdfile(fullname);
491			if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION)
492				error("%s not defined in %s", name, fullname);
493			stunalloc(fullname);
494			goto success;
495		}
496#ifdef notdef
497		if (statb.st_uid == geteuid()) {
498			if ((statb.st_mode & 0100) == 0)
499				goto loop;
500		} else if (statb.st_gid == getegid()) {
501			if ((statb.st_mode & 010) == 0)
502				goto loop;
503		} else {
504			if ((statb.st_mode & 01) == 0)
505				goto loop;
506		}
507#endif
508		TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
509		INTOFF;
510		cmdp = cmdlookup(name, 1);
511		cmdp->cmdtype = CMDNORMAL;
512		cmdp->param.index = index;
513		INTON;
514		goto success;
515	}
516
517	/* We failed.  If there was an entry for this command, delete it */
518	if (cmdp)
519		delete_cmd_entry();
520	if (printerr)
521		outfmt(out2, "%s: %s\n", name, errmsg(e, E_EXEC));
522	entry->cmdtype = CMDUNKNOWN;
523	return;
524
525success:
526	cmdp->rehash = 0;
527	entry->cmdtype = cmdp->cmdtype;
528	entry->u = cmdp->param;
529}
530
531
532
533/*
534 * Search the table of builtin commands.
535 */
536
537int
538find_builtin(name)
539	char *name;
540{
541	register const struct builtincmd *bp;
542
543	for (bp = builtincmd ; bp->name ; bp++) {
544		if (*bp->name == *name && equal(bp->name, name))
545			return bp->code;
546	}
547	return -1;
548}
549
550
551
552/*
553 * Called when a cd is done.  Marks all commands so the next time they
554 * are executed they will be rehashed.
555 */
556
557void
558hashcd() {
559	struct tblentry **pp;
560	struct tblentry *cmdp;
561
562	for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) {
563		for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
564			if (cmdp->cmdtype == CMDNORMAL
565			 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
566				cmdp->rehash = 1;
567		}
568	}
569}
570
571
572
573/*
574 * Called before PATH is changed.  The argument is the new value of PATH;
575 * pathval() still returns the old value at this point.  Called with
576 * interrupts off.
577 */
578
579void
580changepath(newval)
581	char *newval;
582{
583	char *old, *new;
584	int index;
585	int firstchange;
586	int bltin;
587
588	old = pathval();
589	new = newval;
590	firstchange = 9999;	/* assume no change */
591	index = 0;
592	bltin = -1;
593	for (;;) {
594		if (*old != *new) {
595			firstchange = index;
596			if ((*old == '\0' && *new == ':')
597			 || (*old == ':' && *new == '\0'))
598				firstchange++;
599			old = new;	/* ignore subsequent differences */
600		}
601		if (*new == '\0')
602			break;
603		if (*new == '%' && bltin < 0 && prefix("builtin", new + 1))
604			bltin = index;
605		if (*new == ':') {
606			index++;
607		}
608		new++, old++;
609	}
610	if (builtinloc < 0 && bltin >= 0)
611		builtinloc = bltin;		/* zap builtins */
612	if (builtinloc >= 0 && bltin < 0)
613		firstchange = 0;
614	clearcmdentry(firstchange);
615	builtinloc = bltin;
616}
617
618
619/*
620 * Clear out command entries.  The argument specifies the first entry in
621 * PATH which has changed.
622 */
623
624STATIC void
625clearcmdentry(firstchange)
626	int firstchange;
627{
628	struct tblentry **tblp;
629	struct tblentry **pp;
630	struct tblentry *cmdp;
631
632	INTOFF;
633	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
634		pp = tblp;
635		while ((cmdp = *pp) != NULL) {
636			if ((cmdp->cmdtype == CMDNORMAL &&
637			     cmdp->param.index >= firstchange)
638			 || (cmdp->cmdtype == CMDBUILTIN &&
639			     builtinloc >= firstchange)) {
640				*pp = cmdp->next;
641				ckfree(cmdp);
642			} else {
643				pp = &cmdp->next;
644			}
645		}
646	}
647	INTON;
648}
649
650
651/*
652 * Delete all functions.
653 */
654
655#ifdef mkinit
656MKINIT void deletefuncs();
657
658SHELLPROC {
659	deletefuncs();
660}
661#endif
662
663void
664deletefuncs() {
665	struct tblentry **tblp;
666	struct tblentry **pp;
667	struct tblentry *cmdp;
668
669	INTOFF;
670	for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) {
671		pp = tblp;
672		while ((cmdp = *pp) != NULL) {
673			if (cmdp->cmdtype == CMDFUNCTION) {
674				*pp = cmdp->next;
675				freefunc(cmdp->param.func);
676				ckfree(cmdp);
677			} else {
678				pp = &cmdp->next;
679			}
680		}
681	}
682	INTON;
683}
684
685
686
687/*
688 * Locate a command in the command hash table.  If "add" is nonzero,
689 * add the command to the table if it is not already present.  The
690 * variable "lastcmdentry" is set to point to the address of the link
691 * pointing to the entry, so that delete_cmd_entry can delete the
692 * entry.
693 */
694
695struct tblentry **lastcmdentry;
696
697
698STATIC struct tblentry *
699cmdlookup(name, add)
700	char *name;
701	int add;
702{
703	int hashval;
704	register char *p;
705	struct tblentry *cmdp;
706	struct tblentry **pp;
707
708	p = name;
709	hashval = *p << 4;
710	while (*p)
711		hashval += *p++;
712	hashval &= 0x7FFF;
713	pp = &cmdtable[hashval % CMDTABLESIZE];
714	for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) {
715		if (equal(cmdp->cmdname, name))
716			break;
717		pp = &cmdp->next;
718	}
719	if (add && cmdp == NULL) {
720		INTOFF;
721		cmdp = *pp = ckmalloc(sizeof (struct tblentry) - ARB
722					+ strlen(name) + 1);
723		cmdp->next = NULL;
724		cmdp->cmdtype = CMDUNKNOWN;
725		cmdp->rehash = 0;
726		strcpy(cmdp->cmdname, name);
727		INTON;
728	}
729	lastcmdentry = pp;
730	return cmdp;
731}
732
733/*
734 * Delete the command entry returned on the last lookup.
735 */
736
737STATIC void
738delete_cmd_entry() {
739	struct tblentry *cmdp;
740
741	INTOFF;
742	cmdp = *lastcmdentry;
743	*lastcmdentry = cmdp->next;
744	ckfree(cmdp);
745	INTON;
746}
747
748
749
750#ifdef notdef
751void
752getcmdentry(name, entry)
753	char *name;
754	struct cmdentry *entry;
755	{
756	struct tblentry *cmdp = cmdlookup(name, 0);
757
758	if (cmdp) {
759		entry->u = cmdp->param;
760		entry->cmdtype = cmdp->cmdtype;
761	} else {
762		entry->cmdtype = CMDUNKNOWN;
763		entry->u.index = 0;
764	}
765}
766#endif
767
768
769/*
770 * Add a new command entry, replacing any existing command entry for
771 * the same name.
772 */
773
774void
775addcmdentry(name, entry)
776	char *name;
777	struct cmdentry *entry;
778	{
779	struct tblentry *cmdp;
780
781	INTOFF;
782	cmdp = cmdlookup(name, 1);
783	if (cmdp->cmdtype == CMDFUNCTION) {
784		freefunc(cmdp->param.func);
785	}
786	cmdp->cmdtype = entry->cmdtype;
787	cmdp->param = entry->u;
788	INTON;
789}
790
791
792/*
793 * Define a shell function.
794 */
795
796void
797defun(name, func)
798	char *name;
799	union node *func;
800	{
801	struct cmdentry entry;
802
803	INTOFF;
804	entry.cmdtype = CMDFUNCTION;
805	entry.u.func = copyfunc(func);
806	addcmdentry(name, &entry);
807	INTON;
808}
809
810
811/*
812 * Delete a function if it exists.
813 */
814
815int
816unsetfunc(name)
817	char *name;
818	{
819	struct tblentry *cmdp;
820
821	if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) {
822		freefunc(cmdp->param.func);
823		delete_cmd_entry();
824		return (0);
825	}
826	return (1);
827}
828