1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 /* 42 * Miscellaneous builtins. 43 */ 44 45 #include <sys/types.h> 46 #include <sys/stat.h> 47 #include <sys/time.h> 48 #include <sys/resource.h> 49 #include <unistd.h> 50 #include <ctype.h> 51 #include <errno.h> 52 #include <stdint.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <termios.h> 56 57 #include "shell.h" 58 #include "options.h" 59 #include "var.h" 60 #include "output.h" 61 #include "memalloc.h" 62 #include "error.h" 63 #include "mystring.h" 64 65 #undef eflag 66 67 int readcmd(int, char **); 68 int umaskcmd(int, char **); 69 int ulimitcmd(int, char **); 70 71 /* 72 * The read builtin. The -r option causes backslashes to be treated like 73 * ordinary characters. 74 * 75 * This uses unbuffered input, which may be avoidable in some cases. 76 */ 77 78 int 79 readcmd(int argc __unused, char **argv __unused) 80 { 81 char **ap; 82 int backslash; 83 char c; 84 int rflag; 85 char *prompt; 86 char *ifs; 87 char *p; 88 int startword; 89 int status; 90 int i; 91 struct timeval tv; 92 char *tvptr; 93 fd_set ifds; 94 struct termios told, tnew; 95 int tsaved; 96 97 rflag = 0; 98 prompt = NULL; 99 tv.tv_sec = -1; 100 tv.tv_usec = 0; 101 while ((i = nextopt("erp:t:")) != '\0') { 102 switch(i) { 103 case 'p': 104 prompt = shoptarg; 105 break; 106 case 'e': 107 break; 108 case 'r': 109 rflag = 1; 110 break; 111 case 't': 112 tv.tv_sec = strtol(shoptarg, &tvptr, 0); 113 if (tvptr == shoptarg) 114 error("timeout value"); 115 switch(*tvptr) { 116 case 0: 117 case 's': 118 break; 119 case 'h': 120 tv.tv_sec *= 60; 121 /* FALLTHROUGH */ 122 case 'm': 123 tv.tv_sec *= 60; 124 break; 125 default: 126 error("timeout unit"); 127 } 128 break; 129 } 130 } 131 if (prompt && isatty(0)) { 132 out2str(prompt); 133 flushall(); 134 } 135 if (*(ap = argptr) == NULL) 136 error("arg count"); 137 if ((ifs = bltinlookup("IFS", 1)) == NULL) 138 ifs = nullstr; 139 140 if (tv.tv_sec >= 0) { 141 /* 142 * See if we can disable input processing; this will 143 * not give the desired result if we are in a pipeline 144 * and someone upstream is still in line-by-line mode. 145 */ 146 tsaved = 0; 147 if (tcgetattr(0, &told) == 0) { 148 memcpy(&tnew, &told, sizeof(told)); 149 cfmakeraw(&tnew); 150 tcsetattr(0, TCSANOW, &tnew); 151 tsaved = 1; 152 } 153 /* 154 * Wait for something to become available. 155 */ 156 FD_ZERO(&ifds); 157 FD_SET(0, &ifds); 158 status = select(1, &ifds, NULL, NULL, &tv); 159 if (tsaved) 160 tcsetattr(0, TCSANOW, &told); 161 /* 162 * If there's nothing ready, return an error. 163 */ 164 if (status <= 0) 165 return(1); 166 } 167 168 status = 0; 169 startword = 1; 170 backslash = 0; 171 STARTSTACKSTR(p); 172 for (;;) { 173 if (read(STDIN_FILENO, &c, 1) != 1) { 174 status = 1; 175 break; 176 } 177 if (c == '\0') 178 continue; 179 if (backslash) { 180 backslash = 0; 181 if (c != '\n') 182 STPUTC(c, p); 183 continue; 184 } 185 if (!rflag && c == '\\') { 186 backslash++; 187 continue; 188 } 189 if (c == '\n') 190 break; 191 if (startword && *ifs == ' ' && strchr(ifs, c)) { 192 continue; 193 } 194 startword = 0; 195 if (ap[1] != NULL && strchr(ifs, c) != NULL) { 196 STACKSTRNUL(p); 197 setvar(*ap, stackblock(), 0); 198 ap++; 199 startword = 1; 200 STARTSTACKSTR(p); 201 } else { 202 STPUTC(c, p); 203 } 204 } 205 STACKSTRNUL(p); 206 setvar(*ap, stackblock(), 0); 207 while (*++ap != NULL) 208 setvar(*ap, nullstr, 0); 209 return status; 210 } 211 212 213 214 int 215 umaskcmd(int argc __unused, 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(*ap)) { 268 mask = 0; 269 do { 270 if (*ap >= '8' || *ap < '0') 271 error("Illegal number: %s", *argptr); 272 mask = (mask << 3) + (*ap - '0'); 273 } while (*++ap != '\0'); 274 umask(mask); 275 } else { 276 void *set; 277 INTOFF; 278 if ((set = setmode (ap)) == 0) 279 error("Illegal number: %s", ap); 280 281 mask = getmode (set, ~mask & 0777); 282 umask(~mask & 0777); 283 free(set); 284 INTON; 285 } 286 } 287 return 0; 288 } 289 290 /* 291 * ulimit builtin 292 * 293 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 294 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 295 * ash by J.T. Conklin. 296 * 297 * Public domain. 298 */ 299 300 struct limits { 301 const char *name; 302 const char *units; 303 int cmd; 304 int factor; /* multiply by to get rlim_{cur,max} values */ 305 char option; 306 }; 307 308 static const struct limits limits[] = { 309 #ifdef RLIMIT_CPU 310 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 311 #endif 312 #ifdef RLIMIT_FSIZE 313 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 314 #endif 315 #ifdef RLIMIT_DATA 316 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 317 #endif 318 #ifdef RLIMIT_STACK 319 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 320 #endif 321 #ifdef RLIMIT_CORE 322 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 323 #endif 324 #ifdef RLIMIT_RSS 325 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 326 #endif 327 #ifdef RLIMIT_MEMLOCK 328 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 329 #endif 330 #ifdef RLIMIT_NPROC 331 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 332 #endif 333 #ifdef RLIMIT_NOFILE 334 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 335 #endif 336 #ifdef RLIMIT_VMEM 337 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 338 #endif 339 #ifdef RLIMIT_SWAP 340 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 341 #endif 342 #ifdef RLIMIT_SBSIZE 343 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' }, 344 #endif 345 #ifdef RLIMIT_NPTS 346 { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' }, 347 #endif 348 { (char *) 0, (char *)0, 0, 0, '\0' } 349 }; 350 351 int 352 ulimitcmd(int argc __unused, char **argv __unused) 353 { 354 int c; 355 rlim_t val = 0; 356 enum { SOFT = 0x1, HARD = 0x2 } 357 how = SOFT | HARD; 358 const struct limits *l; 359 int set, all = 0; 360 int optc, what; 361 struct rlimit limit; 362 363 what = 'f'; 364 while ((optc = nextopt("HSatfdsmcnuvlbp")) != '\0') 365 switch (optc) { 366 case 'H': 367 how = HARD; 368 break; 369 case 'S': 370 how = SOFT; 371 break; 372 case 'a': 373 all = 1; 374 break; 375 default: 376 what = optc; 377 } 378 379 for (l = limits; l->name && l->option != what; l++) 380 ; 381 if (!l->name) 382 error("internal error (%c)", what); 383 384 set = *argptr ? 1 : 0; 385 if (set) { 386 char *p = *argptr; 387 388 if (all || argptr[1]) 389 error("too many arguments"); 390 if (strcmp(p, "unlimited") == 0) 391 val = RLIM_INFINITY; 392 else { 393 val = 0; 394 395 while ((c = *p++) >= '0' && c <= '9') 396 { 397 val = (val * 10) + (long)(c - '0'); 398 if (val < 0) 399 break; 400 } 401 if (c) 402 error("bad number"); 403 val *= l->factor; 404 } 405 } 406 if (all) { 407 for (l = limits; l->name; l++) { 408 char optbuf[40]; 409 if (getrlimit(l->cmd, &limit) < 0) 410 error("can't get limit: %s", strerror(errno)); 411 if (how & SOFT) 412 val = limit.rlim_cur; 413 else if (how & HARD) 414 val = limit.rlim_max; 415 416 if (l->units) 417 snprintf(optbuf, sizeof(optbuf), 418 "(%s, -%c) ", l->units, l->option); 419 else 420 snprintf(optbuf, sizeof(optbuf), 421 "(-%c) ", l->option); 422 out1fmt("%-18s %18s ", l->name, optbuf); 423 if (val == RLIM_INFINITY) 424 out1fmt("unlimited\n"); 425 else 426 { 427 val /= l->factor; 428 out1fmt("%jd\n", (intmax_t)val); 429 } 430 } 431 return 0; 432 } 433 434 if (getrlimit(l->cmd, &limit) < 0) 435 error("can't get limit: %s", strerror(errno)); 436 if (set) { 437 if (how & SOFT) 438 limit.rlim_cur = val; 439 if (how & HARD) 440 limit.rlim_max = val; 441 if (setrlimit(l->cmd, &limit) < 0) 442 error("bad limit: %s", strerror(errno)); 443 } else { 444 if (how & SOFT) 445 val = limit.rlim_cur; 446 else if (how & HARD) 447 val = limit.rlim_max; 448 449 if (val == RLIM_INFINITY) 450 out1fmt("unlimited\n"); 451 else 452 { 453 val /= l->factor; 454 out1fmt("%jd\n", (intmax_t)val); 455 } 456 } 457 return 0; 458 } 459