1 /* $NetBSD: miscbltin.c,v 1.52 2022/08/19 12:52:31 kre Exp $ */ 2 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 #include <sys/cdefs.h> 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 39 #else 40 __RCSID("$NetBSD: miscbltin.c,v 1.52 2022/08/19 12:52:31 kre Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 /* 45 * Miscellaneous builtins. 46 */ 47 48 #include <sys/types.h> /* quad_t */ 49 #include <sys/param.h> /* BSD4_4 */ 50 #include <sys/stat.h> 51 #include <sys/time.h> 52 #include <sys/resource.h> 53 #include <unistd.h> 54 #include <stdlib.h> 55 #include <ctype.h> 56 #include <errno.h> 57 58 #include "shell.h" 59 #include "options.h" 60 #include "var.h" 61 #include "output.h" 62 #include "memalloc.h" 63 #include "error.h" 64 #include "builtins.h" 65 #include "mystring.h" 66 #include "redir.h" /* for user_fd_limit */ 67 68 #undef rflag 69 70 71 72 /* 73 * The read builtin. 74 * Backslashes escape the next char unless -r is specified. 75 * 76 * This uses unbuffered input, which may be avoidable in some cases. 77 * 78 * Note that if IFS=' :' then read x y should work so that: 79 * 'a b' x='a', y='b' 80 * ' a b ' x='a', y='b' 81 * ':b' x='', y='b' 82 * ':' x='', y='' 83 * '::' x='', y='' 84 * ': :' x='', y='' 85 * ':::' x='', y='::' 86 * ':b c:' x='', y='b c:' 87 */ 88 89 int 90 readcmd(int argc, char **argv) 91 { 92 char **ap; 93 char c; 94 int rflag; 95 char *prompt; 96 const char *ifs; 97 char *p; 98 int startword; 99 int status; 100 int i; 101 int is_ifs; 102 int saveall = 0; 103 ptrdiff_t wordlen = 0; 104 105 rflag = 0; 106 prompt = NULL; 107 while ((i = nextopt("p:r")) != '\0') { 108 if (i == 'p') 109 prompt = optionarg; 110 else 111 rflag = 1; 112 } 113 114 if (*(ap = argptr) == NULL) 115 error("variable name required\n" 116 "Usage: read [-r] [-p prompt] var..."); 117 118 if (prompt && isatty(0)) { 119 out2str(prompt); 120 flushall(); 121 } 122 123 if ((ifs = bltinlookup("IFS", 1)) == NULL) 124 ifs = " \t\n"; 125 126 status = 0; 127 startword = 2; 128 STARTSTACKSTR(p); 129 for (;;) { 130 if (read(0, &c, 1) != 1) { 131 status = 1; 132 break; 133 } 134 if (c == '\0') 135 continue; 136 if (c == '\\' && !rflag) { 137 if (read(0, &c, 1) != 1) { 138 status = 1; 139 break; 140 } 141 if (c != '\n') 142 goto wdch; 143 continue; 144 } 145 if (c == '\n') 146 break; 147 if (strchr(ifs, c)) 148 is_ifs = strchr(" \t\n", c) ? 1 : 2; 149 else 150 is_ifs = 0; 151 152 if (startword != 0) { 153 if (is_ifs == 1) { 154 /* Ignore leading IFS whitespace */ 155 if (saveall) 156 STPUTC(c, p); 157 continue; 158 } 159 if (is_ifs == 2 && startword == 1) { 160 /* Only one non-whitespace IFS per word */ 161 startword = 2; 162 if (saveall) 163 STPUTC(c, p); 164 continue; 165 } 166 } 167 168 if (is_ifs == 0) { 169 wdch:; 170 /* append this character to the current variable */ 171 startword = 0; 172 if (saveall) 173 /* Not just a spare terminator */ 174 saveall++; 175 STPUTC(c, p); 176 wordlen = p - stackblock(); 177 continue; 178 } 179 180 /* end of variable... */ 181 startword = is_ifs; 182 183 if (ap[1] == NULL) { 184 /* Last variable needs all IFS chars */ 185 saveall++; 186 STPUTC(c, p); 187 continue; 188 } 189 190 STACKSTRNUL(p); 191 setvar(*ap, stackblock(), 0); 192 ap++; 193 STARTSTACKSTR(p); 194 wordlen = 0; 195 } 196 STACKSTRNUL(p); 197 198 /* Remove trailing IFS chars */ 199 for (; stackblock() + wordlen <= --p; *p = 0) { 200 if (!strchr(ifs, *p)) 201 break; 202 if (strchr(" \t\n", *p)) 203 /* Always remove whitespace */ 204 continue; 205 if (saveall > 1) 206 /* Don't remove non-whitespace unless it was naked */ 207 break; 208 } 209 setvar(*ap, stackblock(), 0); 210 211 /* Set any remaining args to "" */ 212 while (*++ap != NULL) 213 setvar(*ap, nullstr, 0); 214 return status; 215 } 216 217 218 219 int 220 umaskcmd(int argc, char **argv) 221 { 222 char *ap; 223 mode_t mask; 224 int i; 225 int symbolic_mode = 0; 226 227 while ((i = nextopt("S")) != '\0') { 228 symbolic_mode = 1; 229 } 230 231 INTOFF; 232 mask = umask(0); 233 umask(mask); 234 INTON; 235 236 if ((ap = *argptr) == NULL) { 237 if (symbolic_mode) { 238 char u[4], g[4], o[4]; 239 240 i = 0; 241 if ((mask & S_IRUSR) == 0) 242 u[i++] = 'r'; 243 if ((mask & S_IWUSR) == 0) 244 u[i++] = 'w'; 245 if ((mask & S_IXUSR) == 0) 246 u[i++] = 'x'; 247 u[i] = '\0'; 248 249 i = 0; 250 if ((mask & S_IRGRP) == 0) 251 g[i++] = 'r'; 252 if ((mask & S_IWGRP) == 0) 253 g[i++] = 'w'; 254 if ((mask & S_IXGRP) == 0) 255 g[i++] = 'x'; 256 g[i] = '\0'; 257 258 i = 0; 259 if ((mask & S_IROTH) == 0) 260 o[i++] = 'r'; 261 if ((mask & S_IWOTH) == 0) 262 o[i++] = 'w'; 263 if ((mask & S_IXOTH) == 0) 264 o[i++] = 'x'; 265 o[i] = '\0'; 266 267 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 268 } else { 269 out1fmt("%.4o\n", mask); 270 } 271 } else { 272 if (isdigit((unsigned char)*ap)) { 273 int range = 0; 274 275 mask = 0; 276 do { 277 if (*ap >= '8' || *ap < '0') 278 error("Not a valid octal number: '%s'", 279 *argptr); 280 mask = (mask << 3) + (*ap - '0'); 281 if (mask & ~07777) 282 range = 1; 283 } while (*++ap != '\0'); 284 if (range) 285 error("Mask constant '%s' out of range", *argptr); 286 umask(mask); 287 } else { 288 void *set; 289 290 INTOFF; 291 if ((set = setmode(ap)) != 0) { 292 mask = getmode(set, ~mask & 0777); 293 ckfree(set); 294 } 295 INTON; 296 if (!set) 297 error("Cannot set mode `%s' (%s)", ap, 298 strerror(errno)); 299 300 umask(~mask & 0777); 301 } 302 } 303 flushout(out1); 304 if (io_err(out1)) { 305 out2str("umask: I/O error\n"); 306 return 1; 307 } 308 return 0; 309 } 310 311 /* 312 * ulimit builtin 313 * 314 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 315 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 316 * ash by J.T. Conklin. 317 * 318 * Public domain. 319 */ 320 321 struct limits { 322 const char *name; 323 const char *unit; 324 char option; 325 int8_t cmd; /* all RLIMIT_xxx are <= 127 */ 326 unsigned short factor; /* multiply by to get rlim_{cur,max} values */ 327 }; 328 329 #define OPTSTRING_BASE "HSa" 330 331 static const struct limits limits[] = { 332 #ifdef RLIMIT_CPU 333 { "time", "seconds", 't', RLIMIT_CPU, 1 }, 334 #define OPTSTRING_t OPTSTRING_BASE "t" 335 #else 336 #define OPTSTRING_t OPTSTRING_BASE 337 #endif 338 #ifdef RLIMIT_FSIZE 339 { "file", "blocks", 'f', RLIMIT_FSIZE, 512 }, 340 #define OPTSTRING_f OPTSTRING_t "f" 341 #else 342 #define OPTSTRING_f OPTSTRING_t 343 #endif 344 #ifdef RLIMIT_DATA 345 { "data", "kbytes", 'd', RLIMIT_DATA, 1024 }, 346 #define OPTSTRING_d OPTSTRING_f "d" 347 #else 348 #define OPTSTRING_d OPTSTRING_f 349 #endif 350 #ifdef RLIMIT_STACK 351 { "stack", "kbytes", 's', RLIMIT_STACK, 1024 }, 352 #define OPTSTRING_s OPTSTRING_d "s" 353 #else 354 #define OPTSTRING_s OPTSTRING_d 355 #endif 356 #ifdef RLIMIT_CORE 357 { "coredump", "blocks", 'c', RLIMIT_CORE, 512 }, 358 #define OPTSTRING_c OPTSTRING_s "c" 359 #else 360 #define OPTSTRING_c OPTSTRING_s 361 #endif 362 #ifdef RLIMIT_RSS 363 { "memory", "kbytes", 'm', RLIMIT_RSS, 1024 }, 364 #define OPTSTRING_m OPTSTRING_c "m" 365 #else 366 #define OPTSTRING_m OPTSTRING_c 367 #endif 368 #ifdef RLIMIT_MEMLOCK 369 { "locked memory","kbytes", 'l', RLIMIT_MEMLOCK, 1024 }, 370 #define OPTSTRING_l OPTSTRING_m "l" 371 #else 372 #define OPTSTRING_l OPTSTRING_m 373 #endif 374 #ifdef RLIMIT_NTHR 375 { "thread", "threads", 'r', RLIMIT_NTHR, 1 }, 376 #define OPTSTRING_r OPTSTRING_l "r" 377 #else 378 #define OPTSTRING_r OPTSTRING_l 379 #endif 380 #ifdef RLIMIT_NPROC 381 { "process", "processes", 'p', RLIMIT_NPROC, 1 }, 382 #define OPTSTRING_p OPTSTRING_r "p" 383 #else 384 #define OPTSTRING_p OPTSTRING_r 385 #endif 386 #ifdef RLIMIT_NOFILE 387 { "nofiles", "descriptors", 'n', RLIMIT_NOFILE, 1 }, 388 #define OPTSTRING_n OPTSTRING_p "n" 389 #else 390 #define OPTSTRING_n OPTSTRING_p 391 #endif 392 #ifdef RLIMIT_VMEM 393 { "vmemory", "kbytes", 'v', RLIMIT_VMEM, 1024 }, 394 #define OPTSTRING_v OPTSTRING_n "v" 395 #else 396 #define OPTSTRING_v OPTSTRING_n 397 #endif 398 #ifdef RLIMIT_SWAP 399 { "swap", "kbytes", 'w', RLIMIT_SWAP, 1024 }, 400 #define OPTSTRING_w OPTSTRING_v "w" 401 #else 402 #define OPTSTRING_w OPTSTRING_v 403 #endif 404 #ifdef RLIMIT_SBSIZE 405 { "sbsize", "bytes", 'b', RLIMIT_SBSIZE, 1 }, 406 #define OPTSTRING_b OPTSTRING_w "b" 407 #else 408 #define OPTSTRING_b OPTSTRING_w 409 #endif 410 { NULL, NULL, '\0', 0, 0 } 411 }; 412 #define OPTSTRING OPTSTRING_b 413 414 int 415 ulimitcmd(int argc, char **argv) 416 { 417 int c; 418 rlim_t val = 0; 419 enum { SOFT = 0x1, HARD = 0x2 } 420 how = 0, which; 421 const struct limits *l; 422 int set, all = 0; 423 int optc, what; 424 struct rlimit limit; 425 426 what = 'f'; 427 while ((optc = nextopt(OPTSTRING)) != '\0') 428 switch (optc) { 429 case 'H': 430 how |= HARD; 431 break; 432 case 'S': 433 how |= SOFT; 434 break; 435 case 'a': 436 all = 1; 437 break; 438 default: 439 what = optc; 440 } 441 442 for (l = limits; l->name && l->option != what; l++) 443 ; 444 if (!l->name) 445 error("internal error (%c)", what); 446 447 set = *argptr ? 1 : 0; 448 if (set) { 449 char *p = *argptr; 450 451 if (all || argptr[1]) 452 error("too many arguments"); 453 if (how == 0) 454 how = HARD | SOFT; 455 456 if (strcmp(p, "unlimited") == 0) 457 val = RLIM_INFINITY; 458 else { 459 val = (rlim_t) 0; 460 461 while ((c = *p++) >= '0' && c <= '9') { 462 if (val >= RLIM_INFINITY/10) 463 error("%s: value overflow", *argptr); 464 val = (val * 10); 465 if (val >= RLIM_INFINITY - (long)(c - '0')) 466 error("%s: value overflow", *argptr); 467 val += (long)(c - '0'); 468 } 469 if (c) 470 error("%s: bad number", *argptr); 471 if (val > RLIM_INFINITY / l->factor) 472 error("%s: value overflow", *argptr); 473 val *= l->factor; 474 } 475 } else if (how == 0) 476 how = SOFT; 477 478 if (all) { 479 for (l = limits; l->name; l++) { 480 getrlimit(l->cmd, &limit); 481 out1fmt("%-13s (-%c %-11s) ", l->name, l->option, 482 l->unit); 483 484 which = how; 485 while (which != 0) { 486 if (which & SOFT) { 487 val = limit.rlim_cur; 488 which &= ~SOFT; 489 } else if (which & HARD) { 490 val = limit.rlim_max; 491 which &= ~HARD; 492 } 493 494 if (val == RLIM_INFINITY) 495 out1fmt("unlimited"); 496 else { 497 val /= l->factor; 498 #ifdef BSD4_4 499 out1fmt("%9lld", (long long) val); 500 #else 501 out1fmt("%9ld", (long) val); 502 #endif 503 } 504 out1fmt("%c", which ? '\t' : '\n'); 505 } 506 } 507 goto done; 508 } 509 510 if (getrlimit(l->cmd, &limit) == -1) 511 error("error getting limit (%s)", strerror(errno)); 512 if (set) { 513 if (how & HARD) 514 limit.rlim_max = val; 515 if (how & SOFT) 516 limit.rlim_cur = val; 517 if (setrlimit(l->cmd, &limit) < 0) 518 error("error setting limit (%s)", strerror(errno)); 519 if (l->cmd == RLIMIT_NOFILE) 520 user_fd_limit = sysconf(_SC_OPEN_MAX); 521 } else { 522 if (how & SOFT) 523 val = limit.rlim_cur; 524 else if (how & HARD) 525 val = limit.rlim_max; 526 527 if (val == RLIM_INFINITY) 528 out1fmt("unlimited\n"); 529 else 530 { 531 val /= l->factor; 532 #ifdef BSD4_4 533 out1fmt("%lld\n", (long long) val); 534 #else 535 out1fmt("%ld\n", (long) val); 536 #endif 537 } 538 } 539 done:; 540 flushout(out1); 541 if (io_err(out1)) { 542 out2str("ulimit: I/O error (stdout)\n"); 543 return 1; 544 } 545 return 0; 546 } 547