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 },
93 { " kqueues%-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 },
114 { "csh", "unlimited", "", " -h", "", NULL,
116 { "limit%s cputime %s", ";\n", 1 },
117 { "limit%s filesize %s", ";\n", 1024 },
118 { "limit%s datasize %s", ";\n", 1024 },
119 { "limit%s stacksize %s", ";\n", 1024 },
120 { "limit%s coredumpsize %s", ";\n", 1024 },
121 { "limit%s memoryuse %s", ";\n", 1024 },
122 { "limit%s memorylocked %s", ";\n", 1024 },
123 { "limit%s maxproc %s", ";\n", 1 },
124 { "limit%s openfiles %s", ";\n", 1 },
125 { "limit%s sbsize %s", ";\n", 1 },
126 { "limit%s vmemoryuse %s", ";\n", 1024 },
127 { "limit%s pseudoterminals %s", ";\n", 1 },
128 { "limit%s swapuse %s", ";\n", 1024 },
129 { "limit%s kqueues %s", ";\n", 1 },
132 { "bash|bash2", "unlimited", "", " -H", " -S", "",
134 { "ulimit%s -t %s", ";\n", 1 },
135 { "ulimit%s -f %s", ";\n", 1024 },
136 { "ulimit%s -d %s", ";\n", 1024 },
137 { "ulimit%s -s %s", ";\n", 1024 },
138 { "ulimit%s -c %s", ";\n", 1024 },
139 { "ulimit%s -m %s", ";\n", 1024 },
140 { "ulimit%s -l %s", ";\n", 1024 },
141 { "ulimit%s -u %s", ";\n", 1 },
142 { "ulimit%s -n %s", ";\n", 1 },
143 { "ulimit%s -b %s", ";\n", 1 },
144 { "ulimit%s -v %s", ";\n", 1024 },
145 { "ulimit%s -p %s", ";\n", 1 },
146 { "ulimit%s -w %s", ";\n", 1024 }
149 { "tcsh", "unlimited", "", " -h", "", NULL,
151 { "limit%s cputime %s", ";\n", 1 },
152 { "limit%s filesize %s", ";\n", 1024 },
153 { "limit%s datasize %s", ";\n", 1024 },
154 { "limit%s stacksize %s", ";\n", 1024 },
155 { "limit%s coredumpsize %s", ";\n", 1024 },
156 { "limit%s memoryuse %s", ";\n", 1024 },
157 { "limit%s memorylocked %s", ";\n", 1024 },
158 { "limit%s maxproc %s", ";\n", 1 },
159 { "limit%s descriptors %s", ";\n", 1 },
160 { "limit%s sbsize %s", ";\n", 1 },
161 { "limit%s vmemoryuse %s", ";\n", 1024 },
162 { "limit%s pseudoterminals %s", ";\n", 1 },
163 { "limit%s swapuse %s", ";\n", 1024 },
164 { "limit%s kqueues %s", ";\n", 1 },
167 { "ksh|pdksh", "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 -p %s", ";\n", 1 },
177 { "ulimit%s -n %s", ";\n", 1 },
178 { "ulimit%s -b %s", ";\n", 1 },
179 { "ulimit%s -v %s", ";\n", 1024 },
180 { "ulimit%s -p %s", ";\n", 1 },
181 { "ulimit%s -w %s", ";\n", 1024 }
184 { "zsh", "unlimited", "", " -H", " -S", "",
186 { "ulimit%s -t %s", ";\n", 1 },
187 { "ulimit%s -f %s", ";\n", 512 },
188 { "ulimit%s -d %s", ";\n", 1024 },
189 { "ulimit%s -s %s", ";\n", 1024 },
190 { "ulimit%s -c %s", ";\n", 512 },
191 { "ulimit%s -m %s", ";\n", 1024 },
192 { "ulimit%s -l %s", ";\n", 1024 },
193 { "ulimit%s -u %s", ";\n", 1 },
194 { "ulimit%s -n %s", ";\n", 1 },
195 { "ulimit%s -b %s", ";\n", 1 },
196 { "ulimit%s -v %s", ";\n", 1024 },
197 { "ulimit%s -p %s", ";\n", 1 },
198 { "ulimit%s -w %s", ";\n", 1024 }
201 { "rc|es", "unlimited", "", " -h", "", NULL,
203 { "limit%s cputime %s", ";\n", 1 },
204 { "limit%s filesize %s", ";\n", 1024 },
205 { "limit%s datasize %s", ";\n", 1024 },
206 { "limit%s stacksize %s", ";\n", 1024 },
207 { "limit%s coredumpsize %s", ";\n", 1024 },
208 { "limit%s memoryuse %s", ";\n", 1024 },
209 { "limit%s lockedmemory %s", ";\n", 1024 },
210 { "limit%s processes %s", ";\n", 1 },
211 { "limit%s descriptors %s", ";\n", 1 },
212 { "limit%s sbsize %s", ";\n", 1 },
213 { "limit%s vmemoryuse %s", ";\n", 1024 },
214 { "limit%s pseudoterminals %s", ";\n", 1 },
215 { "limit%s swapuse %s", ";\n", 1024 }
218 { NULL, NULL, NULL, NULL, NULL, NULL,
225 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
226 } resources[RLIM_NLIMITS] = {
227 { "cputime", login_getcaptime },
228 { "filesize", login_getcapsize },
229 { "datasize", login_getcapsize },
230 { "stacksize", login_getcapsize },
231 { "coredumpsize", login_getcapsize },
232 { "memoryuse", login_getcapsize },
233 { "memorylocked", login_getcapsize },
234 { "maxproc", login_getcapnum },
235 { "openfiles", login_getcapnum },
236 { "sbsize", login_getcapsize },
237 { "vmemoryuse", login_getcapsize },
238 { "pseudoterminals",login_getcapnum },
239 { "swapuse", login_getcapsize },
240 { "kqueues", login_getcapnum },
244 * One letter for each resource levels.
245 * NOTE: There is a dependency on the corresponding
246 * letter index being equal to the resource number.
247 * If sys/resource.h defines are changed, this needs
248 * to be modified accordingly!
251 #define RCS_STRING "tfdscmlunbvpw"
253 static rlim_t resource_num(int which, int ch, const char *str);
254 static void usage(void);
255 static int getshelltype(void);
256 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
257 const char *pfx, const char *sfx, const char *which);
258 static void getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp);
259 static void setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp);
260 extern char **environ;
262 static const char rcs_string[] = RCS_STRING;
265 main(int argc, char *argv[])
267 char *p, *cls = NULL;
269 struct passwd * pwd = NULL;
270 int rcswhich, shelltype;
271 int i, num_limits = 0;
272 int ch, doeval = 0, doall = 0;
274 login_cap_t * lc = NULL;
275 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
276 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
277 int which_limits[RLIM_NLIMITS];
278 rlim_t set_limits[RLIM_NLIMITS];
279 struct rlimit limits[RLIM_NLIMITS];
282 /* init resource tables */
283 for (i = 0; i < RLIM_NLIMITS; i++) {
284 which_limits[i] = 0; /* Don't set/display any */
285 set_limits[i] = RLIM_INFINITY;
290 while ((ch = getopt(argc, argv, ":EeC:U:BSHP:ab:c:d:f:l:m:n:s:t:u:v:p:w:")) != -1) {
306 if ((pwd = getpwnam(optarg)) == NULL) {
307 if (!isdigit(*optarg) ||
308 (pwd = getpwuid(atoi(optarg))) == NULL) {
309 warnx("invalid user `%s'", optarg);
324 if (!isdigit(*optarg) || (pid = atoi(optarg)) < 0) {
325 warnx("invalid pid `%s'", optarg);
330 case ':': /* Without arg */
331 if ((p = strchr(rcs_string, optopt)) != NULL) {
332 int rcswhich1 = p - rcs_string;
333 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
334 --optind; /* back one arg, and make arg NULL */
337 todo = optarg == NULL ? RCSSEL : RCSSET;
340 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
341 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
354 warnx("-C cannot be used with -P option");
358 warnx("-U cannot be used with -P option");
363 /* Get current resource values */
365 for (i = 0; i < RLIM_NLIMITS; i++) {
367 getrlimit(i, &limits[i]);
368 } else if (doall || num_limits == 0) {
369 getrlimit_proc(pid, i, &limits[i]);
370 } else if (which_limits[i] != 0) {
371 getrlimit_proc(pid, i, &limits[i]);
376 /* If user was specified, get class from that */
378 lc = login_getpwclass(pwd);
379 else if (cls != NULL && *cls != '\0') {
380 lc = login_getclassbyname(cls, NULL);
381 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
382 fprintf(stderr, "login class '%s' non-existent, using %s\n",
383 cls, lc?lc->lc_class:"current settings");
386 /* If we have a login class, update resource table from that */
388 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
392 /* current value overridden by resourcename or resourcename-cur */
393 sprintf(str, "%s-cur", resources[rcswhich].cap);
394 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
395 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
396 /* maximum value overridden by resourcename or resourcename-max */
397 sprintf(str, "%s-max", resources[rcswhich].cap);
398 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
399 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
403 /* now, let's determine what we wish to do with all this */
407 /* If we're setting limits or doing an eval (ie. we're not just
408 * displaying), then check that hard limits are not lower than
409 * soft limits, and force rasing the hard limit if we need to if
410 * we are raising the soft limit, or lower the soft limit if we
411 * are lowering the hard limit.
413 if ((*argv || doeval) && getuid() == 0) {
415 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
416 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
417 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
418 limits[rcswhich].rlim_max = RLIM_INFINITY;
419 which_limits[rcswhich] |= HARD;
420 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
421 if (which_limits[rcswhich] == SOFT) {
422 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
423 which_limits[rcswhich] |= HARD;
424 } else if (which_limits[rcswhich] == HARD) {
425 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
426 which_limits[rcswhich] |= SOFT;
428 /* else.. if we're specifically setting both to
429 * silly values, then let it error out.
437 /* See if we've overridden anything specific on the command line */
438 if (num_limits && todo == RCSSET) {
439 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
440 if (which_limits[rcswhich] & HARD)
441 limits[rcswhich].rlim_max = set_limits[rcswhich];
442 if (which_limits[rcswhich] & SOFT)
443 limits[rcswhich].rlim_cur = set_limits[rcswhich];
447 /* If *argv is not NULL, then we are being asked to
448 * (perhaps) set environment variables and run a program
452 warnx("-e cannot be used with `cmd' option");
456 warnx("-P cannot be used with `cmd' option");
462 /* set leading environment variables, like eval(1) */
463 while (*argv && (p = strchr(*argv, '='))) {
465 rtrn = setenv(*argv++, p + 1, 1);
468 err(EXIT_FAILURE, "setenv %s", *argv);
472 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
473 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
474 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
475 err(1, "setrlimit %s", resources[rcswhich].cap);
486 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
487 if (which_limits[rcswhich] != 0)
488 setrlimit_proc(pid, rcswhich, &limits[rcswhich]);
493 shelltype = doeval ? getshelltype() : SH_NONE;
495 if (type == ANY) /* Default to soft limits */
499 printf(shellparm[shelltype].cmd,
500 lc ? " for class " : " (current)",
501 lc ? lc->lc_class : "");
503 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
504 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
505 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
506 which_limits[rcswhich] = type;
507 if (shellparm[shelltype].lprm[rcswhich].pfx) {
508 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
509 print_limit(limits[rcswhich].rlim_max,
510 shellparm[shelltype].lprm[rcswhich].divisor,
511 shellparm[shelltype].inf,
512 shellparm[shelltype].lprm[rcswhich].pfx,
513 shellparm[shelltype].lprm[rcswhich].sfx,
514 shellparm[shelltype].both);
516 if (which_limits[rcswhich] & HARD) {
517 print_limit(limits[rcswhich].rlim_max,
518 shellparm[shelltype].lprm[rcswhich].divisor,
519 shellparm[shelltype].inf,
520 shellparm[shelltype].lprm[rcswhich].pfx,
521 shellparm[shelltype].lprm[rcswhich].sfx,
522 shellparm[shelltype].hard);
524 if (which_limits[rcswhich] & SOFT) {
525 print_limit(limits[rcswhich].rlim_cur,
526 shellparm[shelltype].lprm[rcswhich].divisor,
527 shellparm[shelltype].inf,
528 shellparm[shelltype].lprm[rcswhich].pfx,
529 shellparm[shelltype].lprm[rcswhich].sfx,
530 shellparm[shelltype].soft);
545 (void)fprintf(stderr,
546 "usage: limits [-C class|-P pid|-U user] [-eaSHBE] "
547 "[-bcdflmnstuvpw [val]] [[name=val ...] cmd]\n");
552 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
556 if (limit == RLIM_INFINITY)
559 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
560 printf(pfx, which, numbr);
567 resource_num(int which, int ch, const char *str)
569 rlim_t res = RLIM_INFINITY;
572 !(strcasecmp(str, "inf") == 0 ||
573 strcasecmp(str, "infinity") == 0 ||
574 strcasecmp(str, "unlimit") == 0 ||
575 strcasecmp(str, "unlimited") == 0)) {
576 const char * s = str;
580 case RLIMIT_CPU: /* time values */
584 rlim_t tim = strtoq(s, &e, 0);
585 if (e == NULL || e == s || errno)
588 case 0: /* end of string */
591 case 's': case 'S': /* seconds */
593 case 'm': case 'M': /* minutes */
596 case 'h': case 'H': /* hours */
599 case 'd': case 'D': /* days */
600 tim *= (60L * 60L * 24L);
602 case 'w': case 'W': /* weeks */
603 tim *= (60L * 60L * 24L * 7L);
604 case 'y': case 'Y': /* Years */
605 tim *= (60L * 60L * 24L * 365L);
611 case RLIMIT_FSIZE: /* Size values */
623 rlim_t mult, tim = strtoq(s, &e, 0);
624 if (e == NULL || e == s || errno)
627 case 0: /* end of string */
632 case 'b': case 'B': /* 512-byte blocks */
635 case 'k': case 'K': /* 1024-byte Kilobytes */
638 case 'm': case 'M': /* 1024-k kbytes */
641 case 'g': case 'G': /* 1Gbyte */
642 mult = 1024 * 1024 * 1024;
644 case 't': case 'T': /* 1TBte */
645 mult = 1024LL * 1024LL * 1024LL * 1024LL;
656 res = strtoq(s, &e, 0);
661 warnx("invalid value -%c `%s'", ch, str);
670 getshellbyname(const char * shell)
674 const char * p = strrchr(shell, '/');
677 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
679 int j = strcspn(q, "|");
683 if (strncmp(p, q, j) == 0)
694 * Determine the type of shell our parent process is
695 * This is quite tricky, not 100% reliable and probably
696 * not nearly as thorough as it should be. Basically, this
697 * is a "best guess" only, but hopefully will work in
704 pid_t ppid = getppid();
709 char procdir[MAXPATHLEN], buf[128];
710 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
711 char * shell = getenv("SHELL");
713 if (shell != NULL && stat(shell, &st) != -1) {
716 strcpy(procdir+l, "file");
717 /* $SHELL is actual shell? */
718 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
719 return getshellbyname(shell);
721 strcpy(procdir+l, "status");
722 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
723 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
726 return getshellbyname(p);
733 getrlimit_proc(pid_t pid, int resource, struct rlimit *rlp)
741 name[2] = KERN_PROC_RLIMIT;
745 error = sysctl(name, 5, rlp, &len, NULL, 0);
747 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);
748 if (len != sizeof(*rlp))
749 errx(EXIT_FAILURE, "sysctl() returns wrong size");
753 setrlimit_proc(pid_t pid, int resource, const struct rlimit *rlp)
760 name[2] = KERN_PROC_RLIMIT;
763 error = sysctl(name, 5, NULL, 0, rlp, sizeof(*rlp));
765 err(EXIT_FAILURE, "sysctl: kern.proc.rlimit: %d", pid);