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.
23 * $FreeBSD: src/usr.bin/limits/limits.c,v 1.7.2.3 2003/05/22 09:26:57 sheldonh Exp $
29 #include <sys/types.h>
31 #include <sys/param.h>
38 #include <login_cap.h>
40 #include <sys/resource.h>
47 SH_BASH, /* gnu bash */
56 /* eval emitter for popular shells.
57 * Why aren't there any standards here? Most shells support either
58 * the csh 'limit' or sh 'ulimit' command, but each varies just
59 * enough that they aren't very compatible from one to the other.
62 const char * name; /* Name of shell */
63 const char * inf; /* Name used for 'unlimited' resource */
64 const char * cmd; /* Intro text */
65 const char * hard; /* Hard limit text */
66 const char * soft; /* Soft limit text */
67 const char * both; /* Hard+Soft limit text */
75 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
77 { " cputime%-4s %8s", " secs\n", 1 },
78 { " filesize%-4s %8s", " kb\n", 1024 },
79 { " datasize%-4s %8s", " kb\n", 1024 },
80 { " stacksize%-4s %8s", " kb\n", 1024 },
81 { " coredumpsize%-4s %8s", " kb\n", 1024 },
82 { " memoryuse%-4s %8s", " kb\n", 1024 },
83 { " memorylocked%-4s %8s", " kb\n", 1024 },
84 { " maxprocesses%-4s %8s", "\n", 1 },
85 { " openfiles%-4s %8s", "\n", 1 },
86 { " sbsize%-4s %8s", " bytes\n", 1 },
87 { " vmemoryuse%-4s %8s", " kb\n", 1024 },
88 #ifdef RLIMIT_POSIXLOCKS
89 { " posixlocks%-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 #ifdef RLIMIT_POSIXLOCKS
107 { "ulimit%s -k %s", ";\n", 1 },
111 { "csh", "unlimited", "", " -h", "", NULL,
113 { "limit%s cputime %s", ";\n", 1 },
114 { "limit%s filesize %s", ";\n", 1024 },
115 { "limit%s datasize %s", ";\n", 1024 },
116 { "limit%s stacksize %s", ";\n", 1024 },
117 { "limit%s coredumpsize %s", ";\n", 1024 },
118 { "limit%s memoryuse %s", ";\n", 1024 },
119 { "limit%s memorylocked %s", ";\n", 1024 },
120 { "limit%s maxproc %s", ";\n", 1 },
121 { "limit%s openfiles %s", ";\n", 1 },
122 { "limit%s sbsize %s", ";\n", 1 },
123 { "limit%s vmemoryuse %s", ";\n", 1024 },
124 #ifdef RLIMIT_POSIXLOCKS
125 { "limit%s advlocks %s", ";\n", 1 },
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 #ifdef RLIMIT_POSIXLOCKS
143 { "ulimit%s -k %s", ";\n", 1 },
147 { "tcsh", "unlimited", "", " -h", "", NULL,
149 { "limit%s cputime %s", ";\n", 1 },
150 { "limit%s filesize %s", ";\n", 1024 },
151 { "limit%s datasize %s", ";\n", 1024 },
152 { "limit%s stacksize %s", ";\n", 1024 },
153 { "limit%s coredumpsize %s", ";\n", 1024 },
154 { "limit%s memoryuse %s", ";\n", 1024 },
155 { "limit%s memorylocked %s", ";\n", 1024 },
156 { "limit%s maxproc %s", ";\n", 1 },
157 { "limit%s descriptors %s", ";\n", 1 },
158 { "limit%s sbsize %s", ";\n", 1 },
159 { "limit%s vmemoryuse %s", ";\n", 1024 },
160 #ifdef RLIMIT_POSIXLOCKS
161 { "limit%s advlocks %s", ";\n", 1 },
165 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
167 { "ulimit%s -t %s", ";\n", 1 },
168 { "ulimit%s -f %s", ";\n", 512 },
169 { "ulimit%s -d %s", ";\n", 1024 },
170 { "ulimit%s -s %s", ";\n", 1024 },
171 { "ulimit%s -c %s", ";\n", 512 },
172 { "ulimit%s -m %s", ";\n", 1024 },
173 { "ulimit%s -l %s", ";\n", 1024 },
174 { "ulimit%s -p %s", ";\n", 1 },
175 { "ulimit%s -n %s", ";\n", 1 },
176 { "ulimit%s -b %s", ";\n", 1 },
177 { "ulimit%s -v %s", ";\n", 1024 },
178 #ifdef RLIMIT_POSIXLOCKS
179 { "ulimit%s -k %s", ";\n", 1 },
183 { "zsh", "unlimited", "", " -H", " -S", "",
185 { "ulimit%s -t %s", ";\n", 1 },
186 { "ulimit%s -f %s", ";\n", 512 },
187 { "ulimit%s -d %s", ";\n", 1024 },
188 { "ulimit%s -s %s", ";\n", 1024 },
189 { "ulimit%s -c %s", ";\n", 512 },
190 { "ulimit%s -m %s", ";\n", 1024 },
191 { "ulimit%s -l %s", ";\n", 1024 },
192 { "ulimit%s -u %s", ";\n", 1 },
193 { "ulimit%s -n %s", ";\n", 1 },
194 { "ulimit%s -b %s", ";\n", 1 },
195 { "ulimit%s -v %s", ";\n", 1024 },
196 #ifdef RLIMIT_POSIXLOCKS
197 { "ulimit%s -k %s", ";\n", 1 },
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 #ifdef RLIMIT_POSIXLOCKS
215 { "limit%s advlocks %s", ";\n", 1 },
219 { NULL, NULL, NULL, NULL, NULL, NULL, {} }
224 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
225 } resources[RLIM_NLIMITS] = {
226 { "cputime", login_getcaptime },
227 { "filesize", login_getcapsize },
228 { "datasize", login_getcapsize },
229 { "stacksize", login_getcapsize },
230 { "coredumpsize", login_getcapsize },
231 { "memoryuse", login_getcapsize },
232 { "memorylocked", login_getcapsize },
233 { "maxproc", login_getcapnum },
234 { "openfiles", login_getcapnum },
235 { "sbsize", login_getcapsize },
236 { "vmemoryuse", login_getcapsize },
237 { "posixlocks", login_getcapnum },
241 * One letter for each resource levels.
242 * NOTE: There is a dependancy on the corresponding
243 * letter index being equal to the resource number.
244 * If sys/resource.h defines are changed, this needs
245 * to be modified accordingly!
248 #define RCS_STRING "tfdscmlunbvk"
250 static rlim_t resource_num(int which, int ch, const char *str);
251 static void usage(void);
252 static int getshelltype(void);
253 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
254 const char *pfx, const char *sfx, const char *which);
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;
268 login_cap_t * lc = NULL;
269 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
270 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
271 int which_limits[RLIM_NLIMITS];
272 rlim_t set_limits[RLIM_NLIMITS];
273 struct rlimit limits[RLIM_NLIMITS];
275 /* init resource tables */
276 for (i = 0; i < RLIM_NLIMITS; i++) {
277 which_limits[i] = 0; /* Don't set/display any */
278 set_limits[i] = RLIM_INFINITY;
279 /* Get current resource values */
280 if (getrlimit(i, &limits[i]) < 0) {
281 limits[i].rlim_cur = -1;
282 limits[i].rlim_max = -1;
287 while ((ch = getopt(argc, argv, ":EeC:U:BSHabc:d:f:k:l:m:n:s:t:u:v:")) != -1) {
303 if ((pwd = getpwnam(optarg)) == NULL) {
304 if (!isdigit(*optarg) ||
305 (pwd = getpwuid(atoi(optarg))) == NULL) {
306 warnx("invalid user `%s'", optarg);
321 case ':': /* Without arg */
322 if ((p = strchr(rcs_string, optopt)) != NULL) {
323 rcswhich = p - rcs_string;
326 * Backwards compatibility with earlier kernel which might
327 * support fewer resources.
329 if (rcswhich >= RLIM_NLIMITS) {
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[rcswhich] = optarg ? type : DISPLAYONLY;
341 set_limits[rcswhich] = resource_num(rcswhich, optopt, optarg);
352 /* If user was specified, get class from that */
354 lc = login_getpwclass(pwd);
355 else if (cls != NULL && *cls != '\0') {
356 lc = login_getclassbyname(cls, NULL);
357 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
358 fprintf(stderr, "login class '%s' non-existent, using %s\n",
359 cls, lc?lc->lc_class:"current settings");
362 /* If we have a login class, update resource table from that */
364 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
368 /* current value overridden by resourcename or resourcename-cur */
369 sprintf(str, "%s-cur", resources[rcswhich].cap);
370 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
371 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
372 /* maximum value overridden by resourcename or resourcename-max */
373 sprintf(str, "%s-max", resources[rcswhich].cap);
374 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
375 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
379 /* now, let's determine what we wish to do with all this */
383 /* If we're setting limits or doing an eval (ie. we're not just
384 * displaying), then check that hard limits are not lower than
385 * soft limits, and force rasing the hard limit if we need to if
386 * we are raising the soft limit, or lower the soft limit if we
387 * are lowering the hard limit.
389 if ((*argv || doeval) && getuid() == 0) {
391 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
392 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
393 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
394 limits[rcswhich].rlim_max = RLIM_INFINITY;
395 which_limits[rcswhich] |= HARD;
396 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
397 if (which_limits[rcswhich] == SOFT) {
398 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
399 which_limits[rcswhich] |= HARD;
400 } else if (which_limits[rcswhich] == HARD) {
401 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
402 which_limits[rcswhich] |= SOFT;
404 /* else.. if we're specifically setting both to
405 * silly values, then let it error out.
413 /* See if we've overridden anything specific on the command line */
414 if (num_limits && todo == RCSSET) {
415 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
416 if (which_limits[rcswhich] & HARD)
417 limits[rcswhich].rlim_max = set_limits[rcswhich];
418 if (which_limits[rcswhich] & SOFT)
419 limits[rcswhich].rlim_cur = set_limits[rcswhich];
423 /* If *argv is not NULL, then we are being asked to
424 * (perhaps) set environment variables and run a program
428 warnx("-e cannot be used with `cmd' option");
434 /* set leading environment variables, like eval(1) */
435 while (*argv && (p = strchr(*argv, '='))) {
437 if (setenv(*argv++, p + 1, 1) == -1)
438 err(1, "setenv: cannot set %s=%s", *argv, p + 1);
443 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
444 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
445 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
446 err(1, "setrlimit %s", resources[rcswhich].cap);
456 shelltype = doeval ? getshelltype() : SH_NONE;
458 if (type == ANY) /* Default to soft limits */
462 printf(shellparm[shelltype].cmd,
463 lc ? " for class " : " (current)",
464 lc ? lc->lc_class : "");
466 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
467 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
468 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
469 which_limits[rcswhich] = type;
470 if (shellparm[shelltype].lprm[rcswhich].pfx) {
471 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
472 print_limit(limits[rcswhich].rlim_max,
473 shellparm[shelltype].lprm[rcswhich].divisor,
474 shellparm[shelltype].inf,
475 shellparm[shelltype].lprm[rcswhich].pfx,
476 shellparm[shelltype].lprm[rcswhich].sfx,
477 shellparm[shelltype].both);
479 if (which_limits[rcswhich] & HARD) {
480 print_limit(limits[rcswhich].rlim_max,
481 shellparm[shelltype].lprm[rcswhich].divisor,
482 shellparm[shelltype].inf,
483 shellparm[shelltype].lprm[rcswhich].pfx,
484 shellparm[shelltype].lprm[rcswhich].sfx,
485 shellparm[shelltype].hard);
487 if (which_limits[rcswhich] & SOFT) {
488 print_limit(limits[rcswhich].rlim_cur,
489 shellparm[shelltype].lprm[rcswhich].divisor,
490 shellparm[shelltype].inf,
491 shellparm[shelltype].lprm[rcswhich].pfx,
492 shellparm[shelltype].lprm[rcswhich].sfx,
493 shellparm[shelltype].soft);
508 (void)fprintf(stderr,
509 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuvk [val]] [[name=val ...] cmd]\n");
514 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
518 if (limit == RLIM_INFINITY)
521 sprintf(numbr, "%jd", (intmax_t)((limit + divisor/2) / divisor));
522 printf(pfx, which, numbr);
529 resource_num(int which, int ch, const char *str)
531 rlim_t res = RLIM_INFINITY;
534 !(strcasecmp(str, "inf") == 0 ||
535 strcasecmp(str, "infinity") == 0 ||
536 strcasecmp(str, "unlimit") == 0 ||
537 strcasecmp(str, "unlimited") == 0)) {
538 const char * s = str;
542 case RLIMIT_CPU: /* time values */
546 rlim_t tim = strtoq(s, &e, 0);
547 if (e == NULL || e == s || errno)
550 case 0: /* end of string */
553 case 's': case 'S': /* seconds */
555 case 'm': case 'M': /* minutes */
558 case 'h': case 'H': /* hours */
561 case 'd': case 'D': /* days */
562 tim *= (60L * 60L * 24L);
564 case 'w': case 'W': /* weeks */
565 tim *= (60L * 60L * 24L * 7L);
567 case 'y': case 'Y': /* Years */
568 tim *= (60L * 60L * 24L * 365L);
574 case RLIMIT_FSIZE: /* Size values */
584 rlim_t mult, tim = strtoq(s, &e, 0);
585 if (e == NULL || e == s || errno)
588 case 0: /* end of string */
593 case 'b': case 'B': /* 512-byte blocks */
596 case 'k': case 'K': /* 1024-byte Kilobytes */
599 case 'm': case 'M': /* 1024-k kbytes */
602 case 'g': case 'G': /* 1Gbyte */
603 mult = 1024 * 1024 * 1024;
605 case 't': case 'T': /* 1TBte */
606 mult = 1024LL * 1024LL * 1024LL * 1024LL;
615 res = strtoq(s, &e, 0);
620 warnx("invalid value -%c `%s'", ch, str);
629 getshellbyname(const char * shell)
633 const char * p = strrchr(shell, '/');
635 p = p ? p + 1 : shell;
636 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
638 int j = strcspn(q, "|");
642 if (strncmp(p, q, j) == 0)
653 * Determine the type of shell our parent process is
654 * This is quite tricky, not 100% reliable and probably
655 * not nearly as thorough as it should be. Basically, this
656 * is a "best guess" only, but hopefully will work in
663 pid_t ppid = getppid();
668 char procdir[MAXPATHLEN], buf[128];
669 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
670 char * shell = getenv("SHELL");
672 if (shell != NULL && stat(shell, &st) != -1) {
675 strcpy(procdir+l, "file");
676 /* $SHELL is actual shell? */
677 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
678 return getshellbyname(shell);
680 strcpy(procdir+l, "status");
681 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
682 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
685 return getshellbyname(p);