miscbltin.c revision 1.28 1 1.28 christos /* $NetBSD: miscbltin.c,v 1.28 2000/11/22 19:20:31 christos Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.7 jtc * Copyright (c) 1991, 1993
5 1.7 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.19 christos #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.13 cgd #if 0
42 1.14 christos static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
43 1.13 cgd #else
44 1.28 christos __RCSID("$NetBSD: miscbltin.c,v 1.28 2000/11/22 19:20:31 christos Exp $");
45 1.13 cgd #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * Miscelaneous builtins.
50 1.1 cgd */
51 1.1 cgd
52 1.23 christos #include <sys/types.h> /* quad_t */
53 1.23 christos #include <sys/param.h> /* BSD4_4 */
54 1.8 jtc #include <sys/stat.h>
55 1.14 christos #include <sys/time.h>
56 1.14 christos #include <sys/resource.h>
57 1.9 jtc #include <unistd.h>
58 1.27 christos #include <stdlib.h>
59 1.9 jtc #include <ctype.h>
60 1.28 christos #include <errno.h>
61 1.14 christos
62 1.1 cgd #include "shell.h"
63 1.1 cgd #include "options.h"
64 1.1 cgd #include "var.h"
65 1.1 cgd #include "output.h"
66 1.1 cgd #include "memalloc.h"
67 1.1 cgd #include "error.h"
68 1.19 christos #include "miscbltin.h"
69 1.1 cgd #include "mystring.h"
70 1.1 cgd
71 1.20 kleink #undef rflag
72 1.1 cgd
73 1.1 cgd extern char **argptr; /* argument list for builtin command */
74 1.1 cgd
75 1.1 cgd
76 1.1 cgd /*
77 1.1 cgd * The read builtin. The -e option causes backslashes to escape the
78 1.1 cgd * following character.
79 1.1 cgd *
80 1.1 cgd * This uses unbuffered input, which may be avoidable in some cases.
81 1.1 cgd */
82 1.1 cgd
83 1.12 cgd int
84 1.12 cgd readcmd(argc, argv)
85 1.12 cgd int argc;
86 1.16 christos char **argv;
87 1.12 cgd {
88 1.1 cgd char **ap;
89 1.1 cgd int backslash;
90 1.1 cgd char c;
91 1.20 kleink int rflag;
92 1.1 cgd char *prompt;
93 1.1 cgd char *ifs;
94 1.1 cgd char *p;
95 1.1 cgd int startword;
96 1.1 cgd int status;
97 1.1 cgd int i;
98 1.1 cgd
99 1.20 kleink rflag = 0;
100 1.1 cgd prompt = NULL;
101 1.20 kleink while ((i = nextopt("p:r")) != '\0') {
102 1.1 cgd if (i == 'p')
103 1.1 cgd prompt = optarg;
104 1.1 cgd else
105 1.20 kleink rflag = 1;
106 1.1 cgd }
107 1.1 cgd if (prompt && isatty(0)) {
108 1.1 cgd out2str(prompt);
109 1.1 cgd flushall();
110 1.1 cgd }
111 1.6 cgd if (*(ap = argptr) == NULL)
112 1.1 cgd error("arg count");
113 1.1 cgd if ((ifs = bltinlookup("IFS", 1)) == NULL)
114 1.1 cgd ifs = nullstr;
115 1.1 cgd status = 0;
116 1.1 cgd startword = 1;
117 1.1 cgd backslash = 0;
118 1.1 cgd STARTSTACKSTR(p);
119 1.1 cgd for (;;) {
120 1.1 cgd if (read(0, &c, 1) != 1) {
121 1.1 cgd status = 1;
122 1.1 cgd break;
123 1.1 cgd }
124 1.1 cgd if (c == '\0')
125 1.1 cgd continue;
126 1.1 cgd if (backslash) {
127 1.1 cgd backslash = 0;
128 1.1 cgd if (c != '\n')
129 1.1 cgd STPUTC(c, p);
130 1.1 cgd continue;
131 1.1 cgd }
132 1.20 kleink if (!rflag && c == '\\') {
133 1.1 cgd backslash++;
134 1.1 cgd continue;
135 1.1 cgd }
136 1.1 cgd if (c == '\n')
137 1.1 cgd break;
138 1.1 cgd if (startword && *ifs == ' ' && strchr(ifs, c)) {
139 1.1 cgd continue;
140 1.1 cgd }
141 1.1 cgd startword = 0;
142 1.1 cgd if (backslash && c == '\\') {
143 1.1 cgd if (read(0, &c, 1) != 1) {
144 1.1 cgd status = 1;
145 1.1 cgd break;
146 1.1 cgd }
147 1.1 cgd STPUTC(c, p);
148 1.1 cgd } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
149 1.1 cgd STACKSTRNUL(p);
150 1.1 cgd setvar(*ap, stackblock(), 0);
151 1.1 cgd ap++;
152 1.1 cgd startword = 1;
153 1.1 cgd STARTSTACKSTR(p);
154 1.1 cgd } else {
155 1.1 cgd STPUTC(c, p);
156 1.1 cgd }
157 1.1 cgd }
158 1.1 cgd STACKSTRNUL(p);
159 1.21 christos /* Remove trailing blanks */
160 1.22 christos while (stackblock() <= --p && strchr(ifs, *p) != NULL)
161 1.21 christos *p = '\0';
162 1.1 cgd setvar(*ap, stackblock(), 0);
163 1.1 cgd while (*++ap != NULL)
164 1.1 cgd setvar(*ap, nullstr, 0);
165 1.1 cgd return status;
166 1.1 cgd }
167 1.1 cgd
168 1.1 cgd
169 1.1 cgd
170 1.12 cgd int
171 1.12 cgd umaskcmd(argc, argv)
172 1.12 cgd int argc;
173 1.16 christos char **argv;
174 1.12 cgd {
175 1.8 jtc char *ap;
176 1.1 cgd int mask;
177 1.1 cgd int i;
178 1.8 jtc int symbolic_mode = 0;
179 1.8 jtc
180 1.8 jtc while ((i = nextopt("S")) != '\0') {
181 1.8 jtc symbolic_mode = 1;
182 1.8 jtc }
183 1.1 cgd
184 1.8 jtc INTOFF;
185 1.8 jtc mask = umask(0);
186 1.8 jtc umask(mask);
187 1.8 jtc INTON;
188 1.8 jtc
189 1.8 jtc if ((ap = *argptr) == NULL) {
190 1.8 jtc if (symbolic_mode) {
191 1.8 jtc char u[4], g[4], o[4];
192 1.8 jtc
193 1.8 jtc i = 0;
194 1.8 jtc if ((mask & S_IRUSR) == 0)
195 1.8 jtc u[i++] = 'r';
196 1.8 jtc if ((mask & S_IWUSR) == 0)
197 1.8 jtc u[i++] = 'w';
198 1.8 jtc if ((mask & S_IXUSR) == 0)
199 1.8 jtc u[i++] = 'x';
200 1.8 jtc u[i] = '\0';
201 1.8 jtc
202 1.8 jtc i = 0;
203 1.8 jtc if ((mask & S_IRGRP) == 0)
204 1.8 jtc g[i++] = 'r';
205 1.8 jtc if ((mask & S_IWGRP) == 0)
206 1.8 jtc g[i++] = 'w';
207 1.8 jtc if ((mask & S_IXGRP) == 0)
208 1.8 jtc g[i++] = 'x';
209 1.8 jtc g[i] = '\0';
210 1.8 jtc
211 1.8 jtc i = 0;
212 1.8 jtc if ((mask & S_IROTH) == 0)
213 1.8 jtc o[i++] = 'r';
214 1.8 jtc if ((mask & S_IWOTH) == 0)
215 1.8 jtc o[i++] = 'w';
216 1.8 jtc if ((mask & S_IXOTH) == 0)
217 1.8 jtc o[i++] = 'x';
218 1.8 jtc o[i] = '\0';
219 1.8 jtc
220 1.8 jtc out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
221 1.8 jtc } else {
222 1.8 jtc out1fmt("%.4o\n", mask);
223 1.8 jtc }
224 1.1 cgd } else {
225 1.25 christos if (isdigit((unsigned char)*ap)) {
226 1.8 jtc mask = 0;
227 1.8 jtc do {
228 1.8 jtc if (*ap >= '8' || *ap < '0')
229 1.8 jtc error("Illegal number: %s", argv[1]);
230 1.8 jtc mask = (mask << 3) + (*ap - '0');
231 1.8 jtc } while (*++ap != '\0');
232 1.8 jtc umask(mask);
233 1.8 jtc } else {
234 1.16 christos void *set;
235 1.8 jtc
236 1.26 itohy INTOFF;
237 1.26 itohy if ((set = setmode(ap)) != 0) {
238 1.26 itohy mask = getmode(set, ~mask & 0777);
239 1.26 itohy ckfree(set);
240 1.26 itohy }
241 1.26 itohy INTON;
242 1.26 itohy if (!set)
243 1.26 itohy error("Illegal mode: %s", ap);
244 1.26 itohy
245 1.8 jtc umask(~mask & 0777);
246 1.14 christos }
247 1.14 christos }
248 1.14 christos return 0;
249 1.14 christos }
250 1.14 christos
251 1.14 christos /*
252 1.14 christos * ulimit builtin
253 1.14 christos *
254 1.14 christos * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
255 1.14 christos * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
256 1.14 christos * ash by J.T. Conklin.
257 1.14 christos *
258 1.14 christos * Public domain.
259 1.14 christos */
260 1.14 christos
261 1.14 christos struct limits {
262 1.14 christos const char *name;
263 1.14 christos int cmd;
264 1.14 christos int factor; /* multiply by to get rlim_{cur,max} values */
265 1.14 christos char option;
266 1.14 christos };
267 1.14 christos
268 1.14 christos static const struct limits limits[] = {
269 1.14 christos #ifdef RLIMIT_CPU
270 1.14 christos { "time(seconds)", RLIMIT_CPU, 1, 't' },
271 1.14 christos #endif
272 1.14 christos #ifdef RLIMIT_FSIZE
273 1.14 christos { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
274 1.14 christos #endif
275 1.14 christos #ifdef RLIMIT_DATA
276 1.14 christos { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
277 1.14 christos #endif
278 1.14 christos #ifdef RLIMIT_STACK
279 1.14 christos { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
280 1.14 christos #endif
281 1.14 christos #ifdef RLIMIT_CORE
282 1.14 christos { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
283 1.14 christos #endif
284 1.14 christos #ifdef RLIMIT_RSS
285 1.14 christos { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
286 1.14 christos #endif
287 1.14 christos #ifdef RLIMIT_MEMLOCK
288 1.14 christos { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
289 1.14 christos #endif
290 1.14 christos #ifdef RLIMIT_NPROC
291 1.14 christos { "process(processes)", RLIMIT_NPROC, 1, 'p' },
292 1.14 christos #endif
293 1.14 christos #ifdef RLIMIT_NOFILE
294 1.14 christos { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
295 1.14 christos #endif
296 1.14 christos #ifdef RLIMIT_VMEM
297 1.14 christos { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' },
298 1.14 christos #endif
299 1.14 christos #ifdef RLIMIT_SWAP
300 1.14 christos { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' },
301 1.14 christos #endif
302 1.14 christos { (char *) 0, 0, 0, '\0' }
303 1.14 christos };
304 1.14 christos
305 1.14 christos int
306 1.14 christos ulimitcmd(argc, argv)
307 1.14 christos int argc;
308 1.14 christos char **argv;
309 1.14 christos {
310 1.17 tls int c;
311 1.19 christos rlim_t val = 0;
312 1.14 christos enum { SOFT = 0x1, HARD = 0x2 }
313 1.14 christos how = SOFT | HARD;
314 1.14 christos const struct limits *l;
315 1.14 christos int set, all = 0;
316 1.14 christos int optc, what;
317 1.14 christos struct rlimit limit;
318 1.14 christos
319 1.14 christos what = 'f';
320 1.14 christos while ((optc = nextopt("HSatfdsmcnpl")) != '\0')
321 1.14 christos switch (optc) {
322 1.14 christos case 'H':
323 1.14 christos how = HARD;
324 1.14 christos break;
325 1.14 christos case 'S':
326 1.14 christos how = SOFT;
327 1.14 christos break;
328 1.14 christos case 'a':
329 1.14 christos all = 1;
330 1.14 christos break;
331 1.14 christos default:
332 1.14 christos what = optc;
333 1.14 christos }
334 1.14 christos
335 1.14 christos for (l = limits; l->name && l->option != what; l++)
336 1.14 christos ;
337 1.14 christos if (!l->name)
338 1.28 christos error("internal error (%c)", what);
339 1.14 christos
340 1.14 christos set = *argptr ? 1 : 0;
341 1.14 christos if (set) {
342 1.14 christos char *p = *argptr;
343 1.14 christos
344 1.14 christos if (all || argptr[1])
345 1.28 christos error("too many arguments");
346 1.14 christos if (strcmp(p, "unlimited") == 0)
347 1.14 christos val = RLIM_INFINITY;
348 1.14 christos else {
349 1.15 jtc val = (rlim_t) 0;
350 1.14 christos
351 1.14 christos while ((c = *p++) >= '0' && c <= '9')
352 1.14 christos {
353 1.14 christos val = (val * 10) + (long)(c - '0');
354 1.15 jtc if (val < (rlim_t) 0)
355 1.14 christos break;
356 1.14 christos }
357 1.14 christos if (c)
358 1.28 christos error("bad number");
359 1.14 christos val *= l->factor;
360 1.14 christos }
361 1.14 christos }
362 1.14 christos if (all) {
363 1.14 christos for (l = limits; l->name; l++) {
364 1.14 christos getrlimit(l->cmd, &limit);
365 1.14 christos if (how & SOFT)
366 1.14 christos val = limit.rlim_cur;
367 1.14 christos else if (how & HARD)
368 1.14 christos val = limit.rlim_max;
369 1.14 christos
370 1.14 christos out1fmt("%-20s ", l->name);
371 1.14 christos if (val == RLIM_INFINITY)
372 1.14 christos out1fmt("unlimited\n");
373 1.14 christos else
374 1.14 christos {
375 1.14 christos val /= l->factor;
376 1.18 christos #ifdef BSD4_4
377 1.24 thorpej out1fmt("%qd\n", (long long) val);
378 1.18 christos #else
379 1.18 christos out1fmt("%ld\n", (long) val);
380 1.18 christos #endif
381 1.14 christos }
382 1.14 christos }
383 1.14 christos return 0;
384 1.14 christos }
385 1.14 christos
386 1.14 christos getrlimit(l->cmd, &limit);
387 1.14 christos if (set) {
388 1.28 christos if (how & HARD)
389 1.28 christos limit.rlim_max = val;
390 1.14 christos if (how & SOFT)
391 1.14 christos limit.rlim_cur = val;
392 1.14 christos if (setrlimit(l->cmd, &limit) < 0)
393 1.28 christos error("error setting limit (%s)", strerror(errno));
394 1.14 christos } else {
395 1.14 christos if (how & SOFT)
396 1.14 christos val = limit.rlim_cur;
397 1.14 christos else if (how & HARD)
398 1.14 christos val = limit.rlim_max;
399 1.14 christos
400 1.14 christos if (val == RLIM_INFINITY)
401 1.14 christos out1fmt("unlimited\n");
402 1.14 christos else
403 1.14 christos {
404 1.14 christos val /= l->factor;
405 1.18 christos #ifdef BSD4_4
406 1.24 thorpej out1fmt("%qd\n", (long long) val);
407 1.18 christos #else
408 1.18 christos out1fmt("%ld\n", (long) val);
409 1.18 christos #endif
410 1.8 jtc }
411 1.1 cgd }
412 1.1 cgd return 0;
413 1.1 cgd }
414