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.
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 }
88 { "sh", "unlimited", "", " -H", " -S", "",
90 { "ulimit%s -t %s", ";\n", 1 },
91 { "ulimit%s -f %s", ";\n", 512 },
92 { "ulimit%s -d %s", ";\n", 1024 },
93 { "ulimit%s -s %s", ";\n", 1024 },
94 { "ulimit%s -c %s", ";\n", 512 },
95 { "ulimit%s -m %s", ";\n", 1024 },
96 { "ulimit%s -l %s", ";\n", 1024 },
97 { "ulimit%s -u %s", ";\n", 1 },
98 { "ulimit%s -n %s", ";\n", 1 }
101 { "csh", "unlimited", "", " -h", "", NULL,
103 { "limit%s cputime %s", ";\n", 1 },
104 { "limit%s filesize %s", ";\n", 1024 },
105 { "limit%s datasize %s", ";\n", 1024 },
106 { "limit%s stacksize %s", ";\n", 1024 },
107 { "limit%s coredumpsize %s", ";\n", 1024 },
108 { "limit%s memoryuse %s", ";\n", 1024 },
109 { "limit%s memorylocked %s", ";\n", 1024 },
110 { "limit%s maxproc %s", ";\n", 1 },
111 { "limit%s openfiles %s", ";\n", 1 }
114 { "bash|bash2", "unlimited", "", " -H", " -S", "",
116 { "ulimit%s -t %s", ";\n", 1 },
117 { "ulimit%s -f %s", ";\n", 1024 },
118 { "ulimit%s -d %s", ";\n", 1024 },
119 { "ulimit%s -s %s", ";\n", 1024 },
120 { "ulimit%s -c %s", ";\n", 1024 },
121 { "ulimit%s -m %s", ";\n", 1024 },
122 { "ulimit%s -l %s", ";\n", 1024 },
123 { "ulimit%s -u %s", ";\n", 1 },
124 { "ulimit%s -n %s", ";\n", 1 }
127 { "tcsh", "unlimited", "", " -h", "", NULL,
129 { "limit%s cputime %s", ";\n", 1 },
130 { "limit%s filesize %s", ";\n", 1024 },
131 { "limit%s datasize %s", ";\n", 1024 },
132 { "limit%s stacksize %s", ";\n", 1024 },
133 { "limit%s coredumpsize %s", ";\n", 1024 },
134 { "limit%s memoryuse %s", ";\n", 1024 },
135 { "limit%s memorylocked %s", ";\n", 1024 },
136 { "limit%s maxproc %s", ";\n", 1 },
137 { "limit%s descriptors %s", ";\n", 1 }
140 { "ksh|pdksh", "unlimited", "", " -H", " -S", "",
142 { "ulimit%s -t %s", ";\n", 1 },
143 { "ulimit%s -f %s", ";\n", 512 },
144 { "ulimit%s -d %s", ";\n", 1024 },
145 { "ulimit%s -s %s", ";\n", 1024 },
146 { "ulimit%s -c %s", ";\n", 512 },
147 { "ulimit%s -m %s", ";\n", 1024 },
148 { "ulimit%s -l %s", ";\n", 1024 },
149 { "ulimit%s -p %s", ";\n", 1 },
150 { "ulimit%s -n %s", ";\n", 1 }
153 { "zsh", "unlimited", "", " -H", " -S", "",
155 { "ulimit%s -t %s", ";\n", 1 },
156 { "ulimit%s -f %s", ";\n", 512 },
157 { "ulimit%s -d %s", ";\n", 1024 },
158 { "ulimit%s -s %s", ";\n", 1024 },
159 { "ulimit%s -c %s", ";\n", 512 },
160 { "ulimit%s -m %s", ";\n", 1024 },
161 { "ulimit%s -l %s", ";\n", 1024 },
162 { "ulimit%s -u %s", ";\n", 1 },
163 { "ulimit%s -n %s", ";\n", 1 }
166 { "rc|es", "unlimited", "", " -h", "", NULL,
168 { "limit%s cputime %s", ";\n", 1 },
169 { "limit%s filesize %s", ";\n", 1024 },
170 { "limit%s datasize %s", ";\n", 1024 },
171 { "limit%s stacksize %s", ";\n", 1024 },
172 { "limit%s coredumpsize %s", ";\n", 1024 },
173 { "limit%s memoryuse %s", ";\n", 1024 },
174 { "limit%s lockedmemory %s", ";\n", 1024 },
175 { "limit%s processes %s", ";\n", 1 },
176 { "limit%s descriptors %s", ";\n", 1 }
184 rlim_t (*func)(login_cap_t *, const char *, rlim_t, rlim_t);
185 } resources[RLIM_NLIMITS] = {
186 { "cputime", login_getcaptime },
187 { "filesize", login_getcapsize },
188 { "datasize", login_getcapsize },
189 { "stacksize", login_getcapsize },
190 { "coredumpsize", login_getcapsize },
191 { "memoryuse", login_getcapsize },
192 { "memorylocked", login_getcapsize },
193 { "maxproc", login_getcapnum, },
194 { "openfiles", login_getcapnum }
198 * One letter for each resource levels.
199 * NOTE: There is a dependancy on the corresponding
200 * letter index being equal to the resource number.
201 * If sys/resource.h defines are changed, this needs
202 * to be modified accordingly!
205 #define RCS_STRING "tfdscmlun"
207 static rlim_t resource_num(int which, int ch, const char *str);
208 static void usage(const char *msg, ...);
209 static int getshelltype(void);
210 static void print_limit(rlim_t limit, unsigned divisor, const char *inf,
211 const char *pfx, const char *sfx, const char *which);
212 extern char **environ;
214 static const char rcs_string[] = RCS_STRING;
217 main(int argc, char *argv[])
219 char *p, *cls = NULL;
221 struct passwd * pwd = NULL;
222 int rcswhich, shelltype;
223 int i, num_limits = 0;
224 int ch, doeval = 0, doall = 0;
225 login_cap_t * lc = NULL;
226 enum { ANY=0, SOFT=1, HARD=2, BOTH=3, DISPLAYONLY=4 } type = ANY;
227 enum { RCSUNKNOWN=0, RCSSET=1, RCSSEL=2 } todo = RCSUNKNOWN;
228 int which_limits[RLIM_NLIMITS];
229 rlim_t set_limits[RLIM_NLIMITS];
230 struct rlimit limits[RLIM_NLIMITS];
232 /* init resource tables */
233 for (i = 0; i < RLIM_NLIMITS; i++) {
234 which_limits[i] = 0; /* Don't set/display any */
235 set_limits[i] = RLIM_INFINITY;
236 /* Get current resource values */
237 getrlimit(i, &limits[i]);
241 while ((ch = getopt(argc, argv, ":EeC:U:BSHac:d:f:l:m:n:s:t:u:")) != -1) {
257 if ((pwd = getpwnam(optarg)) == NULL) {
258 if (!isdigit(*optarg) ||
259 (pwd = getpwuid(atoi(optarg))) == NULL) {
260 usage("Invalid user `%s'\n", optarg);
274 case ':': /* Without arg */
275 if ((p = strchr(rcs_string, optopt)) != NULL) {
276 int rcswhich = p - rcs_string;
277 if (optarg && *optarg == '-') { /* 'arg' is actually a switch */
278 --optind; /* back one arg, and make arg NULL */
281 todo = optarg == NULL ? RCSSEL : RCSSET;
284 which_limits[rcswhich] = optarg ? type : DISPLAYONLY;
285 set_limits[rcswhich] = resource_num(rcswhich, optopt, optarg);
296 /* If user was specified, get class from that */
298 lc = login_getpwclass(pwd);
299 else if (cls != NULL && *cls != '\0') {
300 lc = login_getclassbyname(cls, NULL);
301 if (lc == NULL || strcmp(cls, lc->lc_class) != 0)
302 fprintf(stderr, "login class '%s' non-existent, using %s\n",
303 cls, lc?lc->lc_class:"current settings");
306 /* If we have a login class, update resource table from that */
308 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
312 /* current value overridden by resourcename or resourcename-cur */
313 sprintf(str, "%s-cur", resources[rcswhich].cap);
314 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_cur, limits[rcswhich].rlim_cur);
315 limits[rcswhich].rlim_cur = resources[rcswhich].func(lc, str, val, val);
316 /* maximum value overridden by resourcename or resourcename-max */
317 sprintf(str, "%s-max", resources[rcswhich].cap);
318 val = resources[rcswhich].func(lc, resources[rcswhich].cap, limits[rcswhich].rlim_max, limits[rcswhich].rlim_max);
319 limits[rcswhich].rlim_max = resources[rcswhich].func(lc, str, val, val);
323 /* now, let's determine what we wish to do with all this */
327 /* If we're setting limits or doing an eval (ie. we're not just
328 * displaying), then check that hard limits are not lower than
329 * soft limits, and force rasing the hard limit if we need to if
330 * we are raising the soft limit, or lower the soft limit if we
331 * are lowering the hard limit.
333 if ((*argv || doeval) && getuid() == 0) {
335 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
336 if (limits[rcswhich].rlim_max != RLIM_INFINITY) {
337 if (limits[rcswhich].rlim_cur == RLIM_INFINITY) {
338 limits[rcswhich].rlim_max = RLIM_INFINITY;
339 which_limits[rcswhich] |= HARD;
340 } else if (limits[rcswhich].rlim_cur > limits[rcswhich].rlim_max) {
341 if (which_limits[rcswhich] == SOFT) {
342 limits[rcswhich].rlim_max = limits[rcswhich].rlim_cur;
343 which_limits[rcswhich] |= HARD;
344 } else if (which_limits[rcswhich] == HARD) {
345 limits[rcswhich].rlim_cur = limits[rcswhich].rlim_max;
346 which_limits[rcswhich] |= SOFT;
348 /* else.. if we're specifically setting both to
349 * silly values, then let it error out.
357 /* See if we've overridden anything specific on the command line */
358 if (num_limits && todo == RCSSET) {
359 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
360 if (which_limits[rcswhich] & HARD)
361 limits[rcswhich].rlim_max = set_limits[rcswhich];
362 if (which_limits[rcswhich] & SOFT)
363 limits[rcswhich].rlim_cur = set_limits[rcswhich];
367 /* If *argv is not NULL, then we are being asked to
368 * (perhaps) set environment variables and run a program
372 usage("-e cannot be used with `cmd' option\n");
376 /* set leading environment variables, like eval(1) */
377 while (*argv && (p = strchr(*argv, '=')))
378 (void)setenv(*argv++, ++p, 1);
381 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
382 if (doall || num_limits == 0 || which_limits[rcswhich] != 0)
383 if (setrlimit(rcswhich, &limits[rcswhich]) == -1)
384 err(1, "setrlimit %s", resources[rcswhich].cap);
394 shelltype = doeval ? getshelltype() : SH_NONE;
396 if (type == ANY) /* Default to soft limits */
400 printf(shellparm[shelltype].cmd,
401 lc ? " for class " : " (current)",
402 lc ? lc->lc_class : "");
404 for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) {
405 if (doall || num_limits == 0 || which_limits[rcswhich] != 0) {
406 if (which_limits[rcswhich] == ANY || which_limits[rcswhich])
407 which_limits[rcswhich] = type;
408 if (shellparm[shelltype].lprm[rcswhich].pfx) {
409 if (shellparm[shelltype].both && limits[rcswhich].rlim_cur == limits[rcswhich].rlim_max) {
410 print_limit(limits[rcswhich].rlim_max,
411 shellparm[shelltype].lprm[rcswhich].divisor,
412 shellparm[shelltype].inf,
413 shellparm[shelltype].lprm[rcswhich].pfx,
414 shellparm[shelltype].lprm[rcswhich].sfx,
415 shellparm[shelltype].both);
417 if (which_limits[rcswhich] & HARD) {
418 print_limit(limits[rcswhich].rlim_max,
419 shellparm[shelltype].lprm[rcswhich].divisor,
420 shellparm[shelltype].inf,
421 shellparm[shelltype].lprm[rcswhich].pfx,
422 shellparm[shelltype].lprm[rcswhich].sfx,
423 shellparm[shelltype].hard);
425 if (which_limits[rcswhich] & SOFT) {
426 print_limit(limits[rcswhich].rlim_cur,
427 shellparm[shelltype].lprm[rcswhich].divisor,
428 shellparm[shelltype].inf,
429 shellparm[shelltype].lprm[rcswhich].pfx,
430 shellparm[shelltype].lprm[rcswhich].sfx,
431 shellparm[shelltype].soft);
444 usage(char const *msg, ...)
449 vfprintf(stderr, msg, argp);
452 (void)fprintf(stderr,
453 "usage: limits [-C class|-U user] [-eaSHBE] [-cdflmnstu [val]] [[name=val ...] cmd]\n");
458 print_limit(rlim_t limit, unsigned divisor, const char * inf, const char * pfx, const char * sfx, const char * which)
462 if (limit == RLIM_INFINITY)
465 sprintf(numbr, "%qd", (quad_t)((limit + divisor/2) / divisor));
466 printf(pfx, which, numbr);
473 resource_num(int which, int ch, const char *str)
475 rlim_t res = RLIM_INFINITY;
478 !(strcasecmp(str, "inf") == 0 ||
479 strcasecmp(str, "infinity") == 0 ||
480 strcasecmp(str, "unlimit") == 0 ||
481 strcasecmp(str, "unlimited") == 0)) {
482 const char * s = str;
486 case RLIMIT_CPU: /* time values */
490 rlim_t tim = strtoq(s, &e, 0);
491 if (e == NULL || e == s || errno)
494 case 0: /* end of string */
497 case 's': case 'S': /* seconds */
499 case 'm': case 'M': /* minutes */
502 case 'h': case 'H': /* hours */
505 case 'd': case 'D': /* days */
506 tim *= (60L * 60L * 24L);
508 case 'w': case 'W': /* weeks */
509 tim *= (60L * 60L * 24L * 7L);
510 case 'y': case 'Y': /* Years */
511 tim *= (60L * 60L * 24L * 365L);
517 case RLIMIT_FSIZE: /* Size values */
526 rlim_t mult, tim = strtoq(s, &e, 0);
527 if (e == NULL || e == s || errno)
530 case 0: /* end of string */
535 case 'b': case 'B': /* 512-byte blocks */
538 case 'k': case 'K': /* 1024-byte Kilobytes */
541 case 'm': case 'M': /* 1024-k kbytes */
544 case 'g': case 'G': /* 1Gbyte */
545 mult = 1024 * 1024 * 1024;
547 case 't': case 'T': /* 1TBte */
548 mult = 1024LL * 1024LL * 1024LL * 1024LL;
557 res = strtoq(s, &e, 0);
562 usage("invalid value -%c `%s'\n", ch, str);
569 getshellbyname(const char * shell)
573 const char * p = strrchr(shell, '/');
576 for (i = 0; (q = shellparm[i].name) != NULL; i++) {
578 int j = strcspn(q, "|");
582 if (strncmp(p, q, j) == 0)
593 * Determine the type of shell our parent process is
594 * This is quite tricky, not 100% reliable and probably
595 * not nearly as thorough as it should be. Basically, this
596 * is a "best guess" only, but hopefully will work in
603 pid_t ppid = getppid();
608 char procdir[MAXPATHLEN], buf[128];
609 int l = sprintf(procdir, "/proc/%ld/", (long)ppid);
610 char * shell = getenv("SHELL");
612 if (shell != NULL && stat(shell, &st) != -1) {
615 strcpy(procdir+l, "file");
616 /* $SHELL is actual shell? */
617 if (stat(procdir, &st1) != -1 && memcmp(&st, &st1, sizeof st) == 0)
618 return getshellbyname(shell);
620 strcpy(procdir+l, "status");
621 if (stat(procdir, &st) == 0 && (fp = fopen(procdir, "r")) != NULL) {
622 char * p = fgets(buf, sizeof buf, fp)==NULL ? NULL : strtok(buf, " \t");
625 return getshellbyname(p);