Home | History | Annotate | Line # | Download | only in sh
miscbltin.c revision 1.46
      1 /*	$NetBSD: miscbltin.c,v 1.46 2021/11/16 11:27:50 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.46 2021/11/16 11:27:50 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 	flushout(out1);
    292 	if (io_err(out1)) {
    293 		out2str("umask: I/O error\n");
    294 		return 1;
    295 	}
    296 	return 0;
    297 }
    298 
    299 /*
    300  * ulimit builtin
    301  *
    302  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
    303  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
    304  * ash by J.T. Conklin.
    305  *
    306  * Public domain.
    307  */
    308 
    309 struct limits {
    310 	const char *name;
    311 	const char *unit;
    312 	int	cmd;
    313 	int	factor;	/* multiply by to get rlim_{cur,max} values */
    314 	char	option;
    315 };
    316 
    317 static const struct limits limits[] = {
    318 #ifdef RLIMIT_CPU
    319 	{ "time",	"seconds",	RLIMIT_CPU,	   1, 't' },
    320 #endif
    321 #ifdef RLIMIT_FSIZE
    322 	{ "file",	"blocks",	RLIMIT_FSIZE,	 512, 'f' },
    323 #endif
    324 #ifdef RLIMIT_DATA
    325 	{ "data",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
    326 #endif
    327 #ifdef RLIMIT_STACK
    328 	{ "stack",	"kbytes",	RLIMIT_STACK,	1024, 's' },
    329 #endif
    330 #ifdef RLIMIT_CORE
    331 	{ "coredump",	"blocks",	RLIMIT_CORE,	 512, 'c' },
    332 #endif
    333 #ifdef RLIMIT_RSS
    334 	{ "memory",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
    335 #endif
    336 #ifdef RLIMIT_MEMLOCK
    337 	{ "locked memory","kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
    338 #endif
    339 #ifdef RLIMIT_NTHR
    340 	{ "thread",	"threads",	RLIMIT_NTHR,       1, 'r' },
    341 #endif
    342 #ifdef RLIMIT_NPROC
    343 	{ "process",	"processes",	RLIMIT_NPROC,      1, 'p' },
    344 #endif
    345 #ifdef RLIMIT_NOFILE
    346 	{ "nofiles",	"descriptors",	RLIMIT_NOFILE,     1, 'n' },
    347 #endif
    348 #ifdef RLIMIT_VMEM
    349 	{ "vmemory",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
    350 #endif
    351 #ifdef RLIMIT_SWAP
    352 	{ "swap",	"kbytes",	RLIMIT_SWAP,	1024, 'w' },
    353 #endif
    354 #ifdef RLIMIT_SBSIZE
    355 	{ "sbsize",	"bytes",	RLIMIT_SBSIZE,	   1, 'b' },
    356 #endif
    357 	{ NULL,		NULL,		0,		   0,  '\0' }
    358 };
    359 
    360 int
    361 ulimitcmd(int argc, char **argv)
    362 {
    363 	int	c;
    364 	rlim_t val = 0;
    365 	enum { SOFT = 0x1, HARD = 0x2 }
    366 			how = 0, which;
    367 	const struct limits	*l;
    368 	int		set, all = 0;
    369 	int		optc, what;
    370 	struct rlimit	limit;
    371 
    372 	what = 'f';
    373 	while ((optc = nextopt("HSabtfdscmlrpnv")) != '\0')
    374 		switch (optc) {
    375 		case 'H':
    376 			how |= HARD;
    377 			break;
    378 		case 'S':
    379 			how |= SOFT;
    380 			break;
    381 		case 'a':
    382 			all = 1;
    383 			break;
    384 		default:
    385 			what = optc;
    386 		}
    387 
    388 	for (l = limits; l->name && l->option != what; l++)
    389 		;
    390 	if (!l->name)
    391 		error("internal error (%c)", what);
    392 
    393 	set = *argptr ? 1 : 0;
    394 	if (set) {
    395 		char *p = *argptr;
    396 
    397 		if (all || argptr[1])
    398 			error("too many arguments");
    399 		if (how == 0)
    400 			how = HARD | SOFT;
    401 
    402 		if (strcmp(p, "unlimited") == 0)
    403 			val = RLIM_INFINITY;
    404 		else {
    405 			val = (rlim_t) 0;
    406 
    407 			while ((c = *p++) >= '0' && c <= '9') {
    408 				if (val >= RLIM_INFINITY/10)
    409 					error("value overflow");
    410 				val = (val * 10);
    411 				if (val >= RLIM_INFINITY - (long)(c - '0'))
    412 					error("value overflow");
    413 				val += (long)(c - '0');
    414 			}
    415 			if (c)
    416 				error("bad number");
    417 			if (val > RLIM_INFINITY / l->factor)
    418 				error("value overflow");
    419 			val *= l->factor;
    420 		}
    421 	} else if (how == 0)
    422 		how = SOFT;
    423 
    424 	if (all) {
    425 		for (l = limits; l->name; l++) {
    426 			getrlimit(l->cmd, &limit);
    427 			out1fmt("%-13s (-%c %-11s)    ", l->name, l->option,
    428 			    l->unit);
    429 
    430 			which = how;
    431 			while (which != 0) {
    432 				if (which & SOFT) {
    433 					val = limit.rlim_cur;
    434 					which &= ~SOFT;
    435 				} else if (which & HARD) {
    436 					val = limit.rlim_max;
    437 					which &= ~HARD;
    438 				}
    439 
    440 				if (val == RLIM_INFINITY)
    441 					out1fmt("unlimited");
    442 				else {
    443 					val /= l->factor;
    444 #ifdef BSD4_4
    445 					out1fmt("%9lld", (long long) val);
    446 #else
    447 					out1fmt("%9ld", (long) val);
    448 #endif
    449 				}
    450 				out1fmt("%c", which ? '\t' : '\n');
    451 			}
    452 		}
    453 		goto done;
    454 	}
    455 
    456 	if (getrlimit(l->cmd, &limit) == -1)
    457 		error("error getting limit (%s)", strerror(errno));
    458 	if (set) {
    459 		if (how & HARD)
    460 			limit.rlim_max = val;
    461 		if (how & SOFT)
    462 			limit.rlim_cur = val;
    463 		if (setrlimit(l->cmd, &limit) < 0)
    464 			error("error setting limit (%s)", strerror(errno));
    465 		if (l->cmd == RLIMIT_NOFILE)
    466 			user_fd_limit = sysconf(_SC_OPEN_MAX);
    467 	} else {
    468 		if (how & SOFT)
    469 			val = limit.rlim_cur;
    470 		else if (how & HARD)
    471 			val = limit.rlim_max;
    472 
    473 		if (val == RLIM_INFINITY)
    474 			out1fmt("unlimited\n");
    475 		else
    476 		{
    477 			val /= l->factor;
    478 #ifdef BSD4_4
    479 			out1fmt("%lld\n", (long long) val);
    480 #else
    481 			out1fmt("%ld\n", (long) val);
    482 #endif
    483 		}
    484 	}
    485   done:;
    486 	flushout(out1);
    487 	if (io_err(out1)) {
    488 		out2str("ulimit: I/O error (stdout)\n");
    489 		return 1;
    490 	}
    491 	return 0;
    492 }
    493