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 #ifndef lint 36 #endif /* not lint */ 37 #include <sys/cdefs.h> 38 /* 39 * Miscellaneous builtins. 40 */ 41 42 #include <sys/types.h> 43 #include <sys/stat.h> 44 #include <sys/time.h> 45 #include <sys/resource.h> 46 #include <unistd.h> 47 #include <errno.h> 48 #include <stdint.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 52 #include "shell.h" 53 #include "options.h" 54 #include "var.h" 55 #include "output.h" 56 #include "memalloc.h" 57 #include "error.h" 58 #include "mystring.h" 59 #include "syntax.h" 60 #include "trap.h" 61 62 #undef eflag 63 64 #define READ_BUFLEN 1024 65 struct fdctx { 66 int fd; 67 size_t off; /* offset in buf */ 68 size_t buflen; 69 char *ep; /* tail pointer */ 70 char buf[READ_BUFLEN]; 71 }; 72 73 static void fdctx_init(int, struct fdctx *); 74 static void fdctx_destroy(struct fdctx *); 75 static ssize_t fdgetc(struct fdctx *, char *); 76 int readcmd(int, char **); 77 int umaskcmd(int, char **); 78 int ulimitcmd(int, char **); 79 80 static void 81 fdctx_init(int fd, struct fdctx *fdc) 82 { 83 off_t cur; 84 85 /* Check if fd is seekable. */ 86 cur = lseek(fd, 0, SEEK_CUR); 87 *fdc = (struct fdctx){ 88 .fd = fd, 89 .buflen = (cur != -1) ? READ_BUFLEN : 1, 90 .ep = &fdc->buf[0], /* No data */ 91 }; 92 } 93 94 static ssize_t 95 fdgetc(struct fdctx *fdc, char *c) 96 { 97 ssize_t nread; 98 99 if (&fdc->buf[fdc->off] == fdc->ep) { 100 nread = read(fdc->fd, fdc->buf, fdc->buflen); 101 if (nread > 0) { 102 fdc->off = 0; 103 fdc->ep = fdc->buf + nread; 104 } else 105 return (nread); 106 } 107 *c = fdc->buf[fdc->off++]; 108 109 return (1); 110 } 111 112 static void 113 fdctx_destroy(struct fdctx *fdc) 114 { 115 off_t residue; 116 117 if (fdc->buflen > 1) { 118 /* 119 * Reposition the file offset. Here is the layout of buf: 120 * 121 * | off 122 * v 123 * |*****************|-------| 124 * buf ep buf+buflen 125 * |<- residue ->| 126 * 127 * off: current character 128 * ep: offset just after read(2) 129 * residue: length for reposition 130 */ 131 residue = (fdc->ep - fdc->buf) - fdc->off; 132 if (residue > 0) 133 (void) lseek(fdc->fd, -residue, SEEK_CUR); 134 } 135 } 136 137 /* 138 * The read builtin. The -r option causes backslashes to be treated like 139 * ordinary characters. 140 * 141 * Note that if IFS=' :' then read x y should work so that: 142 * 'a b' x='a', y='b' 143 * ' a b ' x='a', y='b' 144 * ':b' x='', y='b' 145 * ':' x='', y='' 146 * '::' x='', y='' 147 * ': :' x='', y='' 148 * ':::' x='', y='::' 149 * ':b c:' x='', y='b c:' 150 */ 151 152 int 153 readcmd(int argc __unused, char **argv __unused) 154 { 155 char **ap; 156 int backslash; 157 char c; 158 int rflag; 159 char *prompt; 160 const char *ifs; 161 char *p; 162 int startword; 163 int status; 164 int i; 165 int is_ifs; 166 int saveall = 0; 167 ptrdiff_t lastnonifs, lastnonifsws; 168 struct timeval tv; 169 char *tvptr; 170 fd_set ifds; 171 ssize_t nread; 172 int sig; 173 struct fdctx fdctx; 174 175 rflag = 0; 176 prompt = NULL; 177 tv.tv_sec = -1; 178 tv.tv_usec = 0; 179 while ((i = nextopt("erp:t:")) != '\0') { 180 switch(i) { 181 case 'p': 182 prompt = shoptarg; 183 break; 184 case 'e': 185 break; 186 case 'r': 187 rflag = 1; 188 break; 189 case 't': 190 tv.tv_sec = strtol(shoptarg, &tvptr, 0); 191 if (tvptr == shoptarg) 192 error("timeout value"); 193 switch(*tvptr) { 194 case 0: 195 case 's': 196 break; 197 case 'h': 198 tv.tv_sec *= 60; 199 /* FALLTHROUGH */ 200 case 'm': 201 tv.tv_sec *= 60; 202 break; 203 default: 204 error("timeout unit"); 205 } 206 break; 207 } 208 } 209 if (prompt && isatty(0)) { 210 out2str(prompt); 211 flushall(); 212 } 213 if (*(ap = argptr) == NULL) 214 error("arg count"); 215 if ((ifs = bltinlookup("IFS", 1)) == NULL) 216 ifs = " \t\n"; 217 218 if (tv.tv_sec >= 0) { 219 /* 220 * Wait for something to become available. 221 */ 222 FD_ZERO(&ifds); 223 FD_SET(0, &ifds); 224 status = select(1, &ifds, NULL, NULL, &tv); 225 /* 226 * If there's nothing ready, return an error. 227 */ 228 if (status <= 0) { 229 while (*ap != NULL) 230 setvar(*ap++, "", 0); 231 sig = pendingsig; 232 return (128 + (sig != 0 ? sig : SIGALRM)); 233 } 234 } 235 236 status = 0; 237 startword = 2; 238 backslash = 0; 239 STARTSTACKSTR(p); 240 lastnonifs = lastnonifsws = -1; 241 fdctx_init(STDIN_FILENO, &fdctx); 242 for (;;) { 243 c = 0; 244 nread = fdgetc(&fdctx, &c); 245 if (nread == -1) { 246 if (errno == EINTR) { 247 sig = pendingsig; 248 if (sig == 0) 249 continue; 250 status = 128 + sig; 251 break; 252 } 253 warning("read error: %s", strerror(errno)); 254 status = 2; 255 break; 256 } else if (nread != 1) { 257 status = 1; 258 break; 259 } 260 if (c == '\0') 261 continue; 262 CHECKSTRSPACE(1, p); 263 if (backslash) { 264 backslash = 0; 265 if (c != '\n') { 266 startword = 0; 267 lastnonifs = lastnonifsws = p - stackblock(); 268 USTPUTC(c, p); 269 } 270 continue; 271 } 272 if (!rflag && c == '\\') { 273 backslash++; 274 continue; 275 } 276 if (c == '\n') 277 break; 278 if (strchr(ifs, c)) 279 is_ifs = strchr(" \t\n", c) ? 1 : 2; 280 else 281 is_ifs = 0; 282 283 if (startword != 0) { 284 if (is_ifs == 1) { 285 /* Ignore leading IFS whitespace */ 286 if (saveall) 287 USTPUTC(c, p); 288 continue; 289 } 290 if (is_ifs == 2 && startword == 1) { 291 /* Only one non-whitespace IFS per word */ 292 startword = 2; 293 if (saveall) { 294 lastnonifsws = p - stackblock(); 295 USTPUTC(c, p); 296 } 297 continue; 298 } 299 } 300 301 if (is_ifs == 0) { 302 /* append this character to the current variable */ 303 startword = 0; 304 if (saveall) 305 /* Not just a spare terminator */ 306 saveall++; 307 lastnonifs = lastnonifsws = p - stackblock(); 308 USTPUTC(c, p); 309 continue; 310 } 311 312 /* end of variable... */ 313 startword = is_ifs; 314 315 if (ap[1] == NULL) { 316 /* Last variable needs all IFS chars */ 317 saveall++; 318 if (is_ifs == 2) 319 lastnonifsws = p - stackblock(); 320 USTPUTC(c, p); 321 continue; 322 } 323 324 STACKSTRNUL(p); 325 setvar(*ap, stackblock(), 0); 326 ap++; 327 STARTSTACKSTR(p); 328 lastnonifs = lastnonifsws = -1; 329 } 330 fdctx_destroy(&fdctx); 331 STACKSTRNUL(p); 332 333 /* 334 * Remove trailing IFS chars: always remove whitespace, don't remove 335 * non-whitespace unless it was naked 336 */ 337 if (saveall <= 1) 338 lastnonifsws = lastnonifs; 339 stackblock()[lastnonifsws + 1] = '\0'; 340 setvar(*ap, stackblock(), 0); 341 342 /* Set any remaining args to "" */ 343 while (*++ap != NULL) 344 setvar(*ap, "", 0); 345 return status; 346 } 347 348 349 350 int 351 umaskcmd(int argc __unused, char **argv __unused) 352 { 353 char *ap; 354 int mask; 355 int i; 356 int symbolic_mode = 0; 357 358 while ((i = nextopt("S")) != '\0') { 359 symbolic_mode = 1; 360 } 361 362 INTOFF; 363 mask = umask(0); 364 umask(mask); 365 INTON; 366 367 if ((ap = *argptr) == NULL) { 368 if (symbolic_mode) { 369 char u[4], g[4], o[4]; 370 371 i = 0; 372 if ((mask & S_IRUSR) == 0) 373 u[i++] = 'r'; 374 if ((mask & S_IWUSR) == 0) 375 u[i++] = 'w'; 376 if ((mask & S_IXUSR) == 0) 377 u[i++] = 'x'; 378 u[i] = '\0'; 379 380 i = 0; 381 if ((mask & S_IRGRP) == 0) 382 g[i++] = 'r'; 383 if ((mask & S_IWGRP) == 0) 384 g[i++] = 'w'; 385 if ((mask & S_IXGRP) == 0) 386 g[i++] = 'x'; 387 g[i] = '\0'; 388 389 i = 0; 390 if ((mask & S_IROTH) == 0) 391 o[i++] = 'r'; 392 if ((mask & S_IWOTH) == 0) 393 o[i++] = 'w'; 394 if ((mask & S_IXOTH) == 0) 395 o[i++] = 'x'; 396 o[i] = '\0'; 397 398 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 399 } else { 400 out1fmt("%.4o\n", mask); 401 } 402 } else { 403 if (is_digit(*ap)) { 404 mask = 0; 405 do { 406 if (*ap >= '8' || *ap < '0') 407 error("Illegal number: %s", *argptr); 408 mask = (mask << 3) + (*ap - '0'); 409 } while (*++ap != '\0'); 410 umask(mask); 411 } else { 412 void *set; 413 INTOFF; 414 if ((set = setmode (ap)) == NULL) 415 error("Illegal number: %s", ap); 416 417 mask = getmode (set, ~mask & 0777); 418 umask(~mask & 0777); 419 free(set); 420 INTON; 421 } 422 } 423 return 0; 424 } 425 426 /* 427 * ulimit builtin 428 * 429 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 430 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 431 * ash by J.T. Conklin. 432 * 433 * Public domain. 434 */ 435 436 struct limits { 437 const char *name; 438 const char *units; 439 int cmd; 440 short factor; /* multiply by to get rlim_{cur,max} values */ 441 char option; 442 }; 443 444 static const struct limits limits[] = { 445 #ifdef RLIMIT_CPU 446 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 447 #endif 448 #ifdef RLIMIT_FSIZE 449 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 450 #endif 451 #ifdef RLIMIT_DATA 452 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 453 #endif 454 #ifdef RLIMIT_STACK 455 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 456 #endif 457 #ifdef RLIMIT_CORE 458 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 459 #endif 460 #ifdef RLIMIT_RSS 461 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 462 #endif 463 #ifdef RLIMIT_MEMLOCK 464 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 465 #endif 466 #ifdef RLIMIT_NPROC 467 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 468 #endif 469 #ifdef RLIMIT_NOFILE 470 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 471 #endif 472 #ifdef RLIMIT_VMEM 473 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 474 #endif 475 #ifdef RLIMIT_SWAP 476 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 477 #endif 478 #ifdef RLIMIT_SBSIZE 479 { "socket buffer size", "bytes", RLIMIT_SBSIZE, 1, 'b' }, 480 #endif 481 #ifdef RLIMIT_NPTS 482 { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' }, 483 #endif 484 #ifdef RLIMIT_KQUEUES 485 { "kqueues", (char *)0, RLIMIT_KQUEUES, 1, 'k' }, 486 #endif 487 #ifdef RLIMIT_UMTXP 488 { "umtx shared locks", (char *)0, RLIMIT_UMTXP, 1, 'o' }, 489 #endif 490 { (char *) 0, (char *)0, 0, 0, '\0' } 491 }; 492 493 enum limithow { SOFT = 0x1, HARD = 0x2 }; 494 495 static void 496 printlimit(enum limithow how, const struct rlimit *limit, 497 const struct limits *l) 498 { 499 rlim_t val = 0; 500 501 if (how & SOFT) 502 val = limit->rlim_cur; 503 else if (how & HARD) 504 val = limit->rlim_max; 505 if (val == RLIM_INFINITY) 506 out1str("unlimited\n"); 507 else 508 { 509 val /= l->factor; 510 out1fmt("%jd\n", (intmax_t)val); 511 } 512 } 513 514 int 515 ulimitcmd(int argc __unused, char **argv __unused) 516 { 517 rlim_t val = 0; 518 enum limithow how = SOFT | HARD; 519 const struct limits *l; 520 int set, all = 0; 521 int optc, what; 522 struct rlimit limit; 523 524 what = 'f'; 525 while ((optc = nextopt("HSatfdsmcnuvlbpwko")) != '\0') 526 switch (optc) { 527 case 'H': 528 how = HARD; 529 break; 530 case 'S': 531 how = SOFT; 532 break; 533 case 'a': 534 all = 1; 535 break; 536 default: 537 what = optc; 538 } 539 540 for (l = limits; l->name && l->option != what; l++) 541 ; 542 if (!l->name) 543 error("internal error (%c)", what); 544 545 set = *argptr ? 1 : 0; 546 if (set) { 547 char *p = *argptr; 548 549 if (all || argptr[1]) 550 error("too many arguments"); 551 if (strcmp(p, "unlimited") == 0) 552 val = RLIM_INFINITY; 553 else { 554 char *end; 555 uintmax_t uval; 556 557 if (*p < '0' || *p > '9') 558 error("bad number"); 559 errno = 0; 560 uval = strtoumax(p, &end, 10); 561 if (errno != 0 || *end != '\0') 562 error("bad number"); 563 if (uval > UINTMAX_MAX / l->factor) 564 error("bad number"); 565 uval *= l->factor; 566 val = (rlim_t)uval; 567 if (val < 0 || (uintmax_t)val != uval || 568 val == RLIM_INFINITY) 569 error("bad number"); 570 } 571 } 572 if (all) { 573 for (l = limits; l->name; l++) { 574 char optbuf[40]; 575 if (getrlimit(l->cmd, &limit) < 0) 576 error("can't get limit: %s", strerror(errno)); 577 578 if (l->units) 579 snprintf(optbuf, sizeof(optbuf), 580 "(%s, -%c) ", l->units, l->option); 581 else 582 snprintf(optbuf, sizeof(optbuf), 583 "(-%c) ", l->option); 584 out1fmt("%-18s %18s ", l->name, optbuf); 585 printlimit(how, &limit, l); 586 } 587 return 0; 588 } 589 590 if (getrlimit(l->cmd, &limit) < 0) 591 error("can't get limit: %s", strerror(errno)); 592 if (set) { 593 if (how & SOFT) 594 limit.rlim_cur = val; 595 if (how & HARD) 596 limit.rlim_max = val; 597 if (setrlimit(l->cmd, &limit) < 0) 598 error("bad limit: %s", strerror(errno)); 599 } else 600 printlimit(how, &limit, l); 601 return 0; 602 } 603