limits.c revision 200420
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 $");
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 <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
679