1 /* $NetBSD: miscbltin.c,v 1.45 2021/09/15 18:30:57 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.45 2021/09/15 18:30:57 kre Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 /* 45 * Miscelaneous 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 * Backslahes 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 104 rflag = 0; 105 prompt = NULL; 106 while ((i = nextopt("p:r")) != '\0') { 107 if (i == 'p') 108 prompt = optionarg; 109 else 110 rflag = 1; 111 } 112 113 if (prompt && isatty(0)) { 114 out2str(prompt); 115 flushall(); 116 } 117 118 if (*(ap = argptr) == NULL) 119 error("arg count"); 120 121 if ((ifs = bltinlookup("IFS", 1)) == NULL) 122 ifs = " \t\n"; 123 124 status = 0; 125 startword = 2; 126 STARTSTACKSTR(p); 127 for (;;) { 128 if (read(0, &c, 1) != 1) { 129 status = 1; 130 break; 131 } 132 if (c == '\0') 133 continue; 134 if (c == '\\' && !rflag) { 135 if (read(0, &c, 1) != 1) { 136 status = 1; 137 break; 138 } 139 if (c != '\n') 140 STPUTC(c, p); 141 continue; 142 } 143 if (c == '\n') 144 break; 145 if (strchr(ifs, c)) 146 is_ifs = strchr(" \t\n", c) ? 1 : 2; 147 else 148 is_ifs = 0; 149 150 if (startword != 0) { 151 if (is_ifs == 1) { 152 /* Ignore leading IFS whitespace */ 153 if (saveall) 154 STPUTC(c, p); 155 continue; 156 } 157 if (is_ifs == 2 && startword == 1) { 158 /* Only one non-whitespace IFS per word */ 159 startword = 2; 160 if (saveall) 161 STPUTC(c, p); 162 continue; 163 } 164 } 165 166 if (is_ifs == 0) { 167 /* append this character to the current variable */ 168 startword = 0; 169 if (saveall) 170 /* Not just a spare terminator */ 171 saveall++; 172 STPUTC(c, p); 173 continue; 174 } 175 176 /* end of variable... */ 177 startword = is_ifs; 178 179 if (ap[1] == NULL) { 180 /* Last variable needs all IFS chars */ 181 saveall++; 182 STPUTC(c, p); 183 continue; 184 } 185 186 STACKSTRNUL(p); 187 setvar(*ap, stackblock(), 0); 188 ap++; 189 STARTSTACKSTR(p); 190 } 191 STACKSTRNUL(p); 192 193 /* Remove trailing IFS chars */ 194 for (; stackblock() <= --p; *p = 0) { 195 if (!strchr(ifs, *p)) 196 break; 197 if (strchr(" \t\n", *p)) 198 /* Always remove whitespace */ 199 continue; 200 if (saveall > 1) 201 /* Don't remove non-whitespace unless it was naked */ 202 break; 203 } 204 setvar(*ap, stackblock(), 0); 205 206 /* Set any remaining args to "" */ 207 while (*++ap != NULL) 208 setvar(*ap, nullstr, 0); 209 return status; 210 } 211 212 213 214 int 215 umaskcmd(int argc, char **argv) 216 { 217 char *ap; 218 int mask; 219 int i; 220 int symbolic_mode = 0; 221 222 while ((i = nextopt("S")) != '\0') { 223 symbolic_mode = 1; 224 } 225 226 INTOFF; 227 mask = umask(0); 228 umask(mask); 229 INTON; 230 231 if ((ap = *argptr) == NULL) { 232 if (symbolic_mode) { 233 char u[4], g[4], o[4]; 234 235 i = 0; 236 if ((mask & S_IRUSR) == 0) 237 u[i++] = 'r'; 238 if ((mask & S_IWUSR) == 0) 239 u[i++] = 'w'; 240 if ((mask & S_IXUSR) == 0) 241 u[i++] = 'x'; 242 u[i] = '\0'; 243 244 i = 0; 245 if ((mask & S_IRGRP) == 0) 246 g[i++] = 'r'; 247 if ((mask & S_IWGRP) == 0) 248 g[i++] = 'w'; 249 if ((mask & S_IXGRP) == 0) 250 g[i++] = 'x'; 251 g[i] = '\0'; 252 253 i = 0; 254 if ((mask & S_IROTH) == 0) 255 o[i++] = 'r'; 256 if ((mask & S_IWOTH) == 0) 257 o[i++] = 'w'; 258 if ((mask & S_IXOTH) == 0) 259 o[i++] = 'x'; 260 o[i] = '\0'; 261 262 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 263 } else { 264 out1fmt("%.4o\n", mask); 265 } 266 } else { 267 if (isdigit((unsigned char)*ap)) { 268 mask = 0; 269 do { 270 if (*ap >= '8' || *ap < '0') 271 error("Illegal number: %s", argv[1]); 272 mask = (mask << 3) + (*ap - '0'); 273 } while (*++ap != '\0'); 274 umask(mask); 275 } else { 276 void *set; 277 278 INTOFF; 279 if ((set = setmode(ap)) != 0) { 280 mask = getmode(set, ~mask & 0777); 281 ckfree(set); 282 } 283 INTON; 284 if (!set) 285 error("Cannot set mode `%s' (%s)", ap, 286 strerror(errno)); 287 288 umask(~mask & 0777); 289 } 290 } 291 return 0; 292 } 293 294 /* 295 * ulimit builtin 296 * 297 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 298 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 299 * ash by J.T. Conklin. 300 * 301 * Public domain. 302 */ 303 304 struct limits { 305 const char *name; 306 const char *unit; 307 int cmd; 308 int factor; /* multiply by to get rlim_{cur,max} values */ 309 char option; 310 }; 311 312 static const struct limits limits[] = { 313 #ifdef RLIMIT_CPU 314 { "time", "seconds", RLIMIT_CPU, 1, 't' }, 315 #endif 316 #ifdef RLIMIT_FSIZE 317 { "file", "blocks", RLIMIT_FSIZE, 512, 'f' }, 318 #endif 319 #ifdef RLIMIT_DATA 320 { "data", "kbytes", RLIMIT_DATA, 1024, 'd' }, 321 #endif 322 #ifdef RLIMIT_STACK 323 { "stack", "kbytes", RLIMIT_STACK, 1024, 's' }, 324 #endif 325 #ifdef RLIMIT_CORE 326 { "coredump", "blocks", RLIMIT_CORE, 512, 'c' }, 327 #endif 328 #ifdef RLIMIT_RSS 329 { "memory", "kbytes", RLIMIT_RSS, 1024, 'm' }, 330 #endif 331 #ifdef RLIMIT_MEMLOCK 332 { "locked memory","kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 333 #endif 334 #ifdef RLIMIT_NTHR 335 { "thread", "threads", RLIMIT_NTHR, 1, 'r' }, 336 #endif 337 #ifdef RLIMIT_NPROC 338 { "process", "processes", RLIMIT_NPROC, 1, 'p' }, 339 #endif 340 #ifdef RLIMIT_NOFILE 341 { "nofiles", "descriptors", RLIMIT_NOFILE, 1, 'n' }, 342 #endif 343 #ifdef RLIMIT_VMEM 344 { "vmemory", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 345 #endif 346 #ifdef RLIMIT_SWAP 347 { "swap", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 348 #endif 349 #ifdef RLIMIT_SBSIZE 350 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' }, 351 #endif 352 { NULL, NULL, 0, 0, '\0' } 353 }; 354 355 int 356 ulimitcmd(int argc, char **argv) 357 { 358 int c; 359 rlim_t val = 0; 360 enum { SOFT = 0x1, HARD = 0x2 } 361 how = 0, which; 362 const struct limits *l; 363 int set, all = 0; 364 int optc, what; 365 struct rlimit limit; 366 367 what = 'f'; 368 while ((optc = nextopt("HSabtfdscmlrpnv")) != '\0') 369 switch (optc) { 370 case 'H': 371 how |= HARD; 372 break; 373 case 'S': 374 how |= SOFT; 375 break; 376 case 'a': 377 all = 1; 378 break; 379 default: 380 what = optc; 381 } 382 383 for (l = limits; l->name && l->option != what; l++) 384 ; 385 if (!l->name) 386 error("internal error (%c)", what); 387 388 set = *argptr ? 1 : 0; 389 if (set) { 390 char *p = *argptr; 391 392 if (all || argptr[1]) 393 error("too many arguments"); 394 if (how == 0) 395 how = HARD | SOFT; 396 397 if (strcmp(p, "unlimited") == 0) 398 val = RLIM_INFINITY; 399 else { 400 val = (rlim_t) 0; 401 402 while ((c = *p++) >= '0' && c <= '9') { 403 if (val >= RLIM_INFINITY/10) 404 error("value overflow"); 405 val = (val * 10); 406 if (val >= RLIM_INFINITY - (long)(c - '0')) 407 error("value overflow"); 408 val += (long)(c - '0'); 409 } 410 if (c) 411 error("bad number"); 412 if (val > RLIM_INFINITY / l->factor) 413 error("value overflow"); 414 val *= l->factor; 415 } 416 } else if (how == 0) 417 how = SOFT; 418 419 if (all) { 420 for (l = limits; l->name; l++) { 421 getrlimit(l->cmd, &limit); 422 out1fmt("%-13s (-%c %-11s) ", l->name, l->option, 423 l->unit); 424 425 which = how; 426 while (which != 0) { 427 if (which & SOFT) { 428 val = limit.rlim_cur; 429 which &= ~SOFT; 430 } else if (which & HARD) { 431 val = limit.rlim_max; 432 which &= ~HARD; 433 } 434 435 if (val == RLIM_INFINITY) 436 out1fmt("unlimited"); 437 else { 438 val /= l->factor; 439 #ifdef BSD4_4 440 out1fmt("%9lld", (long long) val); 441 #else 442 out1fmt("%9ld", (long) val); 443 #endif 444 } 445 out1fmt("%c", which ? '\t' : '\n'); 446 } 447 } 448 return 0; 449 } 450 451 if (getrlimit(l->cmd, &limit) == -1) 452 error("error getting limit (%s)", strerror(errno)); 453 if (set) { 454 if (how & HARD) 455 limit.rlim_max = val; 456 if (how & SOFT) 457 limit.rlim_cur = val; 458 if (setrlimit(l->cmd, &limit) < 0) 459 error("error setting limit (%s)", strerror(errno)); 460 if (l->cmd == RLIMIT_NOFILE) 461 user_fd_limit = sysconf(_SC_OPEN_MAX); 462 } else { 463 if (how & SOFT) 464 val = limit.rlim_cur; 465 else if (how & HARD) 466 val = limit.rlim_max; 467 468 if (val == RLIM_INFINITY) 469 out1fmt("unlimited\n"); 470 else 471 { 472 val /= l->factor; 473 #ifdef BSD4_4 474 out1fmt("%lld\n", (long long) val); 475 #else 476 out1fmt("%ld\n", (long) val); 477 #endif 478 } 479 } 480 return 0; 481 } 482