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>
33 #include <sys/param.h>
41 #include <login_cap.h>
43 #include <sys/resource.h>
50 SH_BASH, /* gnu bash */
59 /* eval emitter for popular shells.
60 * Why aren't there any standards here? Most shells support either
61 * the csh 'limit' or sh 'ulimit' command, but each varies just
62 * enough that they aren't very compatible from one to the other.
65 const char * name; /* Name of shell */
66 const char * inf; /* Name used for 'unlimited' resource */
67 const char * cmd; /* Intro text */
68 const char * hard; /* Hard limit text */
69 const char * soft; /* Soft limit text */
70 const char * both; /* Hard+Soft limit text */
78 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
80 { " cputime%-4s %8s", " secs\n", 1 },
81 { " filesize%-4s %8s", " kB\n", 1024 },
82 { " datasize%-4s %8s", " kB\n", 1024 },
83 { " stacksize%-4s %8s", " kB\n", 1024 },
84 { " coredumpsize%-4s %8s", " kB\n", 1024 },
85 { " memoryuse%-4s %8s", " kB\n", 1024 },
86 { " memorylocked%-4s %8s", " kB\n", 1024 },
87 { " maxprocesses%-4s %8s", "\n", 1 },
88 { " openfiles%-4s %8s", "\n", 1 },
89 { " sbsize%-4s %8s", " bytes\n", 1 },
90 { " vmemoryuse%-4s %8s", " kB\n", 1024 },
91 { " pseudo-terminals%-4s %8s", "\n", 1 },
92 { " swapuse%-4s %8s", " kB\n", 1024 }
95 { "sh", "unlimited", "", " -H", " -S", "",
97 { "ulimit%s -t %s", ";\n", 1 },
98 { "ulimit%s -f %s", ";\n", 512 },
99 { "ulimit%s -d %s", ";\n", 1024 },
100 { "ulimit%s -s %s", ";\n", 1024 },
101 { "ulimit%s -c %s", ";\n", 512 },
102 { "ulimit%s -m %s", ";\n", 1024 },
103 { "ulimit%s -l %s", ";\n", 1024 },
104 { "ulimit%s -u %s", ";\n", 1 },
105 { "ulimit%s -n %s", ";\n", 1 },
106 { "ulimit%s -b %s", ";\n", 1 },
107 { "ulimit%s -v %s", ";\n", 1024 },
108 { "ulimit%s -p %s", ";\n", 1 },
109 { "ulimit%s -w %s", ";\n", 1024 }
112 { "csh", "unlimited", "", " -h", "", NULL,
114 { "limit%s cputime %s", ";\n", 1 },
115 { "limit%s filesize %s", ";\n", 1024 },
116 { "limit%s datasize %s", ";\n", 1024 },
117 { "limit%s stacksize %s", ";\n", 1024 },
118 { "limit%s coredumpsize %s", ";\n", 1024 },
119 { "limit%s memoryuse %s", ";\n", 1024 },
120 { "limit%s memorylocked %s", ";\n", 1024 },
121 { "limit%s maxproc %s", ";\n", 1 },
122 { "limit%s openfiles %s", ";\n", 1 },
123 { "limit%s sbsize %s", ";\n", 1 },
124 { "limit%s vmemoryuse %s", ";\n", 1024 },
125 { "limit%s pseudoterminals %s", ";\n", 1 },
126 { "limit%s swapuse %s", ";\n", 1024 }
129 { "bash|bash2", "unlimited", "", " -H", " -S", "",
131 { "ulimit%s -t %s", ";\n", 1 },
132 { "ulimit%s -f %s", ";\n", 1024 },
133 { "ulimit%s -d %s", ";\n", 1024 },
134 { "ulimit%s -s %s", ";\n", 1024 },
135 { "ulimit%s -c %s", ";\n", 1024 },
136 { "ulimit%s -m %s", ";\n", 1024 },
137 { "ulimit%s -l %s", ";\n", 1024 },
138 { "ulimit%s -u %s", ";\n", 1 },
139 { "ulimit%s -n %s", ";\n", 1 },
140 { "ulimit%s -b %s", ";\n", 1 },
141 { "ulimit%s -v %s", ";\n", 1024 },
142 { "ulimit%s -p %s", ";\n", 1 },
143 { "ulimit%s -w %s", ";\n", 1024 }
146 { "tcsh", "unlimited", "", " -h", "", NULL,
148 { "limit%s cputime %s", ";\n", 1 },
149 { "limit%s filesize %s", ";\n", 1024 },
150 { "limit%s datasize %s", ";\n", 1024 },
151 { "limit%s stacksize %s", ";\n", 1024 },
152 { "limit%s coredumpsize %s", ";\n", 1024 },
153 { "limit%s memoryuse %s", ";\n", 1024 },
154 { "limit%s memorylocked %s", ";\n", 1024 },
155 { "limit%s maxproc %s", ";\n", 1 },
156 { "limit%s descriptors %s", ";\n", 1 },
157 { "limit%s sbsize %s", ";\n", 1 },
158 { "limit%s vmemoryuse %s", ";\n", 1024 },
159 { "limit%s pseudoterminals %s", ";\n", 1 },
160 { "limit%s swapuse %s", ";\n", 1024 }
163 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
165 { "ulimit%s -t %s", ";\n", 1 },
166 { "ulimit%s -f %s", ";\n", 512 },
167 { "ulimit%s -d %s", ";\n", 1024 },
168 { "ulimit%s -s %s", ";\n", 1024 },
169 { "ulimit%s -c %s", ";\n", 512 },
170 { "ulimit%s -m %s", ";\n", 1024 },
171 { "ulimit%s -l %s", ";\n", 1024 },
172 { "ulimit%s -p %s", ";\n", 1 },
173 { "ulimit%s -n %s", ";\n", 1 },
174 { "ulimit%s -b %s", ";\n", 1 },
175 { "ulimit%s -v %s", ";\n", 1024 },
176 { "ulimit%s -p %s", ";\n", 1 },
177 { "ulimit%s -w %s", ";\n", 1024 }
180 { "zsh", "unlimited", "", " -H", " -S", "",
182 { "ulimit%s -t %s", ";\n", 1 },
183 { "ulimit%s -f %s", ";\n", 512 },
184 { "ulimit%s -d %s", ";\n", 1024 },
185 { "ulimit%s -s %s", ";\n", 1024 },
186 { "ulimit%s -c %s", ";\n", 512 },
187 { "ulimit%s -m %s", ";\n", 1024 },
188 { "ulimit%s -l %s", ";\n", 1024 },
189 { "ulimit%s -u %s", ";\n", 1 },
190 { "ulimit%s -n %s", ";\n", 1 },
191 { "ulimit%s -b %s", ";\n", 1 },
192 { "ulimit%s -v %s", ";\n", 1024 },
193 { "ulimit%s -p %s", ";\n", 1 },
194 { "ulimit%s -w %s", ";\n", 1024 }
197 { "rc|es", "unlimited", "", " -h", "", NULL,
199 { "limit%s cputime %s", ";\n", 1 },
200 { "limit%s filesize %s", ";\n", 1024 },
201 { "limit%s datasize %s", ";\n", 1024 },
202 { "limit%s stacksize %s", ";\n", 1024 },
203 { "limit%s coredumpsize %s", ";\n", 1024 },
204 { "limit%s memoryuse %s", ";\n", 1024 },
205 { "limit%s lockedmemory %s", ";\n", 1024 },
206 { "limit%s processes %s", ";\n", 1 },
207 { "limit%s descriptors %s", ";\n", 1 },
208 { "limit%s sbsize %s", ";\n", 1 },
209 { "limit%s vmemoryuse %s", ";\n", 1024 },
210 { "limit%s pseudoterminals %s", ";\n", 1 },
211 { "limit%s swapuse %s", ";\n", 1024 }
214 { NULL, NULL, NULL, NULL, NULL, NULL,
221 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
222 } resources[RLIM_NLIMITS] = {
223 { "cputime", login_getcaptime },
224 { "filesize", login_getcapsize },
225 { "datasize", login_getcapsize },
226 { "stacksize", login_getcapsize },
227 { "coredumpsize", login_getcapsize },
228 { "memoryuse", login_getcapsize },
229 { "memorylocked", login_getcapsize },
230 { "maxproc", login_getcapnum },
231 { "openfiles", login_getcapnum },
232 { "sbsize", login_getcapsize },
233 { "vmemoryuse", login_getcapsize },
234 { "pseudoterminals",login_getcapnum },
235 { "swapuse", login_getcapsize }
239 * One letter for each resource levels.
240 * NOTE: There is a dependancy on the corresponding
241 * letter index being equal to the resource number.
242 * If sys/resource.h defines are changed, this needs
243 * to be modified accordingly!
246 #define RCS_STRING "tfdscmlunbvpw"
248 static rlim_t resource_num(int which, int ch, const char *str);
249 static void usage(void);
250 static int getshelltype(void);
251 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
252 const char *pfx, const char *sfx, const char *which);
253 static void getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp);
254 static void setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp);
255 extern char **environ;
257 static const char rcs_string[] = RCS_STRING;
260 main(int argc, char *argv[])
262 char *p, *cls = NULL;
264 struct passwd * pwd = NULL;
265 int rcswhich, shelltype;
266 int i, num_limits = 0;
267 int ch, doeval = 0, doall = 0;
269 login_cap_t * lc = NULL;
270 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
271 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
272 int which_limits[RLIM_NLIMITS];
273 rlim_t set_limits[RLIM_NLIMITS];
274 struct rlimit limits[RLIM_NLIMITS];
277 /* init resource tables */
278 for (i = 0; i < RLIM_NLIMITS; i++) {
279 which_limits[i] = 0; /* Don't set/display any */
280 set_limits[i] = RLIM_INFINITY;
285 while ((ch = getopt(argc, argv, ":EeC:U:BSHP:ab:c:d:f:l:m:n:s:t:u:v:p:w:")) != -1) {
301 if ((pwd = getpwnam(optarg)) == NULL) {
302 if (!isdigit(*optarg) ||
303 (pwd = getpwuid(atoi(optarg))) == NULL) {
304 warnx("invalid user `%s'", optarg);
319 if (!isdigit(*optarg) || (pid = atoi(optarg)) < 0) {
320 warnx("invalid pid `%s'", optarg);
325 case ':': /* Without arg */
326 if ((p = strchr(rcs_string, optopt)) != NULL) {
327 int rcswhich1 = p - rcs_string;
328 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
329 --optind; /* back one arg, and make arg NULL */
332 todo = optarg == NULL ? RCSSEL : RCSSET;
335 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
336 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
349 warnx("-C cannot be used with -P option");
353 warnx("-U cannot be used with -P option");
358 /* Get current resource values */
360 for (i = 0; i < RLIM_NLIMITS; i++) {
362 getrlimit(i, &limits[i]);
363 } else if (doall || num_limits == 0) {
364 getrlimit_proc(pid, i, &limits[i]);
365 } else if (which_limits[i] != 0) {
366 getrlimit_proc(pid, i, &limits[i]);
371 /* If user was specified, get class from that */
373 lc = login_getpwclass(pwd);
374 else if (cls != NULL && *cls != '\0') {
375 lc = login_getclassbyname(cls, NULL);
376 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
377 fprintf(stderr, "login class '%s' non-existent, using %s\n",
378 cls, lc?lc->lc_class:"current settings");
381 /* If we have a login class, update resource table from that */
383 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
387 /* current value overridden by resourcename or resourcename-cur */
388 sprintf(str, "%s-cur", resources[rcswhich].cap);
389 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
390 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
391 /* maximum value overridden by resourcename or resourcename-max */
392 sprintf(str, "%s-max", resources[rcswhich].cap);
393 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
394 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
398 /* now, let's determine what we wish to do with all this */
402 /* If we're setting limits or doing an eval (ie. we're not just
403 * displaying), then check that hard limits are not lower than
404 * soft limits, and force rasing the hard limit if we need to if
405 * we are raising the soft limit, or lower the soft limit if we
406 * are lowering the hard limit.
408 if ((*argv || doeval) && getuid() == 0) {
410 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
411 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
412 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
413 limits[rcswhich].rlim_max = RLIM_INFINITY;
414 which_limits[rcswhich] |= HARD;
415 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
416 if (which_limits[rcswhich] == SOFT) {
417 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
418 which_limits[rcswhich] |= HARD;
419 } else if (which_limits[rcswhich] == HARD) {
420 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
421 which_limits[rcswhich] |= SOFT;
423 /* else.. if we're specifically setting both to
424 * silly values, then let it error out.
432 /* See if we've overridden anything specific on the command line */
433 if (num_limits && todo == RCSSET) {
434 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
435 if (which_limits[rcswhich] & HARD)
436 limits[rcswhich].rlim_max = set_limits[rcswhich];
437 if (which_limits[rcswhich] & SOFT)
438 limits[rcswhich].rlim_cur = set_limits[rcswhich];
442 /* If *argv is not NULL, then we are being asked to
443 * (perhaps) set environment variables and run a program
447 warnx("-e cannot be used with `cmd' option");
451 warnx("-P cannot be used with `cmd' option");
457 /* set leading environment variables, like eval(1) */
458 while (*argv && (p = strchr(*argv, '='))) {
460 rtrn = setenv(*argv++, p + 1, 1);
463 err(EXIT_FAILURE, "setenv %s", *argv);
467 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
468 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
469 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
470 err(1, "setrlimit %s", resources[rcswhich].cap);
481 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
482 if (which_limits[rcswhich] != 0)
483 setrlimit_proc(pid, rcswhich, &limits[rcswhich]);
488 shelltype = doeval ? getshelltype() : SH_NONE;
490 if (type == ANY) /* Default to soft limits */
494 printf(shellparm[shelltype].cmd,
495 lc ? " for class " : " (current)",
496 lc ? lc->lc_class : "");
498 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
499 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
500 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
501 which_limits[rcswhich] = type;
502 if (shellparm[shelltype].lprm[rcswhich].pfx) {
503 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
504 print_limit(limits[rcswhich].rlim_max,
505 shellparm[shelltype].lprm[rcswhich].divisor,
506 shellparm[shelltype].inf,
507 shellparm[shelltype].lprm[rcswhich].pfx,
508 shellparm[shelltype].lprm[rcswhich].sfx,
509 shellparm[shelltype].both);
511 if (which_limits[rcswhich] & HARD) {
512 print_limit(limits[rcswhich].rlim_max,
513 shellparm[shelltype].lprm[rcswhich].divisor,
514 shellparm[shelltype].inf,
515 shellparm[shelltype].lprm[rcswhich].pfx,
516 shellparm[shelltype].lprm[rcswhich].sfx,
517 shellparm[shelltype].hard);
519 if (which_limits[rcswhich] & SOFT) {
520 print_limit(limits[rcswhich].rlim_cur,
521 shellparm[shelltype].lprm[rcswhich].divisor,
522 shellparm[shelltype].inf,
523 shellparm[shelltype].lprm[rcswhich].pfx,
524 shellparm[shelltype].lprm[rcswhich].sfx,
525 shellparm[shelltype].soft);
540 (void)fprintf(stderr,
541 "usage: limits [-C class|-P pid|-U user] [-eaSHBE] "
542 "[-bcdflmnstuvpw [val]] [[name=val ...] cmd]\n");
547 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
551 if (limit == RLIM_INFINITY)
554 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
555 printf(pfx, which, numbr);
562 resource_num(int which, int ch, const char *str)
564 rlim_t res = RLIM_INFINITY;
567 !(strcasecmp(str, "inf") == 0 ||
568 strcasecmp(str, "infinity") == 0 ||
569 strcasecmp(str, "unlimit") == 0 ||
570 strcasecmp(str, "unlimited") == 0)) {
571 const char * s = str;
575 case RLIMIT_CPU: /* time values */
579 rlim_t tim = strtoq(s, &e, 0);
580 if (e == NULL || e == s || errno)
583 case 0: /* end of string */
586 case 's': case 'S': /* seconds */
588 case 'm': case 'M': /* minutes */
591 case 'h': case 'H': /* hours */
594 case 'd': case 'D': /* days */
595 tim *= (60L * 60L * 24L);
597 case 'w': case 'W': /* weeks */
598 tim *= (60L * 60L * 24L * 7L);
599 case 'y': case 'Y': /* Years */
600 tim *= (60L * 60L * 24L * 365L);
606 case RLIMIT_FSIZE: /* Size values */
618 rlim_t mult, tim = strtoq(s, &e, 0);
619 if (e == NULL || e == s || errno)
622 case 0: /* end of string */
627 case 'b': case 'B': /* 512-byte blocks */
630 case 'k': case 'K': /* 1024-byte Kilobytes */
633 case 'm': case 'M': /* 1024-k kbytes */
636 case 'g': case 'G': /* 1Gbyte */
637 mult = 1024 * 1024 * 1024;
639 case 't': case 'T': /* 1TBte */
640 mult = 1024LL * 1024LL * 1024LL * 1024LL;
650 res = strtoq(s, &e, 0);
655 warnx("invalid value -%c `%s'", ch, str);
664 getshellbyname(const char * shell)
668 const char * p = strrchr(shell, '/');
671 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
673 int j = strcspn(q, "|");
677 if (strncmp(p, q, j) == 0)
688 * Determine the type of shell our parent process is
689 * This is quite tricky, not 100% reliable and probably
690 * not nearly as thorough as it should be. Basically, this
691 * is a "best guess" only, but hopefully will work in
698 pid_t ppid = getppid();
703 char procdir[MAXPATHLEN], buf[128];
704 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
705 char * shell = getenv("SHELL");
707 if (shell != NULL && stat(shell, &st) != -1) {
710 strcpy(procdir+l, "file");
711 /* $SHELL is actual shell? */
712 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
713 return getshellbyname(shell);
715 strcpy(procdir+l, "status");
716 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
717 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
720 return getshellbyname(p);
727 getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp)
735 name[2] = KERN_PROC_RLIMIT;
739 error = sysctl(name, 5, rlp, &len, NULL, 0);
741 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);
742 if (len != sizeof(*rlp))
743 errx(EXIT_FAILURE, "sysctl() returns wrong size");
747 setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp)
754 name[2] = KERN_PROC_RLIMIT;
757 error = sysctl(name, 5, NULL, 0, rlp, sizeof(*rlp));
759 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);