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 },
90 { " pseudo-terminals%-4s %8s", "\n", 1 },
91 { " swapuse%-4s %8s", " kB\n", 1024 }
94 { "sh", "unlimited", "", " -H", " -S", "",
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 }
111 { "csh", "unlimited", "", " -h", "", NULL,
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 }
128 { "bash|bash2", "unlimited", "", " -H", " -S", "",
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 }
145 { "tcsh", "unlimited", "", " -h", "", NULL,
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 }
162 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
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 }
179 { "zsh", "unlimited", "", " -H", " -S", "",
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 }
196 { "rc|es", "unlimited", "", " -h", "", NULL,
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 }
213 { NULL, NULL, NULL, NULL, NULL, NULL,
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 }
238 * One letter for each resource levels.
239 * NOTE: There is a dependency 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!
245 #define RCS_STRING "tfdscmlunbvpw"
247 static rlim_t resource_num(int which, int ch, const char *str);
248 static void usage(void);
249 static int getshelltype(void);
250 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
251 const char *pfx, const char *sfx, const char *which);
252 extern char **environ;
254 static const char rcs_string[] = RCS_STRING;
257 main(int argc, char *argv[])
259 char *p, *cls = NULL;
261 struct passwd * pwd = NULL;
262 int rcswhich, shelltype;
263 int i, num_limits = 0;
264 int ch, doeval = 0, doall = 0;
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];
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]);
282 while ((ch = getopt(argc, argv, ":EeC:U:BSHab:c:d:f:l:m:n:s:t:u:v:p:w:")) != -1) {
298 if ((pwd = getpwnam(optarg)) == NULL) {
299 if (!isdigit(*optarg) ||
300 (pwd = getpwuid(atoi(optarg))) == NULL) {
301 warnx("invalid user `%s'", optarg);
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 */
323 todo = optarg == NULL ? RCSSEL : RCSSET;
326 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
327 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
338 /* If user was specified, get class from that */
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");
348 /* If we have a login class, update resource table from that */
350 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
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);
365 /* now, let's determine what we wish to do with all this */
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.
375 if ((*argv || doeval) && getuid() == 0) {
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;
390 /* else.. if we're specifically setting both to
391 * silly values, then let it error out.
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];
409 /* If *argv is not NULL, then we are being asked to
410 * (perhaps) set environment variables and run a program
414 warnx("-e cannot be used with `cmd' option");
420 /* set leading environment variables, like eval(1) */
421 while (*argv && (p = strchr(*argv, '='))) {
423 rtrn = setenv(*argv++, p + 1, 1);
426 err(EXIT_FAILURE, "setenv %s", *argv);
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);
443 shelltype = doeval ? getshelltype() : SH_NONE;
445 if (type == ANY) /* Default to soft limits */
449 printf(shellparm[shelltype].cmd,
450 lc ? " for class " : " (current)",
451 lc ? lc->lc_class : "");
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);
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);
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);
495 (void)fprintf(stderr,
496 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuvpw [val]] [[name=val ...] cmd]\n");
501 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
505 if (limit == RLIM_INFINITY)
508 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
509 printf(pfx, which, numbr);
516 resource_num(int which, int ch, const char *str)
518 rlim_t res = RLIM_INFINITY;
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;
529 case RLIMIT_CPU: /* time values */
533 rlim_t tim = strtoq(s, &e, 0);
534 if (e == NULL || e == s || errno)
537 case 0: /* end of string */
540 case 's': case 'S': /* seconds */
542 case 'm': case 'M': /* minutes */
545 case 'h': case 'H': /* hours */
548 case 'd': case 'D': /* days */
549 tim *= (60L * 60L * 24L);
551 case 'w': case 'W': /* weeks */
552 tim *= (60L * 60L * 24L * 7L);
553 case 'y': case 'Y': /* Years */
554 tim *= (60L * 60L * 24L * 365L);
560 case RLIMIT_FSIZE: /* Size values */
572 rlim_t mult, tim = strtoq(s, &e, 0);
573 if (e == NULL || e == s || errno)
576 case 0: /* end of string */
581 case 'b': case 'B': /* 512-byte blocks */
584 case 'k': case 'K': /* 1024-byte Kilobytes */
587 case 'm': case 'M': /* 1024-k kbytes */
590 case 'g': case 'G': /* 1Gbyte */
591 mult = 1024 * 1024 * 1024;
593 case 't': case 'T': /* 1TBte */
594 mult = 1024LL * 1024LL * 1024LL * 1024LL;
604 res = strtoq(s, &e, 0);
609 warnx("invalid value -%c `%s'", ch, str);
618 getshellbyname(const char * shell)
622 const char * p = strrchr(shell, '/');
625 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
627 int j = strcspn(q, "|");
631 if (strncmp(p, q, j) == 0)
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
652 pid_t ppid = getppid();
657 char procdir[MAXPATHLEN], buf[128];
658 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
659 char * shell = getenv("SHELL");
661 if (shell != NULL && stat(shell, &st) != -1) {
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);
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");
674 return getshellbyname(p);