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 },
90 { " pseudo-terminals%-4s %8s", "\n", 1 }
93 { "sh", "unlimited", "", " -H", " -S", "",
95 { "ulimit%s -t %s", ";\n", 1 },
96 { "ulimit%s -f %s", ";\n", 512 },
97 { "ulimit%s -d %s", ";\n", 1024 },
98 { "ulimit%s -s %s", ";\n", 1024 },
99 { "ulimit%s -c %s", ";\n", 512 },
100 { "ulimit%s -m %s", ";\n", 1024 },
101 { "ulimit%s -l %s", ";\n", 1024 },
102 { "ulimit%s -u %s", ";\n", 1 },
103 { "ulimit%s -n %s", ";\n", 1 },
104 { "ulimit%s -b %s", ";\n", 1 },
105 { "ulimit%s -v %s", ";\n", 1024 },
106 { "ulimit%s -p %s", ";\n", 1 }
109 { "csh", "unlimited", "", " -h", "", NULL,
111 { "limit%s cputime %s", ";\n", 1 },
112 { "limit%s filesize %s", ";\n", 1024 },
113 { "limit%s datasize %s", ";\n", 1024 },
114 { "limit%s stacksize %s", ";\n", 1024 },
115 { "limit%s coredumpsize %s", ";\n", 1024 },
116 { "limit%s memoryuse %s", ";\n", 1024 },
117 { "limit%s memorylocked %s", ";\n", 1024 },
118 { "limit%s maxproc %s", ";\n", 1 },
119 { "limit%s openfiles %s", ";\n", 1 },
120 { "limit%s sbsize %s", ";\n", 1 },
121 { "limit%s vmemoryuse %s", ";\n", 1024 },
122 { "limit%s pseudoterminals %s", ";\n", 1 }
125 { "bash|bash2", "unlimited", "", " -H", " -S", "",
127 { "ulimit%s -t %s", ";\n", 1 },
128 { "ulimit%s -f %s", ";\n", 1024 },
129 { "ulimit%s -d %s", ";\n", 1024 },
130 { "ulimit%s -s %s", ";\n", 1024 },
131 { "ulimit%s -c %s", ";\n", 1024 },
132 { "ulimit%s -m %s", ";\n", 1024 },
133 { "ulimit%s -l %s", ";\n", 1024 },
134 { "ulimit%s -u %s", ";\n", 1 },
135 { "ulimit%s -n %s", ";\n", 1 },
136 { "ulimit%s -b %s", ";\n", 1 },
137 { "ulimit%s -v %s", ";\n", 1024 },
138 { "ulimit%s -p %s", ";\n", 1 }
141 { "tcsh", "unlimited", "", " -h", "", NULL,
143 { "limit%s cputime %s", ";\n", 1 },
144 { "limit%s filesize %s", ";\n", 1024 },
145 { "limit%s datasize %s", ";\n", 1024 },
146 { "limit%s stacksize %s", ";\n", 1024 },
147 { "limit%s coredumpsize %s", ";\n", 1024 },
148 { "limit%s memoryuse %s", ";\n", 1024 },
149 { "limit%s memorylocked %s", ";\n", 1024 },
150 { "limit%s maxproc %s", ";\n", 1 },
151 { "limit%s descriptors %s", ";\n", 1 },
152 { "limit%s sbsize %s", ";\n", 1 },
153 { "limit%s vmemoryuse %s", ";\n", 1024 },
154 { "limit%s pseudoterminals %s", ";\n", 1 }
157 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
159 { "ulimit%s -t %s", ";\n", 1 },
160 { "ulimit%s -f %s", ";\n", 512 },
161 { "ulimit%s -d %s", ";\n", 1024 },
162 { "ulimit%s -s %s", ";\n", 1024 },
163 { "ulimit%s -c %s", ";\n", 512 },
164 { "ulimit%s -m %s", ";\n", 1024 },
165 { "ulimit%s -l %s", ";\n", 1024 },
166 { "ulimit%s -p %s", ";\n", 1 },
167 { "ulimit%s -n %s", ";\n", 1 },
168 { "ulimit%s -b %s", ";\n", 1 },
169 { "ulimit%s -v %s", ";\n", 1024 },
170 { "ulimit%s -p %s", ";\n", 1 }
173 { "zsh", "unlimited", "", " -H", " -S", "",
175 { "ulimit%s -t %s", ";\n", 1 },
176 { "ulimit%s -f %s", ";\n", 512 },
177 { "ulimit%s -d %s", ";\n", 1024 },
178 { "ulimit%s -s %s", ";\n", 1024 },
179 { "ulimit%s -c %s", ";\n", 512 },
180 { "ulimit%s -m %s", ";\n", 1024 },
181 { "ulimit%s -l %s", ";\n", 1024 },
182 { "ulimit%s -u %s", ";\n", 1 },
183 { "ulimit%s -n %s", ";\n", 1 },
184 { "ulimit%s -b %s", ";\n", 1 },
185 { "ulimit%s -v %s", ";\n", 1024 },
186 { "ulimit%s -p %s", ";\n", 1 }
189 { "rc|es", "unlimited", "", " -h", "", NULL,
191 { "limit%s cputime %s", ";\n", 1 },
192 { "limit%s filesize %s", ";\n", 1024 },
193 { "limit%s datasize %s", ";\n", 1024 },
194 { "limit%s stacksize %s", ";\n", 1024 },
195 { "limit%s coredumpsize %s", ";\n", 1024 },
196 { "limit%s memoryuse %s", ";\n", 1024 },
197 { "limit%s lockedmemory %s", ";\n", 1024 },
198 { "limit%s processes %s", ";\n", 1 },
199 { "limit%s descriptors %s", ";\n", 1 },
200 { "limit%s sbsize %s", ";\n", 1 },
201 { "limit%s vmemoryuse %s", ";\n", 1024 },
202 { "limit%s pseudoterminals %s", ";\n", 1 }
205 { NULL, NULL, NULL, NULL, NULL, NULL,
212 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
213 } resources[RLIM_NLIMITS] = {
214 { "cputime", login_getcaptime },
215 { "filesize", login_getcapsize },
216 { "datasize", login_getcapsize },
217 { "stacksize", login_getcapsize },
218 { "coredumpsize", login_getcapsize },
219 { "memoryuse", login_getcapsize },
220 { "memorylocked", login_getcapsize },
221 { "maxproc", login_getcapnum },
222 { "openfiles", login_getcapnum },
223 { "sbsize", login_getcapsize },
224 { "vmemoryuse", login_getcapsize },
225 { "pseudoterminals",login_getcapnum },
229 * One letter for each resource levels.
230 * NOTE: There is a dependancy on the corresponding
231 * letter index being equal to the resource number.
232 * If sys/resource.h defines are changed, this needs
233 * to be modified accordingly!
236 #define RCS_STRING "tfdscmlunbvp"
238 static rlim_t resource_num(int which, int ch, const char *str);
239 static void usage(void);
240 static int getshelltype(void);
241 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
242 const char *pfx, const char *sfx, const char *which);
243 extern char **environ;
245 static const char rcs_string[] = RCS_STRING;
248 main(int argc, char *argv[])
250 char *p, *cls = NULL;
252 struct passwd * pwd = NULL;
253 int rcswhich, shelltype;
254 int i, num_limits = 0;
255 int ch, doeval = 0, doall = 0;
257 login_cap_t * lc = NULL;
258 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
259 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
260 int which_limits[RLIM_NLIMITS];
261 rlim_t set_limits[RLIM_NLIMITS];
262 struct rlimit limits[RLIM_NLIMITS];
264 /* init resource tables */
265 for (i = 0; i < RLIM_NLIMITS; i++) {
266 which_limits[i] = 0; /* Don't set/display any */
267 set_limits[i] = RLIM_INFINITY;
268 /* Get current resource values */
269 getrlimit(i, &limits[i]);
273 while ((ch = getopt(argc, argv, ":EeC:U:BSHab:c:d:f:l:m:n:s:t:u:v:p:")) != -1) {
289 if ((pwd = getpwnam(optarg)) == NULL) {
290 if (!isdigit(*optarg) ||
291 (pwd = getpwuid(atoi(optarg))) == NULL) {
292 warnx("invalid user `%s'", optarg);
307 case ':': /* Without arg */
308 if ((p = strchr(rcs_string, optopt)) != NULL) {
309 int rcswhich1 = p - rcs_string;
310 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
311 --optind; /* back one arg, and make arg NULL */
314 todo = optarg == NULL ? RCSSEL : RCSSET;
317 which_limits[rcswhich1] = optarg ? type : DISPLAYONLY;
318 set_limits[rcswhich1] = resource_num(rcswhich1, optopt, optarg);
329 /* If user was specified, get class from that */
331 lc = login_getpwclass(pwd);
332 else if (cls != NULL && *cls != '\0') {
333 lc = login_getclassbyname(cls, NULL);
334 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
335 fprintf(stderr, "login class '%s' non-existent, using %s\n",
336 cls, lc?lc->lc_class:"current settings");
339 /* If we have a login class, update resource table from that */
341 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
345 /* current value overridden by resourcename or resourcename-cur */
346 sprintf(str, "%s-cur", resources[rcswhich].cap);
347 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
348 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
349 /* maximum value overridden by resourcename or resourcename-max */
350 sprintf(str, "%s-max", resources[rcswhich].cap);
351 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
352 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
356 /* now, let's determine what we wish to do with all this */
360 /* If we're setting limits or doing an eval (ie. we're not just
361 * displaying), then check that hard limits are not lower than
362 * soft limits, and force rasing the hard limit if we need to if
363 * we are raising the soft limit, or lower the soft limit if we
364 * are lowering the hard limit.
366 if ((*argv || doeval) && getuid() == 0) {
368 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
369 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
370 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
371 limits[rcswhich].rlim_max = RLIM_INFINITY;
372 which_limits[rcswhich] |= HARD;
373 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
374 if (which_limits[rcswhich] == SOFT) {
375 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
376 which_limits[rcswhich] |= HARD;
377 } else if (which_limits[rcswhich] == HARD) {
378 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
379 which_limits[rcswhich] |= SOFT;
381 /* else.. if we're specifically setting both to
382 * silly values, then let it error out.
390 /* See if we've overridden anything specific on the command line */
391 if (num_limits && todo == RCSSET) {
392 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
393 if (which_limits[rcswhich] & HARD)
394 limits[rcswhich].rlim_max = set_limits[rcswhich];
395 if (which_limits[rcswhich] & SOFT)
396 limits[rcswhich].rlim_cur = set_limits[rcswhich];
400 /* If *argv is not NULL, then we are being asked to
401 * (perhaps) set environment variables and run a program
405 warnx("-e cannot be used with `cmd' option");
411 /* set leading environment variables, like eval(1) */
412 while (*argv && (p = strchr(*argv, '='))) {
414 rtrn = setenv(*argv++, p + 1, 1);
417 err(EXIT_FAILURE, "setenv %s", *argv);
421 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
422 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
423 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
424 err(1, "setrlimit %s", resources[rcswhich].cap);
434 shelltype = doeval ? getshelltype() : SH_NONE;
436 if (type == ANY) /* Default to soft limits */
440 printf(shellparm[shelltype].cmd,
441 lc ? " for class " : " (current)",
442 lc ? lc->lc_class : "");
444 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
445 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
446 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
447 which_limits[rcswhich] = type;
448 if (shellparm[shelltype].lprm[rcswhich].pfx) {
449 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
450 print_limit(limits[rcswhich].rlim_max,
451 shellparm[shelltype].lprm[rcswhich].divisor,
452 shellparm[shelltype].inf,
453 shellparm[shelltype].lprm[rcswhich].pfx,
454 shellparm[shelltype].lprm[rcswhich].sfx,
455 shellparm[shelltype].both);
457 if (which_limits[rcswhich] & HARD) {
458 print_limit(limits[rcswhich].rlim_max,
459 shellparm[shelltype].lprm[rcswhich].divisor,
460 shellparm[shelltype].inf,
461 shellparm[shelltype].lprm[rcswhich].pfx,
462 shellparm[shelltype].lprm[rcswhich].sfx,
463 shellparm[shelltype].hard);
465 if (which_limits[rcswhich] & SOFT) {
466 print_limit(limits[rcswhich].rlim_cur,
467 shellparm[shelltype].lprm[rcswhich].divisor,
468 shellparm[shelltype].inf,
469 shellparm[shelltype].lprm[rcswhich].pfx,
470 shellparm[shelltype].lprm[rcswhich].sfx,
471 shellparm[shelltype].soft);
486 (void)fprintf(stderr,
487 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuvp [val]] [[name=val ...] cmd]\n");
492 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
496 if (limit == RLIM_INFINITY)
499 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
500 printf(pfx, which, numbr);
507 resource_num(int which, int ch, const char *str)
509 rlim_t res = RLIM_INFINITY;
512 !(strcasecmp(str, "inf") == 0 ||
513 strcasecmp(str, "infinity") == 0 ||
514 strcasecmp(str, "unlimit") == 0 ||
515 strcasecmp(str, "unlimited") == 0)) {
516 const char * s = str;
520 case RLIMIT_CPU: /* time values */
524 rlim_t tim = strtoq(s, &e, 0);
525 if (e == NULL || e == s || errno)
528 case 0: /* end of string */
531 case 's': case 'S': /* seconds */
533 case 'm': case 'M': /* minutes */
536 case 'h': case 'H': /* hours */
539 case 'd': case 'D': /* days */
540 tim *= (60L * 60L * 24L);
542 case 'w': case 'W': /* weeks */
543 tim *= (60L * 60L * 24L * 7L);
544 case 'y': case 'Y': /* Years */
545 tim *= (60L * 60L * 24L * 365L);
551 case RLIMIT_FSIZE: /* Size values */
562 rlim_t mult, tim = strtoq(s, &e, 0);
563 if (e == NULL || e == s || errno)
566 case 0: /* end of string */
571 case 'b': case 'B': /* 512-byte blocks */
574 case 'k': case 'K': /* 1024-byte Kilobytes */
577 case 'm': case 'M': /* 1024-k kbytes */
580 case 'g': case 'G': /* 1Gbyte */
581 mult = 1024 * 1024 * 1024;
583 case 't': case 'T': /* 1TBte */
584 mult = 1024LL * 1024LL * 1024LL * 1024LL;
594 res = strtoq(s, &e, 0);
599 warnx("invalid value -%c `%s'", ch, str);
608 getshellbyname(const char * shell)
612 const char * p = strrchr(shell, '/');
615 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
617 int j = strcspn(q, "|");
621 if (strncmp(p, q, j) == 0)
632 * Determine the type of shell our parent process is
633 * This is quite tricky, not 100% reliable and probably
634 * not nearly as thorough as it should be. Basically, this
635 * is a "best guess" only, but hopefully will work in
642 pid_t ppid = getppid();
647 char procdir[MAXPATHLEN], buf[128];
648 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
649 char * shell = getenv("SHELL");
651 if (shell != NULL && stat(shell, &st) != -1) {
654 strcpy(procdir+l, "file");
655 /* $SHELL is actual shell? */
656 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
657 return getshellbyname(shell);
659 strcpy(procdir+l, "status");
660 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
661 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
664 return getshellbyname(p);