wexec.c (14945B)
1 /* see LICENSE file for copyright and license details */ 2 #include "../wexec.h" 3 #include "../config.h" 4 #include "../paths.h" 5 #include "../util.h" 6 7 #include <errno.h> 8 #include <setjmp.h> 9 #include <signal.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <sys/stat.h> 13 #include <sys/wait.h> 14 #include <unistd.h> 15 16 extern char **environ; 17 18 #define MAX_BUILTINS 256 19 #define MAX_POPEN 32 20 21 struct builtin { 22 const char *name; 23 wexec_fn fn; 24 }; 25 26 static struct builtin builtins[MAX_BUILTINS]; 27 static int nbuiltins; 28 29 struct popen_entry { 30 FILE *fp; 31 pid_t pid; 32 }; 33 34 static struct popen_entry popen_tab[MAX_POPEN]; 35 static int npopen; 36 37 static char *(*wexec_env_lookup)(const char *name) = NULL; 38 39 void 40 wexec_register_env_lookup(char *(*fn)(const char *name)) 41 { 42 wexec_env_lookup = fn; 43 } 44 45 static const char * 46 wexec_getenv(const char *name) 47 { 48 if (wexec_env_lookup) 49 return wexec_env_lookup(name); 50 return getenv(name); 51 } 52 53 int 54 wpopen_track(FILE *fp, pid_t pid) 55 { 56 if (npopen >= MAX_POPEN) 57 return -1; 58 popen_tab[npopen].fp = fp; 59 popen_tab[npopen].pid = pid; 60 npopen++; 61 return 0; 62 } 63 64 pid_t 65 wpopen_untrack(FILE *fp) 66 { 67 int i; 68 pid_t pid = -1; 69 for (i = 0; i < npopen; i++) { 70 if (popen_tab[i].fp == fp) { 71 pid = popen_tab[i].pid; 72 popen_tab[i] = popen_tab[npopen - 1]; 73 npopen--; 74 break; 75 } 76 } 77 return pid; 78 } 79 80 int 81 wexec_register(const char *name, wexec_fn fn) 82 { 83 int i; 84 for (i = 0; i < nbuiltins; i++) { 85 if (strcmp(builtins[i].name, name) == 0) 86 return -1; 87 } 88 if (nbuiltins >= MAX_BUILTINS) 89 return -1; 90 builtins[nbuiltins].name = name; 91 builtins[nbuiltins].fn = fn; 92 nbuiltins++; 93 return 0; 94 } 95 96 int 97 wexec_is_builtin(const char *name) 98 { 99 int i; 100 for (i = 0; i < nbuiltins; i++) { 101 if (strcmp(builtins[i].name, name) == 0) 102 return 1; 103 } 104 return 0; 105 } 106 107 int 108 wexec_is_nofork(const char *name) 109 { 110 #include "nofork_list.h" 111 int i; 112 for (i = 0; nofork_list[i]; i++) { 113 if (strcmp(nofork_list[i], name) == 0) 114 return 1; 115 } 116 return 0; 117 } 118 119 #if FEATURE_NOEXEC 120 static int noexec_enabled = 1; 121 122 #if FEATURE_NOEXEC_OVERRIDE 123 static void 124 noexec_read_env(void) 125 { 126 const char *e = wexec_getenv("ARUU_NOEXEC"); 127 if (e && strcmp(e, "0") == 0) 128 noexec_enabled = 0; 129 else 130 noexec_enabled = 1; 131 } 132 #endif 133 134 void 135 wexec_set_noexec(int enabled) 136 { 137 noexec_enabled = enabled; 138 } 139 140 int 141 wexec_get_noexec(void) 142 { 143 #if FEATURE_NOEXEC_OVERRIDE 144 noexec_read_env(); 145 #endif 146 return noexec_enabled; 147 } 148 #endif 149 150 #if FEATURE_NOFORK 151 static int nofork_enabled = 1; 152 153 #if FEATURE_NOFORK_OVERRIDE 154 static void 155 nofork_read_env(void) 156 { 157 const char *e = wexec_getenv("ARUU_NOFORK"); 158 if (e && strcmp(e, "0") == 0) 159 nofork_enabled = 0; 160 else 161 nofork_enabled = 1; 162 } 163 #endif 164 165 void 166 wexec_set_nofork(int enabled) 167 { 168 nofork_enabled = enabled; 169 } 170 171 int 172 wexec_get_nofork(void) 173 { 174 #if FEATURE_NOFORK_OVERRIDE 175 nofork_read_env(); 176 #endif 177 return nofork_enabled; 178 } 179 #endif 180 181 char * 182 wwhich(const char *name) 183 { 184 const char *path, *p, *end; 185 char *buf; 186 size_t dlen, nlen; 187 struct stat st; 188 189 #if FEATURE_NOEXEC 190 /* when builtin dispatch is active, registered builtins have no filesystem path */ 191 if (wexec_get_noexec() && wexec_is_builtin(name)) 192 return NULL; 193 #endif 194 195 /* slash present: treat as a literal path */ 196 if (strchr(name, '/') != NULL) { 197 if (stat(name, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111)) 198 return estrdup(name); 199 return NULL; 200 } 201 202 path = wexec_getenv("PATH"); 203 if (path == NULL) 204 path = ARUU_PATH_DEFPATH; 205 206 nlen = strlen(name); 207 p = path; 208 for (;;) { 209 end = strchr(p, ':'); 210 dlen = end ? (size_t)(end - p) : strlen(p); 211 /* skip empty components (current dir) for safety */ 212 if (dlen > 0) { 213 buf = emalloc(dlen + 1 + nlen + 1); 214 memcpy(buf, p, dlen); 215 buf[dlen] = '/'; 216 memcpy(buf + dlen + 1, name, nlen + 1); 217 if (stat(buf, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111)) 218 return buf; 219 free(buf); 220 } 221 if (!end) 222 break; 223 p = end + 1; 224 } 225 return NULL; 226 } 227 228 #if FEATURE_NOEXEC || FEATURE_NOFORK 229 static sigjmp_buf inproc_jmp; 230 static volatile sig_atomic_t inproc_active = 0; 231 static volatile int inproc_exit_status = 0; 232 233 static void 234 inproc_sig_handler(int signo) 235 { 236 if (inproc_active) { 237 siglongjmp(inproc_jmp, signo); 238 } 239 } 240 241 int 242 wexec_call_builtin(const char *name, char *const *argv) 243 { 244 struct sigaction sa, osa_int, osa_quit, osa_pipe; 245 int i, argc, ret, sig; 246 char **p; 247 248 sig = sigsetjmp(inproc_jmp, 1); 249 if (sig != 0) { 250 inproc_active = 0; 251 sigaction(SIGINT, &osa_int, NULL); 252 sigaction(SIGQUIT, &osa_quit, NULL); 253 sigaction(SIGPIPE, &osa_pipe, NULL); 254 if (sig == 1) 255 return inproc_exit_status; 256 kill(getpid(), sig); 257 return 128 + sig; 258 } 259 260 sa.sa_handler = inproc_sig_handler; 261 sigemptyset(&sa.sa_mask); 262 sa.sa_flags = 0; 263 sigaction(SIGINT, &sa, &osa_int); 264 sigaction(SIGQUIT, &sa, &osa_quit); 265 266 sa.sa_handler = SIG_IGN; 267 sigaction(SIGPIPE, &sa, &osa_pipe); 268 269 inproc_active = 1; 270 271 ret = -1; 272 argc = 0; 273 for (p = (char **)argv; *p; p++) 274 argc++; 275 for (i = 0; i < nbuiltins; i++) { 276 if (strcmp(builtins[i].name, name) == 0) { 277 fflush(stdout); 278 fflush(stderr); 279 ret = builtins[i].fn(argc, (char **)argv); 280 fflush(stdout); 281 fflush(stderr); 282 break; 283 } 284 } 285 286 inproc_active = 0; 287 288 sigaction(SIGINT, &osa_int, NULL); 289 sigaction(SIGQUIT, &osa_quit, NULL); 290 sigaction(SIGPIPE, &osa_pipe, NULL); 291 292 return ret; 293 } 294 #endif 295 296 static int 297 fork_exec(const char *path, char *const *argv, int use_path) 298 { 299 pid_t pid; 300 int st; 301 302 pid = fork(); 303 if (pid < 0) { 304 weprintf("fork:"); 305 return -1; 306 } 307 if (pid == 0) { 308 const char *base = strrchr(path, '/'); 309 base = base ? base + 1 : path; 310 #if FEATURE_NOEXEC 311 if (wexec_get_noexec() && wexec_is_builtin(base)) { 312 struct sigaction sa; 313 int i, ret; 314 for (i = 1; i < NSIG; i++) { 315 if (sigaction(i, NULL, &sa) == 0) { 316 if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { 317 sa.sa_handler = SIG_DFL; 318 sa.sa_flags = 0; 319 sigemptyset(&sa.sa_mask); 320 sigaction(i, &sa, NULL); 321 } 322 } 323 } 324 ret = wexec_call_builtin(base, argv); 325 _exit(ret); 326 } 327 #endif 328 if (use_path) 329 execvp(path, argv); 330 else 331 execv(path, argv); 332 weprintf(use_path ? "execvp %s:" : "execv %s:", path); 333 _exit((errno == ENOENT) ? 127 : 126); 334 } 335 if (waitpid(pid, &st, 0) < 0) { 336 weprintf("waitpid:"); 337 return -1; 338 } 339 if (WIFEXITED(st)) 340 return WEXITSTATUS(st); 341 if (WIFSIGNALED(st)) 342 return 128 + WTERMSIG(st); 343 return -1; 344 } 345 346 int 347 wexecvp(const char *name, char *const *argv) 348 { 349 #if FEATURE_NOFORK 350 if (wexec_get_nofork() && wexec_is_builtin(name) && wexec_is_nofork(name)) { 351 fflush(stdout); 352 fflush(stderr); 353 return wexec_call_builtin(name, argv); 354 } 355 #endif 356 return fork_exec(name, argv, 1); 357 } 358 359 int 360 wexecv(const char *path, char *const *argv) 361 { 362 const char *base = strrchr(path, '/'); 363 base = base ? base + 1 : path; 364 #if FEATURE_NOFORK 365 if (wexec_get_nofork() && wexec_is_builtin(base) && wexec_is_nofork(base)) { 366 fflush(stdout); 367 fflush(stderr); 368 return wexec_call_builtin(base, argv); 369 } 370 #endif 371 return fork_exec(path, argv, 0); 372 } 373 374 FILE * 375 wpopen(const char *name, char *const *argv, const char *mode) 376 { 377 int pfd[2]; 378 pid_t pid; 379 int parent_end, child_end; 380 char *sh_argv[4]; 381 #if FEATURE_NOEXEC 382 struct sigaction sa; 383 int i; 384 #endif 385 386 if (pipe(pfd) < 0) { 387 weprintf("pipe:"); 388 return NULL; 389 } 390 391 if (*mode == 'r') { 392 parent_end = pfd[0]; 393 child_end = pfd[1]; 394 } else { 395 parent_end = pfd[1]; 396 child_end = pfd[0]; 397 } 398 399 pid = fork(); 400 if (pid < 0) { 401 weprintf("fork:"); 402 close(pfd[0]); 403 close(pfd[1]); 404 return NULL; 405 } 406 407 if (pid == 0) { 408 close(parent_end); 409 if (child_end != (*mode == 'r' ? STDOUT_FILENO : STDIN_FILENO)) { 410 dup2(child_end, *mode == 'r' ? STDOUT_FILENO : STDIN_FILENO); 411 close(child_end); 412 } 413 if (argv) { 414 #if FEATURE_NOEXEC 415 if (wexec_get_noexec() && wexec_is_builtin(name)) { 416 int argc; 417 char **p; 418 419 /* reset caught signal handlers to default for inproc command */ 420 for (i = 1; i < NSIG; i++) { 421 if (sigaction(i, NULL, &sa) == 0) { 422 if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { 423 sa.sa_handler = SIG_DFL; 424 sa.sa_flags = 0; 425 sigemptyset(&sa.sa_mask); 426 sigaction(i, &sa, NULL); 427 } 428 } 429 } 430 431 argc = 0; 432 for (p = (char **)argv; *p; p++) 433 argc++; 434 wexec_call_builtin(name, argv); 435 fflush(stdout); 436 fflush(stderr); 437 _exit(0); 438 } 439 #endif 440 execvp(name, argv); 441 weprintf("execvp %s:", name); 442 _exit((errno == ENOENT) ? 127 : 126); 443 } else { 444 sh_argv[0] = "sh"; 445 sh_argv[1] = "-c"; 446 sh_argv[2] = (char *)name; 447 sh_argv[3] = NULL; 448 #if FEATURE_NOEXEC 449 if (wexec_get_noexec() && wexec_is_builtin("sh")) { 450 /* reset caught signal handlers to default for inproc command */ 451 for (i = 1; i < NSIG; i++) { 452 if (sigaction(i, NULL, &sa) == 0) { 453 if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { 454 sa.sa_handler = SIG_DFL; 455 sa.sa_flags = 0; 456 sigemptyset(&sa.sa_mask); 457 sigaction(i, &sa, NULL); 458 } 459 } 460 } 461 462 wexec_call_builtin("sh", sh_argv); 463 fflush(stdout); 464 fflush(stderr); 465 _exit(0); 466 } 467 #endif 468 execvp("sh", sh_argv); 469 weprintf("execvp sh:"); 470 _exit((errno == ENOENT) ? 127 : 126); 471 } 472 } 473 474 close(child_end); 475 { 476 FILE *fp = fdopen(parent_end, mode); 477 if (!fp) { 478 weprintf("fdopen:"); 479 close(parent_end); 480 kill(pid, SIGTERM); 481 waitpid(pid, NULL, 0); 482 return NULL; 483 } 484 if (wpopen_track(fp, pid) < 0) { 485 fclose(fp); 486 kill(pid, SIGTERM); 487 waitpid(pid, NULL, 0); 488 return NULL; 489 } 490 return fp; 491 } 492 } 493 494 int 495 wpclose(FILE *fp) 496 { 497 int st; 498 pid_t pid; 499 500 pid = wpopen_untrack(fp); 501 if (fclose(fp) == EOF) 502 weprintf("fclose:"); 503 if (pid < 0) 504 return -1; 505 if (waitpid(pid, &st, 0) < 0) { 506 weprintf("waitpid:"); 507 return -1; 508 } 509 if (WIFEXITED(st)) 510 return WEXITSTATUS(st); 511 if (WIFSIGNALED(st)) 512 return 128 + WTERMSIG(st); 513 return -1; 514 } 515 516 int 517 wsystem(const char *cmd) 518 { 519 #if FEATURE_NOEXEC 520 if (wexec_get_noexec() && wexec_is_builtin("sh")) { 521 pid_t pid; 522 int st; 523 524 pid = fork(); 525 if (pid < 0) { 526 weprintf("fork:"); 527 return -1; 528 } 529 if (pid == 0) { 530 char *sh_argv[4]; 531 struct sigaction sa; 532 int i; 533 int ret; 534 535 /* reset caught signal handlers to default for inproc command */ 536 for (i = 1; i < NSIG; i++) { 537 if (sigaction(i, NULL, &sa) == 0) { 538 if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { 539 sa.sa_handler = SIG_DFL; 540 sa.sa_flags = 0; 541 sigemptyset(&sa.sa_mask); 542 sigaction(i, &sa, NULL); 543 } 544 } 545 } 546 547 sh_argv[0] = "sh"; 548 sh_argv[1] = "-c"; 549 sh_argv[2] = (char *)cmd; 550 sh_argv[3] = NULL; 551 ret = wexec_call_builtin("sh", sh_argv); 552 _exit(ret); 553 } 554 if (waitpid(pid, &st, 0) < 0) { 555 weprintf("waitpid:"); 556 return -1; 557 } 558 if (WIFEXITED(st)) 559 return WEXITSTATUS(st); 560 if (WIFSIGNALED(st)) 561 return 128 + WTERMSIG(st); 562 return -1; 563 } 564 #endif 565 { 566 char *sh_argv[4]; 567 int ret; 568 sh_argv[0] = "sh"; 569 sh_argv[1] = "-c"; 570 sh_argv[2] = (char *)cmd; 571 sh_argv[3] = NULL; 572 ret = fork_exec("sh", sh_argv, 1); 573 return ret; 574 } 575 } 576 577 void 578 wexecvp_self(const char *name, char *const *argv) 579 { 580 #if FEATURE_NOEXEC 581 if (wexec_get_noexec() && wexec_is_builtin(name)) { 582 struct sigaction sa; 583 int i; 584 int ret; 585 586 /* reset caught signal handlers to default for inproc command */ 587 for (i = 1; i < NSIG; i++) { 588 if (sigaction(i, NULL, &sa) == 0) { 589 if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { 590 sa.sa_handler = SIG_DFL; 591 sa.sa_flags = 0; 592 sigemptyset(&sa.sa_mask); 593 sigaction(i, &sa, NULL); 594 } 595 } 596 } 597 598 fflush(stdout); 599 fflush(stderr); 600 ret = wexec_call_builtin(name, argv); 601 fflush(stdout); 602 fflush(stderr); 603 exit(ret); 604 } 605 #endif 606 execvp(name, argv); 607 weprintf("execvp %s:", name); 608 exit((errno == ENOENT) ? 127 : 126); 609 } 610 611 void 612 wexecv_self(const char *path, char *const *argv) 613 { 614 #if FEATURE_NOEXEC 615 { 616 const char *base = strrchr(path, '/'); 617 base = base ? base + 1 : path; 618 if (wexec_get_noexec() && wexec_is_builtin(base)) { 619 struct sigaction sa; 620 int i; 621 int ret; 622 623 /* reset caught signal handlers to default for inproc command */ 624 for (i = 1; i < NSIG; i++) { 625 if (sigaction(i, NULL, &sa) == 0) { 626 if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { 627 sa.sa_handler = SIG_DFL; 628 sa.sa_flags = 0; 629 sigemptyset(&sa.sa_mask); 630 sigaction(i, &sa, NULL); 631 } 632 } 633 } 634 635 fflush(stdout); 636 fflush(stderr); 637 ret = wexec_call_builtin(base, argv); 638 fflush(stdout); 639 fflush(stderr); 640 exit(ret); 641 } 642 } 643 #endif 644 execv(path, argv); 645 weprintf("execv %s:", path); 646 exit((errno == ENOENT) ? 127 : 126); 647 } 648 649 void 650 wexecve_self(const char *path, char *const *argv, char *const *envp) 651 { 652 #if FEATURE_NOEXEC 653 { 654 const char *base = strrchr(path, '/'); 655 base = base ? base + 1 : path; 656 if (wexec_get_noexec() && wexec_is_builtin(base)) { 657 struct sigaction sa; 658 char **oldenv; 659 int i, ret; 660 661 for (i = 1; i < NSIG; i++) { 662 if (sigaction(i, NULL, &sa) == 0) { 663 if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { 664 sa.sa_handler = SIG_DFL; 665 sa.sa_flags = 0; 666 sigemptyset(&sa.sa_mask); 667 sigaction(i, &sa, NULL); 668 } 669 } 670 } 671 672 oldenv = environ; 673 environ = (char **)envp; 674 fflush(stdout); 675 fflush(stderr); 676 ret = wexec_call_builtin(base, argv); 677 environ = oldenv; 678 fflush(stdout); 679 fflush(stderr); 680 exit(ret); 681 } 682 } 683 #endif 684 execve(path, argv, envp); 685 weprintf("execve %s:", path); 686 exit((errno == ENOENT) ? 127 : 126); 687 } 688 689 void(exit)(int); 690 691 void 692 wexec_exit(int code) 693 { 694 #if FEATURE_NOEXEC || FEATURE_NOFORK 695 if (inproc_active) { 696 inproc_exit_status = code; 697 siglongjmp(inproc_jmp, 1); 698 } 699 #endif 700 (exit)(code); 701 }