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