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 }
90 { "sh", "unlimited", "", " -H", " -S", "",
92 { "ulimit%s -t %s", ";\n", 1 },
93 { "ulimit%s -f %s", ";\n", 512 },
94 { "ulimit%s -d %s", ";\n", 1024 },
95 { "ulimit%s -s %s", ";\n", 1024 },
96 { "ulimit%s -c %s", ";\n", 512 },
97 { "ulimit%s -m %s", ";\n", 1024 },
98 { "ulimit%s -l %s", ";\n", 1024 },
99 { "ulimit%s -u %s", ";\n", 1 },
100 { "ulimit%s -n %s", ";\n", 1 },
101 { "ulimit%s -b %s", ";\n", 1 },
102 { "ulimit%s -v %s", ";\n", 1024 }
105 { "csh", "unlimited", "", " -h", "", NULL,
107 { "limit%s cputime %s", ";\n", 1 },
108 { "limit%s filesize %s", ";\n", 1024 },
109 { "limit%s datasize %s", ";\n", 1024 },
110 { "limit%s stacksize %s", ";\n", 1024 },
111 { "limit%s coredumpsize %s", ";\n", 1024 },
112 { "limit%s memoryuse %s", ";\n", 1024 },
113 { "limit%s memorylocked %s", ";\n", 1024 },
114 { "limit%s maxproc %s", ";\n", 1 },
115 { "limit%s openfiles %s", ";\n", 1 },
116 { "limit%s sbsize %s", ";\n", 1 },
117 { "limit%s vmemoryuse %s", ";\n", 1024 }
120 { "bash|bash2", "unlimited", "", " -H", " -S", "",
122 { "ulimit%s -t %s", ";\n", 1 },
123 { "ulimit%s -f %s", ";\n", 1024 },
124 { "ulimit%s -d %s", ";\n", 1024 },
125 { "ulimit%s -s %s", ";\n", 1024 },
126 { "ulimit%s -c %s", ";\n", 1024 },
127 { "ulimit%s -m %s", ";\n", 1024 },
128 { "ulimit%s -l %s", ";\n", 1024 },
129 { "ulimit%s -u %s", ";\n", 1 },
130 { "ulimit%s -n %s", ";\n", 1 },
131 { "ulimit%s -b %s", ";\n", 1 },
132 { "ulimit%s -v %s", ";\n", 1024 }
135 { "tcsh", "unlimited", "", " -h", "", NULL,
137 { "limit%s cputime %s", ";\n", 1 },
138 { "limit%s filesize %s", ";\n", 1024 },
139 { "limit%s datasize %s", ";\n", 1024 },
140 { "limit%s stacksize %s", ";\n", 1024 },
141 { "limit%s coredumpsize %s", ";\n", 1024 },
142 { "limit%s memoryuse %s", ";\n", 1024 },
143 { "limit%s memorylocked %s", ";\n", 1024 },
144 { "limit%s maxproc %s", ";\n", 1 },
145 { "limit%s descriptors %s", ";\n", 1 },
146 { "limit%s sbsize %s", ";\n", 1 },
147 { "limit%s vmemoryuse %s", ";\n", 1024 }
150 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
152 { "ulimit%s -t %s", ";\n", 1 },
153 { "ulimit%s -f %s", ";\n", 512 },
154 { "ulimit%s -d %s", ";\n", 1024 },
155 { "ulimit%s -s %s", ";\n", 1024 },
156 { "ulimit%s -c %s", ";\n", 512 },
157 { "ulimit%s -m %s", ";\n", 1024 },
158 { "ulimit%s -l %s", ";\n", 1024 },
159 { "ulimit%s -p %s", ";\n", 1 },
160 { "ulimit%s -n %s", ";\n", 1 },
161 { "ulimit%s -b %s", ";\n", 1 },
162 { "ulimit%s -v %s", ";\n", 1024 }
165 { "zsh", "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 -u %s", ";\n", 1 },
175 { "ulimit%s -n %s", ";\n", 1 },
176 { "ulimit%s -b %s", ";\n", 1 },
177 { "ulimit%s -v %s", ";\n", 1024 }
180 { "rc|es", "unlimited", "", " -h", "", NULL,
182 { "limit%s cputime %s", ";\n", 1 },
183 { "limit%s filesize %s", ";\n", 1024 },
184 { "limit%s datasize %s", ";\n", 1024 },
185 { "limit%s stacksize %s", ";\n", 1024 },
186 { "limit%s coredumpsize %s", ";\n", 1024 },
187 { "limit%s memoryuse %s", ";\n", 1024 },
188 { "limit%s lockedmemory %s", ";\n", 1024 },
189 { "limit%s processes %s", ";\n", 1 },
190 { "limit%s descriptors %s", ";\n", 1 },
191 { "limit%s sbsize %s", ";\n", 1 },
192 { "limit%s vmemoryuse %s", ";\n", 1024 }
200 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
201 } resources[RLIM_NLIMITS] = {
202 { "cputime", login_getcaptime },
203 { "filesize", login_getcapsize },
204 { "datasize", login_getcapsize },
205 { "stacksize", login_getcapsize },
206 { "coredumpsize", login_getcapsize },
207 { "memoryuse", login_getcapsize },
208 { "memorylocked", login_getcapsize },
209 { "maxproc", login_getcapnum },
210 { "openfiles", login_getcapnum },
211 { "sbsize", login_getcapsize },
212 { "vmemoryuse", login_getcapsize }
216 * One letter for each resource levels.
217 * NOTE: There is a dependancy on the corresponding
218 * letter index being equal to the resource number.
219 * If sys/resource.h defines are changed, this needs
220 * to be modified accordingly!
223 #define RCS_STRING "tfdscmlunbv"
225 static rlim_t resource_num(int which, int ch, const char *str);
226 static void usage(void);
227 static int getshelltype(void);
228 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
229 const char *pfx, const char *sfx, const char *which);
230 extern char **environ;
232 static const char rcs_string[] = RCS_STRING;
235 main(int argc, char *argv[])
237 char *p, *cls = NULL;
239 struct passwd * pwd = NULL;
240 int rcswhich, shelltype;
241 int i, num_limits = 0;
242 int ch, doeval = 0, doall = 0;
243 login_cap_t * lc = NULL;
244 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
245 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
246 int which_limits[RLIM_NLIMITS];
247 rlim_t set_limits[RLIM_NLIMITS];
248 struct rlimit limits[RLIM_NLIMITS];
250 /* init resource tables */
251 for (i = 0; i < RLIM_NLIMITS; i++) {
252 which_limits[i] = 0; /* Don't set/display any */
253 set_limits[i] = RLIM_INFINITY;
254 /* Get current resource values */
255 getrlimit(i, &limits[i]);
259 while ((ch = getopt(argc, argv, ":EeC:U:BSHabc:d:f:l:m:n:s:t:u:v:")) != -1) {
275 if ((pwd = getpwnam(optarg)) == NULL) {
276 if (!isdigit(*optarg) ||
277 (pwd = getpwuid(atoi(optarg))) == NULL) {
278 warnx("invalid user `%s'", optarg);
293 case ':': /* Without arg */
294 if ((p = strchr(rcs_string, optopt)) != NULL) {
295 int rcswhich = p - rcs_string;
296 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
297 --optind; /* back one arg, and make arg NULL */
300 todo = optarg == NULL ? RCSSEL : RCSSET;
303 which_limits[rcswhich] = optarg ? type : DISPLAYONLY;
304 set_limits[rcswhich] = resource_num(rcswhich, optopt, optarg);
315 /* If user was specified, get class from that */
317 lc = login_getpwclass(pwd);
318 else if (cls != NULL && *cls != '\0') {
319 lc = login_getclassbyname(cls, NULL);
320 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
321 fprintf(stderr, "login class '%s' non-existent, using %s\n",
322 cls, lc?lc->lc_class:"current settings");
325 /* If we have a login class, update resource table from that */
327 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
331 /* current value overridden by resourcename or resourcename-cur */
332 sprintf(str, "%s-cur", resources[rcswhich].cap);
333 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
334 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
335 /* maximum value overridden by resourcename or resourcename-max */
336 sprintf(str, "%s-max", resources[rcswhich].cap);
337 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
338 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
342 /* now, let's determine what we wish to do with all this */
346 /* If we're setting limits or doing an eval (ie. we're not just
347 * displaying), then check that hard limits are not lower than
348 * soft limits, and force rasing the hard limit if we need to if
349 * we are raising the soft limit, or lower the soft limit if we
350 * are lowering the hard limit.
352 if ((*argv || doeval) && getuid() == 0) {
354 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
355 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
356 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
357 limits[rcswhich].rlim_max = RLIM_INFINITY;
358 which_limits[rcswhich] |= HARD;
359 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
360 if (which_limits[rcswhich] == SOFT) {
361 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
362 which_limits[rcswhich] |= HARD;
363 } else if (which_limits[rcswhich] == HARD) {
364 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
365 which_limits[rcswhich] |= SOFT;
367 /* else.. if we're specifically setting both to
368 * silly values, then let it error out.
376 /* See if we've overridden anything specific on the command line */
377 if (num_limits && todo == RCSSET) {
378 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
379 if (which_limits[rcswhich] & HARD)
380 limits[rcswhich].rlim_max = set_limits[rcswhich];
381 if (which_limits[rcswhich] & SOFT)
382 limits[rcswhich].rlim_cur = set_limits[rcswhich];
386 /* If *argv is not NULL, then we are being asked to
387 * (perhaps) set environment variables and run a program
391 warnx("-e cannot be used with `cmd' option");
397 /* set leading environment variables, like eval(1) */
398 while (*argv && (p = strchr(*argv, '=')))
399 (void)setenv(*argv++, ++p, 1);
402 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
403 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
404 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
405 err(1, "setrlimit %s", resources[rcswhich].cap);
415 shelltype = doeval ? getshelltype() : SH_NONE;
417 if (type == ANY) /* Default to soft limits */
421 printf(shellparm[shelltype].cmd,
422 lc ? " for class " : " (current)",
423 lc ? lc->lc_class : "");
425 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
426 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
427 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
428 which_limits[rcswhich] = type;
429 if (shellparm[shelltype].lprm[rcswhich].pfx) {
430 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
431 print_limit(limits[rcswhich].rlim_max,
432 shellparm[shelltype].lprm[rcswhich].divisor,
433 shellparm[shelltype].inf,
434 shellparm[shelltype].lprm[rcswhich].pfx,
435 shellparm[shelltype].lprm[rcswhich].sfx,
436 shellparm[shelltype].both);
438 if (which_limits[rcswhich] & HARD) {
439 print_limit(limits[rcswhich].rlim_max,
440 shellparm[shelltype].lprm[rcswhich].divisor,
441 shellparm[shelltype].inf,
442 shellparm[shelltype].lprm[rcswhich].pfx,
443 shellparm[shelltype].lprm[rcswhich].sfx,
444 shellparm[shelltype].hard);
446 if (which_limits[rcswhich] & SOFT) {
447 print_limit(limits[rcswhich].rlim_cur,
448 shellparm[shelltype].lprm[rcswhich].divisor,
449 shellparm[shelltype].inf,
450 shellparm[shelltype].lprm[rcswhich].pfx,
451 shellparm[shelltype].lprm[rcswhich].sfx,
452 shellparm[shelltype].soft);
467 (void)fprintf(stderr,
468 "usage: limits [-C class|-U user] [-eaSHBE] [-bcdflmnstuv [val]] [[name=val ...] cmd]\n");
473 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
477 if (limit == RLIM_INFINITY)
480 sprintf(numbr, "%qd", (quad_t)((limit + divisor/2) / divisor));
481 printf(pfx, which, numbr);
488 resource_num(int which, int ch, const char *str)
490 rlim_t res = RLIM_INFINITY;
493 !(strcasecmp(str, "inf") == 0 ||
494 strcasecmp(str, "infinity") == 0 ||
495 strcasecmp(str, "unlimit") == 0 ||
496 strcasecmp(str, "unlimited") == 0)) {
497 const char * s = str;
501 case RLIMIT_CPU: /* time values */
505 rlim_t tim = strtoq(s, &e, 0);
506 if (e == NULL || e == s || errno)
509 case 0: /* end of string */
512 case 's': case 'S': /* seconds */
514 case 'm': case 'M': /* minutes */
517 case 'h': case 'H': /* hours */
520 case 'd': case 'D': /* days */
521 tim *= (60L * 60L * 24L);
523 case 'w': case 'W': /* weeks */
524 tim *= (60L * 60L * 24L * 7L);
525 case 'y': case 'Y': /* Years */
526 tim *= (60L * 60L * 24L * 365L);
532 case RLIMIT_FSIZE: /* Size values */
542 rlim_t mult, tim = strtoq(s, &e, 0);
543 if (e == NULL || e == s || errno)
546 case 0: /* end of string */
551 case 'b': case 'B': /* 512-byte blocks */
554 case 'k': case 'K': /* 1024-byte Kilobytes */
557 case 'm': case 'M': /* 1024-k kbytes */
560 case 'g': case 'G': /* 1Gbyte */
561 mult = 1024 * 1024 * 1024;
563 case 't': case 'T': /* 1TBte */
564 mult = 1024LL * 1024LL * 1024LL * 1024LL;
573 res = strtoq(s, &e, 0);
578 warnx("invalid value -%c `%s'", ch, str);
587 getshellbyname(const char * shell)
591 const char * p = strrchr(shell, '/');
594 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
596 int j = strcspn(q, "|");
600 if (strncmp(p, q, j) == 0)
611 * Determine the type of shell our parent process is
612 * This is quite tricky, not 100% reliable and probably
613 * not nearly as thorough as it should be. Basically, this
614 * is a "best guess" only, but hopefully will work in
621 pid_t ppid = getppid();
626 char procdir[MAXPATHLEN], buf[128];
627 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
628 char * shell = getenv("SHELL");
630 if (shell != NULL && stat(shell, &st) != -1) {
633 strcpy(procdir+l, "file");
634 /* $SHELL is actual shell? */
635 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
636 return getshellbyname(shell);
638 strcpy(procdir+l, "status");
639 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
640 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
643 return getshellbyname(p);