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