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>
28 #include <sys/types.h>
30 #include <sys/sysctl.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 },
92 { " kqueues%-4s %8s", "\n", 1 },
93 { " umtxp%-4s %8s", "\n", 1 },
96 { "sh", "unlimited", "", " -H", " -S", "",
98 { "ulimit%s -t %s", ";\n", 1 },
99 { "ulimit%s -f %s", ";\n", 512 },
100 { "ulimit%s -d %s", ";\n", 1024 },
101 { "ulimit%s -s %s", ";\n", 1024 },
102 { "ulimit%s -c %s", ";\n", 512 },
103 { "ulimit%s -m %s", ";\n", 1024 },
104 { "ulimit%s -l %s", ";\n", 1024 },
105 { "ulimit%s -u %s", ";\n", 1 },
106 { "ulimit%s -n %s", ";\n", 1 },
107 { "ulimit%s -b %s", ";\n", 1 },
108 { "ulimit%s -v %s", ";\n", 1024 },
109 { "ulimit%s -p %s", ";\n", 1 },
110 { "ulimit%s -w %s", ";\n", 1024 },
111 { "ulimit%s -k %s", ";\n", 1 },
112 { "ulimit%s -o %s", ";\n", 1 },
115 { "csh", "unlimited", "", " -h", "", NULL,
117 { "limit%s cputime %s", ";\n", 1 },
118 { "limit%s filesize %s", ";\n", 1024 },
119 { "limit%s datasize %s", ";\n", 1024 },
120 { "limit%s stacksize %s", ";\n", 1024 },
121 { "limit%s coredumpsize %s", ";\n", 1024 },
122 { "limit%s memoryuse %s", ";\n", 1024 },
123 { "limit%s memorylocked %s", ";\n", 1024 },
124 { "limit%s maxproc %s", ";\n", 1 },
125 { "limit%s openfiles %s", ";\n", 1 },
126 { "limit%s sbsize %s", ";\n", 1 },
127 { "limit%s vmemoryuse %s", ";\n", 1024 },
128 { "limit%s pseudoterminals %s", ";\n", 1 },
129 { "limit%s swapsize %s", ";\n", 1024 },
130 { "limit%s kqueues %s", ";\n", 1 },
131 { "limit%s umtxp %s", ";\n", 1 },
134 { "bash|bash2", "unlimited", "", " -H", " -S", "",
136 { "ulimit%s -t %s", ";\n", 1 },
137 { "ulimit%s -f %s", ";\n", 1024 },
138 { "ulimit%s -d %s", ";\n", 1024 },
139 { "ulimit%s -s %s", ";\n", 1024 },
140 { "ulimit%s -c %s", ";\n", 1024 },
141 { "ulimit%s -m %s", ";\n", 1024 },
142 { "ulimit%s -l %s", ";\n", 1024 },
143 { "ulimit%s -u %s", ";\n", 1 },
144 { "ulimit%s -n %s", ";\n", 1 },
145 { "ulimit%s -b %s", ";\n", 1 },
146 { "ulimit%s -v %s", ";\n", 1024 },
147 { "ulimit%s -p %s", ";\n", 1 },
148 { "ulimit%s -w %s", ";\n", 1024 }
151 { "tcsh", "unlimited", "", " -h", "", NULL,
153 { "limit%s cputime %s", ";\n", 1 },
154 { "limit%s filesize %s", ";\n", 1024 },
155 { "limit%s datasize %s", ";\n", 1024 },
156 { "limit%s stacksize %s", ";\n", 1024 },
157 { "limit%s coredumpsize %s", ";\n", 1024 },
158 { "limit%s memoryuse %s", ";\n", 1024 },
159 { "limit%s memorylocked %s", ";\n", 1024 },
160 { "limit%s maxproc %s", ";\n", 1 },
161 { "limit%s descriptors %s", ";\n", 1 },
162 { "limit%s sbsize %s", ";\n", 1 },
163 { "limit%s vmemoryuse %s", ";\n", 1024 },
164 { "limit%s pseudoterminals %s", ";\n", 1 },
165 { "limit%s swapsize %s", ";\n", 1024 },
166 { "limit%s kqueues %s", ";\n", 1 },
167 { "limit%s umtxp %s", ";\n", 1 },
170 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
172 { "ulimit%s -t %s", ";\n", 1 },
173 { "ulimit%s -f %s", ";\n", 512 },
174 { "ulimit%s -d %s", ";\n", 1024 },
175 { "ulimit%s -s %s", ";\n", 1024 },
176 { "ulimit%s -c %s", ";\n", 512 },
177 { "ulimit%s -m %s", ";\n", 1024 },
178 { "ulimit%s -l %s", ";\n", 1024 },
179 { "ulimit%s -p %s", ";\n", 1 },
180 { "ulimit%s -n %s", ";\n", 1 },
181 { "ulimit%s -b %s", ";\n", 1 },
182 { "ulimit%s -v %s", ";\n", 1024 },
183 { "ulimit%s -p %s", ";\n", 1 },
184 { "ulimit%s -w %s", ";\n", 1024 }
187 { "zsh", "unlimited", "", " -H", " -S", "",
189 { "ulimit%s -t %s", ";\n", 1 },
190 { "ulimit%s -f %s", ";\n", 512 },
191 { "ulimit%s -d %s", ";\n", 1024 },
192 { "ulimit%s -s %s", ";\n", 1024 },
193 { "ulimit%s -c %s", ";\n", 512 },
194 { "ulimit%s -m %s", ";\n", 1024 },
195 { "ulimit%s -l %s", ";\n", 1024 },
196 { "ulimit%s -u %s", ";\n", 1 },
197 { "ulimit%s -n %s", ";\n", 1 },
198 { "ulimit%s -b %s", ";\n", 1 },
199 { "ulimit%s -v %s", ";\n", 1024 },
200 { "ulimit%s -p %s", ";\n", 1 },
201 { "ulimit%s -w %s", ";\n", 1024 }
204 { "rc|es", "unlimited", "", " -h", "", NULL,
206 { "limit%s cputime %s", ";\n", 1 },
207 { "limit%s filesize %s", ";\n", 1024 },
208 { "limit%s datasize %s", ";\n", 1024 },
209 { "limit%s stacksize %s", ";\n", 1024 },
210 { "limit%s coredumpsize %s", ";\n", 1024 },
211 { "limit%s memoryuse %s", ";\n", 1024 },
212 { "limit%s lockedmemory %s", ";\n", 1024 },
213 { "limit%s processes %s", ";\n", 1 },
214 { "limit%s descriptors %s", ";\n", 1 },
215 { "limit%s sbsize %s", ";\n", 1 },
216 { "limit%s vmemoryuse %s", ";\n", 1024 },
217 { "limit%s pseudoterminals %s", ";\n", 1 },
218 { "limit%s swapuse %s", ";\n", 1024 }
221 { NULL, NULL, NULL, NULL, NULL, NULL,
228 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
229 } resources[RLIM_NLIMITS] = {
230 { "cputime", login_getcaptime },
231 { "filesize", login_getcapsize },
232 { "datasize", login_getcapsize },
233 { "stacksize", login_getcapsize },
234 { "coredumpsize", login_getcapsize },
235 { "memoryuse", login_getcapsize },
236 { "memorylocked", login_getcapsize },
237 { "maxproc", login_getcapnum },
238 { "openfiles", login_getcapnum },
239 { "sbsize", login_getcapsize },
240 { "vmemoryuse", login_getcapsize },
241 { "pseudoterminals",login_getcapnum },
242 { "swapuse", login_getcapsize },
243 { "kqueues", login_getcapnum },
244 { "umtxp", login_getcapnum },
248 * One letter for each resource levels.
249 * NOTE: There is a dependency on the corresponding
250 * letter index being equal to the resource number.
251 * If sys/resource.h defines are changed, this needs
252 * to be modified accordingly!
255 #define RCS_STRING "tfdscmlunbvpwko"
257 static rlim_t resource_num(int which, int ch, const char *str);
258 static void usage(void) __dead2;
259 static int getshelltype(void);
260 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
261 const char *pfx, const char *sfx, const char *which);
262 static void getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp);
263 static void setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp);
264 extern char **environ;
266 static const char rcs_string[] = RCS_STRING;
269 main(int argc, char *argv[])
271 char *p, *cls = NULL;
273 struct passwd * pwd = NULL;
274 int rcswhich, shelltype;
275 int i, num_limits = 0;
276 int ch, doeval = 0, doall = 0;
278 login_cap_t * lc = NULL;
279 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
280 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
281 int which_limits[RLIM_NLIMITS];
282 rlim_t set_limits[RLIM_NLIMITS];
283 struct rlimit limits[RLIM_NLIMITS];
286 /* init resource tables */
287 for (i = 0; i < RLIM_NLIMITS; i++) {
288 which_limits[i] = 0; /* Don't set/display any */
289 set_limits[i] = RLIM_INFINITY;
294 while ((ch = getopt(argc, argv,
295 ":EeC:U:BSHP:ab:c:d:f:l:m:n:s:t:u:v:p:w:k:o:")) != -1) {
311 if ((pwd = getpwnam(optarg)) == NULL) {
312 if (!isdigit(*optarg) ||
313 (pwd = getpwuid(atoi(optarg))) == NULL) {
314 warnx("invalid user `%s'", optarg);
329 if (!isdigit(*optarg) || (pid = atoi(optarg)) < 0) {
330 warnx("invalid pid `%s'", optarg);
335 case ':': /* Without arg */
336 if ((p = strchr(rcs_string, optopt)) != NULL) {
337 int rcswhich1 = p - rcs_string;
338 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
339 --optind; /* back one arg, and make arg NULL */
342 todo = optarg == NULL ? RCSSEL : RCSSET;
345 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
346 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
359 warnx("-C cannot be used with -P option");
363 warnx("-U cannot be used with -P option");
368 /* Get current resource values */
370 for (i = 0; i < RLIM_NLIMITS; i++) {
372 getrlimit(i, &limits[i]);
373 } else if (doall || num_limits == 0) {
374 getrlimit_proc(pid, i, &limits[i]);
375 } else if (which_limits[i] != 0) {
376 getrlimit_proc(pid, i, &limits[i]);
381 /* If user was specified, get class from that */
383 lc = login_getpwclass(pwd);
384 else if (cls != NULL && *cls != '\0') {
385 lc = login_getclassbyname(cls, NULL);
386 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
387 fprintf(stderr, "login class '%s' non-existent, using %s\n",
388 cls, lc?lc->lc_class:"current settings");
391 /* If we have a login class, update resource table from that */
393 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
397 /* current value overridden by resourcename or resourcename-cur */
398 sprintf(str, "%s-cur", resources[rcswhich].cap);
399 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
400 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
401 /* maximum value overridden by resourcename or resourcename-max */
402 sprintf(str, "%s-max", resources[rcswhich].cap);
403 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
404 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
408 /* now, let's determine what we wish to do with all this */
412 /* If we're setting limits or doing an eval (ie. we're not just
413 * displaying), then check that hard limits are not lower than
414 * soft limits, and force rasing the hard limit if we need to if
415 * we are raising the soft limit, or lower the soft limit if we
416 * are lowering the hard limit.
418 if ((*argv || doeval) && getuid() == 0) {
420 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
421 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
422 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
423 limits[rcswhich].rlim_max = RLIM_INFINITY;
424 which_limits[rcswhich] |= HARD;
425 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
426 if (which_limits[rcswhich] == SOFT) {
427 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
428 which_limits[rcswhich] |= HARD;
429 } else if (which_limits[rcswhich] == HARD) {
430 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
431 which_limits[rcswhich] |= SOFT;
433 /* else.. if we're specifically setting both to
434 * silly values, then let it error out.
442 /* See if we've overridden anything specific on the command line */
443 if (num_limits && todo == RCSSET) {
444 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
445 if (which_limits[rcswhich] & HARD)
446 limits[rcswhich].rlim_max = set_limits[rcswhich];
447 if (which_limits[rcswhich] & SOFT)
448 limits[rcswhich].rlim_cur = set_limits[rcswhich];
452 /* If *argv is not NULL, then we are being asked to
453 * (perhaps) set environment variables and run a program
457 warnx("-e cannot be used with `cmd' option");
461 warnx("-P cannot be used with `cmd' option");
467 /* set leading environment variables, like eval(1) */
468 while (*argv && (p = strchr(*argv, '='))) {
470 rtrn = setenv(*argv++, p + 1, 1);
473 err(EXIT_FAILURE, "setenv %s", *argv);
477 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
478 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
479 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
480 err(1, "setrlimit %s", resources[rcswhich].cap);
491 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
492 if (which_limits[rcswhich] != 0)
493 setrlimit_proc(pid, rcswhich, &limits[rcswhich]);
498 shelltype = doeval ? getshelltype() : SH_NONE;
500 if (type == ANY) /* Default to soft limits */
504 printf(shellparm[shelltype].cmd,
505 lc ? " for class " : " (current)",
506 lc ? lc->lc_class : "");
508 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
509 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
510 if (which_limits[rcswhich] == ANY)
511 which_limits[rcswhich] = type;
512 if (shellparm[shelltype].lprm[rcswhich].pfx) {
513 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
514 print_limit(limits[rcswhich].rlim_max,
515 shellparm[shelltype].lprm[rcswhich].divisor,
516 shellparm[shelltype].inf,
517 shellparm[shelltype].lprm[rcswhich].pfx,
518 shellparm[shelltype].lprm[rcswhich].sfx,
519 shellparm[shelltype].both);
521 if (which_limits[rcswhich] & HARD) {
522 print_limit(limits[rcswhich].rlim_max,
523 shellparm[shelltype].lprm[rcswhich].divisor,
524 shellparm[shelltype].inf,
525 shellparm[shelltype].lprm[rcswhich].pfx,
526 shellparm[shelltype].lprm[rcswhich].sfx,
527 shellparm[shelltype].hard);
529 if (which_limits[rcswhich] & SOFT) {
530 print_limit(limits[rcswhich].rlim_cur,
531 shellparm[shelltype].lprm[rcswhich].divisor,
532 shellparm[shelltype].inf,
533 shellparm[shelltype].lprm[rcswhich].pfx,
534 shellparm[shelltype].lprm[rcswhich].sfx,
535 shellparm[shelltype].soft);
550 (void)fprintf(stderr,
551 "usage: limits [-C class|-P pid|-U user] [-eaSHBE] "
552 "[-bcdfklmnostuvpw [val]] [[name=val ...] cmd]\n");
557 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
561 if (limit == RLIM_INFINITY)
562 strlcpy(numbr, inf, sizeof(numbr));
564 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
565 printf(pfx, which, numbr);
572 resource_num(int which, int ch, const char *str)
574 rlim_t res = RLIM_INFINITY;
577 !(strcasecmp(str, "inf") == 0 ||
578 strcasecmp(str, "infinity") == 0 ||
579 strcasecmp(str, "unlimit") == 0 ||
580 strcasecmp(str, "unlimited") == 0)) {
581 const char * s = str;
585 case RLIMIT_CPU: /* time values */
589 rlim_t tim = strtoq(s, &e, 0);
590 if (e == NULL || e == s || errno)
593 case 0: /* end of string */
596 case 's': case 'S': /* seconds */
598 case 'm': case 'M': /* minutes */
601 case 'h': case 'H': /* hours */
604 case 'd': case 'D': /* days */
605 tim *= (60L * 60L * 24L);
607 case 'w': case 'W': /* weeks */
608 tim *= (60L * 60L * 24L * 7L);
610 case 'y': case 'Y': /* Years */
611 tim *= (60L * 60L * 24L * 365L);
617 case RLIMIT_FSIZE: /* Size values */
629 rlim_t mult, tim = strtoq(s, &e, 0);
630 if (e == NULL || e == s || errno)
633 case 0: /* end of string */
638 case 'b': case 'B': /* 512-byte blocks */
641 case 'k': case 'K': /* 1024-byte Kilobytes */
644 case 'm': case 'M': /* 1024-k kbytes */
647 case 'g': case 'G': /* 1Gbyte */
648 mult = 1024 * 1024 * 1024;
650 case 't': case 'T': /* 1TBte */
651 mult = 1024LL * 1024LL * 1024LL * 1024LL;
663 res = strtoq(s, &e, 0);
668 warnx("invalid value -%c `%s'", ch, str);
677 getshellbyname(const char * shell)
681 const char * p = strrchr(shell, '/');
684 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
686 int j = strcspn(q, "|");
690 if (strncmp(p, q, j) == 0)
701 * Determine the type of shell our parent process is
702 * This is quite tricky, not 100% reliable and probably
703 * not nearly as thorough as it should be. Basically, this
704 * is a "best guess" only, but hopefully will work in
711 pid_t ppid = getppid();
714 struct kinfo_proc kp;
716 char path[MAXPATHLEN];
717 char * shell = getenv("SHELL");
725 if (shell != NULL && stat(shell, &st) != -1) {
728 mib[2] = KERN_PROC_PATHNAME;
730 if (sysctl(mib, 4, path, &len, NULL, 0) != -1) {
731 /* $SHELL is actual shell? */
732 if (stat(path, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
733 return getshellbyname(shell);
736 mib[2] = KERN_PROC_PID;
738 if (sysctl(mib, 4, &kp, &len, NULL, 0) != -1)
739 return getshellbyname(kp.ki_comm);
745 getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp)
753 name[2] = KERN_PROC_RLIMIT;
757 error = sysctl(name, 5, rlp, &len, NULL, 0);
759 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);
760 if (len != sizeof(*rlp))
761 errx(EXIT_FAILURE, "sysctl() returns wrong size");
765 setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp)
772 name[2] = KERN_PROC_RLIMIT;
775 error = sysctl(name, 5, NULL, 0, rlp, sizeof(*rlp));
777 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);