miscbltin.c revision 189542
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 35static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 36#endif 37#endif /* not lint */ 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/bin/sh/miscbltin.c 189542 2009-03-08 19:09:55Z ed $"); 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 67int readcmd(int, char **); 68int umaskcmd(int, char **); 69int 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 78int 79readcmd(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 tnew.c_iflag |= told.c_iflag & ICRNL; 151 tcsetattr(0, TCSANOW, &tnew); 152 tsaved = 1; 153 } 154 /* 155 * Wait for something to become available. 156 */ 157 FD_ZERO(&ifds); 158 FD_SET(0, &ifds); 159 status = select(1, &ifds, NULL, NULL, &tv); 160 if (tsaved) 161 tcsetattr(0, TCSANOW, &told); 162 /* 163 * If there's nothing ready, return an error. 164 */ 165 if (status <= 0) 166 return(1); 167 } 168 169 status = 0; 170 startword = 1; 171 backslash = 0; 172 STARTSTACKSTR(p); 173 for (;;) { 174 if (read(STDIN_FILENO, &c, 1) != 1) { 175 status = 1; 176 break; 177 } 178 if (c == '\0') 179 continue; 180 if (backslash) { 181 backslash = 0; 182 if (c != '\n') 183 STPUTC(c, p); 184 continue; 185 } 186 if (!rflag && c == '\\') { 187 backslash++; 188 continue; 189 } 190 if (c == '\n') 191 break; 192 if (startword && *ifs == ' ' && strchr(ifs, c)) { 193 continue; 194 } 195 startword = 0; 196 if (ap[1] != NULL && strchr(ifs, c) != NULL) { 197 STACKSTRNUL(p); 198 setvar(*ap, stackblock(), 0); 199 ap++; 200 startword = 1; 201 STARTSTACKSTR(p); 202 } else { 203 STPUTC(c, p); 204 } 205 } 206 STACKSTRNUL(p); 207 setvar(*ap, stackblock(), 0); 208 while (*++ap != NULL) 209 setvar(*ap, nullstr, 0); 210 return status; 211} 212 213 214 215int 216umaskcmd(int argc __unused, char **argv) 217{ 218 char *ap; 219 int mask; 220 int i; 221 int symbolic_mode = 0; 222 223 while ((i = nextopt("S")) != '\0') { 224 symbolic_mode = 1; 225 } 226 227 INTOFF; 228 mask = umask(0); 229 umask(mask); 230 INTON; 231 232 if ((ap = *argptr) == NULL) { 233 if (symbolic_mode) { 234 char u[4], g[4], o[4]; 235 236 i = 0; 237 if ((mask & S_IRUSR) == 0) 238 u[i++] = 'r'; 239 if ((mask & S_IWUSR) == 0) 240 u[i++] = 'w'; 241 if ((mask & S_IXUSR) == 0) 242 u[i++] = 'x'; 243 u[i] = '\0'; 244 245 i = 0; 246 if ((mask & S_IRGRP) == 0) 247 g[i++] = 'r'; 248 if ((mask & S_IWGRP) == 0) 249 g[i++] = 'w'; 250 if ((mask & S_IXGRP) == 0) 251 g[i++] = 'x'; 252 g[i] = '\0'; 253 254 i = 0; 255 if ((mask & S_IROTH) == 0) 256 o[i++] = 'r'; 257 if ((mask & S_IWOTH) == 0) 258 o[i++] = 'w'; 259 if ((mask & S_IXOTH) == 0) 260 o[i++] = 'x'; 261 o[i] = '\0'; 262 263 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 264 } else { 265 out1fmt("%.4o\n", mask); 266 } 267 } else { 268 if (isdigit(*ap)) { 269 mask = 0; 270 do { 271 if (*ap >= '8' || *ap < '0') 272 error("Illegal number: %s", *argptr); 273 mask = (mask << 3) + (*ap - '0'); 274 } while (*++ap != '\0'); 275 umask(mask); 276 } else { 277 void *set; 278 INTOFF; 279 if ((set = setmode (ap)) == 0) 280 error("Illegal number: %s", ap); 281 282 mask = getmode (set, ~mask & 0777); 283 umask(~mask & 0777); 284 free(set); 285 INTON; 286 } 287 } 288 return 0; 289} 290 291/* 292 * ulimit builtin 293 * 294 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 295 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 296 * ash by J.T. Conklin. 297 * 298 * Public domain. 299 */ 300 301struct limits { 302 const char *name; 303 const char *units; 304 int cmd; 305 int factor; /* multiply by to get rlim_{cur,max} values */ 306 char option; 307}; 308 309static const struct limits limits[] = { 310#ifdef RLIMIT_CPU 311 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 312#endif 313#ifdef RLIMIT_FSIZE 314 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 315#endif 316#ifdef RLIMIT_DATA 317 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 318#endif 319#ifdef RLIMIT_STACK 320 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 321#endif 322#ifdef RLIMIT_CORE 323 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 324#endif 325#ifdef RLIMIT_RSS 326 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 327#endif 328#ifdef RLIMIT_MEMLOCK 329 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 330#endif 331#ifdef RLIMIT_NPROC 332 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 333#endif 334#ifdef RLIMIT_NOFILE 335 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 336#endif 337#ifdef RLIMIT_VMEM 338 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 339#endif 340#ifdef RLIMIT_SWAP 341 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 342#endif 343#ifdef RLIMIT_SBSIZE 344 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' }, 345#endif 346#ifdef RLIMIT_NPTS 347 { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' }, 348#endif 349 { (char *) 0, (char *)0, 0, 0, '\0' } 350}; 351 352int 353ulimitcmd(int argc __unused, char **argv __unused) 354{ 355 int c; 356 rlim_t val = 0; 357 enum { SOFT = 0x1, HARD = 0x2 } 358 how = SOFT | HARD; 359 const struct limits *l; 360 int set, all = 0; 361 int optc, what; 362 struct rlimit limit; 363 364 what = 'f'; 365 while ((optc = nextopt("HSatfdsmcnuvlbp")) != '\0') 366 switch (optc) { 367 case 'H': 368 how = HARD; 369 break; 370 case 'S': 371 how = SOFT; 372 break; 373 case 'a': 374 all = 1; 375 break; 376 default: 377 what = optc; 378 } 379 380 for (l = limits; l->name && l->option != what; l++) 381 ; 382 if (!l->name) 383 error("internal error (%c)", what); 384 385 set = *argptr ? 1 : 0; 386 if (set) { 387 char *p = *argptr; 388 389 if (all || argptr[1]) 390 error("too many arguments"); 391 if (strcmp(p, "unlimited") == 0) 392 val = RLIM_INFINITY; 393 else { 394 val = 0; 395 396 while ((c = *p++) >= '0' && c <= '9') 397 { 398 val = (val * 10) + (long)(c - '0'); 399 if (val < 0) 400 break; 401 } 402 if (c) 403 error("bad number"); 404 val *= l->factor; 405 } 406 } 407 if (all) { 408 for (l = limits; l->name; l++) { 409 char optbuf[40]; 410 if (getrlimit(l->cmd, &limit) < 0) 411 error("can't get limit: %s", strerror(errno)); 412 if (how & SOFT) 413 val = limit.rlim_cur; 414 else if (how & HARD) 415 val = limit.rlim_max; 416 417 if (l->units) 418 snprintf(optbuf, sizeof(optbuf), 419 "(%s, -%c) ", l->units, l->option); 420 else 421 snprintf(optbuf, sizeof(optbuf), 422 "(-%c) ", l->option); 423 out1fmt("%-18s %18s ", l->name, optbuf); 424 if (val == RLIM_INFINITY) 425 out1fmt("unlimited\n"); 426 else 427 { 428 val /= l->factor; 429 out1fmt("%jd\n", (intmax_t)val); 430 } 431 } 432 return 0; 433 } 434 435 if (getrlimit(l->cmd, &limit) < 0) 436 error("can't get limit: %s", strerror(errno)); 437 if (set) { 438 if (how & SOFT) 439 limit.rlim_cur = val; 440 if (how & HARD) 441 limit.rlim_max = val; 442 if (setrlimit(l->cmd, &limit) < 0) 443 error("bad limit: %s", strerror(errno)); 444 } else { 445 if (how & SOFT) 446 val = limit.rlim_cur; 447 else if (how & HARD) 448 val = limit.rlim_max; 449 450 if (val == RLIM_INFINITY) 451 out1fmt("unlimited\n"); 452 else 453 { 454 val /= l->factor; 455 out1fmt("%jd\n", (intmax_t)val); 456 } 457 } 458 return 0; 459} 460