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 $
24 * $DragonFly: src/usr.bin/limits/limits.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
30 #include <sys/types.h>
32 #include <sys/param.h>
39 #include <login_cap.h>
41 #include <sys/resource.h>
48 SH_BASH, /* gnu bash */
57 /* eval emitter for popular shells.
58 * Why aren't there any standards here? Most shells support either
59 * the csh 'limit' or sh 'ulimit' command, but each varies just
60 * enough that they aren't very compatible from one to the other.
63 const char * name; /* Name of shell */
64 const char * inf; /* Name used for 'unlimited' resource */
65 const char * cmd; /* Intro text */
66 const char * hard; /* Hard limit text */
67 const char * soft; /* Soft limit text */
68 const char * both; /* Hard+Soft limit text */
76 { "", "infinity", "Resource limits%s%s:\n", "-max", "-cur", "",
78 { " cputime%-4s %8s", " secs\n", 1 },
79 { " filesize%-4s %8s", " kb\n", 1024 },
80 { " datasize%-4s %8s", " kb\n", 1024 },
81 { " stacksize%-4s %8s", " kb\n", 1024 },
82 { " coredumpsize%-4s %8s", " kb\n", 1024 },
83 { " memoryuse%-4s %8s", " kb\n", 1024 },
84 { " memorylocked%-4s %8s", " kb\n", 1024 },
85 { " maxprocesses%-4s %8s", "\n", 1 },
86 { " openfiles%-4s %8s", "\n", 1 },
87 { " sbsize%-4s %8s", " bytes\n", 1 },
88 { " vmemoryuse%-4s %8s", " kb\n", 1024 }
91 { "sh", "unlimited", "", " -H", " -S", "",
93 { "ulimit%s -t %s", ";\n", 1 },
94 { "ulimit%s -f %s", ";\n", 512 },
95 { "ulimit%s -d %s", ";\n", 1024 },
96 { "ulimit%s -s %s", ";\n", 1024 },
97 { "ulimit%s -c %s", ";\n", 512 },
98 { "ulimit%s -m %s", ";\n", 1024 },
99 { "ulimit%s -l %s", ";\n", 1024 },
100 { "ulimit%s -u %s", ";\n", 1 },
101 { "ulimit%s -n %s", ";\n", 1 },
102 { "ulimit%s -b %s", ";\n", 1 },
103 { "ulimit%s -v %s", ";\n", 1024 }
106 { "csh", "unlimited", "", " -h", "", NULL,
108 { "limit%s cputime %s", ";\n", 1 },
109 { "limit%s filesize %s", ";\n", 1024 },
110 { "limit%s datasize %s", ";\n", 1024 },
111 { "limit%s stacksize %s", ";\n", 1024 },
112 { "limit%s coredumpsize %s", ";\n", 1024 },
113 { "limit%s memoryuse %s", ";\n", 1024 },
114 { "limit%s memorylocked %s", ";\n", 1024 },
115 { "limit%s maxproc %s", ";\n", 1 },
116 { "limit%s openfiles %s", ";\n", 1 },
117 { "limit%s sbsize %s", ";\n", 1 },
118 { "limit%s vmemoryuse %s", ";\n", 1024 }
121 { "bash|bash2", "unlimited", "", " -H", " -S", "",
123 { "ulimit%s -t %s", ";\n", 1 },
124 { "ulimit%s -f %s", ";\n", 1024 },
125 { "ulimit%s -d %s", ";\n", 1024 },
126 { "ulimit%s -s %s", ";\n", 1024 },
127 { "ulimit%s -c %s", ";\n", 1024 },
128 { "ulimit%s -m %s", ";\n", 1024 },
129 { "ulimit%s -l %s", ";\n", 1024 },
130 { "ulimit%s -u %s", ";\n", 1 },
131 { "ulimit%s -n %s", ";\n", 1 },
132 { "ulimit%s -b %s", ";\n", 1 },
133 { "ulimit%s -v %s", ";\n", 1024 }
136 { "tcsh", "unlimited", "", " -h", "", NULL,
138 { "limit%s cputime %s", ";\n", 1 },
139 { "limit%s filesize %s", ";\n", 1024 },
140 { "limit%s datasize %s", ";\n", 1024 },
141 { "limit%s stacksize %s", ";\n", 1024 },
142 { "limit%s coredumpsize %s", ";\n", 1024 },
143 { "limit%s memoryuse %s", ";\n", 1024 },
144 { "limit%s memorylocked %s", ";\n", 1024 },
145 { "limit%s maxproc %s", ";\n", 1 },
146 { "limit%s descriptors %s", ";\n", 1 },
147 { "limit%s sbsize %s", ";\n", 1 },
148 { "limit%s vmemoryuse %s", ";\n", 1024 }
151 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
153 { "ulimit%s -t %s", ";\n", 1 },
154 { "ulimit%s -f %s", ";\n", 512 },
155 { "ulimit%s -d %s", ";\n", 1024 },
156 { "ulimit%s -s %s", ";\n", 1024 },
157 { "ulimit%s -c %s", ";\n", 512 },
158 { "ulimit%s -m %s", ";\n", 1024 },
159 { "ulimit%s -l %s", ";\n", 1024 },
160 { "ulimit%s -p %s", ";\n", 1 },
161 { "ulimit%s -n %s", ";\n", 1 },
162 { "ulimit%s -b %s", ";\n", 1 },
163 { "ulimit%s -v %s", ";\n", 1024 }
166 { "zsh", "unlimited", "", " -H", " -S", "",
168 { "ulimit%s -t %s", ";\n", 1 },
169 { "ulimit%s -f %s", ";\n", 512 },
170 { "ulimit%s -d %s", ";\n", 1024 },
171 { "ulimit%s -s %s", ";\n", 1024 },
172 { "ulimit%s -c %s", ";\n", 512 },
173 { "ulimit%s -m %s", ";\n", 1024 },
174 { "ulimit%s -l %s", ";\n", 1024 },
175 { "ulimit%s -u %s", ";\n", 1 },
176 { "ulimit%s -n %s", ";\n", 1 },
177 { "ulimit%s -b %s", ";\n", 1 },
178 { "ulimit%s -v %s", ";\n", 1024 }
181 { "rc|es", "unlimited", "", " -h", "", NULL,
183 { "limit%s cputime %s", ";\n", 1 },
184 { "limit%s filesize %s", ";\n", 1024 },
185 { "limit%s datasize %s", ";\n", 1024 },
186 { "limit%s stacksize %s", ";\n", 1024 },
187 { "limit%s coredumpsize %s", ";\n", 1024 },
188 { "limit%s memoryuse %s", ";\n", 1024 },
189 { "limit%s lockedmemory %s", ";\n", 1024 },
190 { "limit%s processes %s", ";\n", 1 },
191 { "limit%s descriptors %s", ";\n", 1 },
192 { "limit%s sbsize %s", ";\n", 1 },
193 { "limit%s vmemoryuse %s", ";\n", 1024 }
201 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
202 } resources[RLIM_NLIMITS] = {
203 { "cputime", login_getcaptime },
204 { "filesize", login_getcapsize },
205 { "datasize", login_getcapsize },
206 { "stacksize", login_getcapsize },
207 { "coredumpsize", login_getcapsize },
208 { "memoryuse", login_getcapsize },
209 { "memorylocked", login_getcapsize },
210 { "maxproc", login_getcapnum },
211 { "openfiles", login_getcapnum },
212 { "sbsize", login_getcapsize },
213 { "vmemoryuse", login_getcapsize }
217 * One letter for each resource levels.
218 * NOTE: There is a dependancy on the corresponding
219 * letter index being equal to the resource number.
220 * If sys/resource.h defines are changed, this needs
221 * to be modified accordingly!
224 #define RCS_STRING "tfdscmlunbv"
226 static rlim_t resource_num(int which, int ch, const char *str);
227 static void usage(void);
228 static int getshelltype(void);
229 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
230 const char *pfx, const char *sfx, const char *which);
231 extern char **environ;
233 static const char rcs_string[] = RCS_STRING;
236 main(int argc, char *argv[])
238 char *p, *cls = NULL;
240 struct passwd * pwd = NULL;
241 int rcswhich, shelltype;
242 int i, num_limits = 0;
243 int ch, doeval = 0, doall = 0;
244 login_cap_t * lc = NULL;
245 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
246 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
247 int which_limits[RLIM_NLIMITS];
248 rlim_t set_limits[RLIM_NLIMITS];
249 struct rlimit limits[RLIM_NLIMITS];
251 /* init resource tables */
252 for (i = 0; i < RLIM_NLIMITS; i++) {
253 which_limits[i] = 0; /* Don't set/display any */
254 set_limits[i] = RLIM_INFINITY;
255 /* Get current resource values */
256 getrlimit(i, &limits[i]);
260 while ((ch = getopt(argc, argv, ":EeC:U:BSHabc:d:f:l:m:n:s:t:u:v:")) != -1) {
276 if ((pwd = getpwnam(optarg)) == NULL) {
277 if (!isdigit(*optarg) ||
278 (pwd = getpwuid(atoi(optarg))) == NULL) {
279 warnx("invalid user `%s'", optarg);
294 case ':': /* Without arg */
295 if ((p = strchr(rcs_string, optopt)) != NULL) {
296 int rcswhich = p - rcs_string;
297 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
298 --optind; /* back one arg, and make arg NULL */
301 todo = optarg == NULL ? RCSSEL : RCSSET;
304 which_limits[rcswhich] = optarg ? type : DISPLAYONLY;
305 set_limits[rcswhich] = resource_num(rcswhich, optopt, optarg);
316 /* If user was specified, get class from that */
318 lc = login_getpwclass(pwd);
319 else if (cls != NULL && *cls != '\0') {
320 lc = login_getclassbyname(cls, NULL);
321 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
322 fprintf(stderr, "login class '%s' non-existent, using %s\n",
323 cls, lc?lc->lc_class:"current settings");
326 /* If we have a login class, update resource table from that */
328 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
332 /* current value overridden by resourcename or resourcename-cur */
333 sprintf(str, "%s-cur", resources[rcswhich].cap);
334 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
335 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
336 /* maximum value overridden by resourcename or resourcename-max */
337 sprintf(str, "%s-max", resources[rcswhich].cap);
338 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
339 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
343 /* now, let's determine what we wish to do with all this */
347 /* If we're setting limits or doing an eval (ie. we're not just
348 * displaying), then check that hard limits are not lower than
349 * soft limits, and force rasing the hard limit if we need to if
350 * we are raising the soft limit, or lower the soft limit if we
351 * are lowering the hard limit.
353 if ((*argv || doeval) && getuid() == 0) {
355 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
356 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
357 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
358 limits[rcswhich].rlim_max = RLIM_INFINITY;
359 which_limits[rcswhich] |= HARD;
360 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
361 if (which_limits[rcswhich] == SOFT) {
362 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
363 which_limits[rcswhich] |= HARD;
364 } else if (which_limits[rcswhich] == HARD) {
365 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
366 which_limits[rcswhich] |= SOFT;
368 /* else.. if we're specifically setting both to
369 * silly values, then let it error out.
377 /* See if we've overridden anything specific on the command line */
378 if (num_limits && todo == RCSSET) {
379 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
380 if (which_limits[rcswhich] & HARD)
381 limits[rcswhich].rlim_max = set_limits[rcswhich];
382 if (which_limits[rcswhich] & SOFT)
383 limits[rcswhich].rlim_cur = set_limits[rcswhich];
387 /* If *argv is not NULL, then we are being asked to
388 * (perhaps) set environment variables and run a program
392 warnx("-e cannot be used with `cmd' option");
398 /* set leading environment variables, like eval(1) */
399 while (*argv && (p = strchr(*argv, '=')))
400 (void)setenv(*argv++, ++p, 1);
403 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
404 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
405 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
406 err(1, "setrlimit %s", resources[rcswhich].cap);
416 shelltype = doeval ? getshelltype() : SH_NONE;
418 if (type == ANY) /* Default to soft limits */
422 printf(shellparm[shelltype].cmd,
423 lc ? " for class " : " (current)",
424 lc ? lc->lc_class : "");
426 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
427 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
428 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
429 which_limits[rcswhich] = type;
430 if (shellparm[shelltype].lprm[rcswhich].pfx) {
431 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
432 print_limit(limits[rcswhich].rlim_max,
433 shellparm[shelltype].lprm[rcswhich].divisor,
434 shellparm[shelltype].inf,
435 shellparm[shelltype].lprm[rcswhich].pfx,
436 shellparm[shelltype].lprm[rcswhich].sfx,
437 shellparm[shelltype].both);
439 if (which_limits[rcswhich] & HARD) {
440 print_limit(limits[rcswhich].rlim_max,
441 shellparm[shelltype].lprm[rcswhich].divisor,
442 shellparm[shelltype].inf,
443 shellparm[shelltype].lprm[rcswhich].pfx,
444 shellparm[shelltype].lprm[rcswhich].sfx,
445 shellparm[shelltype].hard);
447 if (which_limits[rcswhich] & SOFT) {
448 print_limit(limits[rcswhich].rlim_cur,
449 shellparm[shelltype].lprm[rcswhich].divisor,
450 shellparm[shelltype].inf,
451 shellparm[shelltype].lprm[rcswhich].pfx,
452 shellparm[shelltype].lprm[rcswhich].sfx,
453 shellparm[shelltype].soft);
468 (void)fprintf(stderr,
469 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuv [val]] [[name=val ...] cmd]\n");
474 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
478 if (limit == RLIM_INFINITY)
481 sprintf(numbr, "%qd", (quad_t)((limit + divisor/2) / divisor));
482 printf(pfx, which, numbr);
489 resource_num(int which, int ch, const char *str)
491 rlim_t res = RLIM_INFINITY;
494 !(strcasecmp(str, "inf") == 0 ||
495 strcasecmp(str, "infinity") == 0 ||
496 strcasecmp(str, "unlimit") == 0 ||
497 strcasecmp(str, "unlimited") == 0)) {
498 const char * s = str;
502 case RLIMIT_CPU: /* time values */
506 rlim_t tim = strtoq(s, &e, 0);
507 if (e == NULL || e == s || errno)
510 case 0: /* end of string */
513 case 's': case 'S': /* seconds */
515 case 'm': case 'M': /* minutes */
518 case 'h': case 'H': /* hours */
521 case 'd': case 'D': /* days */
522 tim *= (60L * 60L * 24L);
524 case 'w': case 'W': /* weeks */
525 tim *= (60L * 60L * 24L * 7L);
526 case 'y': case 'Y': /* Years */
527 tim *= (60L * 60L * 24L * 365L);
533 case RLIMIT_FSIZE: /* Size values */
543 rlim_t mult, tim = strtoq(s, &e, 0);
544 if (e == NULL || e == s || errno)
547 case 0: /* end of string */
552 case 'b': case 'B': /* 512-byte blocks */
555 case 'k': case 'K': /* 1024-byte Kilobytes */
558 case 'm': case 'M': /* 1024-k kbytes */
561 case 'g': case 'G': /* 1Gbyte */
562 mult = 1024 * 1024 * 1024;
564 case 't': case 'T': /* 1TBte */
565 mult = 1024LL * 1024LL * 1024LL * 1024LL;
574 res = strtoq(s, &e, 0);
579 warnx("invalid value -%c `%s'", ch, str);
588 getshellbyname(const char * shell)
592 const char * p = strrchr(shell, '/');
595 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
597 int j = strcspn(q, "|");
601 if (strncmp(p, q, j) == 0)
612 * Determine the type of shell our parent process is
613 * This is quite tricky, not 100% reliable and probably
614 * not nearly as thorough as it should be. Basically, this
615 * is a "best guess" only, but hopefully will work in
622 pid_t ppid = getppid();
627 char procdir[MAXPATHLEN], buf[128];
628 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
629 char * shell = getenv("SHELL");
631 if (shell != NULL && stat(shell, &st) != -1) {
634 strcpy(procdir+l, "file");
635 /* $SHELL is actual shell? */
636 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
637 return getshellbyname(shell);
639 strcpy(procdir+l, "status");
640 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
641 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
644 return getshellbyname(p);