miscbltin.c (16462B)
1 /* - 2 * spdx-license-identifier: bsd-3-clause 3 * 4 * copyright (c) 1991, 1993 5 * the regents of the university of california. all rights reserved 6 * 7 * this code is derived from software contributed to berkeley by 8 * kenneth almquist 9 * 10 * redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer 15 * 2. redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution 18 * 3. neither the name of the university nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR a PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE 33 */ 34 35 /* 36 * miscellaneous builtins 37 */ 38 39 #if FEATURE_SH_EXT 40 #ifndef _DEFAULT_SOURCE 41 #define _DEFAULT_SOURCE 42 #endif 43 #ifndef _GNU_SOURCE 44 #define _GNU_SOURCE 45 #endif 46 #endif 47 #include <sys/resource.h> 48 #include <sys/stat.h> 49 #include <sys/time.h> 50 #include <sys/types.h> 51 52 #include <errno.h> 53 #include <poll.h> 54 #include <signal.h> 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <time.h> 59 #include <unistd.h> 60 61 #include "redline.h" 62 #include "shell.h" 63 64 #ifndef timespeccmp 65 #define timespeccmp(tvp, uvp, cmp) \ 66 (((tvp)->tv_sec == (uvp)->tv_sec) ? ((tvp)->tv_nsec cmp(uvp)->tv_nsec) \ 67 : ((tvp)->tv_sec cmp(uvp)->tv_sec)) 68 #endif 69 #include "error.h" 70 #include "memalloc.h" 71 #include "mystring.h" 72 #include "options.h" 73 #include "output.h" 74 #include "syntax.h" 75 #include "trap.h" 76 #include "var.h" 77 78 #undef eflag 79 80 #define READ_BUFLEN 1024 81 struct fdctx { 82 int fd; 83 size_t off; /* offset in buf */ 84 size_t buflen; 85 char *ep; /* tail pointer */ 86 char buf[READ_BUFLEN]; 87 }; 88 89 static void fdctx_init(int, struct fdctx *); 90 static void fdctx_destroy(struct fdctx *); 91 static ssize_t fdgetc(struct fdctx *, char *); 92 int readcmd(int, char **); 93 int umaskcmd(int, char **); 94 int ulimitcmd(int, char **); 95 96 extern mode_t parsemode(const char *str, mode_t mode, mode_t mask); 97 98 static void 99 fdctx_init(int fd, struct fdctx *fdc) 100 { 101 off_t cur; 102 103 /* check if fd is seekable */ 104 cur = lseek(fd, 0, SEEK_CUR); 105 *fdc = (struct fdctx){ 106 .fd = fd, 107 .buflen = (cur != -1) ? READ_BUFLEN : 1, 108 .ep = &fdc->buf[0], /* no data */ 109 }; 110 } 111 112 static ssize_t 113 fdgetc(struct fdctx *fdc, char *c) 114 { 115 ssize_t nread; 116 117 if (&fdc->buf[fdc->off] == fdc->ep) { 118 nread = read(fdc->fd, fdc->buf, fdc->buflen); 119 if (nread > 0) { 120 fdc->off = 0; 121 fdc->ep = fdc->buf + nread; 122 } else 123 return (nread); 124 } 125 *c = fdc->buf[fdc->off++]; 126 127 return (1); 128 } 129 130 static void 131 fdctx_destroy(struct fdctx *fdc) 132 { 133 off_t residue; 134 135 if (fdc->buflen > 1) { 136 /* 137 * reposition the file offset. here is the layout of buf: 138 * 139 * | off 140 * v 141 * |*****************|-------| 142 * buf ep buf+buflen 143 * |<- residue ->| 144 * 145 * off: current character 146 * ep: offset just after read(2) 147 * residue: length for reposition 148 */ 149 residue = (fdc->ep - fdc->buf) - fdc->off; 150 if (residue > 0) 151 (void)lseek(fdc->fd, -residue, SEEK_CUR); 152 } 153 } 154 155 /* 156 * the read builtin. the -r option causes backslashes to be treated like 157 * ordinary characters 158 * 159 * note that if IFS=' :' then read x y should work so that: 160 * 'a b' x='a', y='b' 161 * ' a b ' x='a', y='b' 162 * ':b' x='', y='b' 163 * ':' x='', y='' 164 * '::' x='', y='' 165 * ': :' x='', y='' 166 * ':::' x='', y='::' 167 * ':b c:' x='', y='b c:' 168 */ 169 170 int 171 readcmd(int argc __unused, char **argv __unused) 172 { 173 char **ap; 174 int backslash; 175 char c; 176 int rflag; 177 char *prompt; 178 const char *ifs; 179 char *p; 180 int startword; 181 int status; 182 int i; 183 int is_ifs; 184 int saveall = 0; 185 ptrdiff_t lastnonifs, lastnonifsws; 186 sigset_t set, oset; 187 intmax_t number, timeout; 188 struct timespec tnow, tend, tresid; 189 struct pollfd pfd; 190 char *endptr; 191 ssize_t nread; 192 int sig; 193 struct fdctx fdctx; 194 #if FEATURE_SH_HISTEDIT 195 int eflag; 196 char *rl_line; 197 char *rl_line_ptr; 198 size_t rl_idx; 199 #endif 200 201 rflag = 0; 202 prompt = NULL; 203 timeout = -1; 204 #if FEATURE_SH_HISTEDIT 205 eflag = 0; 206 rl_line = NULL; 207 rl_line_ptr = NULL; 208 rl_idx = 0; 209 #endif 210 211 while ((i = nextopt( 212 #if FEATURE_SH_HISTEDIT 213 "erp:t:" 214 #else 215 "rp:t:" 216 #endif 217 )) 218 != '\0') { 219 switch (i) { 220 case 'p': 221 prompt = shoptarg; 222 break; 223 #if FEATURE_SH_HISTEDIT 224 case 'e': 225 eflag = 1; 226 break; 227 #endif 228 case 'r': 229 rflag = 1; 230 break; 231 case 't': 232 timeout = 0; 233 do { 234 number = strtol(shoptarg, &endptr, 0); 235 if (number < 0 || endptr == shoptarg) 236 error("timeout value"); 237 switch (*endptr) { 238 case 's': 239 endptr++; 240 break; 241 case 'h': 242 number *= 60; 243 /* FALLTHROUGH */ 244 case 'm': 245 number *= 60; 246 endptr++; 247 break; 248 } 249 if (*endptr != '\0' && !(*endptr >= '0' && *endptr <= '9')) 250 error("timeout unit"); 251 timeout += number; 252 shoptarg = endptr; 253 } while (*shoptarg != '\0'); 254 break; 255 } 256 } 257 #if FEATURE_SH_HISTEDIT 258 if (eflag && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { 259 rl_line = redline(prompt ? prompt : ""); 260 rl_line_ptr = rl_line; 261 } else 262 #endif 263 if (prompt && isatty(0)) { 264 out2str(prompt); 265 flushall(); 266 } 267 if (*(ap = argptr) == NULL) 268 error("arg count"); 269 if ((ifs = bltinlookup("IFS", 1)) == NULL) 270 ifs = " \t\n"; 271 272 if (timeout >= 0) { 273 /* 274 * wait for something to become available 275 */ 276 pfd.fd = STDIN_FILENO; 277 pfd.events = POLLIN; 278 status = sig = 0; 279 sigfillset(&set); 280 sigprocmask(SIG_SETMASK, &set, &oset); 281 if (pendingsig) { 282 /* caught a signal already */ 283 status = -1; 284 } else if (timeout == 0) { 285 status = poll(&pfd, 1, 0); 286 } else { 287 clock_gettime(CLOCK_UPTIME, &tnow); 288 tend = tnow; 289 tend.tv_sec += timeout; 290 do { 291 timespecsub(&tend, &tnow, &tresid); 292 status = ppoll(&pfd, 1, &tresid, &oset); 293 if (status >= 0 || pendingsig != 0) 294 break; 295 clock_gettime(CLOCK_UPTIME, &tnow); 296 } while (timespeccmp(&tnow, &tend, <)); 297 } 298 sigprocmask(SIG_SETMASK, &oset, NULL); 299 /* 300 * if theres nothing ready, return an error 301 */ 302 if (status <= 0) { 303 while (*ap != NULL) 304 setvar(*ap++, "", 0); 305 sig = pendingsig; 306 return (128 + (sig != 0 ? sig : SIGALRM)); 307 } 308 } 309 310 status = 0; 311 startword = 2; 312 backslash = 0; 313 STARTSTACKSTR(p); 314 lastnonifs = lastnonifsws = -1; 315 fdctx_init(STDIN_FILENO, &fdctx); 316 for (;;) { 317 c = 0; 318 #if FEATURE_SH_HISTEDIT 319 if (rl_line_ptr) { 320 c = rl_line_ptr[rl_idx]; 321 if (c == '\0') { 322 c = '\n'; 323 nread = 1; 324 rl_line_ptr = NULL; 325 } else { 326 rl_idx++; 327 nread = 1; 328 } 329 } else 330 #endif 331 { 332 nread = fdgetc(&fdctx, &c); 333 } 334 if (nread == -1) { 335 if (errno == EINTR) { 336 sig = pendingsig; 337 if (sig == 0) 338 continue; 339 status = 128 + sig; 340 break; 341 } 342 warning("read error: %s", strerror(errno)); 343 status = 2; 344 break; 345 } else if (nread != 1) { 346 status = 1; 347 break; 348 } 349 if (c == '\0') 350 continue; 351 CHECKSTRSPACE(1, p); 352 if (backslash) { 353 backslash = 0; 354 if (c != '\n') { 355 startword = 0; 356 lastnonifs = lastnonifsws = p - stackblock(); 357 USTPUTC(c, p); 358 } 359 continue; 360 } 361 if (!rflag && c == '\\' && !backslash) { 362 backslash++; 363 continue; 364 } 365 if (c == '\n') 366 break; 367 if (strchr(ifs, c)) 368 is_ifs = strchr(" \t\n", c) ? 1 : 2; 369 else 370 is_ifs = 0; 371 372 if (startword != 0) { 373 if (is_ifs == 1) { 374 /* ignore leading IFS whitespace */ 375 if (saveall) 376 USTPUTC(c, p); 377 continue; 378 } 379 if (is_ifs == 2 && startword == 1) { 380 /* only one non-whitespace IFS per word */ 381 startword = 2; 382 if (saveall) { 383 lastnonifsws = p - stackblock(); 384 USTPUTC(c, p); 385 } 386 continue; 387 } 388 } 389 390 if (is_ifs == 0) { 391 /* append this character to the current variable */ 392 startword = 0; 393 if (saveall) 394 /* not just a spare terminator */ 395 saveall++; 396 lastnonifs = lastnonifsws = p - stackblock(); 397 USTPUTC(c, p); 398 continue; 399 } 400 401 /* end of variable... */ 402 startword = is_ifs; 403 404 if (ap[1] == NULL) { 405 /* last variable needs all IFS chars */ 406 saveall++; 407 if (is_ifs == 2) 408 lastnonifsws = p - stackblock(); 409 USTPUTC(c, p); 410 continue; 411 } 412 413 STACKSTRNUL(p); 414 setvar(*ap, stackblock(), 0); 415 ap++; 416 STARTSTACKSTR(p); 417 lastnonifs = lastnonifsws = -1; 418 } 419 fdctx_destroy(&fdctx); 420 STACKSTRNUL(p); 421 422 /* 423 * remove trailing IFS chars: always remove whitespace, dont remove 424 * non-whitespace unless it was naked 425 */ 426 if (saveall <= 1) 427 lastnonifsws = lastnonifs; 428 stackblock()[lastnonifsws + 1] = '\0'; 429 setvar(*ap, stackblock(), 0); 430 431 /* set any remaining args to "" */ 432 while (*++ap != NULL) 433 setvar(*ap, "", 0); 434 #if FEATURE_SH_HISTEDIT 435 free(rl_line); 436 #endif 437 return status; 438 } 439 440 int 441 umaskcmd(int argc __unused, char **argv __unused) 442 { 443 char *ap; 444 int mask; 445 int i; 446 int symbolic_mode = 0; 447 448 while ((i = nextopt("S")) != '\0') { 449 symbolic_mode = 1; 450 } 451 452 INTOFF; 453 mask = umask(0); 454 umask(mask); 455 INTON; 456 457 if ((ap = *argptr) == NULL) { 458 if (symbolic_mode) { 459 char u[4], g[4], o[4]; 460 461 i = 0; 462 if ((mask & S_IRUSR) == 0) 463 u[i++] = 'r'; 464 if ((mask & S_IWUSR) == 0) 465 u[i++] = 'w'; 466 if ((mask & S_IXUSR) == 0) 467 u[i++] = 'x'; 468 u[i] = '\0'; 469 470 i = 0; 471 if ((mask & S_IRGRP) == 0) 472 g[i++] = 'r'; 473 if ((mask & S_IWGRP) == 0) 474 g[i++] = 'w'; 475 if ((mask & S_IXGRP) == 0) 476 g[i++] = 'x'; 477 g[i] = '\0'; 478 479 i = 0; 480 if ((mask & S_IROTH) == 0) 481 o[i++] = 'r'; 482 if ((mask & S_IWOTH) == 0) 483 o[i++] = 'w'; 484 if ((mask & S_IXOTH) == 0) 485 o[i++] = 'x'; 486 o[i] = '\0'; 487 488 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 489 } else { 490 out1fmt("%.4o\n", mask); 491 } 492 } else { 493 if (is_digit(*ap)) { 494 mask = 0; 495 do { 496 if (*ap >= '8' || *ap < '0') 497 error("Illegal number: %s", *argptr); 498 mask = (mask << 3) + (*ap - '0'); 499 } while (*++ap != '\0'); 500 umask(mask); 501 } else { 502 mode_t newmask; 503 INTOFF; 504 newmask = parsemode(ap, ~mask & 0777, mask); 505 umask(~newmask & 0777); 506 INTON; 507 } 508 } 509 return 0; 510 } 511 512 /* 513 * ulimit builtin 514 * 515 * this code, originally by doug gwyn, doug kingston, eric gisin, and 516 * michael rendell was ripped from pdksh 5.0.8 and hacked for use with 517 * ash by J.T. conklin 518 * 519 * public domain 520 */ 521 522 struct limits { 523 const char *name; 524 const char *units; 525 int cmd; 526 short factor; /* multiply by to get rlim_{cur,max} values */ 527 char option; 528 }; 529 530 static const struct limits limits[] = { 531 #ifdef RLIMIT_CPU 532 {"cpu time", "seconds", RLIMIT_CPU, 1, 't'}, 533 #endif 534 #ifdef RLIMIT_FSIZE 535 {"file size", "512-blocks", RLIMIT_FSIZE, 512, 'f'}, 536 #endif 537 #ifdef RLIMIT_DATA 538 {"data seg size", "kbytes", RLIMIT_DATA, 1024, 'd'}, 539 #endif 540 #ifdef RLIMIT_STACK 541 {"stack size", "kbytes", RLIMIT_STACK, 1024, 's'}, 542 #endif 543 #ifdef RLIMIT_CORE 544 {"core file size", "512-blocks", RLIMIT_CORE, 512, 'c'}, 545 #endif 546 #ifdef RLIMIT_RSS 547 {"max memory size", "kbytes", RLIMIT_RSS, 1024, 'm'}, 548 #endif 549 #ifdef RLIMIT_MEMLOCK 550 {"locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l'}, 551 #endif 552 #ifdef RLIMIT_NPROC 553 {"max user processes", (char *)0, RLIMIT_NPROC, 1, 'u'}, 554 #endif 555 #ifdef RLIMIT_NOFILE 556 {"open files", (char *)0, RLIMIT_NOFILE, 1, 'n'}, 557 #endif 558 #ifdef RLIMIT_VMEM 559 {"virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v'}, 560 #endif 561 #ifdef RLIMIT_SWAP 562 {"swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w'}, 563 #endif 564 #ifdef RLIMIT_SBSIZE 565 {"socket buffer size", "bytes", RLIMIT_SBSIZE, 1, 'b'}, 566 #endif 567 #ifdef RLIMIT_NPTS 568 {"pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p'}, 569 #endif 570 #ifdef RLIMIT_KQUEUES 571 {"kqueues", (char *)0, RLIMIT_KQUEUES, 1, 'k'}, 572 #endif 573 #ifdef RLIMIT_UMTXP 574 {"umtx shared locks", (char *)0, RLIMIT_UMTXP, 1, 'o'}, 575 #endif 576 #ifdef RLIMIT_PIPEBUF 577 {"pipebuf", "kbytes", RLIMIT_PIPEBUF, 1024, 'y'}, 578 #endif 579 #ifdef RLIMIT_VMM 580 {"virtual machines", (char *)0, RLIMIT_VMM, 1, 'V'}, 581 #endif 582 {(char *)0, (char *)0, 0, 0, '\0'} 583 }; 584 585 enum limithow { SOFT = 0x1, HARD = 0x2 }; 586 587 static void 588 printlimit(enum limithow how, const struct rlimit *limit, const struct limits *l) 589 { 590 rlim_t val = 0; 591 592 if (how & SOFT) 593 val = limit->rlim_cur; 594 else if (how & HARD) 595 val = limit->rlim_max; 596 if (val == RLIM_INFINITY) 597 out1str("unlimited\n"); 598 else { 599 val /= l->factor; 600 out1fmt("%jd\n", (intmax_t)val); 601 } 602 } 603 604 int 605 ulimitcmd(int argc __unused, char **argv __unused) 606 { 607 rlim_t val = 0; 608 enum limithow how = SOFT | HARD; 609 const struct limits *l; 610 int set, all = 0; 611 int optc, what; 612 struct rlimit limit; 613 614 what = 'f'; 615 while ((optc = nextopt("abcdfHklmnopSstuVvwy")) != '\0') 616 switch (optc) { 617 case 'H': 618 how = HARD; 619 break; 620 case 'S': 621 how = SOFT; 622 break; 623 case 'a': 624 all = 1; 625 break; 626 default: 627 what = optc; 628 } 629 630 for (l = limits; l->name && l->option != what; l++) 631 ; 632 if (!l->name) 633 error("internal error (%c)", what); 634 635 set = *argptr ? 1 : 0; 636 if (set) { 637 char *p = *argptr; 638 639 if (all || argptr[1]) 640 error("too many arguments"); 641 if (strcmp(p, "unlimited") == 0) 642 val = RLIM_INFINITY; 643 else { 644 char *end; 645 uintmax_t uval; 646 647 if (*p < '0' || *p > '9') 648 error("bad number"); 649 errno = 0; 650 uval = strtoumax(p, &end, 10); 651 if (errno != 0 || *end != '\0') 652 error("bad number"); 653 if (uval > UINTMAX_MAX / l->factor) 654 error("bad number"); 655 uval *= l->factor; 656 val = (rlim_t)uval; 657 if ((intmax_t)val < 0 || (uintmax_t)val != uval || val == RLIM_INFINITY) 658 error("bad number"); 659 } 660 } 661 if (all) { 662 for (l = limits; l->name; l++) { 663 char optbuf[40]; 664 if (getrlimit(l->cmd, &limit) < 0) 665 error("can't get limit: %s", strerror(errno)); 666 667 if (l->units) 668 snprintf(optbuf, sizeof(optbuf), "(%s, -%c) ", l->units, l->option); 669 else 670 snprintf(optbuf, sizeof(optbuf), "(-%c) ", l->option); 671 out1fmt("%-18s %18s ", l->name, optbuf); 672 printlimit(how, &limit, l); 673 } 674 return 0; 675 } 676 677 if (getrlimit(l->cmd, &limit) < 0) 678 error("can't get limit: %s", strerror(errno)); 679 if (set) { 680 if (how & SOFT) 681 limit.rlim_cur = val; 682 if (how & HARD) 683 limit.rlim_max = val; 684 if (setrlimit(l->cmd, &limit) < 0) 685 error("bad limit: %s", strerror(errno)); 686 } else 687 printlimit(how, &limit, l); 688 return 0; 689 }