Deleted Added
full compact
limits.c (200420) limits.c (200462)
1/*-
2 * Copyright (c) 1997 by
3 * David L. Nugent <davidn@blaze.net.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. This work was done expressly for inclusion into FreeBSD. Other use
16 * is permitted provided this notation is included.
17 * 4. Absolutely no warranty of function or purpose is made by the authors.
18 * 5. Modifications may be freely made to this file providing the above
19 * conditions are met.
20 *
21 * Display/change(+runprogram)/eval resource limits.
22 */
23
24#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1997 by
3 * David L. Nugent <davidn@blaze.net.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. This work was done expressly for inclusion into FreeBSD. Other use
16 * is permitted provided this notation is included.
17 * 4. Absolutely no warranty of function or purpose is made by the authors.
18 * 5. Modifications may be freely made to this file providing the above
19 * conditions are met.
20 *
21 * Display/change(+runprogram)/eval resource limits.
22 */
23
24#include <sys/cdefs.h>
25__FBSDID("$FreeBSD: head/usr.bin/limits/limits.c 200420 2009-12-11 23:35:38Z delphij $");
25__FBSDID("$FreeBSD: head/usr.bin/limits/limits.c 200462 2009-12-13 03:14:06Z delphij $");
26
27#include <err.h>
28#include <stdio.h>
29#include <string.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/param.h>
33#include <stdlib.h>
34#include <unistd.h>
26
27#include <err.h>
28#include <stdio.h>
29#include <string.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/param.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <stdarg.h>
35#include <stdint.h>
36#include <ctype.h>
37#include <errno.h>
38#include <pwd.h>
39#include <login_cap.h>
40#include <sys/time.h>
41#include <sys/resource.h>
42
43enum
44{
45 SH_NONE,
46 SH_SH, /* sh */
47 SH_CSH, /* csh */
48 SH_BASH, /* gnu bash */
49 SH_TCSH, /* tcsh */
50 SH_KSH, /* (pd)ksh */
51 SH_ZSH, /* zsh */
52 SH_RC, /* rc or es */
53 SH_NUMBER
54};
55
56
57/* eval emitter for popular shells.
58 * Why aren't there any standards here? Most shells support either
59 * the csh 'limit' or sh 'ulimit' command, but each varies just
60 * enough that they aren't very compatible from one to the other.
61 */
62static struct {
63 const char * name; /* Name of shell */
64 const char * inf; /* Name used for 'unlimited' resource */
65 const char * cmd; /* Intro text */
66 const char * hard; /* Hard limit text */
67 const char * soft; /* Soft limit text */
68 const char * both; /* Hard+Soft limit text */
69 struct {
70 const char * pfx;
71 const char * sfx;
72 int divisor;
73 } lprm[RLIM_NLIMITS];
74} shellparm[] =
75{
76 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
77 {
78 { " cputime%-4s %8s", " secs\n", 1 },
79 { " filesize%-4s %8s", " kB\n", 1024 },
80 { " datasize%-4s %8s", " kB\n", 1024 },
81 { " stacksize%-4s %8s", " kB\n", 1024 },
82 { " coredumpsize%-4s %8s", " kB\n", 1024 },
83 { " memoryuse%-4s %8s", " kB\n", 1024 },
84 { " memorylocked%-4s %8s", " kB\n", 1024 },
85 { " maxprocesses%-4s %8s", "\n", 1 },
86 { " openfiles%-4s %8s", "\n", 1 },
87 { " sbsize%-4s %8s", " bytes\n", 1 },
88 { " vmemoryuse%-4s %8s", " kB\n", 1024 },
89 { " pseudo-terminals%-4s %8s", "\n", 1 },
90 { " swapuse%-4s %8s", " kB\n", 1024 }
91 }
92 },
93 { "sh", "unlimited", "", " -H", " -S", "",
94 {
95 { "ulimit%s -t %s", ";\n", 1 },
96 { "ulimit%s -f %s", ";\n", 512 },
97 { "ulimit%s -d %s", ";\n", 1024 },
98 { "ulimit%s -s %s", ";\n", 1024 },
99 { "ulimit%s -c %s", ";\n", 512 },
100 { "ulimit%s -m %s", ";\n", 1024 },
101 { "ulimit%s -l %s", ";\n", 1024 },
102 { "ulimit%s -u %s", ";\n", 1 },
103 { "ulimit%s -n %s", ";\n", 1 },
104 { "ulimit%s -b %s", ";\n", 1 },
105 { "ulimit%s -v %s", ";\n", 1024 },
106 { "ulimit%s -p %s", ";\n", 1 },
107 { "ulimit%s -w %s", ";\n", 1024 }
108 }
109 },
110 { "csh", "unlimited", "", " -h", "", NULL,
111 {
112 { "limit%s cputime %s", ";\n", 1 },
113 { "limit%s filesize %s", ";\n", 1024 },
114 { "limit%s datasize %s", ";\n", 1024 },
115 { "limit%s stacksize %s", ";\n", 1024 },
116 { "limit%s coredumpsize %s", ";\n", 1024 },
117 { "limit%s memoryuse %s", ";\n", 1024 },
118 { "limit%s memorylocked %s", ";\n", 1024 },
119 { "limit%s maxproc %s", ";\n", 1 },
120 { "limit%s openfiles %s", ";\n", 1 },
121 { "limit%s sbsize %s", ";\n", 1 },
122 { "limit%s vmemoryuse %s", ";\n", 1024 },
123 { "limit%s pseudoterminals %s", ";\n", 1 },
124 { "limit%s swapuse %s", ";\n", 1024 }
125 }
126 },
127 { "bash|bash2", "unlimited", "", " -H", " -S", "",
128 {
129 { "ulimit%s -t %s", ";\n", 1 },
130 { "ulimit%s -f %s", ";\n", 1024 },
131 { "ulimit%s -d %s", ";\n", 1024 },
132 { "ulimit%s -s %s", ";\n", 1024 },
133 { "ulimit%s -c %s", ";\n", 1024 },
134 { "ulimit%s -m %s", ";\n", 1024 },
135 { "ulimit%s -l %s", ";\n", 1024 },
136 { "ulimit%s -u %s", ";\n", 1 },
137 { "ulimit%s -n %s", ";\n", 1 },
138 { "ulimit%s -b %s", ";\n", 1 },
139 { "ulimit%s -v %s", ";\n", 1024 },
140 { "ulimit%s -p %s", ";\n", 1 },
141 { "ulimit%s -w %s", ";\n", 1024 }
142 }
143 },
144 { "tcsh", "unlimited", "", " -h", "", NULL,
145 {
146 { "limit%s cputime %s", ";\n", 1 },
147 { "limit%s filesize %s", ";\n", 1024 },
148 { "limit%s datasize %s", ";\n", 1024 },
149 { "limit%s stacksize %s", ";\n", 1024 },
150 { "limit%s coredumpsize %s", ";\n", 1024 },
151 { "limit%s memoryuse %s", ";\n", 1024 },
152 { "limit%s memorylocked %s", ";\n", 1024 },
153 { "limit%s maxproc %s", ";\n", 1 },
154 { "limit%s descriptors %s", ";\n", 1 },
155 { "limit%s sbsize %s", ";\n", 1 },
156 { "limit%s vmemoryuse %s", ";\n", 1024 },
157 { "limit%s pseudoterminals %s", ";\n", 1 },
158 { "limit%s swapuse %s", ";\n", 1024 }
159 }
160 },
161 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
162 {
163 { "ulimit%s -t %s", ";\n", 1 },
164 { "ulimit%s -f %s", ";\n", 512 },
165 { "ulimit%s -d %s", ";\n", 1024 },
166 { "ulimit%s -s %s", ";\n", 1024 },
167 { "ulimit%s -c %s", ";\n", 512 },
168 { "ulimit%s -m %s", ";\n", 1024 },
169 { "ulimit%s -l %s", ";\n", 1024 },
170 { "ulimit%s -p %s", ";\n", 1 },
171 { "ulimit%s -n %s", ";\n", 1 },
172 { "ulimit%s -b %s", ";\n", 1 },
173 { "ulimit%s -v %s", ";\n", 1024 },
174 { "ulimit%s -p %s", ";\n", 1 },
175 { "ulimit%s -w %s", ";\n", 1024 }
176 }
177 },
178 { "zsh", "unlimited", "", " -H", " -S", "",
179 {
180 { "ulimit%s -t %s", ";\n", 1 },
181 { "ulimit%s -f %s", ";\n", 512 },
182 { "ulimit%s -d %s", ";\n", 1024 },
183 { "ulimit%s -s %s", ";\n", 1024 },
184 { "ulimit%s -c %s", ";\n", 512 },
185 { "ulimit%s -m %s", ";\n", 1024 },
186 { "ulimit%s -l %s", ";\n", 1024 },
187 { "ulimit%s -u %s", ";\n", 1 },
188 { "ulimit%s -n %s", ";\n", 1 },
189 { "ulimit%s -b %s", ";\n", 1 },
190 { "ulimit%s -v %s", ";\n", 1024 },
191 { "ulimit%s -p %s", ";\n", 1 },
192 { "ulimit%s -w %s", ";\n", 1024 }
193 }
194 },
195 { "rc|es", "unlimited", "", " -h", "", NULL,
196 {
197 { "limit%s cputime %s", ";\n", 1 },
198 { "limit%s filesize %s", ";\n", 1024 },
199 { "limit%s datasize %s", ";\n", 1024 },
200 { "limit%s stacksize %s", ";\n", 1024 },
201 { "limit%s coredumpsize %s", ";\n", 1024 },
202 { "limit%s memoryuse %s", ";\n", 1024 },
203 { "limit%s lockedmemory %s", ";\n", 1024 },
204 { "limit%s processes %s", ";\n", 1 },
205 { "limit%s descriptors %s", ";\n", 1 },
206 { "limit%s sbsize %s", ";\n", 1 },
207 { "limit%s vmemoryuse %s", ";\n", 1024 },
208 { "limit%s pseudoterminals %s", ";\n", 1 },
209 { "limit%s swapuse %s", ";\n", 1024 }
210 }
211 },
212 { NULL, NULL, NULL, NULL, NULL, NULL,
213 { }
214 }
215};
216
217static struct {
218 const char * cap;
219 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
220} resources[RLIM_NLIMITS] = {
221 { "cputime", login_getcaptime },
222 { "filesize", login_getcapsize },
223 { "datasize", login_getcapsize },
224 { "stacksize", login_getcapsize },
225 { "coredumpsize", login_getcapsize },
226 { "memoryuse", login_getcapsize },
227 { "memorylocked", login_getcapsize },
228 { "maxproc", login_getcapnum },
229 { "openfiles", login_getcapnum },
230 { "sbsize", login_getcapsize },
231 { "vmemoryuse", login_getcapsize },
232 { "pseudoterminals",login_getcapnum },
233 { "swapuse", login_getcapsize }
234};
235
236/*
237 * One letter for each resource levels.
238 * NOTE: There is a dependancy on the corresponding
239 * letter index being equal to the resource number.
240 * If sys/resource.h defines are changed, this needs
241 * to be modified accordingly!
242 */
243
244#define RCS_STRING "tfdscmlunbvpw"
245
246static rlim_t resource_num(int which, int ch, const char *str);
247static void usage(void);
248static int getshelltype(void);
249static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
250 const char *pfx, const char *sfx, const char *which);
251extern char **environ;
252
253static const char rcs_string[] = RCS_STRING;
254
255int
256main(int argc, char *argv[])
257{
258 char *p, *cls = NULL;
259 char *cleanenv[1];
260 struct passwd * pwd = NULL;
261 int rcswhich, shelltype;
262 int i, num_limits = 0;
263 int ch, doeval = 0, doall = 0;
264 int rtrn;
265 login_cap_t * lc = NULL;
266 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
267 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
268 int which_limits[RLIM_NLIMITS];
269 rlim_t set_limits[RLIM_NLIMITS];
270 struct rlimit limits[RLIM_NLIMITS];
271
272 /* init resource tables */
273 for (i = 0; i < RLIM_NLIMITS; i++) {
274 which_limits[i] = 0; /* Don't set/display any */
275 set_limits[i] = RLIM_INFINITY;
276 /* Get current resource values */
277 getrlimit(i, &limits[i]);
278 }
279
280 optarg = NULL;
281 while ((ch = getopt(argc, argv, ":EeC:U:BSHab:c:d:f:l:m:n:s:t:u:v:p:w:")) != -1) {
282 switch(ch) {
283 case 'a':
284 doall = 1;
285 break;
286 case 'E':
287 environ = cleanenv;
288 cleanenv[0] = NULL;
289 break;
290 case 'e':
291 doeval = 1;
292 break;
293 case 'C':
294 cls = optarg;
295 break;
296 case 'U':
297 if ((pwd = getpwnam(optarg)) == NULL) {
298 if (!isdigit(*optarg) ||
299 (pwd = getpwuid(atoi(optarg))) == NULL) {
300 warnx("invalid user `%s'", optarg);
301 usage();
302 }
303 }
304 break;
305 case 'H':
306 type = HARD;
307 break;
308 case 'S':
309 type = SOFT;
310 break;
311 case 'B':
312 type = SOFT|HARD;
313 break;
314 default:
315 case ':': /* Without arg */
316 if ((p = strchr(rcs_string, optopt)) != NULL) {
317 int rcswhich1 = p - rcs_string;
318 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
319 --optind; /* back one arg, and make arg NULL */
320 optarg = NULL;
321 }
322 todo = optarg == NULL ? RCSSEL : RCSSET;
323 if (type == ANY)
324 type = BOTH;
325 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
326 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
327 num_limits++;
328 break;
329 }
330 /* FALLTHRU */
331 case '?':
332 usage();
333 }
334 optarg = NULL;
335 }
336
337 /* If user was specified, get class from that */
338 if (pwd != NULL)
339 lc = login_getpwclass(pwd);
340 else if (cls != NULL && *cls != '\0') {
341 lc = login_getclassbyname(cls, NULL);
342 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
343 fprintf(stderr, "login class '%s' non-existent, using %s\n",
344 cls, lc?lc->lc_class:"current settings");
345 }
346
347 /* If we have a login class, update resource table from that */
348 if (lc != NULL) {
349 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
350 char str[40];
351 rlim_t val;
352
353 /* current value overridden by resourcename or resourcename-cur */
354 sprintf(str, "%s-cur", resources[rcswhich].cap);
355 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
356 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
357 /* maximum value overridden by resourcename or resourcename-max */
358 sprintf(str, "%s-max", resources[rcswhich].cap);
359 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
360 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
361 }
362 }
363
364 /* now, let's determine what we wish to do with all this */
365
366 argv += optind;
367
368 /* If we're setting limits or doing an eval (ie. we're not just
369 * displaying), then check that hard limits are not lower than
370 * soft limits, and force rasing the hard limit if we need to if
371 * we are raising the soft limit, or lower the soft limit if we
372 * are lowering the hard limit.
373 */
374 if ((*argv || doeval) && getuid() == 0) {
375
376 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
377 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
378 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
379 limits[rcswhich].rlim_max = RLIM_INFINITY;
380 which_limits[rcswhich] |= HARD;
381 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
382 if (which_limits[rcswhich] == SOFT) {
383 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
384 which_limits[rcswhich] |= HARD;
385 } else if (which_limits[rcswhich] == HARD) {
386 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
387 which_limits[rcswhich] |= SOFT;
388 } else {
389 /* else.. if we're specifically setting both to
390 * silly values, then let it error out.
391 */
392 }
393 }
394 }
395 }
396 }
397
398 /* See if we've overridden anything specific on the command line */
399 if (num_limits && todo == RCSSET) {
400 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
401 if (which_limits[rcswhich] & HARD)
402 limits[rcswhich].rlim_max = set_limits[rcswhich];
403 if (which_limits[rcswhich] & SOFT)
404 limits[rcswhich].rlim_cur = set_limits[rcswhich];
405 }
406 }
407
408 /* If *argv is not NULL, then we are being asked to
409 * (perhaps) set environment variables and run a program
410 */
411 if (*argv) {
412 if (doeval) {
413 warnx("-e cannot be used with `cmd' option");
414 usage();
415 }
416
417 login_close(lc);
418
419 /* set leading environment variables, like eval(1) */
420 while (*argv && (p = strchr(*argv, '='))) {
421 *p = '\0';
422 rtrn = setenv(*argv++, p + 1, 1);
423 *p = '=';
424 if (rtrn == -1)
425 err(EXIT_FAILURE, "setenv %s", *argv);
426 }
427
428 /* Set limits */
429 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
430 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
431 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
432 err(1, "setrlimit %s", resources[rcswhich].cap);
433 }
434
435 if (*argv == NULL)
436 usage();
437
438 execvp(*argv, argv);
439 err(1, "%s", *argv);
440 }
441
442 shelltype = doeval ? getshelltype() : SH_NONE;
443
444 if (type == ANY) /* Default to soft limits */
445 type = SOFT;
446
447 /* Display limits */
448 printf(shellparm[shelltype].cmd,
449 lc ? " for class " : " (current)",
450 lc ? lc->lc_class : "");
451
452 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
453 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
454 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
455 which_limits[rcswhich] = type;
456 if (shellparm[shelltype].lprm[rcswhich].pfx) {
457 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
458 print_limit(limits[rcswhich].rlim_max,
459 shellparm[shelltype].lprm[rcswhich].divisor,
460 shellparm[shelltype].inf,
461 shellparm[shelltype].lprm[rcswhich].pfx,
462 shellparm[shelltype].lprm[rcswhich].sfx,
463 shellparm[shelltype].both);
464 } else {
465 if (which_limits[rcswhich] & HARD) {
466 print_limit(limits[rcswhich].rlim_max,
467 shellparm[shelltype].lprm[rcswhich].divisor,
468 shellparm[shelltype].inf,
469 shellparm[shelltype].lprm[rcswhich].pfx,
470 shellparm[shelltype].lprm[rcswhich].sfx,
471 shellparm[shelltype].hard);
472 }
473 if (which_limits[rcswhich] & SOFT) {
474 print_limit(limits[rcswhich].rlim_cur,
475 shellparm[shelltype].lprm[rcswhich].divisor,
476 shellparm[shelltype].inf,
477 shellparm[shelltype].lprm[rcswhich].pfx,
478 shellparm[shelltype].lprm[rcswhich].sfx,
479 shellparm[shelltype].soft);
480 }
481 }
482 }
483 }
484 }
485
486 login_close(lc);
487 exit(EXIT_SUCCESS);
488}
489
490
491static void
492usage(void)
493{
494 (void)fprintf(stderr,
495"usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuvpw [val]] [[name=val ...] cmd]\n");
496 exit(EXIT_FAILURE);
497}
498
499static void
500print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
501{
502 char numbr[64];
503
504 if (limit == RLIM_INFINITY)
505 strcpy(numbr, inf);
506 else
507 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
508 printf(pfx, which, numbr);
509 printf(sfx, which);
510
511}
512
513
514static rlim_t
515resource_num(int which, int ch, const char *str)
516{
517 rlim_t res = RLIM_INFINITY;
518
519 if (str != NULL &&
520 !(strcasecmp(str, "inf") == 0 ||
521 strcasecmp(str, "infinity") == 0 ||
522 strcasecmp(str, "unlimit") == 0 ||
523 strcasecmp(str, "unlimited") == 0)) {
524 const char * s = str;
525 char *e;
526
527 switch (which) {
528 case RLIMIT_CPU: /* time values */
529 errno = 0;
530 res = 0;
531 while (*s) {
532 rlim_t tim = strtoq(s, &e, 0);
533 if (e == NULL || e == s || errno)
534 break;
535 switch (*e++) {
536 case 0: /* end of string */
537 e--;
538 default:
539 case 's': case 'S': /* seconds */
540 break;
541 case 'm': case 'M': /* minutes */
542 tim *= 60L;
543 break;
544 case 'h': case 'H': /* hours */
545 tim *= (60L * 60L);
546 break;
547 case 'd': case 'D': /* days */
548 tim *= (60L * 60L * 24L);
549 break;
550 case 'w': case 'W': /* weeks */
551 tim *= (60L * 60L * 24L * 7L);
552 case 'y': case 'Y': /* Years */
553 tim *= (60L * 60L * 24L * 365L);
554 }
555 s = e;
556 res += tim;
557 }
558 break;
559 case RLIMIT_FSIZE: /* Size values */
560 case RLIMIT_DATA:
561 case RLIMIT_STACK:
562 case RLIMIT_CORE:
563 case RLIMIT_RSS:
564 case RLIMIT_MEMLOCK:
565 case RLIMIT_SBSIZE:
566 case RLIMIT_VMEM:
567 case RLIMIT_SWAP:
568 errno = 0;
569 res = 0;
570 while (*s) {
571 rlim_t mult, tim = strtoq(s, &e, 0);
572 if (e == NULL || e == s || errno)
573 break;
574 switch (*e++) {
575 case 0: /* end of string */
576 e--;
577 default:
578 mult = 1;
579 break;
580 case 'b': case 'B': /* 512-byte blocks */
581 mult = 512;
582 break;
583 case 'k': case 'K': /* 1024-byte Kilobytes */
584 mult = 1024;
585 break;
586 case 'm': case 'M': /* 1024-k kbytes */
587 mult = 1024 * 1024;
588 break;
589 case 'g': case 'G': /* 1Gbyte */
590 mult = 1024 * 1024 * 1024;
591 break;
592 case 't': case 'T': /* 1TBte */
593 mult = 1024LL * 1024LL * 1024LL * 1024LL;
594 break;
595 }
596 s = e;
597 res += (tim * mult);
598 }
599 break;
600 case RLIMIT_NPROC:
601 case RLIMIT_NOFILE:
602 case RLIMIT_NPTS:
603 res = strtoq(s, &e, 0);
604 s = e;
605 break;
606 }
607 if (*s) {
608 warnx("invalid value -%c `%s'", ch, str);
609 usage();
610 }
611 }
612 return res;
613}
614
615
616static int
617getshellbyname(const char * shell)
618{
619 int i;
620 const char * q;
621 const char * p = strrchr(shell, '/');
622
623 p = p ? p+1 : shell;
624 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
625 while (*q) {
626 int j = strcspn(q, "|");
627
628 if (j == 0)
629 break;
630 if (strncmp(p, q, j) == 0)
631 return i;
632 if (*(q += j))
633 ++q;
634 }
635 }
636 return SH_SH;
637}
638
639
640/*
641 * Determine the type of shell our parent process is
642 * This is quite tricky, not 100% reliable and probably
643 * not nearly as thorough as it should be. Basically, this
644 * is a "best guess" only, but hopefully will work in
645 * most cases.
646 */
647
648static int
649getshelltype(void)
650{
651 pid_t ppid = getppid();
652
653 if (ppid != 1) {
654 FILE * fp;
655 struct stat st;
656 char procdir[MAXPATHLEN], buf[128];
657 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
658 char * shell = getenv("SHELL");
659
660 if (shell != NULL && stat(shell, &st) != -1) {
661 struct stat st1;
662
663 strcpy(procdir+l, "file");
664 /* $SHELL is actual shell? */
665 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
666 return getshellbyname(shell);
667 }
668 strcpy(procdir+l, "status");
669 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
670 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
671 fclose(fp);
672 if (p != NULL)
673 return getshellbyname(p);
674 }
675 }
676 return SH_SH;
677}
678
36#include <stdint.h>
37#include <ctype.h>
38#include <errno.h>
39#include <pwd.h>
40#include <login_cap.h>
41#include <sys/time.h>
42#include <sys/resource.h>
43
44enum
45{
46 SH_NONE,
47 SH_SH, /* sh */
48 SH_CSH, /* csh */
49 SH_BASH, /* gnu bash */
50 SH_TCSH, /* tcsh */
51 SH_KSH, /* (pd)ksh */
52 SH_ZSH, /* zsh */
53 SH_RC, /* rc or es */
54 SH_NUMBER
55};
56
57
58/* eval emitter for popular shells.
59 * Why aren't there any standards here? Most shells support either
60 * the csh 'limit' or sh 'ulimit' command, but each varies just
61 * enough that they aren't very compatible from one to the other.
62 */
63static struct {
64 const char * name; /* Name of shell */
65 const char * inf; /* Name used for 'unlimited' resource */
66 const char * cmd; /* Intro text */
67 const char * hard; /* Hard limit text */
68 const char * soft; /* Soft limit text */
69 const char * both; /* Hard+Soft limit text */
70 struct {
71 const char * pfx;
72 const char * sfx;
73 int divisor;
74 } lprm[RLIM_NLIMITS];
75} shellparm[] =
76{
77 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
78 {
79 { " cputime%-4s %8s", " secs\n", 1 },
80 { " filesize%-4s %8s", " kB\n", 1024 },
81 { " datasize%-4s %8s", " kB\n", 1024 },
82 { " stacksize%-4s %8s", " kB\n", 1024 },
83 { " coredumpsize%-4s %8s", " kB\n", 1024 },
84 { " memoryuse%-4s %8s", " kB\n", 1024 },
85 { " memorylocked%-4s %8s", " kB\n", 1024 },
86 { " maxprocesses%-4s %8s", "\n", 1 },
87 { " openfiles%-4s %8s", "\n", 1 },
88 { " sbsize%-4s %8s", " bytes\n", 1 },
89 { " vmemoryuse%-4s %8s", " kB\n", 1024 },
90 { " pseudo-terminals%-4s %8s", "\n", 1 },
91 { " swapuse%-4s %8s", " kB\n", 1024 }
92 }
93 },
94 { "sh", "unlimited", "", " -H", " -S", "",
95 {
96 { "ulimit%s -t %s", ";\n", 1 },
97 { "ulimit%s -f %s", ";\n", 512 },
98 { "ulimit%s -d %s", ";\n", 1024 },
99 { "ulimit%s -s %s", ";\n", 1024 },
100 { "ulimit%s -c %s", ";\n", 512 },
101 { "ulimit%s -m %s", ";\n", 1024 },
102 { "ulimit%s -l %s", ";\n", 1024 },
103 { "ulimit%s -u %s", ";\n", 1 },
104 { "ulimit%s -n %s", ";\n", 1 },
105 { "ulimit%s -b %s", ";\n", 1 },
106 { "ulimit%s -v %s", ";\n", 1024 },
107 { "ulimit%s -p %s", ";\n", 1 },
108 { "ulimit%s -w %s", ";\n", 1024 }
109 }
110 },
111 { "csh", "unlimited", "", " -h", "", NULL,
112 {
113 { "limit%s cputime %s", ";\n", 1 },
114 { "limit%s filesize %s", ";\n", 1024 },
115 { "limit%s datasize %s", ";\n", 1024 },
116 { "limit%s stacksize %s", ";\n", 1024 },
117 { "limit%s coredumpsize %s", ";\n", 1024 },
118 { "limit%s memoryuse %s", ";\n", 1024 },
119 { "limit%s memorylocked %s", ";\n", 1024 },
120 { "limit%s maxproc %s", ";\n", 1 },
121 { "limit%s openfiles %s", ";\n", 1 },
122 { "limit%s sbsize %s", ";\n", 1 },
123 { "limit%s vmemoryuse %s", ";\n", 1024 },
124 { "limit%s pseudoterminals %s", ";\n", 1 },
125 { "limit%s swapuse %s", ";\n", 1024 }
126 }
127 },
128 { "bash|bash2", "unlimited", "", " -H", " -S", "",
129 {
130 { "ulimit%s -t %s", ";\n", 1 },
131 { "ulimit%s -f %s", ";\n", 1024 },
132 { "ulimit%s -d %s", ";\n", 1024 },
133 { "ulimit%s -s %s", ";\n", 1024 },
134 { "ulimit%s -c %s", ";\n", 1024 },
135 { "ulimit%s -m %s", ";\n", 1024 },
136 { "ulimit%s -l %s", ";\n", 1024 },
137 { "ulimit%s -u %s", ";\n", 1 },
138 { "ulimit%s -n %s", ";\n", 1 },
139 { "ulimit%s -b %s", ";\n", 1 },
140 { "ulimit%s -v %s", ";\n", 1024 },
141 { "ulimit%s -p %s", ";\n", 1 },
142 { "ulimit%s -w %s", ";\n", 1024 }
143 }
144 },
145 { "tcsh", "unlimited", "", " -h", "", NULL,
146 {
147 { "limit%s cputime %s", ";\n", 1 },
148 { "limit%s filesize %s", ";\n", 1024 },
149 { "limit%s datasize %s", ";\n", 1024 },
150 { "limit%s stacksize %s", ";\n", 1024 },
151 { "limit%s coredumpsize %s", ";\n", 1024 },
152 { "limit%s memoryuse %s", ";\n", 1024 },
153 { "limit%s memorylocked %s", ";\n", 1024 },
154 { "limit%s maxproc %s", ";\n", 1 },
155 { "limit%s descriptors %s", ";\n", 1 },
156 { "limit%s sbsize %s", ";\n", 1 },
157 { "limit%s vmemoryuse %s", ";\n", 1024 },
158 { "limit%s pseudoterminals %s", ";\n", 1 },
159 { "limit%s swapuse %s", ";\n", 1024 }
160 }
161 },
162 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
163 {
164 { "ulimit%s -t %s", ";\n", 1 },
165 { "ulimit%s -f %s", ";\n", 512 },
166 { "ulimit%s -d %s", ";\n", 1024 },
167 { "ulimit%s -s %s", ";\n", 1024 },
168 { "ulimit%s -c %s", ";\n", 512 },
169 { "ulimit%s -m %s", ";\n", 1024 },
170 { "ulimit%s -l %s", ";\n", 1024 },
171 { "ulimit%s -p %s", ";\n", 1 },
172 { "ulimit%s -n %s", ";\n", 1 },
173 { "ulimit%s -b %s", ";\n", 1 },
174 { "ulimit%s -v %s", ";\n", 1024 },
175 { "ulimit%s -p %s", ";\n", 1 },
176 { "ulimit%s -w %s", ";\n", 1024 }
177 }
178 },
179 { "zsh", "unlimited", "", " -H", " -S", "",
180 {
181 { "ulimit%s -t %s", ";\n", 1 },
182 { "ulimit%s -f %s", ";\n", 512 },
183 { "ulimit%s -d %s", ";\n", 1024 },
184 { "ulimit%s -s %s", ";\n", 1024 },
185 { "ulimit%s -c %s", ";\n", 512 },
186 { "ulimit%s -m %s", ";\n", 1024 },
187 { "ulimit%s -l %s", ";\n", 1024 },
188 { "ulimit%s -u %s", ";\n", 1 },
189 { "ulimit%s -n %s", ";\n", 1 },
190 { "ulimit%s -b %s", ";\n", 1 },
191 { "ulimit%s -v %s", ";\n", 1024 },
192 { "ulimit%s -p %s", ";\n", 1 },
193 { "ulimit%s -w %s", ";\n", 1024 }
194 }
195 },
196 { "rc|es", "unlimited", "", " -h", "", NULL,
197 {
198 { "limit%s cputime %s", ";\n", 1 },
199 { "limit%s filesize %s", ";\n", 1024 },
200 { "limit%s datasize %s", ";\n", 1024 },
201 { "limit%s stacksize %s", ";\n", 1024 },
202 { "limit%s coredumpsize %s", ";\n", 1024 },
203 { "limit%s memoryuse %s", ";\n", 1024 },
204 { "limit%s lockedmemory %s", ";\n", 1024 },
205 { "limit%s processes %s", ";\n", 1 },
206 { "limit%s descriptors %s", ";\n", 1 },
207 { "limit%s sbsize %s", ";\n", 1 },
208 { "limit%s vmemoryuse %s", ";\n", 1024 },
209 { "limit%s pseudoterminals %s", ";\n", 1 },
210 { "limit%s swapuse %s", ";\n", 1024 }
211 }
212 },
213 { NULL, NULL, NULL, NULL, NULL, NULL,
214 { }
215 }
216};
217
218static struct {
219 const char * cap;
220 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
221} resources[RLIM_NLIMITS] = {
222 { "cputime", login_getcaptime },
223 { "filesize", login_getcapsize },
224 { "datasize", login_getcapsize },
225 { "stacksize", login_getcapsize },
226 { "coredumpsize", login_getcapsize },
227 { "memoryuse", login_getcapsize },
228 { "memorylocked", login_getcapsize },
229 { "maxproc", login_getcapnum },
230 { "openfiles", login_getcapnum },
231 { "sbsize", login_getcapsize },
232 { "vmemoryuse", login_getcapsize },
233 { "pseudoterminals",login_getcapnum },
234 { "swapuse", login_getcapsize }
235};
236
237/*
238 * One letter for each resource levels.
239 * NOTE: There is a dependancy on the corresponding
240 * letter index being equal to the resource number.
241 * If sys/resource.h defines are changed, this needs
242 * to be modified accordingly!
243 */
244
245#define RCS_STRING "tfdscmlunbvpw"
246
247static rlim_t resource_num(int which, int ch, const char *str);
248static void usage(void);
249static int getshelltype(void);
250static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
251 const char *pfx, const char *sfx, const char *which);
252extern char **environ;
253
254static const char rcs_string[] = RCS_STRING;
255
256int
257main(int argc, char *argv[])
258{
259 char *p, *cls = NULL;
260 char *cleanenv[1];
261 struct passwd * pwd = NULL;
262 int rcswhich, shelltype;
263 int i, num_limits = 0;
264 int ch, doeval = 0, doall = 0;
265 int rtrn;
266 login_cap_t * lc = NULL;
267 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
268 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
269 int which_limits[RLIM_NLIMITS];
270 rlim_t set_limits[RLIM_NLIMITS];
271 struct rlimit limits[RLIM_NLIMITS];
272
273 /* init resource tables */
274 for (i = 0; i < RLIM_NLIMITS; i++) {
275 which_limits[i] = 0; /* Don't set/display any */
276 set_limits[i] = RLIM_INFINITY;
277 /* Get current resource values */
278 getrlimit(i, &limits[i]);
279 }
280
281 optarg = NULL;
282 while ((ch = getopt(argc, argv, ":EeC:U:BSHab:c:d:f:l:m:n:s:t:u:v:p:w:")) != -1) {
283 switch(ch) {
284 case 'a':
285 doall = 1;
286 break;
287 case 'E':
288 environ = cleanenv;
289 cleanenv[0] = NULL;
290 break;
291 case 'e':
292 doeval = 1;
293 break;
294 case 'C':
295 cls = optarg;
296 break;
297 case 'U':
298 if ((pwd = getpwnam(optarg)) == NULL) {
299 if (!isdigit(*optarg) ||
300 (pwd = getpwuid(atoi(optarg))) == NULL) {
301 warnx("invalid user `%s'", optarg);
302 usage();
303 }
304 }
305 break;
306 case 'H':
307 type = HARD;
308 break;
309 case 'S':
310 type = SOFT;
311 break;
312 case 'B':
313 type = SOFT|HARD;
314 break;
315 default:
316 case ':': /* Without arg */
317 if ((p = strchr(rcs_string, optopt)) != NULL) {
318 int rcswhich1 = p - rcs_string;
319 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
320 --optind; /* back one arg, and make arg NULL */
321 optarg = NULL;
322 }
323 todo = optarg == NULL ? RCSSEL : RCSSET;
324 if (type == ANY)
325 type = BOTH;
326 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
327 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
328 num_limits++;
329 break;
330 }
331 /* FALLTHRU */
332 case '?':
333 usage();
334 }
335 optarg = NULL;
336 }
337
338 /* If user was specified, get class from that */
339 if (pwd != NULL)
340 lc = login_getpwclass(pwd);
341 else if (cls != NULL && *cls != '\0') {
342 lc = login_getclassbyname(cls, NULL);
343 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
344 fprintf(stderr, "login class '%s' non-existent, using %s\n",
345 cls, lc?lc->lc_class:"current settings");
346 }
347
348 /* If we have a login class, update resource table from that */
349 if (lc != NULL) {
350 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
351 char str[40];
352 rlim_t val;
353
354 /* current value overridden by resourcename or resourcename-cur */
355 sprintf(str, "%s-cur", resources[rcswhich].cap);
356 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
357 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
358 /* maximum value overridden by resourcename or resourcename-max */
359 sprintf(str, "%s-max", resources[rcswhich].cap);
360 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
361 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
362 }
363 }
364
365 /* now, let's determine what we wish to do with all this */
366
367 argv += optind;
368
369 /* If we're setting limits or doing an eval (ie. we're not just
370 * displaying), then check that hard limits are not lower than
371 * soft limits, and force rasing the hard limit if we need to if
372 * we are raising the soft limit, or lower the soft limit if we
373 * are lowering the hard limit.
374 */
375 if ((*argv || doeval) && getuid() == 0) {
376
377 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
378 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
379 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
380 limits[rcswhich].rlim_max = RLIM_INFINITY;
381 which_limits[rcswhich] |= HARD;
382 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
383 if (which_limits[rcswhich] == SOFT) {
384 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
385 which_limits[rcswhich] |= HARD;
386 } else if (which_limits[rcswhich] == HARD) {
387 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
388 which_limits[rcswhich] |= SOFT;
389 } else {
390 /* else.. if we're specifically setting both to
391 * silly values, then let it error out.
392 */
393 }
394 }
395 }
396 }
397 }
398
399 /* See if we've overridden anything specific on the command line */
400 if (num_limits && todo == RCSSET) {
401 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
402 if (which_limits[rcswhich] & HARD)
403 limits[rcswhich].rlim_max = set_limits[rcswhich];
404 if (which_limits[rcswhich] & SOFT)
405 limits[rcswhich].rlim_cur = set_limits[rcswhich];
406 }
407 }
408
409 /* If *argv is not NULL, then we are being asked to
410 * (perhaps) set environment variables and run a program
411 */
412 if (*argv) {
413 if (doeval) {
414 warnx("-e cannot be used with `cmd' option");
415 usage();
416 }
417
418 login_close(lc);
419
420 /* set leading environment variables, like eval(1) */
421 while (*argv && (p = strchr(*argv, '='))) {
422 *p = '\0';
423 rtrn = setenv(*argv++, p + 1, 1);
424 *p = '=';
425 if (rtrn == -1)
426 err(EXIT_FAILURE, "setenv %s", *argv);
427 }
428
429 /* Set limits */
430 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
431 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
432 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
433 err(1, "setrlimit %s", resources[rcswhich].cap);
434 }
435
436 if (*argv == NULL)
437 usage();
438
439 execvp(*argv, argv);
440 err(1, "%s", *argv);
441 }
442
443 shelltype = doeval ? getshelltype() : SH_NONE;
444
445 if (type == ANY) /* Default to soft limits */
446 type = SOFT;
447
448 /* Display limits */
449 printf(shellparm[shelltype].cmd,
450 lc ? " for class " : " (current)",
451 lc ? lc->lc_class : "");
452
453 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
454 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
455 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
456 which_limits[rcswhich] = type;
457 if (shellparm[shelltype].lprm[rcswhich].pfx) {
458 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
459 print_limit(limits[rcswhich].rlim_max,
460 shellparm[shelltype].lprm[rcswhich].divisor,
461 shellparm[shelltype].inf,
462 shellparm[shelltype].lprm[rcswhich].pfx,
463 shellparm[shelltype].lprm[rcswhich].sfx,
464 shellparm[shelltype].both);
465 } else {
466 if (which_limits[rcswhich] & HARD) {
467 print_limit(limits[rcswhich].rlim_max,
468 shellparm[shelltype].lprm[rcswhich].divisor,
469 shellparm[shelltype].inf,
470 shellparm[shelltype].lprm[rcswhich].pfx,
471 shellparm[shelltype].lprm[rcswhich].sfx,
472 shellparm[shelltype].hard);
473 }
474 if (which_limits[rcswhich] & SOFT) {
475 print_limit(limits[rcswhich].rlim_cur,
476 shellparm[shelltype].lprm[rcswhich].divisor,
477 shellparm[shelltype].inf,
478 shellparm[shelltype].lprm[rcswhich].pfx,
479 shellparm[shelltype].lprm[rcswhich].sfx,
480 shellparm[shelltype].soft);
481 }
482 }
483 }
484 }
485 }
486
487 login_close(lc);
488 exit(EXIT_SUCCESS);
489}
490
491
492static void
493usage(void)
494{
495 (void)fprintf(stderr,
496"usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuvpw [val]] [[name=val ...] cmd]\n");
497 exit(EXIT_FAILURE);
498}
499
500static void
501print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
502{
503 char numbr[64];
504
505 if (limit == RLIM_INFINITY)
506 strcpy(numbr, inf);
507 else
508 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
509 printf(pfx, which, numbr);
510 printf(sfx, which);
511
512}
513
514
515static rlim_t
516resource_num(int which, int ch, const char *str)
517{
518 rlim_t res = RLIM_INFINITY;
519
520 if (str != NULL &&
521 !(strcasecmp(str, "inf") == 0 ||
522 strcasecmp(str, "infinity") == 0 ||
523 strcasecmp(str, "unlimit") == 0 ||
524 strcasecmp(str, "unlimited") == 0)) {
525 const char * s = str;
526 char *e;
527
528 switch (which) {
529 case RLIMIT_CPU: /* time values */
530 errno = 0;
531 res = 0;
532 while (*s) {
533 rlim_t tim = strtoq(s, &e, 0);
534 if (e == NULL || e == s || errno)
535 break;
536 switch (*e++) {
537 case 0: /* end of string */
538 e--;
539 default:
540 case 's': case 'S': /* seconds */
541 break;
542 case 'm': case 'M': /* minutes */
543 tim *= 60L;
544 break;
545 case 'h': case 'H': /* hours */
546 tim *= (60L * 60L);
547 break;
548 case 'd': case 'D': /* days */
549 tim *= (60L * 60L * 24L);
550 break;
551 case 'w': case 'W': /* weeks */
552 tim *= (60L * 60L * 24L * 7L);
553 case 'y': case 'Y': /* Years */
554 tim *= (60L * 60L * 24L * 365L);
555 }
556 s = e;
557 res += tim;
558 }
559 break;
560 case RLIMIT_FSIZE: /* Size values */
561 case RLIMIT_DATA:
562 case RLIMIT_STACK:
563 case RLIMIT_CORE:
564 case RLIMIT_RSS:
565 case RLIMIT_MEMLOCK:
566 case RLIMIT_SBSIZE:
567 case RLIMIT_VMEM:
568 case RLIMIT_SWAP:
569 errno = 0;
570 res = 0;
571 while (*s) {
572 rlim_t mult, tim = strtoq(s, &e, 0);
573 if (e == NULL || e == s || errno)
574 break;
575 switch (*e++) {
576 case 0: /* end of string */
577 e--;
578 default:
579 mult = 1;
580 break;
581 case 'b': case 'B': /* 512-byte blocks */
582 mult = 512;
583 break;
584 case 'k': case 'K': /* 1024-byte Kilobytes */
585 mult = 1024;
586 break;
587 case 'm': case 'M': /* 1024-k kbytes */
588 mult = 1024 * 1024;
589 break;
590 case 'g': case 'G': /* 1Gbyte */
591 mult = 1024 * 1024 * 1024;
592 break;
593 case 't': case 'T': /* 1TBte */
594 mult = 1024LL * 1024LL * 1024LL * 1024LL;
595 break;
596 }
597 s = e;
598 res += (tim * mult);
599 }
600 break;
601 case RLIMIT_NPROC:
602 case RLIMIT_NOFILE:
603 case RLIMIT_NPTS:
604 res = strtoq(s, &e, 0);
605 s = e;
606 break;
607 }
608 if (*s) {
609 warnx("invalid value -%c `%s'", ch, str);
610 usage();
611 }
612 }
613 return res;
614}
615
616
617static int
618getshellbyname(const char * shell)
619{
620 int i;
621 const char * q;
622 const char * p = strrchr(shell, '/');
623
624 p = p ? p+1 : shell;
625 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
626 while (*q) {
627 int j = strcspn(q, "|");
628
629 if (j == 0)
630 break;
631 if (strncmp(p, q, j) == 0)
632 return i;
633 if (*(q += j))
634 ++q;
635 }
636 }
637 return SH_SH;
638}
639
640
641/*
642 * Determine the type of shell our parent process is
643 * This is quite tricky, not 100% reliable and probably
644 * not nearly as thorough as it should be. Basically, this
645 * is a "best guess" only, but hopefully will work in
646 * most cases.
647 */
648
649static int
650getshelltype(void)
651{
652 pid_t ppid = getppid();
653
654 if (ppid != 1) {
655 FILE * fp;
656 struct stat st;
657 char procdir[MAXPATHLEN], buf[128];
658 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
659 char * shell = getenv("SHELL");
660
661 if (shell != NULL && stat(shell, &st) != -1) {
662 struct stat st1;
663
664 strcpy(procdir+l, "file");
665 /* $SHELL is actual shell? */
666 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
667 return getshellbyname(shell);
668 }
669 strcpy(procdir+l, "status");
670 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
671 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
672 fclose(fp);
673 if (p != NULL)
674 return getshellbyname(p);
675 }
676 }
677 return SH_SH;
678}
679