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/sysctl.h>
34 #include <sys/param.h>
42 #include <login_cap.h>
44 #include <sys/resource.h>
51 SH_BASH, /* gnu bash */
60 /* eval emitter for popular shells.
61 * Why aren't there any standards here? Most shells support either
62 * the csh 'limit' or sh 'ulimit' command, but each varies just
63 * enough that they aren't very compatible from one to the other.
66 const char * name; /* Name of shell */
67 const char * inf; /* Name used for 'unlimited' resource */
68 const char * cmd; /* Intro text */
69 const char * hard; /* Hard limit text */
70 const char * soft; /* Soft limit text */
71 const char * both; /* Hard+Soft limit text */
79 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
81 { " cputime%-4s %8s", " secs\n", 1 },
82 { " filesize%-4s %8s", " kB\n", 1024 },
83 { " datasize%-4s %8s", " kB\n", 1024 },
84 { " stacksize%-4s %8s", " kB\n", 1024 },
85 { " coredumpsize%-4s %8s", " kB\n", 1024 },
86 { " memoryuse%-4s %8s", " kB\n", 1024 },
87 { " memorylocked%-4s %8s", " kB\n", 1024 },
88 { " maxprocesses%-4s %8s", "\n", 1 },
89 { " openfiles%-4s %8s", "\n", 1 },
90 { " sbsize%-4s %8s", " bytes\n", 1 },
91 { " vmemoryuse%-4s %8s", " kB\n", 1024 },
92 { " pseudo-terminals%-4s %8s", "\n", 1 },
93 { " swapuse%-4s %8s", " kB\n", 1024 },
94 { " kqueues%-4s %8s", "\n", 1 },
95 { " umtxp%-4s %8s", "\n", 1 },
98 { "sh", "unlimited", "", " -H", " -S", "",
100 { "ulimit%s -t %s", ";\n", 1 },
101 { "ulimit%s -f %s", ";\n", 512 },
102 { "ulimit%s -d %s", ";\n", 1024 },
103 { "ulimit%s -s %s", ";\n", 1024 },
104 { "ulimit%s -c %s", ";\n", 512 },
105 { "ulimit%s -m %s", ";\n", 1024 },
106 { "ulimit%s -l %s", ";\n", 1024 },
107 { "ulimit%s -u %s", ";\n", 1 },
108 { "ulimit%s -n %s", ";\n", 1 },
109 { "ulimit%s -b %s", ";\n", 1 },
110 { "ulimit%s -v %s", ";\n", 1024 },
111 { "ulimit%s -p %s", ";\n", 1 },
112 { "ulimit%s -w %s", ";\n", 1024 },
113 { "ulimit%s -k %s", ";\n", 1 },
114 { "ulimit%s -o %s", ";\n", 1 },
117 { "csh", "unlimited", "", " -h", "", NULL,
119 { "limit%s cputime %s", ";\n", 1 },
120 { "limit%s filesize %s", ";\n", 1024 },
121 { "limit%s datasize %s", ";\n", 1024 },
122 { "limit%s stacksize %s", ";\n", 1024 },
123 { "limit%s coredumpsize %s", ";\n", 1024 },
124 { "limit%s memoryuse %s", ";\n", 1024 },
125 { "limit%s memorylocked %s", ";\n", 1024 },
126 { "limit%s maxproc %s", ";\n", 1 },
127 { "limit%s openfiles %s", ";\n", 1 },
128 { "limit%s sbsize %s", ";\n", 1 },
129 { "limit%s vmemoryuse %s", ";\n", 1024 },
130 { "limit%s pseudoterminals %s", ";\n", 1 },
131 { "limit%s swapsize %s", ";\n", 1024 },
132 { "limit%s kqueues %s", ";\n", 1 },
133 { "limit%s umtxp %s", ";\n", 1 },
136 { "bash|bash2", "unlimited", "", " -H", " -S", "",
138 { "ulimit%s -t %s", ";\n", 1 },
139 { "ulimit%s -f %s", ";\n", 1024 },
140 { "ulimit%s -d %s", ";\n", 1024 },
141 { "ulimit%s -s %s", ";\n", 1024 },
142 { "ulimit%s -c %s", ";\n", 1024 },
143 { "ulimit%s -m %s", ";\n", 1024 },
144 { "ulimit%s -l %s", ";\n", 1024 },
145 { "ulimit%s -u %s", ";\n", 1 },
146 { "ulimit%s -n %s", ";\n", 1 },
147 { "ulimit%s -b %s", ";\n", 1 },
148 { "ulimit%s -v %s", ";\n", 1024 },
149 { "ulimit%s -p %s", ";\n", 1 },
150 { "ulimit%s -w %s", ";\n", 1024 }
153 { "tcsh", "unlimited", "", " -h", "", NULL,
155 { "limit%s cputime %s", ";\n", 1 },
156 { "limit%s filesize %s", ";\n", 1024 },
157 { "limit%s datasize %s", ";\n", 1024 },
158 { "limit%s stacksize %s", ";\n", 1024 },
159 { "limit%s coredumpsize %s", ";\n", 1024 },
160 { "limit%s memoryuse %s", ";\n", 1024 },
161 { "limit%s memorylocked %s", ";\n", 1024 },
162 { "limit%s maxproc %s", ";\n", 1 },
163 { "limit%s descriptors %s", ";\n", 1 },
164 { "limit%s sbsize %s", ";\n", 1 },
165 { "limit%s vmemoryuse %s", ";\n", 1024 },
166 { "limit%s pseudoterminals %s", ";\n", 1 },
167 { "limit%s swapsize %s", ";\n", 1024 },
168 { "limit%s kqueues %s", ";\n", 1 },
169 { "limit%s umtxp %s", ";\n", 1 },
172 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
174 { "ulimit%s -t %s", ";\n", 1 },
175 { "ulimit%s -f %s", ";\n", 512 },
176 { "ulimit%s -d %s", ";\n", 1024 },
177 { "ulimit%s -s %s", ";\n", 1024 },
178 { "ulimit%s -c %s", ";\n", 512 },
179 { "ulimit%s -m %s", ";\n", 1024 },
180 { "ulimit%s -l %s", ";\n", 1024 },
181 { "ulimit%s -p %s", ";\n", 1 },
182 { "ulimit%s -n %s", ";\n", 1 },
183 { "ulimit%s -b %s", ";\n", 1 },
184 { "ulimit%s -v %s", ";\n", 1024 },
185 { "ulimit%s -p %s", ";\n", 1 },
186 { "ulimit%s -w %s", ";\n", 1024 }
189 { "zsh", "unlimited", "", " -H", " -S", "",
191 { "ulimit%s -t %s", ";\n", 1 },
192 { "ulimit%s -f %s", ";\n", 512 },
193 { "ulimit%s -d %s", ";\n", 1024 },
194 { "ulimit%s -s %s", ";\n", 1024 },
195 { "ulimit%s -c %s", ";\n", 512 },
196 { "ulimit%s -m %s", ";\n", 1024 },
197 { "ulimit%s -l %s", ";\n", 1024 },
198 { "ulimit%s -u %s", ";\n", 1 },
199 { "ulimit%s -n %s", ";\n", 1 },
200 { "ulimit%s -b %s", ";\n", 1 },
201 { "ulimit%s -v %s", ";\n", 1024 },
202 { "ulimit%s -p %s", ";\n", 1 },
203 { "ulimit%s -w %s", ";\n", 1024 }
206 { "rc|es", "unlimited", "", " -h", "", NULL,
208 { "limit%s cputime %s", ";\n", 1 },
209 { "limit%s filesize %s", ";\n", 1024 },
210 { "limit%s datasize %s", ";\n", 1024 },
211 { "limit%s stacksize %s", ";\n", 1024 },
212 { "limit%s coredumpsize %s", ";\n", 1024 },
213 { "limit%s memoryuse %s", ";\n", 1024 },
214 { "limit%s lockedmemory %s", ";\n", 1024 },
215 { "limit%s processes %s", ";\n", 1 },
216 { "limit%s descriptors %s", ";\n", 1 },
217 { "limit%s sbsize %s", ";\n", 1 },
218 { "limit%s vmemoryuse %s", ";\n", 1024 },
219 { "limit%s pseudoterminals %s", ";\n", 1 },
220 { "limit%s swapuse %s", ";\n", 1024 }
223 { NULL, NULL, NULL, NULL, NULL, NULL,
230 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
231 } resources[RLIM_NLIMITS] = {
232 { "cputime", login_getcaptime },
233 { "filesize", login_getcapsize },
234 { "datasize", login_getcapsize },
235 { "stacksize", login_getcapsize },
236 { "coredumpsize", login_getcapsize },
237 { "memoryuse", login_getcapsize },
238 { "memorylocked", login_getcapsize },
239 { "maxproc", login_getcapnum },
240 { "openfiles", login_getcapnum },
241 { "sbsize", login_getcapsize },
242 { "vmemoryuse", login_getcapsize },
243 { "pseudoterminals",login_getcapnum },
244 { "swapuse", login_getcapsize },
245 { "kqueues", login_getcapnum },
246 { "umtxp", login_getcapnum },
250 * One letter for each resource levels.
251 * NOTE: There is a dependency on the corresponding
252 * letter index being equal to the resource number.
253 * If sys/resource.h defines are changed, this needs
254 * to be modified accordingly!
257 #define RCS_STRING "tfdscmlunbvpwko"
259 static rlim_t resource_num(int which, int ch, const char *str);
260 static void usage(void);
261 static int getshelltype(void);
262 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
263 const char *pfx, const char *sfx, const char *which);
264 static void getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp);
265 static void setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp);
266 extern char **environ;
268 static const char rcs_string[] = RCS_STRING;
271 main(int argc, char *argv[])
273 char *p, *cls = NULL;
275 struct passwd * pwd = NULL;
276 int rcswhich, shelltype;
277 int i, num_limits = 0;
278 int ch, doeval = 0, doall = 0;
280 login_cap_t * lc = NULL;
281 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
282 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
283 int which_limits[RLIM_NLIMITS];
284 rlim_t set_limits[RLIM_NLIMITS];
285 struct rlimit limits[RLIM_NLIMITS];
288 /* init resource tables */
289 for (i = 0; i < RLIM_NLIMITS; i++) {
290 which_limits[i] = 0; /* Don't set/display any */
291 set_limits[i] = RLIM_INFINITY;
296 while ((ch = getopt(argc, argv,
297 ":EeC:U:BSHP:ab:c:d:f:l:m:n:s:t:u:v:p:w:k:o:")) != -1) {
313 if ((pwd = getpwnam(optarg)) == NULL) {
314 if (!isdigit(*optarg) ||
315 (pwd = getpwuid(atoi(optarg))) == NULL) {
316 warnx("invalid user `%s'", optarg);
331 if (!isdigit(*optarg) || (pid = atoi(optarg)) < 0) {
332 warnx("invalid pid `%s'", optarg);
337 case ':': /* Without arg */
338 if ((p = strchr(rcs_string, optopt)) != NULL) {
339 int rcswhich1 = p - rcs_string;
340 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
341 --optind; /* back one arg, and make arg NULL */
344 todo = optarg == NULL ? RCSSEL : RCSSET;
347 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
348 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
361 warnx("-C cannot be used with -P option");
365 warnx("-U cannot be used with -P option");
370 /* Get current resource values */
372 for (i = 0; i < RLIM_NLIMITS; i++) {
374 getrlimit(i, &limits[i]);
375 } else if (doall || num_limits == 0) {
376 getrlimit_proc(pid, i, &limits[i]);
377 } else if (which_limits[i] != 0) {
378 getrlimit_proc(pid, i, &limits[i]);
383 /* If user was specified, get class from that */
385 lc = login_getpwclass(pwd);
386 else if (cls != NULL && *cls != '\0') {
387 lc = login_getclassbyname(cls, NULL);
388 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
389 fprintf(stderr, "login class '%s' non-existent, using %s\n",
390 cls, lc?lc->lc_class:"current settings");
393 /* If we have a login class, update resource table from that */
395 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
399 /* current value overridden by resourcename or resourcename-cur */
400 sprintf(str, "%s-cur", resources[rcswhich].cap);
401 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
402 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
403 /* maximum value overridden by resourcename or resourcename-max */
404 sprintf(str, "%s-max", resources[rcswhich].cap);
405 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
406 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
410 /* now, let's determine what we wish to do with all this */
414 /* If we're setting limits or doing an eval (ie. we're not just
415 * displaying), then check that hard limits are not lower than
416 * soft limits, and force rasing the hard limit if we need to if
417 * we are raising the soft limit, or lower the soft limit if we
418 * are lowering the hard limit.
420 if ((*argv || doeval) && getuid() == 0) {
422 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
423 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
424 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
425 limits[rcswhich].rlim_max = RLIM_INFINITY;
426 which_limits[rcswhich] |= HARD;
427 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
428 if (which_limits[rcswhich] == SOFT) {
429 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
430 which_limits[rcswhich] |= HARD;
431 } else if (which_limits[rcswhich] == HARD) {
432 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
433 which_limits[rcswhich] |= SOFT;
435 /* else.. if we're specifically setting both to
436 * silly values, then let it error out.
444 /* See if we've overridden anything specific on the command line */
445 if (num_limits && todo == RCSSET) {
446 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
447 if (which_limits[rcswhich] & HARD)
448 limits[rcswhich].rlim_max = set_limits[rcswhich];
449 if (which_limits[rcswhich] & SOFT)
450 limits[rcswhich].rlim_cur = set_limits[rcswhich];
454 /* If *argv is not NULL, then we are being asked to
455 * (perhaps) set environment variables and run a program
459 warnx("-e cannot be used with `cmd' option");
463 warnx("-P cannot be used with `cmd' option");
469 /* set leading environment variables, like eval(1) */
470 while (*argv && (p = strchr(*argv, '='))) {
472 rtrn = setenv(*argv++, p + 1, 1);
475 err(EXIT_FAILURE, "setenv %s", *argv);
479 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
480 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
481 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
482 err(1, "setrlimit %s", resources[rcswhich].cap);
493 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
494 if (which_limits[rcswhich] != 0)
495 setrlimit_proc(pid, rcswhich, &limits[rcswhich]);
500 shelltype = doeval ? getshelltype() : SH_NONE;
502 if (type == ANY) /* Default to soft limits */
506 printf(shellparm[shelltype].cmd,
507 lc ? " for class " : " (current)",
508 lc ? lc->lc_class : "");
510 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
511 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
512 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
513 which_limits[rcswhich] = type;
514 if (shellparm[shelltype].lprm[rcswhich].pfx) {
515 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
516 print_limit(limits[rcswhich].rlim_max,
517 shellparm[shelltype].lprm[rcswhich].divisor,
518 shellparm[shelltype].inf,
519 shellparm[shelltype].lprm[rcswhich].pfx,
520 shellparm[shelltype].lprm[rcswhich].sfx,
521 shellparm[shelltype].both);
523 if (which_limits[rcswhich] & HARD) {
524 print_limit(limits[rcswhich].rlim_max,
525 shellparm[shelltype].lprm[rcswhich].divisor,
526 shellparm[shelltype].inf,
527 shellparm[shelltype].lprm[rcswhich].pfx,
528 shellparm[shelltype].lprm[rcswhich].sfx,
529 shellparm[shelltype].hard);
531 if (which_limits[rcswhich] & SOFT) {
532 print_limit(limits[rcswhich].rlim_cur,
533 shellparm[shelltype].lprm[rcswhich].divisor,
534 shellparm[shelltype].inf,
535 shellparm[shelltype].lprm[rcswhich].pfx,
536 shellparm[shelltype].lprm[rcswhich].sfx,
537 shellparm[shelltype].soft);
552 (void)fprintf(stderr,
553 "usage: limits [-C class|-P pid|-U user] [-eaSHBE] "
554 "[-bcdfklmnostuvpw [val]] [[name=val ...] cmd]\n");
559 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
563 if (limit == RLIM_INFINITY)
566 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
567 printf(pfx, which, numbr);
574 resource_num(int which, int ch, const char *str)
576 rlim_t res = RLIM_INFINITY;
579 !(strcasecmp(str, "inf") == 0 ||
580 strcasecmp(str, "infinity") == 0 ||
581 strcasecmp(str, "unlimit") == 0 ||
582 strcasecmp(str, "unlimited") == 0)) {
583 const char * s = str;
587 case RLIMIT_CPU: /* time values */
591 rlim_t tim = strtoq(s, &e, 0);
592 if (e == NULL || e == s || errno)
595 case 0: /* end of string */
598 case 's': case 'S': /* seconds */
600 case 'm': case 'M': /* minutes */
603 case 'h': case 'H': /* hours */
606 case 'd': case 'D': /* days */
607 tim *= (60L * 60L * 24L);
609 case 'w': case 'W': /* weeks */
610 tim *= (60L * 60L * 24L * 7L);
611 case 'y': case 'Y': /* Years */
612 tim *= (60L * 60L * 24L * 365L);
618 case RLIMIT_FSIZE: /* Size values */
630 rlim_t mult, tim = strtoq(s, &e, 0);
631 if (e == NULL || e == s || errno)
634 case 0: /* end of string */
639 case 'b': case 'B': /* 512-byte blocks */
642 case 'k': case 'K': /* 1024-byte Kilobytes */
645 case 'm': case 'M': /* 1024-k kbytes */
648 case 'g': case 'G': /* 1Gbyte */
649 mult = 1024 * 1024 * 1024;
651 case 't': case 'T': /* 1TBte */
652 mult = 1024LL * 1024LL * 1024LL * 1024LL;
664 res = strtoq(s, &e, 0);
669 warnx("invalid value -%c `%s'", ch, str);
678 getshellbyname(const char * shell)
682 const char * p = strrchr(shell, '/');
685 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
687 int j = strcspn(q, "|");
691 if (strncmp(p, q, j) == 0)
702 * Determine the type of shell our parent process is
703 * This is quite tricky, not 100% reliable and probably
704 * not nearly as thorough as it should be. Basically, this
705 * is a "best guess" only, but hopefully will work in
712 pid_t ppid = getppid();
715 struct kinfo_proc kp;
717 char path[MAXPATHLEN];
718 char * shell = getenv("SHELL");
726 if (shell != NULL && stat(shell, &st) != -1) {
729 mib[2] = KERN_PROC_PATHNAME;
731 if (sysctl(mib, 4, path, &len, NULL, 0) != -1) {
732 /* $SHELL is actual shell? */
733 if (stat(path, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
734 return getshellbyname(shell);
737 mib[2] = KERN_PROC_PID;
739 if (sysctl(mib, 4, &kp, &len, NULL, 0) != -1)
740 return getshellbyname(kp.ki_comm);
746 getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp)
754 name[2] = KERN_PROC_RLIMIT;
758 error = sysctl(name, 5, rlp, &len, NULL, 0);
760 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);
761 if (len != sizeof(*rlp))
762 errx(EXIT_FAILURE, "sysctl() returns wrong size");
766 setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp)
773 name[2] = KERN_PROC_RLIMIT;
776 error = sysctl(name, 5, NULL, 0, rlp, sizeof(*rlp));
778 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);