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