| 1 | /* $NetBSD: miscbltin.c,v 1.35 2005/03/19 14:22:50 dsl 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 | #ifdef HAVE_SYS_CDEFS_H
|
|---|
| 36 | #include <sys/cdefs.h>
|
|---|
| 37 | #endif
|
|---|
| 38 | #ifndef lint
|
|---|
| 39 | #if 0
|
|---|
| 40 | static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
|
|---|
| 41 | #else
|
|---|
| 42 | __RCSID("$NetBSD: miscbltin.c,v 1.35 2005/03/19 14:22:50 dsl Exp $");
|
|---|
| 43 | #endif
|
|---|
| 44 | #endif /* not lint */
|
|---|
| 45 |
|
|---|
| 46 | /*
|
|---|
| 47 | * Miscelaneous builtins.
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | #include <sys/types.h> /* quad_t */
|
|---|
| 51 | #include <sys/param.h> /* BSD4_4 */
|
|---|
| 52 | #include <sys/stat.h>
|
|---|
| 53 | #include <sys/time.h>
|
|---|
| 54 | #include <sys/resource.h>
|
|---|
| 55 | #include <unistd.h>
|
|---|
| 56 | #include <stdlib.h>
|
|---|
| 57 | #include <ctype.h>
|
|---|
| 58 | #include <errno.h>
|
|---|
| 59 |
|
|---|
| 60 | #include "shell.h"
|
|---|
| 61 | #include "options.h"
|
|---|
| 62 | #include "var.h"
|
|---|
| 63 | #include "output.h"
|
|---|
| 64 | #include "memalloc.h"
|
|---|
| 65 | #include "error.h"
|
|---|
| 66 | #include "miscbltin.h"
|
|---|
| 67 | #include "mystring.h"
|
|---|
| 68 |
|
|---|
| 69 | #undef rflag
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | * The read builtin.
|
|---|
| 75 | * Backslahes escape the next char unless -r is specified.
|
|---|
| 76 | *
|
|---|
| 77 | * This uses unbuffered input, which may be avoidable in some cases.
|
|---|
| 78 | *
|
|---|
| 79 | * Note that if IFS=' :' then read x y should work so that:
|
|---|
| 80 | * 'a b' x='a', y='b'
|
|---|
| 81 | * ' a b ' x='a', y='b'
|
|---|
| 82 | * ':b' x='', y='b'
|
|---|
| 83 | * ':' x='', y=''
|
|---|
| 84 | * '::' x='', y=''
|
|---|
| 85 | * ': :' x='', y=''
|
|---|
| 86 | * ':::' x='', y='::'
|
|---|
| 87 | * ':b c:' x='', y='b c:'
|
|---|
| 88 | */
|
|---|
| 89 |
|
|---|
| 90 | int
|
|---|
| 91 | readcmd(int argc, char **argv)
|
|---|
| 92 | {
|
|---|
| 93 | char **ap;
|
|---|
| 94 | char c;
|
|---|
| 95 | int rflag;
|
|---|
| 96 | char *prompt;
|
|---|
| 97 | const char *ifs;
|
|---|
| 98 | char *p;
|
|---|
| 99 | int startword;
|
|---|
| 100 | int status;
|
|---|
| 101 | int i;
|
|---|
| 102 | int is_ifs;
|
|---|
| 103 | int saveall = 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 (prompt && isatty(0)) {
|
|---|
| 115 | out2str(prompt);
|
|---|
| 116 | output_flushall();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | if (*(ap = argptr) == NULL)
|
|---|
| 120 | error("arg count");
|
|---|
| 121 |
|
|---|
| 122 | if ((ifs = bltinlookup("IFS", 1)) == NULL)
|
|---|
| 123 | ifs = " \t\n";
|
|---|
| 124 |
|
|---|
| 125 | status = 0;
|
|---|
| 126 | startword = 2;
|
|---|
| 127 | STARTSTACKSTR(p);
|
|---|
| 128 | for (;;) {
|
|---|
| 129 | if (read(0, &c, 1) != 1) {
|
|---|
| 130 | status = 1;
|
|---|
| 131 | break;
|
|---|
| 132 | }
|
|---|
| 133 | if (c == '\0')
|
|---|
| 134 | continue;
|
|---|
| 135 | if (c == '\\' && !rflag) {
|
|---|
| 136 | if (read(0, &c, 1) != 1) {
|
|---|
| 137 | status = 1;
|
|---|
| 138 | break;
|
|---|
| 139 | }
|
|---|
| 140 | if (c != '\n')
|
|---|
| 141 | STPUTC(c, p);
|
|---|
| 142 | continue;
|
|---|
| 143 | }
|
|---|
| 144 | if (c == '\n')
|
|---|
| 145 | break;
|
|---|
| 146 | if (strchr(ifs, c))
|
|---|
| 147 | is_ifs = strchr(" \t\n", c) ? 1 : 2;
|
|---|
| 148 | else
|
|---|
| 149 | is_ifs = 0;
|
|---|
| 150 |
|
|---|
| 151 | if (startword != 0) {
|
|---|
| 152 | if (is_ifs == 1) {
|
|---|
| 153 | /* Ignore leading IFS whitespace */
|
|---|
| 154 | if (saveall)
|
|---|
| 155 | STPUTC(c, p);
|
|---|
| 156 | continue;
|
|---|
| 157 | }
|
|---|
| 158 | if (is_ifs == 2 && startword == 1) {
|
|---|
| 159 | /* Only one non-whitespace IFS per word */
|
|---|
| 160 | startword = 2;
|
|---|
| 161 | if (saveall)
|
|---|
| 162 | STPUTC(c, p);
|
|---|
| 163 | continue;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | if (is_ifs == 0) {
|
|---|
| 168 | /* append this character to the current variable */
|
|---|
| 169 | startword = 0;
|
|---|
| 170 | if (saveall)
|
|---|
| 171 | /* Not just a spare terminator */
|
|---|
| 172 | saveall++;
|
|---|
| 173 | STPUTC(c, p);
|
|---|
| 174 | continue;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /* end of variable... */
|
|---|
| 178 | startword = is_ifs;
|
|---|
| 179 |
|
|---|
| 180 | if (ap[1] == NULL) {
|
|---|
| 181 | /* Last variable needs all IFS chars */
|
|---|
| 182 | saveall++;
|
|---|
| 183 | STPUTC(c, p);
|
|---|
| 184 | continue;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | STACKSTRNUL(p);
|
|---|
| 188 | setvar(*ap, stackblock(), 0);
|
|---|
| 189 | ap++;
|
|---|
| 190 | STARTSTACKSTR(p);
|
|---|
| 191 | }
|
|---|
| 192 | STACKSTRNUL(p);
|
|---|
| 193 |
|
|---|
| 194 | /* Remove trailing IFS chars */
|
|---|
| 195 | for (; stackblock() <= --p; *p = 0) {
|
|---|
| 196 | if (!strchr(ifs, *p))
|
|---|
| 197 | break;
|
|---|
| 198 | if (strchr(" \t\n", *p))
|
|---|
| 199 | /* Always remove whitespace */
|
|---|
| 200 | continue;
|
|---|
| 201 | if (saveall > 1)
|
|---|
| 202 | /* Don't remove non-whitespace unless it was naked */
|
|---|
| 203 | break;
|
|---|
| 204 | }
|
|---|
| 205 | setvar(*ap, stackblock(), 0);
|
|---|
| 206 |
|
|---|
| 207 | /* Set any remaining args to "" */
|
|---|
| 208 | while (*++ap != NULL)
|
|---|
| 209 | setvar(*ap, nullstr, 0);
|
|---|
| 210 | return status;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | int
|
|---|
| 216 | umaskcmd(int argc, 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((unsigned char)*ap)) {
|
|---|
| 269 | mask = 0;
|
|---|
| 270 | do {
|
|---|
| 271 | if (*ap >= '8' || *ap < '0')
|
|---|
| 272 | error("Illegal number: %s", argv[1]);
|
|---|
| 273 | mask = (mask << 3) + (*ap - '0');
|
|---|
| 274 | } while (*++ap != '\0');
|
|---|
| 275 | umask(mask);
|
|---|
| 276 | } else {
|
|---|
| 277 | void *set;
|
|---|
| 278 |
|
|---|
| 279 | INTOFF;
|
|---|
| 280 | #ifdef __INNOTEK_LIBC__
|
|---|
| 281 | if ((set = bsd_setmode(ap)) != 0) {
|
|---|
| 282 | #else
|
|---|
| 283 | if ((set = setmode(ap)) != 0) {
|
|---|
| 284 | #endif
|
|---|
| 285 | mask = getmode(set, ~mask & 0777);
|
|---|
| 286 | ckfree(set);
|
|---|
| 287 | }
|
|---|
| 288 | INTON;
|
|---|
| 289 | if (!set)
|
|---|
| 290 | error("Illegal mode: %s", ap);
|
|---|
| 291 |
|
|---|
| 292 | umask(~mask & 0777);
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 | return 0;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /*
|
|---|
| 299 | * ulimit builtin
|
|---|
| 300 | *
|
|---|
| 301 | * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
|
|---|
| 302 | * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
|
|---|
| 303 | * ash by J.T. Conklin.
|
|---|
| 304 | *
|
|---|
| 305 | * Public domain.
|
|---|
| 306 | */
|
|---|
| 307 |
|
|---|
| 308 | struct limits {
|
|---|
| 309 | const char *name;
|
|---|
| 310 | int cmd;
|
|---|
| 311 | int factor; /* multiply by to get rlim_{cur,max} values */
|
|---|
| 312 | char option;
|
|---|
| 313 | };
|
|---|
| 314 |
|
|---|
| 315 | static const struct limits limits[] = {
|
|---|
| 316 | #ifdef RLIMIT_CPU
|
|---|
| 317 | { "time(seconds)", RLIMIT_CPU, 1, 't' },
|
|---|
| 318 | #endif
|
|---|
| 319 | #ifdef RLIMIT_FSIZE
|
|---|
| 320 | { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
|
|---|
| 321 | #endif
|
|---|
| 322 | #ifdef RLIMIT_DATA
|
|---|
| 323 | { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
|
|---|
| 324 | #endif
|
|---|
| 325 | #ifdef RLIMIT_STACK
|
|---|
| 326 | { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
|
|---|
| 327 | #endif
|
|---|
| 328 | #ifdef RLIMIT_CORE
|
|---|
| 329 | { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
|
|---|
| 330 | #endif
|
|---|
| 331 | #ifdef RLIMIT_RSS
|
|---|
| 332 | { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
|
|---|
| 333 | #endif
|
|---|
| 334 | #ifdef RLIMIT_MEMLOCK
|
|---|
| 335 | { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
|
|---|
| 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 | { (char *) 0, 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 = SOFT | HARD;
|
|---|
| 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("HSabtfdsmcnpl")) != '\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 (strcmp(p, "unlimited") == 0)
|
|---|
| 395 | val = RLIM_INFINITY;
|
|---|
| 396 | else {
|
|---|
| 397 | val = (rlim_t) 0;
|
|---|
| 398 |
|
|---|
| 399 | while ((c = *p++) >= '0' && c <= '9')
|
|---|
| 400 | {
|
|---|
| 401 | val = (val * 10) + (long)(c - '0');
|
|---|
| 402 | if (val < (rlim_t) 0)
|
|---|
| 403 | break;
|
|---|
| 404 | }
|
|---|
| 405 | if (c)
|
|---|
| 406 | error("bad number");
|
|---|
| 407 | val *= l->factor;
|
|---|
| 408 | }
|
|---|
| 409 | }
|
|---|
| 410 | if (all) {
|
|---|
| 411 | for (l = limits; l->name; l++) {
|
|---|
| 412 | getrlimit(l->cmd, &limit);
|
|---|
| 413 | if (how & SOFT)
|
|---|
| 414 | val = limit.rlim_cur;
|
|---|
| 415 | else if (how & HARD)
|
|---|
| 416 | val = limit.rlim_max;
|
|---|
| 417 |
|
|---|
| 418 | out1fmt("%-20s ", l->name);
|
|---|
| 419 | if (val == RLIM_INFINITY)
|
|---|
| 420 | out1fmt("unlimited\n");
|
|---|
| 421 | else
|
|---|
| 422 | {
|
|---|
| 423 | val /= l->factor;
|
|---|
| 424 | #ifdef BSD4_4
|
|---|
| 425 | out1fmt("%lld\n", (long long) val);
|
|---|
| 426 | #else
|
|---|
| 427 | out1fmt("%ld\n", (long) val);
|
|---|
| 428 | #endif
|
|---|
| 429 | }
|
|---|
| 430 | }
|
|---|
| 431 | return 0;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | getrlimit(l->cmd, &limit);
|
|---|
| 435 | if (set) {
|
|---|
| 436 | if (how & HARD)
|
|---|
| 437 | limit.rlim_max = val;
|
|---|
| 438 | if (how & SOFT)
|
|---|
| 439 | limit.rlim_cur = val;
|
|---|
| 440 | if (setrlimit(l->cmd, &limit) < 0)
|
|---|
| 441 | error("error setting limit (%s)", strerror(errno));
|
|---|
| 442 | } else {
|
|---|
| 443 | if (how & SOFT)
|
|---|
| 444 | val = limit.rlim_cur;
|
|---|
| 445 | else if (how & HARD)
|
|---|
| 446 | val = limit.rlim_max;
|
|---|
| 447 |
|
|---|
| 448 | if (val == RLIM_INFINITY)
|
|---|
| 449 | out1fmt("unlimited\n");
|
|---|
| 450 | else
|
|---|
| 451 | {
|
|---|
| 452 | val /= l->factor;
|
|---|
| 453 | #ifdef BSD4_4
|
|---|
| 454 | out1fmt("%lld\n", (long long) val);
|
|---|
| 455 | #else
|
|---|
| 456 | out1fmt("%ld\n", (long) val);
|
|---|
| 457 | #endif
|
|---|
| 458 | }
|
|---|
| 459 | }
|
|---|
| 460 | return 0;
|
|---|
| 461 | }
|
|---|