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;
248 login_cap_t * lc = NULL;
249 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
250 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
251 int which_limits[RLIM_NLIMITS];
252 rlim_t set_limits[RLIM_NLIMITS];
253 struct rlimit limits[RLIM_NLIMITS];
255 /* init resource tables */
256 for (i = 0; i < RLIM_NLIMITS; i++) {
257 which_limits[i] = 0; /* Don't set/display any */
258 set_limits[i] = RLIM_INFINITY;
259 /* Get current resource values */
260 getrlimit(i, &limits[i]);
264 while ((ch = getopt(argc, argv, ":EeC:U:BSHab:c:d:f:l:m:n:s:t:u:v:")) != -1) {
280 if ((pwd = getpwnam(optarg)) == NULL) {
281 if (!isdigit(*optarg) ||
282 (pwd = getpwuid(atoi(optarg))) == NULL) {
283 warnx("invalid user `%s'", optarg);
298 case ':': /* Without arg */
299 if ((p = strchr(rcs_string, optopt)) != NULL) {
300 int rcswhich1 = p - rcs_string;
301 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
302 --optind; /* back one arg, and make arg NULL */
305 todo = optarg == NULL ? RCSSEL : RCSSET;
308 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
309 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
320 /* If user was specified, get class from that */
322 lc = login_getpwclass(pwd);
323 else if (cls != NULL && *cls != '\0') {
324 lc = login_getclassbyname(cls, NULL);
325 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
326 fprintf(stderr, "login class '%s' non-existent, using %s\n",
327 cls, lc?lc->lc_class:"current settings");
330 /* If we have a login class, update resource table from that */
332 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
336 /* current value overridden by resourcename or resourcename-cur */
337 sprintf(str, "%s-cur", resources[rcswhich].cap);
338 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
339 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
340 /* maximum value overridden by resourcename or resourcename-max */
341 sprintf(str, "%s-max", resources[rcswhich].cap);
342 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
343 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
347 /* now, let's determine what we wish to do with all this */
351 /* If we're setting limits or doing an eval (ie. we're not just
352 * displaying), then check that hard limits are not lower than
353 * soft limits, and force rasing the hard limit if we need to if
354 * we are raising the soft limit, or lower the soft limit if we
355 * are lowering the hard limit.
357 if ((*argv || doeval) && getuid() == 0) {
359 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
360 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
361 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
362 limits[rcswhich].rlim_max = RLIM_INFINITY;
363 which_limits[rcswhich] |= HARD;
364 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
365 if (which_limits[rcswhich] == SOFT) {
366 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
367 which_limits[rcswhich] |= HARD;
368 } else if (which_limits[rcswhich] == HARD) {
369 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
370 which_limits[rcswhich] |= SOFT;
372 /* else.. if we're specifically setting both to
373 * silly values, then let it error out.
381 /* See if we've overridden anything specific on the command line */
382 if (num_limits && todo == RCSSET) {
383 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
384 if (which_limits[rcswhich] & HARD)
385 limits[rcswhich].rlim_max = set_limits[rcswhich];
386 if (which_limits[rcswhich] & SOFT)
387 limits[rcswhich].rlim_cur = set_limits[rcswhich];
391 /* If *argv is not NULL, then we are being asked to
392 * (perhaps) set environment variables and run a program
396 warnx("-e cannot be used with `cmd' option");
402 /* set leading environment variables, like eval(1) */
403 while (*argv && (p = strchr(*argv, '='))) {
405 rtrn = setenv(*argv++, p + 1, 1);
408 err(EXIT_FAILURE, "setenv %s", *argv);
412 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
413 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
414 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
415 err(1, "setrlimit %s", resources[rcswhich].cap);
425 shelltype = doeval ? getshelltype() : SH_NONE;
427 if (type == ANY) /* Default to soft limits */
431 printf(shellparm[shelltype].cmd,
432 lc ? " for class " : " (current)",
433 lc ? lc->lc_class : "");
435 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
436 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
437 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
438 which_limits[rcswhich] = type;
439 if (shellparm[shelltype].lprm[rcswhich].pfx) {
440 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
441 print_limit(limits[rcswhich].rlim_max,
442 shellparm[shelltype].lprm[rcswhich].divisor,
443 shellparm[shelltype].inf,
444 shellparm[shelltype].lprm[rcswhich].pfx,
445 shellparm[shelltype].lprm[rcswhich].sfx,
446 shellparm[shelltype].both);
448 if (which_limits[rcswhich] & HARD) {
449 print_limit(limits[rcswhich].rlim_max,
450 shellparm[shelltype].lprm[rcswhich].divisor,
451 shellparm[shelltype].inf,
452 shellparm[shelltype].lprm[rcswhich].pfx,
453 shellparm[shelltype].lprm[rcswhich].sfx,
454 shellparm[shelltype].hard);
456 if (which_limits[rcswhich] & SOFT) {
457 print_limit(limits[rcswhich].rlim_cur,
458 shellparm[shelltype].lprm[rcswhich].divisor,
459 shellparm[shelltype].inf,
460 shellparm[shelltype].lprm[rcswhich].pfx,
461 shellparm[shelltype].lprm[rcswhich].sfx,
462 shellparm[shelltype].soft);
477 (void)fprintf(stderr,
478 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuv [val]] [[name=val ...] cmd]\n");
483 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
487 if (limit == RLIM_INFINITY)
490 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
491 printf(pfx, which, numbr);
498 resource_num(int which, int ch, const char *str)
500 rlim_t res = RLIM_INFINITY;
503 !(strcasecmp(str, "inf") == 0 ||
504 strcasecmp(str, "infinity") == 0 ||
505 strcasecmp(str, "unlimit") == 0 ||
506 strcasecmp(str, "unlimited") == 0)) {
507 const char * s = str;
511 case RLIMIT_CPU: /* time values */
515 rlim_t tim = strtoq(s, &e, 0);
516 if (e == NULL || e == s || errno)
519 case 0: /* end of string */
522 case 's': case 'S': /* seconds */
524 case 'm': case 'M': /* minutes */
527 case 'h': case 'H': /* hours */
530 case 'd': case 'D': /* days */
531 tim *= (60L * 60L * 24L);
533 case 'w': case 'W': /* weeks */
534 tim *= (60L * 60L * 24L * 7L);
535 case 'y': case 'Y': /* Years */
536 tim *= (60L * 60L * 24L * 365L);
542 case RLIMIT_FSIZE: /* Size values */
553 rlim_t mult, tim = strtoq(s, &e, 0);
554 if (e == NULL || e == s || errno)
557 case 0: /* end of string */
562 case 'b': case 'B': /* 512-byte blocks */
565 case 'k': case 'K': /* 1024-byte Kilobytes */
568 case 'm': case 'M': /* 1024-k kbytes */
571 case 'g': case 'G': /* 1Gbyte */
572 mult = 1024 * 1024 * 1024;
574 case 't': case 'T': /* 1TBte */
575 mult = 1024LL * 1024LL * 1024LL * 1024LL;
584 res = strtoq(s, &e, 0);
589 warnx("invalid value -%c `%s'", ch, str);
598 getshellbyname(const char * shell)
602 const char * p = strrchr(shell, '/');
605 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
607 int j = strcspn(q, "|");
611 if (strncmp(p, q, j) == 0)
622 * Determine the type of shell our parent process is
623 * This is quite tricky, not 100% reliable and probably
624 * not nearly as thorough as it should be. Basically, this
625 * is a "best guess" only, but hopefully will work in
632 pid_t ppid = getppid();
637 char procdir[MAXPATHLEN], buf[128];
638 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
639 char * shell = getenv("SHELL");
641 if (shell != NULL && stat(shell, &st) != -1) {
644 strcpy(procdir+l, "file");
645 /* $SHELL is actual shell? */
646 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
647 return getshellbyname(shell);
649 strcpy(procdir+l, "status");
650 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
651 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
654 return getshellbyname(p);