2 * Copyright (c) 1997 by
3 * David L. Nugent <davidn@blaze.net.au>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, is permitted provided that the following conditions
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
21 * Display/change(+runprogram)/eval resource limits.
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
32 #include <sys/param.h>
40 #include <login_cap.h>
42 #include <sys/resource.h>
49 SH_BASH, /* gnu bash */
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.
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 */
77 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
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 }
92 { "sh", "unlimited", "", " -H", " -S", "",
94 { "ulimit%s -t %s", ";\n", 1 },
95 { "ulimit%s -f %s", ";\n", 512 },
96 { "ulimit%s -d %s", ";\n", 1024 },
97 { "ulimit%s -s %s", ";\n", 1024 },
98 { "ulimit%s -c %s", ";\n", 512 },
99 { "ulimit%s -m %s", ";\n", 1024 },
100 { "ulimit%s -l %s", ";\n", 1024 },
101 { "ulimit%s -u %s", ";\n", 1 },
102 { "ulimit%s -n %s", ";\n", 1 },
103 { "ulimit%s -b %s", ";\n", 1 },
104 { "ulimit%s -v %s", ";\n", 1024 }
107 { "csh", "unlimited", "", " -h", "", NULL,
109 { "limit%s cputime %s", ";\n", 1 },
110 { "limit%s filesize %s", ";\n", 1024 },
111 { "limit%s datasize %s", ";\n", 1024 },
112 { "limit%s stacksize %s", ";\n", 1024 },
113 { "limit%s coredumpsize %s", ";\n", 1024 },
114 { "limit%s memoryuse %s", ";\n", 1024 },
115 { "limit%s memorylocked %s", ";\n", 1024 },
116 { "limit%s maxproc %s", ";\n", 1 },
117 { "limit%s openfiles %s", ";\n", 1 },
118 { "limit%s sbsize %s", ";\n", 1 },
119 { "limit%s vmemoryuse %s", ";\n", 1024 }
122 { "bash|bash2", "unlimited", "", " -H", " -S", "",
124 { "ulimit%s -t %s", ";\n", 1 },
125 { "ulimit%s -f %s", ";\n", 1024 },
126 { "ulimit%s -d %s", ";\n", 1024 },
127 { "ulimit%s -s %s", ";\n", 1024 },
128 { "ulimit%s -c %s", ";\n", 1024 },
129 { "ulimit%s -m %s", ";\n", 1024 },
130 { "ulimit%s -l %s", ";\n", 1024 },
131 { "ulimit%s -u %s", ";\n", 1 },
132 { "ulimit%s -n %s", ";\n", 1 },
133 { "ulimit%s -b %s", ";\n", 1 },
134 { "ulimit%s -v %s", ";\n", 1024 }
137 { "tcsh", "unlimited", "", " -h", "", NULL,
139 { "limit%s cputime %s", ";\n", 1 },
140 { "limit%s filesize %s", ";\n", 1024 },
141 { "limit%s datasize %s", ";\n", 1024 },
142 { "limit%s stacksize %s", ";\n", 1024 },
143 { "limit%s coredumpsize %s", ";\n", 1024 },
144 { "limit%s memoryuse %s", ";\n", 1024 },
145 { "limit%s memorylocked %s", ";\n", 1024 },
146 { "limit%s maxproc %s", ";\n", 1 },
147 { "limit%s descriptors %s", ";\n", 1 },
148 { "limit%s sbsize %s", ";\n", 1 },
149 { "limit%s vmemoryuse %s", ";\n", 1024 }
152 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
154 { "ulimit%s -t %s", ";\n", 1 },
155 { "ulimit%s -f %s", ";\n", 512 },
156 { "ulimit%s -d %s", ";\n", 1024 },
157 { "ulimit%s -s %s", ";\n", 1024 },
158 { "ulimit%s -c %s", ";\n", 512 },
159 { "ulimit%s -m %s", ";\n", 1024 },
160 { "ulimit%s -l %s", ";\n", 1024 },
161 { "ulimit%s -p %s", ";\n", 1 },
162 { "ulimit%s -n %s", ";\n", 1 },
163 { "ulimit%s -b %s", ";\n", 1 },
164 { "ulimit%s -v %s", ";\n", 1024 }
167 { "zsh", "unlimited", "", " -H", " -S", "",
169 { "ulimit%s -t %s", ";\n", 1 },
170 { "ulimit%s -f %s", ";\n", 512 },
171 { "ulimit%s -d %s", ";\n", 1024 },
172 { "ulimit%s -s %s", ";\n", 1024 },
173 { "ulimit%s -c %s", ";\n", 512 },
174 { "ulimit%s -m %s", ";\n", 1024 },
175 { "ulimit%s -l %s", ";\n", 1024 },
176 { "ulimit%s -u %s", ";\n", 1 },
177 { "ulimit%s -n %s", ";\n", 1 },
178 { "ulimit%s -b %s", ";\n", 1 },
179 { "ulimit%s -v %s", ";\n", 1024 }
182 { "rc|es", "unlimited", "", " -h", "", NULL,
184 { "limit%s cputime %s", ";\n", 1 },
185 { "limit%s filesize %s", ";\n", 1024 },
186 { "limit%s datasize %s", ";\n", 1024 },
187 { "limit%s stacksize %s", ";\n", 1024 },
188 { "limit%s coredumpsize %s", ";\n", 1024 },
189 { "limit%s memoryuse %s", ";\n", 1024 },
190 { "limit%s lockedmemory %s", ";\n", 1024 },
191 { "limit%s processes %s", ";\n", 1 },
192 { "limit%s descriptors %s", ";\n", 1 },
193 { "limit%s sbsize %s", ";\n", 1 },
194 { "limit%s vmemoryuse %s", ";\n", 1024 }
197 { NULL, NULL, NULL, NULL, NULL, NULL,
204 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
205 } resources[RLIM_NLIMITS] = {
206 { "cputime", login_getcaptime },
207 { "filesize", login_getcapsize },
208 { "datasize", login_getcapsize },
209 { "stacksize", login_getcapsize },
210 { "coredumpsize", login_getcapsize },
211 { "memoryuse", login_getcapsize },
212 { "memorylocked", login_getcapsize },
213 { "maxproc", login_getcapnum },
214 { "openfiles", login_getcapnum },
215 { "sbsize", login_getcapsize },
216 { "vmemoryuse", login_getcapsize }
220 * One letter for each resource levels.
221 * NOTE: There is a dependancy on the corresponding
222 * letter index being equal to the resource number.
223 * If sys/resource.h defines are changed, this needs
224 * to be modified accordingly!
227 #define RCS_STRING "tfdscmlunbv"
229 static rlim_t resource_num(int which, int ch, const char *str);
230 static void usage(void);
231 static int getshelltype(void);
232 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
233 const char *pfx, const char *sfx, const char *which);
234 extern char **environ;
236 static const char rcs_string[] = RCS_STRING;
239 main(int argc, char *argv[])
241 char *p, *cls = NULL;
243 struct passwd * pwd = NULL;
244 int rcswhich, shelltype;
245 int i, num_limits = 0;
246 int ch, doeval = 0, doall = 0;
247 login_cap_t * lc = NULL;
248 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
249 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
250 int which_limits[RLIM_NLIMITS];
251 rlim_t set_limits[RLIM_NLIMITS];
252 struct rlimit limits[RLIM_NLIMITS];
254 /* init resource tables */
255 for (i = 0; i < RLIM_NLIMITS; i++) {
256 which_limits[i] = 0; /* Don't set/display any */
257 set_limits[i] = RLIM_INFINITY;
258 /* Get current resource values */
259 getrlimit(i, &limits[i]);
263 while ((ch = getopt(argc, argv, ":EeC:U:BSHab:c:d:f:l:m:n:s:t:u:v:")) != -1) {
279 if ((pwd = getpwnam(optarg)) == NULL) {
280 if (!isdigit(*optarg) ||
281 (pwd = getpwuid(atoi(optarg))) == NULL) {
282 warnx("invalid user `%s'", optarg);
297 case ':': /* Without arg */
298 if ((p = strchr(rcs_string, optopt)) != NULL) {
299 int rcswhich1 = p - rcs_string;
300 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
301 --optind; /* back one arg, and make arg NULL */
304 todo = optarg == NULL ? RCSSEL : RCSSET;
307 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
308 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
319 /* If user was specified, get class from that */
321 lc = login_getpwclass(pwd);
322 else if (cls != NULL && *cls != '\0') {
323 lc = login_getclassbyname(cls, NULL);
324 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
325 fprintf(stderr, "login class '%s' non-existent, using %s\n",
326 cls, lc?lc->lc_class:"current settings");
329 /* If we have a login class, update resource table from that */
331 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
335 /* current value overridden by resourcename or resourcename-cur */
336 sprintf(str, "%s-cur", resources[rcswhich].cap);
337 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
338 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
339 /* maximum value overridden by resourcename or resourcename-max */
340 sprintf(str, "%s-max", resources[rcswhich].cap);
341 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
342 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
346 /* now, let's determine what we wish to do with all this */
350 /* If we're setting limits or doing an eval (ie. we're not just
351 * displaying), then check that hard limits are not lower than
352 * soft limits, and force rasing the hard limit if we need to if
353 * we are raising the soft limit, or lower the soft limit if we
354 * are lowering the hard limit.
356 if ((*argv || doeval) && getuid() == 0) {
358 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
359 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
360 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
361 limits[rcswhich].rlim_max = RLIM_INFINITY;
362 which_limits[rcswhich] |= HARD;
363 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
364 if (which_limits[rcswhich] == SOFT) {
365 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
366 which_limits[rcswhich] |= HARD;
367 } else if (which_limits[rcswhich] == HARD) {
368 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
369 which_limits[rcswhich] |= SOFT;
371 /* else.. if we're specifically setting both to
372 * silly values, then let it error out.
380 /* See if we've overridden anything specific on the command line */
381 if (num_limits && todo == RCSSET) {
382 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
383 if (which_limits[rcswhich] & HARD)
384 limits[rcswhich].rlim_max = set_limits[rcswhich];
385 if (which_limits[rcswhich] & SOFT)
386 limits[rcswhich].rlim_cur = set_limits[rcswhich];
390 /* If *argv is not NULL, then we are being asked to
391 * (perhaps) set environment variables and run a program
395 warnx("-e cannot be used with `cmd' option");
401 /* set leading environment variables, like eval(1) */
402 while (*argv && (p = strchr(*argv, '=')))
403 (void)setenv(*argv++, ++p, 1);
406 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
407 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
408 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
409 err(1, "setrlimit %s", resources[rcswhich].cap);
419 shelltype = doeval ? getshelltype() : SH_NONE;
421 if (type == ANY) /* Default to soft limits */
425 printf(shellparm[shelltype].cmd,
426 lc ? " for class " : " (current)",
427 lc ? lc->lc_class : "");
429 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
430 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
431 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
432 which_limits[rcswhich] = type;
433 if (shellparm[shelltype].lprm[rcswhich].pfx) {
434 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
435 print_limit(limits[rcswhich].rlim_max,
436 shellparm[shelltype].lprm[rcswhich].divisor,
437 shellparm[shelltype].inf,
438 shellparm[shelltype].lprm[rcswhich].pfx,
439 shellparm[shelltype].lprm[rcswhich].sfx,
440 shellparm[shelltype].both);
442 if (which_limits[rcswhich] & HARD) {
443 print_limit(limits[rcswhich].rlim_max,
444 shellparm[shelltype].lprm[rcswhich].divisor,
445 shellparm[shelltype].inf,
446 shellparm[shelltype].lprm[rcswhich].pfx,
447 shellparm[shelltype].lprm[rcswhich].sfx,
448 shellparm[shelltype].hard);
450 if (which_limits[rcswhich] & SOFT) {
451 print_limit(limits[rcswhich].rlim_cur,
452 shellparm[shelltype].lprm[rcswhich].divisor,
453 shellparm[shelltype].inf,
454 shellparm[shelltype].lprm[rcswhich].pfx,
455 shellparm[shelltype].lprm[rcswhich].sfx,
456 shellparm[shelltype].soft);
471 (void)fprintf(stderr,
472 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuv [val]] [[name=val ...] cmd]\n");
477 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
481 if (limit == RLIM_INFINITY)
484 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
485 printf(pfx, which, numbr);
492 resource_num(int which, int ch, const char *str)
494 rlim_t res = RLIM_INFINITY;
497 !(strcasecmp(str, "inf") == 0 ||
498 strcasecmp(str, "infinity") == 0 ||
499 strcasecmp(str, "unlimit") == 0 ||
500 strcasecmp(str, "unlimited") == 0)) {
501 const char * s = str;
505 case RLIMIT_CPU: /* time values */
509 rlim_t tim = strtoq(s, &e, 0);
510 if (e == NULL || e == s || errno)
513 case 0: /* end of string */
516 case 's': case 'S': /* seconds */
518 case 'm': case 'M': /* minutes */
521 case 'h': case 'H': /* hours */
524 case 'd': case 'D': /* days */
525 tim *= (60L * 60L * 24L);
527 case 'w': case 'W': /* weeks */
528 tim *= (60L * 60L * 24L * 7L);
529 case 'y': case 'Y': /* Years */
530 tim *= (60L * 60L * 24L * 365L);
536 case RLIMIT_FSIZE: /* Size values */
547 rlim_t mult, tim = strtoq(s, &e, 0);
548 if (e == NULL || e == s || errno)
551 case 0: /* end of string */
556 case 'b': case 'B': /* 512-byte blocks */
559 case 'k': case 'K': /* 1024-byte Kilobytes */
562 case 'm': case 'M': /* 1024-k kbytes */
565 case 'g': case 'G': /* 1Gbyte */
566 mult = 1024 * 1024 * 1024;
568 case 't': case 'T': /* 1TBte */
569 mult = 1024LL * 1024LL * 1024LL * 1024LL;
578 res = strtoq(s, &e, 0);
583 warnx("invalid value -%c `%s'", ch, str);
592 getshellbyname(const char * shell)
596 const char * p = strrchr(shell, '/');
599 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
601 int j = strcspn(q, "|");
605 if (strncmp(p, q, j) == 0)
616 * Determine the type of shell our parent process is
617 * This is quite tricky, not 100% reliable and probably
618 * not nearly as thorough as it should be. Basically, this
619 * is a "best guess" only, but hopefully will work in
626 pid_t ppid = getppid();
631 char procdir[MAXPATHLEN], buf[128];
632 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
633 char * shell = getenv("SHELL");
635 if (shell != NULL && stat(shell, &st) != -1) {
638 strcpy(procdir+l, "file");
639 /* $SHELL is actual shell? */
640 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
641 return getshellbyname(shell);
643 strcpy(procdir+l, "status");
644 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
645 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
648 return getshellbyname(p);