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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)exec.c 8.4 (Berkeley) 6/8/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <unistd.h> 44 #include <fcntl.h> 45 #include <errno.h> 46 #include <paths.h> 47 #include <stdlib.h> 48 49 /* 50 * When commands are first encountered, they are entered in a hash table. 51 * This ensures that a full path search will not have to be done for them 52 * on each invocation. 53 * 54 * We should investigate converting to a linear search, even though that 55 * would make the command name "hash" a misnomer. 56 */ 57 58 #include "shell.h" 59 #include "main.h" 60 #include "nodes.h" 61 #include "parser.h" 62 #include "redir.h" 63 #include "eval.h" 64 #include "exec.h" 65 #include "builtins.h" 66 #include "var.h" 67 #include "options.h" 68 #include "input.h" 69 #include "output.h" 70 #include "syntax.h" 71 #include "memalloc.h" 72 #include "error.h" 73 #include "mystring.h" 74 #include "show.h" 75 #include "jobs.h" 76 #include "alias.h" 77 78 79 #define CMDTABLESIZE 31 /* should be prime */ 80 81 82 83 struct tblentry { 84 struct tblentry *next; /* next entry in hash chain */ 85 union param param; /* definition of builtin function */ 86 int special; /* flag for special builtin commands */ 87 signed char cmdtype; /* index identifying command */ 88 char cmdname[]; /* name of command */ 89 }; 90 91 92 static struct tblentry *cmdtable[CMDTABLESIZE]; 93 static int cmdtable_cd = 0; /* cmdtable contains cd-dependent entries */ 94 95 96 static void tryexec(char *, char **, char **); 97 static void printentry(struct tblentry *, int); 98 static struct tblentry *cmdlookup(const char *, int); 99 static void delete_cmd_entry(void); 100 static void addcmdentry(const char *, struct cmdentry *); 101 102 103 104 /* 105 * Exec a program. Never returns. If you change this routine, you may 106 * have to change the find_command routine as well. 107 * 108 * The argv array may be changed and element argv[-1] should be writable. 109 */ 110 111 void 112 shellexec(char **argv, char **envp, const char *path, int idx) 113 { 114 char *cmdname; 115 const char *opt; 116 int e; 117 118 if (strchr(argv[0], '/') != NULL) { 119 tryexec(argv[0], argv, envp); 120 e = errno; 121 } else { 122 e = ENOENT; 123 while ((cmdname = padvance(&path, &opt, argv[0])) != NULL) { 124 if (--idx < 0 && opt == NULL) { 125 tryexec(cmdname, argv, envp); 126 if (errno != ENOENT && errno != ENOTDIR) 127 e = errno; 128 if (e == ENOEXEC) 129 break; 130 } 131 stunalloc(cmdname); 132 } 133 } 134 135 /* Map to POSIX errors */ 136 if (e == ENOENT || e == ENOTDIR) { 137 exitstatus = 127; 138 exerror(EXEXEC, "%s: not found", argv[0]); 139 } else { 140 exitstatus = 126; 141 exerror(EXEXEC, "%s: %s", argv[0], strerror(e)); 142 } 143 } 144 145 146 static void 147 tryexec(char *cmd, char **argv, char **envp) 148 { 149 int e, in; 150 ssize_t n; 151 char buf[256]; 152 153 execve(cmd, argv, envp); 154 e = errno; 155 if (e == ENOEXEC) { 156 INTOFF; 157 in = open(cmd, O_RDONLY | O_NONBLOCK); 158 if (in != -1) { 159 n = pread(in, buf, sizeof buf, 0); 160 close(in); 161 if (n > 0 && memchr(buf, '\0', n) != NULL) { 162 errno = ENOEXEC; 163 return; 164 } 165 } 166 *argv = cmd; 167 *--argv = __DECONST(char *, _PATH_BSHELL); 168 execve(_PATH_BSHELL, argv, envp); 169 } 170 errno = e; 171 } 172 173 /* 174 * Do a path search. The variable path (passed by reference) should be 175 * set to the start of the path before the first call; padvance will update 176 * this value as it proceeds. Successive calls to padvance will return 177 * the possible path expansions in sequence. If popt is not NULL, options 178 * are processed: if an option (indicated by a percent sign) appears in 179 * the path entry then *popt will be set to point to it; else *popt will be 180 * set to NULL. If popt is NULL, percent signs are not special. 181 */ 182 183 char * 184 padvance(const char **path, const char **popt, const char *name) 185 { 186 const char *p, *start; 187 char *q; 188 size_t len, namelen; 189 190 if (*path == NULL) 191 return NULL; 192 start = *path; 193 if (popt != NULL) 194 for (p = start; *p && *p != ':' && *p != '%'; p++) 195 ; /* nothing */ 196 else 197 for (p = start; *p && *p != ':'; p++) 198 ; /* nothing */ 199 namelen = strlen(name); 200 len = p - start + namelen + 2; /* "2" is for '/' and '\0' */ 201 STARTSTACKSTR(q); 202 CHECKSTRSPACE(len, q); 203 if (p != start) { 204 memcpy(q, start, p - start); 205 q += p - start; 206 *q++ = '/'; 207 } 208 memcpy(q, name, namelen + 1); 209 if (popt != NULL) { 210 if (*p == '%') { 211 *popt = ++p; 212 while (*p && *p != ':') p++; 213 } else 214 *popt = NULL; 215 } 216 if (*p == ':') 217 *path = p + 1; 218 else 219 *path = NULL; 220 return stalloc(len); 221 } 222 223 224 225 /*** Command hashing code ***/ 226 227 228 int 229 hashcmd(int argc __unused, char **argv __unused) 230 { 231 struct tblentry **pp; 232 struct tblentry *cmdp; 233 int c; 234 int verbose; 235 struct cmdentry entry; 236 char *name; 237 int errors; 238 239 errors = 0; 240 verbose = 0; 241 while ((c = nextopt("rv")) != '\0') { 242 if (c == 'r') { 243 clearcmdentry(); 244 } else if (c == 'v') { 245 verbose++; 246 } 247 } 248 if (*argptr == NULL) { 249 for (pp = cmdtable ; pp < &cmdtable[CMDTABLESIZE] ; pp++) { 250 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 251 if (cmdp->cmdtype == CMDNORMAL) 252 printentry(cmdp, verbose); 253 } 254 } 255 return 0; 256 } 257 while ((name = *argptr) != NULL) { 258 if ((cmdp = cmdlookup(name, 0)) != NULL 259 && cmdp->cmdtype == CMDNORMAL) 260 delete_cmd_entry(); 261 find_command(name, &entry, DO_ERR, pathval()); 262 if (entry.cmdtype == CMDUNKNOWN) 263 errors = 1; 264 else if (verbose) { 265 cmdp = cmdlookup(name, 0); 266 if (cmdp != NULL) 267 printentry(cmdp, verbose); 268 else { 269 outfmt(out2, "%s: not found\n", name); 270 errors = 1; 271 } 272 flushall(); 273 } 274 argptr++; 275 } 276 return errors; 277 } 278 279 280 static void 281 printentry(struct tblentry *cmdp, int verbose) 282 { 283 int idx; 284 const char *path, *opt; 285 char *name; 286 287 if (cmdp->cmdtype == CMDNORMAL) { 288 idx = cmdp->param.index; 289 path = pathval(); 290 do { 291 name = padvance(&path, &opt, cmdp->cmdname); 292 stunalloc(name); 293 } while (--idx >= 0); 294 out1str(name); 295 } else if (cmdp->cmdtype == CMDBUILTIN) { 296 out1fmt("builtin %s", cmdp->cmdname); 297 } else if (cmdp->cmdtype == CMDFUNCTION) { 298 out1fmt("function %s", cmdp->cmdname); 299 if (verbose) { 300 INTOFF; 301 name = commandtext(getfuncnode(cmdp->param.func)); 302 out1c(' '); 303 out1str(name); 304 ckfree(name); 305 INTON; 306 } 307 #ifdef DEBUG 308 } else { 309 error("internal error: cmdtype %d", cmdp->cmdtype); 310 #endif 311 } 312 out1c('\n'); 313 } 314 315 316 317 /* 318 * Resolve a command name. If you change this routine, you may have to 319 * change the shellexec routine as well. 320 */ 321 322 void 323 find_command(const char *name, struct cmdentry *entry, int act, 324 const char *path) 325 { 326 struct tblentry *cmdp, loc_cmd; 327 int idx; 328 const char *opt; 329 char *fullname; 330 struct stat statb; 331 int e; 332 int i; 333 int spec; 334 int cd; 335 336 /* If name contains a slash, don't use the hash table */ 337 if (strchr(name, '/') != NULL) { 338 entry->cmdtype = CMDNORMAL; 339 entry->u.index = 0; 340 entry->special = 0; 341 return; 342 } 343 344 cd = 0; 345 346 /* If name is in the table, we're done */ 347 if ((cmdp = cmdlookup(name, 0)) != NULL) { 348 if (cmdp->cmdtype == CMDFUNCTION && act & DO_NOFUNC) 349 cmdp = NULL; 350 else 351 goto success; 352 } 353 354 /* Check for builtin next */ 355 if ((i = find_builtin(name, &spec)) >= 0) { 356 INTOFF; 357 cmdp = cmdlookup(name, 1); 358 if (cmdp->cmdtype == CMDFUNCTION) 359 cmdp = &loc_cmd; 360 cmdp->cmdtype = CMDBUILTIN; 361 cmdp->param.index = i; 362 cmdp->special = spec; 363 INTON; 364 goto success; 365 } 366 367 /* We have to search path. */ 368 369 e = ENOENT; 370 idx = -1; 371 for (;(fullname = padvance(&path, &opt, name)) != NULL; 372 stunalloc(fullname)) { 373 idx++; 374 if (opt) { 375 if (strncmp(opt, "func", 4) == 0) { 376 /* handled below */ 377 } else { 378 continue; /* ignore unimplemented options */ 379 } 380 } 381 if (fullname[0] != '/') 382 cd = 1; 383 if (stat(fullname, &statb) < 0) { 384 if (errno != ENOENT && errno != ENOTDIR) 385 e = errno; 386 continue; 387 } 388 e = EACCES; /* if we fail, this will be the error */ 389 if (!S_ISREG(statb.st_mode)) 390 continue; 391 if (opt) { /* this is a %func directory */ 392 readcmdfile(fullname); 393 if ((cmdp = cmdlookup(name, 0)) == NULL || cmdp->cmdtype != CMDFUNCTION) 394 error("%s not defined in %s", name, fullname); 395 stunalloc(fullname); 396 goto success; 397 } 398 #ifdef notdef 399 if (statb.st_uid == geteuid()) { 400 if ((statb.st_mode & 0100) == 0) 401 goto loop; 402 } else if (statb.st_gid == getegid()) { 403 if ((statb.st_mode & 010) == 0) 404 goto loop; 405 } else { 406 if ((statb.st_mode & 01) == 0) 407 goto loop; 408 } 409 #endif 410 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); 411 INTOFF; 412 stunalloc(fullname); 413 cmdp = cmdlookup(name, 1); 414 if (cmdp->cmdtype == CMDFUNCTION) 415 cmdp = &loc_cmd; 416 cmdp->cmdtype = CMDNORMAL; 417 cmdp->param.index = idx; 418 cmdp->special = 0; 419 INTON; 420 goto success; 421 } 422 423 if (act & DO_ERR) { 424 if (e == ENOENT || e == ENOTDIR) 425 outfmt(out2, "%s: not found\n", name); 426 else 427 outfmt(out2, "%s: %s\n", name, strerror(e)); 428 } 429 entry->cmdtype = CMDUNKNOWN; 430 entry->u.index = 0; 431 entry->special = 0; 432 return; 433 434 success: 435 if (cd) 436 cmdtable_cd = 1; 437 entry->cmdtype = cmdp->cmdtype; 438 entry->u = cmdp->param; 439 entry->special = cmdp->special; 440 } 441 442 443 444 /* 445 * Search the table of builtin commands. 446 */ 447 448 int 449 find_builtin(const char *name, int *special) 450 { 451 const unsigned char *bp; 452 size_t len; 453 454 len = strlen(name); 455 for (bp = builtincmd ; *bp ; bp += 2 + bp[0]) { 456 if (bp[0] == len && memcmp(bp + 2, name, len) == 0) { 457 *special = (bp[1] & BUILTIN_SPECIAL) != 0; 458 return bp[1] & ~BUILTIN_SPECIAL; 459 } 460 } 461 return -1; 462 } 463 464 465 466 /* 467 * Called when a cd is done. If any entry in cmdtable depends on the current 468 * directory, simply clear cmdtable completely. 469 */ 470 471 void 472 hashcd(void) 473 { 474 if (cmdtable_cd) 475 clearcmdentry(); 476 } 477 478 479 480 /* 481 * Called before PATH is changed. The argument is the new value of PATH; 482 * pathval() still returns the old value at this point. Called with 483 * interrupts off. 484 */ 485 486 void 487 changepath(const char *newval __unused) 488 { 489 clearcmdentry(); 490 } 491 492 493 /* 494 * Clear out cached utility locations. 495 */ 496 497 void 498 clearcmdentry(void) 499 { 500 struct tblentry **tblp; 501 struct tblentry **pp; 502 struct tblentry *cmdp; 503 504 INTOFF; 505 for (tblp = cmdtable ; tblp < &cmdtable[CMDTABLESIZE] ; tblp++) { 506 pp = tblp; 507 while ((cmdp = *pp) != NULL) { 508 if (cmdp->cmdtype == CMDNORMAL) { 509 *pp = cmdp->next; 510 ckfree(cmdp); 511 } else { 512 pp = &cmdp->next; 513 } 514 } 515 } 516 cmdtable_cd = 0; 517 INTON; 518 } 519 520 521 /* 522 * Locate a command in the command hash table. If "add" is nonzero, 523 * add the command to the table if it is not already present. The 524 * variable "lastcmdentry" is set to point to the address of the link 525 * pointing to the entry, so that delete_cmd_entry can delete the 526 * entry. 527 */ 528 529 static struct tblentry **lastcmdentry; 530 531 532 static struct tblentry * 533 cmdlookup(const char *name, int add) 534 { 535 unsigned int hashval; 536 const char *p; 537 struct tblentry *cmdp; 538 struct tblentry **pp; 539 size_t len; 540 541 p = name; 542 hashval = (unsigned char)*p << 4; 543 while (*p) 544 hashval += *p++; 545 pp = &cmdtable[hashval % CMDTABLESIZE]; 546 for (cmdp = *pp ; cmdp ; cmdp = cmdp->next) { 547 if (equal(cmdp->cmdname, name)) 548 break; 549 pp = &cmdp->next; 550 } 551 if (add && cmdp == NULL) { 552 INTOFF; 553 len = strlen(name); 554 cmdp = *pp = ckmalloc(sizeof (struct tblentry) + len + 1); 555 cmdp->next = NULL; 556 cmdp->cmdtype = CMDUNKNOWN; 557 memcpy(cmdp->cmdname, name, len + 1); 558 INTON; 559 } 560 lastcmdentry = pp; 561 return cmdp; 562 } 563 564 /* 565 * Delete the command entry returned on the last lookup. 566 */ 567 568 static void 569 delete_cmd_entry(void) 570 { 571 struct tblentry *cmdp; 572 573 INTOFF; 574 cmdp = *lastcmdentry; 575 *lastcmdentry = cmdp->next; 576 ckfree(cmdp); 577 INTON; 578 } 579 580 581 582 /* 583 * Add a new command entry, replacing any existing command entry for 584 * the same name. 585 */ 586 587 static void 588 addcmdentry(const char *name, struct cmdentry *entry) 589 { 590 struct tblentry *cmdp; 591 592 INTOFF; 593 cmdp = cmdlookup(name, 1); 594 if (cmdp->cmdtype == CMDFUNCTION) { 595 unreffunc(cmdp->param.func); 596 } 597 cmdp->cmdtype = entry->cmdtype; 598 cmdp->param = entry->u; 599 cmdp->special = entry->special; 600 INTON; 601 } 602 603 604 /* 605 * Define a shell function. 606 */ 607 608 void 609 defun(const char *name, union node *func) 610 { 611 struct cmdentry entry; 612 613 INTOFF; 614 entry.cmdtype = CMDFUNCTION; 615 entry.u.func = copyfunc(func); 616 entry.special = 0; 617 addcmdentry(name, &entry); 618 INTON; 619 } 620 621 622 /* 623 * Delete a function if it exists. 624 * Called with interrupts off. 625 */ 626 627 int 628 unsetfunc(const char *name) 629 { 630 struct tblentry *cmdp; 631 632 if ((cmdp = cmdlookup(name, 0)) != NULL && cmdp->cmdtype == CMDFUNCTION) { 633 unreffunc(cmdp->param.func); 634 delete_cmd_entry(); 635 return (0); 636 } 637 return (0); 638 } 639 640 641 /* 642 * Check if a function by a certain name exists. 643 */ 644 int 645 isfunc(const char *name) 646 { 647 struct tblentry *cmdp; 648 cmdp = cmdlookup(name, 0); 649 return (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION); 650 } 651 652 653 /* 654 * Shared code for the following builtin commands: 655 * type, command -v, command -V 656 */ 657 658 int 659 typecmd_impl(int argc, char **argv, int cmd, const char *path) 660 { 661 struct cmdentry entry; 662 struct tblentry *cmdp; 663 const char *const *pp; 664 struct alias *ap; 665 int i; 666 int error1 = 0; 667 668 if (path != pathval()) 669 clearcmdentry(); 670 671 for (i = 1; i < argc; i++) { 672 /* First look at the keywords */ 673 for (pp = parsekwd; *pp; pp++) 674 if (**pp == *argv[i] && equal(*pp, argv[i])) 675 break; 676 677 if (*pp) { 678 if (cmd == TYPECMD_SMALLV) 679 out1fmt("%s\n", argv[i]); 680 else 681 out1fmt("%s is a shell keyword\n", argv[i]); 682 continue; 683 } 684 685 /* Then look at the aliases */ 686 if ((ap = lookupalias(argv[i], 1)) != NULL) { 687 if (cmd == TYPECMD_SMALLV) { 688 out1fmt("alias %s=", argv[i]); 689 out1qstr(ap->val); 690 outcslow('\n', out1); 691 } else 692 out1fmt("%s is an alias for %s\n", argv[i], 693 ap->val); 694 continue; 695 } 696 697 /* Then check if it is a tracked alias */ 698 if ((cmdp = cmdlookup(argv[i], 0)) != NULL) { 699 entry.cmdtype = cmdp->cmdtype; 700 entry.u = cmdp->param; 701 entry.special = cmdp->special; 702 } 703 else { 704 /* Finally use brute force */ 705 find_command(argv[i], &entry, 0, path); 706 } 707 708 switch (entry.cmdtype) { 709 case CMDNORMAL: { 710 if (strchr(argv[i], '/') == NULL) { 711 const char *path2 = path; 712 const char *opt2; 713 char *name; 714 int j = entry.u.index; 715 do { 716 name = padvance(&path2, &opt2, argv[i]); 717 stunalloc(name); 718 } while (--j >= 0); 719 if (cmd == TYPECMD_SMALLV) 720 out1fmt("%s\n", name); 721 else 722 out1fmt("%s is%s %s\n", argv[i], 723 (cmdp && cmd == TYPECMD_TYPE) ? 724 " a tracked alias for" : "", 725 name); 726 } else { 727 if (eaccess(argv[i], X_OK) == 0) { 728 if (cmd == TYPECMD_SMALLV) 729 out1fmt("%s\n", argv[i]); 730 else 731 out1fmt("%s is %s\n", argv[i], 732 argv[i]); 733 } else { 734 if (cmd != TYPECMD_SMALLV) 735 outfmt(out2, "%s: %s\n", 736 argv[i], strerror(errno)); 737 error1 |= 127; 738 } 739 } 740 break; 741 } 742 case CMDFUNCTION: 743 if (cmd == TYPECMD_SMALLV) 744 out1fmt("%s\n", argv[i]); 745 else 746 out1fmt("%s is a shell function\n", argv[i]); 747 break; 748 749 case CMDBUILTIN: 750 if (cmd == TYPECMD_SMALLV) 751 out1fmt("%s\n", argv[i]); 752 else if (entry.special) 753 out1fmt("%s is a special shell builtin\n", 754 argv[i]); 755 else 756 out1fmt("%s is a shell builtin\n", argv[i]); 757 break; 758 759 default: 760 if (cmd != TYPECMD_SMALLV) 761 outfmt(out2, "%s: not found\n", argv[i]); 762 error1 |= 127; 763 break; 764 } 765 } 766 767 if (path != pathval()) 768 clearcmdentry(); 769 770 return error1; 771 } 772 773 /* 774 * Locate and print what a word is... 775 */ 776 777 int 778 typecmd(int argc, char **argv) 779 { 780 if (argc > 2 && strcmp(argv[1], "--") == 0) 781 argc--, argv++; 782 return typecmd_impl(argc, argv, TYPECMD_TYPE, bltinlookup("PATH", 1)); 783 } 784