exec.c revision 1.6 1 /* $NetBSD: exec.c,v 1.6 1995/03/21 09:02:49 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)exec.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: exec.c,v 1.6 1995/03/21 09:02:49 cgd Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <dirent.h>
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #if __STDC__
54 # include <stdarg.h>
55 #else
56 # include <varargs.h>
57 #endif
58
59 #include "csh.h"
60 #include "extern.h"
61
62 /*
63 * System level search and execute of a command. We look in each directory
64 * for the specified command name. If the name contains a '/' then we
65 * execute only the full path name. If there is no search path then we
66 * execute only full path names.
67 */
68 extern char **environ;
69
70 /*
71 * As we search for the command we note the first non-trivial error
72 * message for presentation to the user. This allows us often
73 * to show that a file has the wrong mode/no access when the file
74 * is not in the last component of the search path, so we must
75 * go on after first detecting the error.
76 */
77 static char *exerr; /* Execution error message */
78 static Char *expath; /* Path for exerr */
79
80 /*
81 * Xhash is an array of HSHSIZ bits (HSHSIZ / 8 chars), which are used
82 * to hash execs. If it is allocated (havhash true), then to tell
83 * whether ``name'' is (possibly) present in the i'th component
84 * of the variable path, you look at the bit in xhash indexed by
85 * hash(hashname("name"), i). This is setup automatically
86 * after .login is executed, and recomputed whenever ``path'' is
87 * changed.
88 * The two part hash function is designed to let texec() call the
89 * more expensive hashname() only once and the simple hash() several
90 * times (once for each path component checked).
91 * Byte size is assumed to be 8.
92 */
93 #define HSHSIZ 8192 /* 1k bytes */
94 #define HSHMASK (HSHSIZ - 1)
95 #define HSHMUL 243
96 static char xhash[HSHSIZ / 8];
97
98 #define hash(a, b) (((a) * HSHMUL + (b)) & HSHMASK)
99 #define bit(h, b) ((h)[(b) >> 3] & 1 << ((b) & 7)) /* bit test */
100 #define bis(h, b) ((h)[(b) >> 3] |= 1 << ((b) & 7)) /* bit set */
101 static int hits, misses;
102
103 /* Dummy search path for just absolute search when no path */
104 static Char *justabs[] = {STRNULL, 0};
105
106 static void pexerr __P((void));
107 static void texec __P((Char *, Char **));
108 static int hashname __P((Char *));
109 static void tellmewhat __P((struct wordent *));
110 static int executable __P((Char *, Char *, bool));
111 static int iscommand __P((Char *));
112
113
114 void
115 /*ARGSUSED*/
116 doexec(v, t)
117 Char **v;
118 struct command *t;
119 {
120 register Char *dp, **pv, **av, *sav;
121 register struct varent *pathv;
122 register bool slash;
123 register int hashval = 0, hashval1, i;
124 Char *blk[2];
125
126 /*
127 * Glob the command name. We will search $path even if this does something,
128 * as in sh but not in csh. One special case: if there is no PATH, then we
129 * execute only commands which start with '/'.
130 */
131 blk[0] = t->t_dcom[0];
132 blk[1] = 0;
133 gflag = 0, tglob(blk);
134 if (gflag) {
135 pv = globall(blk);
136 if (pv == 0) {
137 setname(vis_str(blk[0]));
138 stderror(ERR_NAME | ERR_NOMATCH);
139 }
140 gargv = 0;
141 }
142 else
143 pv = saveblk(blk);
144
145 trim(pv);
146
147 exerr = 0;
148 expath = Strsave(pv[0]);
149 Vexpath = expath;
150
151 pathv = adrof(STRpath);
152 if (pathv == 0 && expath[0] != '/') {
153 blkfree(pv);
154 pexerr();
155 }
156 slash = any(short2str(expath), '/');
157
158 /*
159 * Glob the argument list, if necessary. Otherwise trim off the quote bits.
160 */
161 gflag = 0;
162 av = &t->t_dcom[1];
163 tglob(av);
164 if (gflag) {
165 av = globall(av);
166 if (av == 0) {
167 blkfree(pv);
168 setname(vis_str(expath));
169 stderror(ERR_NAME | ERR_NOMATCH);
170 }
171 gargv = 0;
172 }
173 else
174 av = saveblk(av);
175
176 blkfree(t->t_dcom);
177 t->t_dcom = blkspl(pv, av);
178 xfree((ptr_t) pv);
179 xfree((ptr_t) av);
180 av = t->t_dcom;
181 trim(av);
182
183 if (*av == NULL || **av == '\0')
184 pexerr();
185
186 xechoit(av); /* Echo command if -x */
187 /*
188 * Since all internal file descriptors are set to close on exec, we don't
189 * need to close them explicitly here. Just reorient ourselves for error
190 * messages.
191 */
192 SHIN = 0;
193 SHOUT = 1;
194 SHERR = 2;
195 OLDSTD = 0;
196 /*
197 * We must do this AFTER any possible forking (like `foo` in glob) so that
198 * this shell can still do subprocesses.
199 */
200 (void) sigsetmask((sigset_t) 0);
201 /*
202 * If no path, no words in path, or a / in the filename then restrict the
203 * command search.
204 */
205 if (pathv == 0 || pathv->vec[0] == 0 || slash)
206 pv = justabs;
207 else
208 pv = pathv->vec;
209 sav = Strspl(STRslash, *av);/* / command name for postpending */
210 Vsav = sav;
211 if (havhash)
212 hashval = hashname(*av);
213 i = 0;
214 hits++;
215 do {
216 /*
217 * Try to save time by looking at the hash table for where this command
218 * could be. If we are doing delayed hashing, then we put the names in
219 * one at a time, as the user enters them. This is kinda like Korn
220 * Shell's "tracked aliases".
221 */
222 if (!slash && pv[0][0] == '/' && havhash) {
223 hashval1 = hash(hashval, i);
224 if (!bit(xhash, hashval1))
225 goto cont;
226 }
227 if (pv[0][0] == 0 || eq(pv[0], STRdot)) /* don't make ./xxx */
228 texec(*av, av);
229 else {
230 dp = Strspl(*pv, sav);
231 Vdp = dp;
232 texec(dp, av);
233 Vdp = 0;
234 xfree((ptr_t) dp);
235 }
236 misses++;
237 cont:
238 pv++;
239 i++;
240 } while (*pv);
241 hits--;
242 Vsav = 0;
243 xfree((ptr_t) sav);
244 pexerr();
245 }
246
247 static void
248 pexerr()
249 {
250 /* Couldn't find the damn thing */
251 if (expath) {
252 setname(vis_str(expath));
253 Vexpath = 0;
254 xfree((ptr_t) expath);
255 expath = 0;
256 }
257 else
258 setname("");
259 if (exerr)
260 stderror(ERR_NAME | ERR_STRING, exerr);
261 stderror(ERR_NAME | ERR_COMMAND);
262 }
263
264 /*
265 * Execute command f, arg list t.
266 * Record error message if not found.
267 * Also do shell scripts here.
268 */
269 static void
270 texec(sf, st)
271 Char *sf;
272 register Char **st;
273 {
274 register char **t;
275 register char *f;
276 register struct varent *v;
277 register Char **vp;
278 Char *lastsh[2];
279 int fd;
280 unsigned char c;
281 Char *st0, **ost;
282
283 /* The order for the conversions is significant */
284 t = short2blk(st);
285 f = short2str(sf);
286 Vt = t;
287 errno = 0; /* don't use a previous error */
288 (void) execve(f, t, environ);
289 Vt = 0;
290 blkfree((Char **) t);
291 switch (errno) {
292
293 case ENOEXEC:
294 /*
295 * From: casper (at) fwi.uva.nl (Casper H.S. Dik) If we could not execute
296 * it, don't feed it to the shell if it looks like a binary!
297 */
298 if ((fd = open(f, O_RDONLY)) != -1) {
299 if (read(fd, (char *) &c, 1) == 1) {
300 if (!Isprint(c) && (c != '\n' && c != '\t')) {
301 (void) close(fd);
302 /*
303 * We *know* what ENOEXEC means.
304 */
305 stderror(ERR_ARCH, f, strerror(errno));
306 }
307 }
308 #ifdef _PATH_BSHELL
309 else
310 c = '#';
311 #endif
312 (void) close(fd);
313 }
314 /*
315 * If there is an alias for shell, then put the words of the alias in
316 * front of the argument list replacing the command name. Note no
317 * interpretation of the words at this point.
318 */
319 v = adrof1(STRshell, &aliases);
320 if (v == 0) {
321 vp = lastsh;
322 vp[0] = adrof(STRshell) ? value(STRshell) : STR_SHELLPATH;
323 vp[1] = NULL;
324 #ifdef _PATH_BSHELL
325 if (fd != -1 && c != '#')
326 vp[0] = STR_BSHELL;
327 #endif
328 }
329 else
330 vp = v->vec;
331 st0 = st[0];
332 st[0] = sf;
333 ost = st;
334 st = blkspl(vp, st); /* Splice up the new arglst */
335 ost[0] = st0;
336 sf = *st;
337 /* The order for the conversions is significant */
338 t = short2blk(st);
339 f = short2str(sf);
340 xfree((ptr_t) st);
341 Vt = t;
342 (void) execve(f, t, environ);
343 Vt = 0;
344 blkfree((Char **) t);
345 /* The sky is falling, the sky is falling! */
346
347 case ENOMEM:
348 stderror(ERR_SYSTEM, f, strerror(errno));
349
350 case ENOENT:
351 break;
352
353 default:
354 if (exerr == 0) {
355 exerr = strerror(errno);
356 if (expath)
357 xfree((ptr_t) expath);
358 expath = Strsave(sf);
359 Vexpath = expath;
360 }
361 }
362 }
363
364 /*ARGSUSED*/
365 void
366 execash(t, kp)
367 Char **t;
368 register struct command *kp;
369 {
370 int saveIN, saveOUT, saveDIAG, saveSTD;
371 int oSHIN;
372 int oSHOUT;
373 int oSHERR;
374 int oOLDSTD;
375 jmp_buf osetexit;
376 int my_reenter;
377 int odidfds;
378 sig_t osigint, osigquit, osigterm;
379
380 if (chkstop == 0 && setintr)
381 panystop(0);
382 /*
383 * Hmm, we don't really want to do that now because we might
384 * fail, but what is the choice
385 */
386 rechist();
387
388 osigint = signal(SIGINT, parintr);
389 osigquit = signal(SIGQUIT, parintr);
390 osigterm = signal(SIGTERM, parterm);
391
392 odidfds = didfds;
393 oSHIN = SHIN;
394 oSHOUT = SHOUT;
395 oSHERR = SHERR;
396 oOLDSTD = OLDSTD;
397
398 saveIN = dcopy(SHIN, -1);
399 saveOUT = dcopy(SHOUT, -1);
400 saveDIAG = dcopy(SHERR, -1);
401 saveSTD = dcopy(OLDSTD, -1);
402
403 lshift(kp->t_dcom, 1);
404
405 getexit(osetexit);
406
407 if ((my_reenter = setexit()) == 0) {
408 SHIN = dcopy(0, -1);
409 SHOUT = dcopy(1, -1);
410 SHERR = dcopy(2, -1);
411 didfds = 0;
412 doexec(t, kp);
413 }
414
415 (void) signal(SIGINT, osigint);
416 (void) signal(SIGQUIT, osigquit);
417 (void) signal(SIGTERM, osigterm);
418
419 doneinp = 0;
420 didfds = odidfds;
421 (void) close(SHIN);
422 (void) close(SHOUT);
423 (void) close(SHERR);
424 (void) close(OLDSTD);
425 SHIN = dmove(saveIN, oSHIN);
426 SHOUT = dmove(saveOUT, oSHOUT);
427 SHERR = dmove(saveDIAG, oSHERR);
428 OLDSTD = dmove(saveSTD, oOLDSTD);
429
430 resexit(osetexit);
431 if (my_reenter)
432 stderror(ERR_SILENT);
433 }
434
435 void
436 xechoit(t)
437 Char **t;
438 {
439 if (adrof(STRecho)) {
440 (void) fflush(csherr);
441 blkpr(csherr, t);
442 (void) fputc('\n', csherr);
443 }
444 }
445
446 void
447 /*ARGSUSED*/
448 dohash(v, t)
449 Char **v;
450 struct command *t;
451 {
452 DIR *dirp;
453 register struct dirent *dp;
454 register int cnt;
455 int i = 0;
456 struct varent *pathv = adrof(STRpath);
457 Char **pv;
458 int hashval;
459
460 havhash = 1;
461 for (cnt = 0; cnt < sizeof xhash; cnt++)
462 xhash[cnt] = 0;
463 if (pathv == 0)
464 return;
465 for (pv = pathv->vec; *pv; pv++, i++) {
466 if (pv[0][0] != '/')
467 continue;
468 dirp = opendir(short2str(*pv));
469 if (dirp == NULL)
470 continue;
471 while ((dp = readdir(dirp)) != NULL) {
472 if (dp->d_ino == 0)
473 continue;
474 if (dp->d_name[0] == '.' &&
475 (dp->d_name[1] == '\0' ||
476 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
477 continue;
478 hashval = hash(hashname(str2short(dp->d_name)), i);
479 bis(xhash, hashval);
480 /* tw_add_comm_name (dp->d_name); */
481 }
482 (void) closedir(dirp);
483 }
484 }
485
486 void
487 /*ARGSUSED*/
488 dounhash(v, t)
489 Char **v;
490 struct command *t;
491 {
492 havhash = 0;
493 }
494
495 void
496 /*ARGSUSED*/
497 hashstat(v, t)
498 Char **v;
499 struct command *t;
500 {
501 if (hits + misses)
502 (void) fprintf(cshout, "%d hits, %d misses, %d%%\n",
503 hits, misses, 100 * hits / (hits + misses));
504 }
505
506 /*
507 * Hash a command name.
508 */
509 static int
510 hashname(cp)
511 register Char *cp;
512 {
513 register long h = 0;
514
515 while (*cp)
516 h = hash(h, *cp++);
517 return ((int) h);
518 }
519
520 static int
521 iscommand(name)
522 Char *name;
523 {
524 register Char **pv;
525 register Char *sav;
526 register struct varent *v;
527 register bool slash = any(short2str(name), '/');
528 register int hashval = 0, hashval1, i;
529
530 v = adrof(STRpath);
531 if (v == 0 || v->vec[0] == 0 || slash)
532 pv = justabs;
533 else
534 pv = v->vec;
535 sav = Strspl(STRslash, name); /* / command name for postpending */
536 if (havhash)
537 hashval = hashname(name);
538 i = 0;
539 do {
540 if (!slash && pv[0][0] == '/' && havhash) {
541 hashval1 = hash(hashval, i);
542 if (!bit(xhash, hashval1))
543 goto cont;
544 }
545 if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */
546 if (executable(NULL, name, 0)) {
547 xfree((ptr_t) sav);
548 return i + 1;
549 }
550 }
551 else {
552 if (executable(*pv, sav, 0)) {
553 xfree((ptr_t) sav);
554 return i + 1;
555 }
556 }
557 cont:
558 pv++;
559 i++;
560 } while (*pv);
561 xfree((ptr_t) sav);
562 return 0;
563 }
564
565 /* Also by:
566 * Andreas Luik <luik (at) isaak.isa.de>
567 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
568 * Azenberstr. 35
569 * D-7000 Stuttgart 1
570 * West-Germany
571 * is the executable() routine below and changes to iscommand().
572 * Thanks again!!
573 */
574
575 /*
576 * executable() examines the pathname obtained by concatenating dir and name
577 * (dir may be NULL), and returns 1 either if it is executable by us, or
578 * if dir_ok is set and the pathname refers to a directory.
579 * This is a bit kludgy, but in the name of optimization...
580 */
581 static int
582 executable(dir, name, dir_ok)
583 Char *dir, *name;
584 bool dir_ok;
585 {
586 struct stat stbuf;
587 Char path[MAXPATHLEN + 1], *dp, *sp;
588 char *strname;
589
590 if (dir && *dir) {
591 for (dp = path, sp = dir; *sp; *dp++ = *sp++)
592 if (dp == &path[MAXPATHLEN + 1]) {
593 *--dp = '\0';
594 break;
595 }
596 for (sp = name; *sp; *dp++ = *sp++)
597 if (dp == &path[MAXPATHLEN + 1]) {
598 *--dp = '\0';
599 break;
600 }
601 *dp = '\0';
602 strname = short2str(path);
603 }
604 else
605 strname = short2str(name);
606 return (stat(strname, &stbuf) != -1 &&
607 ((S_ISREG(stbuf.st_mode) &&
608 /* save time by not calling access() in the hopeless case */
609 (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&
610 access(strname, X_OK) == 0) ||
611 (dir_ok && S_ISDIR(stbuf.st_mode))));
612 }
613
614 /* The dowhich() is by:
615 * Andreas Luik <luik (at) isaak.isa.de>
616 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
617 * Azenberstr. 35
618 * D-7000 Stuttgart 1
619 * West-Germany
620 * Thanks!!
621 */
622 /*ARGSUSED*/
623 void
624 dowhich(v, c)
625 register Char **v;
626 struct command *c;
627 {
628 struct wordent lex[3];
629 struct varent *vp;
630
631 lex[0].next = &lex[1];
632 lex[1].next = &lex[2];
633 lex[2].next = &lex[0];
634
635 lex[0].prev = &lex[2];
636 lex[1].prev = &lex[0];
637 lex[2].prev = &lex[1];
638
639 lex[0].word = STRNULL;
640 lex[2].word = STRret;
641
642 while (*++v) {
643 if ((vp = adrof1(*v, &aliases)) != NULL) {
644 (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v));
645 blkpr(cshout, vp->vec);
646 (void) fputc('\n', cshout);
647 }
648 else {
649 lex[1].word = *v;
650 tellmewhat(lex);
651 }
652 }
653 }
654
655 static void
656 tellmewhat(lex)
657 struct wordent *lex;
658 {
659 register int i;
660 register struct biltins *bptr;
661 register struct wordent *sp = lex->next;
662 bool aliased = 0;
663 Char *s0, *s1, *s2;
664 Char qc;
665
666 if (adrof1(sp->word, &aliases)) {
667 alias(lex);
668 sp = lex->next;
669 aliased = 1;
670 }
671
672 s0 = sp->word; /* to get the memory freeing right... */
673
674 /* handle quoted alias hack */
675 if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE)
676 (sp->word)++;
677
678 /* do quoting, if it hasn't been done */
679 s1 = s2 = sp->word;
680 while (*s2)
681 switch (*s2) {
682 case '\'':
683 case '"':
684 qc = *s2++;
685 while (*s2 && *s2 != qc)
686 *s1++ = *s2++ | QUOTE;
687 if (*s2)
688 s2++;
689 break;
690 case '\\':
691 if (*++s2)
692 *s1++ = *s2++ | QUOTE;
693 break;
694 default:
695 *s1++ = *s2++;
696 }
697 *s1 = '\0';
698
699 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) {
700 if (eq(sp->word, str2short(bptr->bname))) {
701 if (aliased)
702 prlex(cshout, lex);
703 (void) fprintf(cshout, "%s: shell built-in command.\n",
704 vis_str(sp->word));
705 sp->word = s0; /* we save and then restore this */
706 return;
707 }
708 }
709
710 if ((i = iscommand(strip(sp->word))) != 0) {
711 register Char **pv;
712 register struct varent *v;
713 bool slash = any(short2str(sp->word), '/');
714
715 v = adrof(STRpath);
716 if (v == 0 || v->vec[0] == 0 || slash)
717 pv = justabs;
718 else
719 pv = v->vec;
720
721 while (--i)
722 pv++;
723 if (pv[0][0] == 0 || eq(pv[0], STRdot)) {
724 sp->word = Strspl(STRdotsl, sp->word);
725 prlex(cshout, lex);
726 xfree((ptr_t) sp->word);
727 sp->word = s0; /* we save and then restore this */
728 return;
729 }
730 s1 = Strspl(*pv, STRslash);
731 sp->word = Strspl(s1, sp->word);
732 xfree((ptr_t) s1);
733 prlex(cshout, lex);
734 xfree((ptr_t) sp->word);
735 }
736 else {
737 if (aliased)
738 prlex(cshout, lex);
739 (void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word));
740 }
741 sp->word = s0; /* we save and then restore this */
742 }
743