1 This file is ulimit.def, from which is created ulimit.c.
2 It implements the builtin "ulimit" in Bash.
4 Copyright (C) 1987-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $FUNCTION ulimit_builtin
26 $SHORT_DOC ulimit [-SHabcdefiklmnpqrstuvxPT] [limit]
27 Modify shell resource limits.
29 Provides control over the resources available to the shell and processes
30 it creates, on systems that allow such control.
33 -S use the `soft' resource limit
34 -H use the `hard' resource limit
35 -a all current limits are reported
36 -b the socket buffer size
37 -c the maximum size of core files created
38 -d the maximum size of a process's data segment
39 -e the maximum scheduling priority (`nice')
40 -f the maximum size of files written by the shell and its children
41 -i the maximum number of pending signals
42 -k the maximum number of kqueues allocated for this process
43 -l the maximum size a process may lock into memory
44 -m the maximum resident set size
45 -n the maximum number of open file descriptors
46 -p the pipe buffer size
47 -q the maximum number of bytes in POSIX message queues
48 -r the maximum real-time scheduling priority
49 -s the maximum stack size
50 -t the maximum amount of cpu time in seconds
51 -u the maximum number of user processes
52 -v the size of virtual memory
53 -x the maximum number of file locks
54 -P the maximum number of pseudoterminals
55 -R the maximum time a real-time process can run before blocking
56 -T the maximum number of threads
58 Not all options are available on all platforms.
60 If LIMIT is given, it is the new value of the specified resource; the
61 special LIMIT values `soft', `hard', and `unlimited' stand for the
62 current soft limit, the current hard limit, and no limit, respectively.
63 Otherwise, the current value of the specified resource is printed. If
64 no option is given, then -f is assumed.
66 Values are in 1024-byte increments, except for -t, which is in seconds,
67 -p, which is in increments of 512 bytes, and -u, which is an unscaled
71 Returns success unless an invalid option is supplied or an error occurs.
78 #include "../bashtypes.h"
79 #if defined (HAVE_SYS_PARAM_H)
80 # include <sys/param.h>
83 #if defined (HAVE_UNISTD_H)
90 #include "../bashintl.h"
94 #include "bashgetopt.h"
101 /* For some reason, HPUX chose to make these definitions visible only if
102 _KERNEL is defined, so we define _KERNEL before including <sys/resource.h>
103 and #undef it afterward. */
104 #if defined (HAVE_RESOURCE)
105 # include <sys/time.h>
106 # if defined (HPUX) && defined (RLIMIT_NEEDS_KERNEL)
109 # include <sys/resource.h>
110 # if defined (HPUX) && defined (RLIMIT_NEEDS_KERNEL)
113 #elif defined (HAVE_SYS_TIMES_H)
114 # include <sys/times.h>
117 #if defined (HAVE_LIMITS_H)
121 /* Check for the most basic symbols. If they aren't present, this
122 system's <sys/resource.h> isn't very useful to us. */
123 #if !defined (RLIMIT_FSIZE) || !defined (HAVE_GETRLIMIT)
124 # undef HAVE_RESOURCE
127 #if !defined (HAVE_RESOURCE) && defined (HAVE_ULIMIT_H)
131 #if !defined (RLIMTYPE)
132 # define RLIMTYPE long
133 # define string_to_rlimtype(s) strtol(s, (char **)NULL, 10)
134 # define print_rlimtype(num, nl) printf ("%ld%s", num, nl ? "\n" : "")
137 /* Alternate names */
139 /* Some systems use RLIMIT_NOFILE, others use RLIMIT_OFILE */
140 #if defined (HAVE_RESOURCE) && defined (RLIMIT_OFILE) && !defined (RLIMIT_NOFILE)
141 # define RLIMIT_NOFILE RLIMIT_OFILE
142 #endif /* HAVE_RESOURCE && RLIMIT_OFILE && !RLIMIT_NOFILE */
144 #if defined (HAVE_RESOURCE) && defined (RLIMIT_POSIXLOCKS) && !defined (RLIMIT_LOCKS)
145 # define RLIMIT_LOCKS RLIMIT_POSIXLOCKS
146 #endif /* HAVE_RESOURCE && RLIMIT_POSIXLOCKS && !RLIMIT_LOCKS */
148 /* Some systems have these, some do not. */
150 # define RLIMIT_FILESIZE RLIMIT_FSIZE
152 # define RLIMIT_FILESIZE 256
155 #define RLIMIT_PIPESIZE 257
158 # define RLIMIT_OPENFILES RLIMIT_NOFILE
160 # define RLIMIT_OPENFILES 258
164 # define RLIMIT_VIRTMEM RLIMIT_VMEM
165 # define RLIMIT_VMBLKSZ 1024
168 # define RLIMIT_VIRTMEM RLIMIT_AS
169 # define RLIMIT_VMBLKSZ 1024
171 # define RLIMIT_VIRTMEM 259
172 # define RLIMIT_VMBLKSZ 1
177 # define RLIMIT_MAXUPROC RLIMIT_NPROC
179 # define RLIMIT_MAXUPROC 260
182 #if !defined (RLIMIT_PTHREAD) && defined (RLIMIT_NTHR)
183 # define RLIMIT_PTHREAD RLIMIT_NTHR
186 #if !defined (RLIM_INFINITY)
187 # define RLIM_INFINITY 0x7fffffff
190 #if !defined (RLIM_SAVED_CUR)
191 # define RLIM_SAVED_CUR RLIM_INFINITY
194 #if !defined (RLIM_SAVED_MAX)
195 # define RLIM_SAVED_MAX RLIM_INFINITY
198 #define LIMIT_HARD 0x01
199 #define LIMIT_SOFT 0x02
201 /* "Blocks" are defined as 512 bytes when in Posix mode and 1024 bytes
205 #define BLOCKSIZE(x) (((x) == POSIXBLK) ? (posixly_correct ? 512 : 1024) : (x))
207 static int _findlim PARAMS((int));
209 static int ulimit_internal PARAMS((int, char *, int, int));
211 static int get_limit PARAMS((int, RLIMTYPE *, RLIMTYPE *));
212 static int set_limit PARAMS((int, RLIMTYPE, int));
214 static void printone PARAMS((int, RLIMTYPE, int));
215 static void print_all_limits PARAMS((int));
217 static int set_all_limits PARAMS((int, RLIMTYPE));
219 static int filesize PARAMS((RLIMTYPE *));
220 static int pipesize PARAMS((RLIMTYPE *));
221 static int getmaxuprc PARAMS((RLIMTYPE *));
222 static int getmaxvm PARAMS((RLIMTYPE *, RLIMTYPE *));
225 int option; /* The ulimit option for this limit. */
226 int parameter; /* Parameter to pass to get_limit (). */
227 int block_factor; /* Blocking factor for specific limit. */
228 const char * const description; /* Descriptive string to output. */
229 const char * const units; /* scale */
232 static RESOURCE_LIMITS limits[] = {
234 { 'P', RLIMIT_NPTS, 1, "number of pseudoterminals", (char *)NULL },
237 { 'R', RLIMIT_RTTIME, 1, "real-time non-blocking time", "microseconds" },
239 #ifdef RLIMIT_PTHREAD
240 { 'T', RLIMIT_PTHREAD, 1, "number of threads", (char *)NULL },
243 { 'b', RLIMIT_SBSIZE, 1, "socket buffer size", "bytes" },
246 { 'c', RLIMIT_CORE, POSIXBLK, "core file size", "blocks" },
249 { 'd', RLIMIT_DATA, 1024, "data seg size", "kbytes" },
252 { 'e', RLIMIT_NICE, 1, "scheduling priority", (char *)NULL },
254 { 'f', RLIMIT_FILESIZE, POSIXBLK, "file size", "blocks" },
255 #ifdef RLIMIT_SIGPENDING
256 { 'i', RLIMIT_SIGPENDING, 1, "pending signals", (char *)NULL },
258 #ifdef RLIMIT_KQUEUES
259 { 'k', RLIMIT_KQUEUES, 1, "max kqueues", (char *)NULL },
261 #ifdef RLIMIT_MEMLOCK
262 { 'l', RLIMIT_MEMLOCK, 1024, "max locked memory", "kbytes" },
265 { 'm', RLIMIT_RSS, 1024, "max memory size", "kbytes" },
266 #endif /* RLIMIT_RSS */
267 { 'n', RLIMIT_OPENFILES, 1, "open files", (char *)NULL},
268 { 'p', RLIMIT_PIPESIZE, 512, "pipe size", "512 bytes" },
269 #ifdef RLIMIT_MSGQUEUE
270 { 'q', RLIMIT_MSGQUEUE, 1, "POSIX message queues", "bytes" },
273 { 'r', RLIMIT_RTPRIO, 1, "real-time priority", (char *)NULL },
276 { 's', RLIMIT_STACK, 1024, "stack size", "kbytes" },
279 { 't', RLIMIT_CPU, 1, "cpu time", "seconds" },
280 #endif /* RLIMIT_CPU */
281 { 'u', RLIMIT_MAXUPROC, 1, "max user processes", (char *)NULL },
282 #if defined (HAVE_RESOURCE)
283 { 'v', RLIMIT_VIRTMEM, RLIMIT_VMBLKSZ, "virtual memory", "kbytes" },
286 { 'w', RLIMIT_SWAP, 1024, "swap size", "kbytes" },
289 { 'x', RLIMIT_LOCKS, 1, "file locks", (char *)NULL },
291 { -1, -1, -1, (char *)NULL, (char *)NULL }
293 #define NCMDS (sizeof(limits) / sizeof(limits[0]))
295 typedef struct _cmd {
300 static ULCMD *cmdlist;
302 static int cmdlistsz;
304 #if !defined (HAVE_RESOURCE) && !defined (HAVE_ULIMIT)
313 #endif /* !HAVE_RESOURCE && !HAVE_ULIMIT */
321 for (i = 0; limits[i].option > 0; i++)
322 if (limits[i].option == opt)
327 static char optstring[4 + 2 * NCMDS];
329 /* Report or set limits associated with certain per-process resources.
330 See the help documentation in builtins.c for a full description. */
332 ulimit_builtin (list)
333 register WORD_LIST *list;
336 int c, limind, mode, opt, all_limits;
342 /* Idea stolen from pdksh -- build option string the first time called. */
343 if (optstring[0] == 0)
346 *s++ = 'a'; *s++ = 'S'; *s++ = 'H';
347 for (c = 0; limits[c].option > 0; c++)
349 *s++ = limits[c].option;
355 /* Initialize the command list. */
357 cmdlist = (ULCMD *)xmalloc ((cmdlistsz = 16) * sizeof (ULCMD));
360 reset_internal_getopt ();
361 while ((opt = internal_getopt (list, optstring)) != -1)
369 /* -S and -H are modifiers, not real options. */
384 if (ncmd >= cmdlistsz)
385 cmdlist = (ULCMD *)xrealloc (cmdlist, (cmdlistsz *= 2) * sizeof (ULCMD));
386 cmdlist[ncmd].cmd = opt;
387 cmdlist[ncmd++].arg = list_optarg;
396 if (list) /* setting */
398 if (STREQ (list->word->word, "unlimited") == 0)
400 builtin_error (_("%s: invalid limit argument"), list->word->word);
401 return (EXECUTION_FAILURE);
403 return (set_all_limits (mode == 0 ? LIMIT_SOFT|LIMIT_HARD : mode, RLIM_INFINITY));
406 print_all_limits (mode == 0 ? LIMIT_SOFT : mode);
407 return (sh_chkwrite (EXECUTION_SUCCESS));
410 /* default is `ulimit -f' */
413 cmdlist[ncmd].cmd = 'f';
414 /* `ulimit something' is same as `ulimit -f something' */
415 cmdlist[ncmd++].arg = list ? list->word->word : (char *)NULL;
420 /* verify each command in the list. */
421 for (c = 0; c < ncmd; c++)
423 limind = _findlim (cmdlist[c].cmd);
426 builtin_error (_("`%c': bad command"), cmdlist[c].cmd);
431 for (c = 0; c < ncmd; c++)
432 if (ulimit_internal (cmdlist[c].cmd, cmdlist[c].arg, mode, ncmd > 1) == EXECUTION_FAILURE)
433 return (EXECUTION_FAILURE);
435 return (EXECUTION_SUCCESS);
439 ulimit_internal (cmd, cmdarg, mode, multiple)
444 int opt, limind, setting;
446 RLIMTYPE soft_limit, hard_limit, real_limit, limit;
448 setting = cmdarg != 0;
449 limind = _findlim (cmd);
451 mode = setting ? (LIMIT_HARD|LIMIT_SOFT) : LIMIT_SOFT;
452 opt = get_limit (limind, &soft_limit, &hard_limit);
455 builtin_error (_("%s: cannot get limit: %s"), limits[limind].description,
457 return (EXECUTION_FAILURE);
460 if (setting == 0) /* print the value of the specified limit */
462 printone (limind, (mode & LIMIT_SOFT) ? soft_limit : hard_limit, multiple);
463 return (EXECUTION_SUCCESS);
466 /* Setting the limit. */
467 if (STREQ (cmdarg, "hard"))
468 real_limit = hard_limit;
469 else if (STREQ (cmdarg, "soft"))
470 real_limit = soft_limit;
471 else if (STREQ (cmdarg, "unlimited"))
472 real_limit = RLIM_INFINITY;
473 else if (all_digits (cmdarg))
475 limit = string_to_rlimtype (cmdarg);
476 block_factor = BLOCKSIZE(limits[limind].block_factor);
477 real_limit = limit * block_factor;
479 if ((real_limit / block_factor) != limit)
481 sh_erange (cmdarg, _("limit"));
482 return (EXECUTION_FAILURE);
487 sh_invalidnum (cmdarg);
488 return (EXECUTION_FAILURE);
491 if (set_limit (limind, real_limit, mode) < 0)
493 builtin_error (_("%s: cannot modify limit: %s"), limits[limind].description,
495 return (EXECUTION_FAILURE);
498 return (EXECUTION_SUCCESS);
502 get_limit (ind, softlim, hardlim)
504 RLIMTYPE *softlim, *hardlim;
507 #if defined (HAVE_RESOURCE)
511 if (limits[ind].parameter >= 256)
513 switch (limits[ind].parameter)
515 case RLIMIT_FILESIZE:
516 if (filesize (&value) < 0)
519 case RLIMIT_PIPESIZE:
520 if (pipesize (&value) < 0)
523 case RLIMIT_OPENFILES:
524 value = (RLIMTYPE)getdtablesize ();
527 return (getmaxvm (softlim, hardlim));
528 case RLIMIT_MAXUPROC:
529 if (getmaxuprc (&value) < 0)
536 *softlim = *hardlim = value;
541 #if defined (HAVE_RESOURCE)
542 if (getrlimit (limits[ind].parameter, &limit) < 0)
544 *softlim = limit.rlim_cur;
545 *hardlim = limit.rlim_max;
547 if (limits[ind].parameter == RLIMIT_FILESIZE)
550 *hardlim *= 512; /* Ugh. */
563 set_limit (ind, newlim, mode)
568 #if defined (HAVE_RESOURCE)
573 if (limits[ind].parameter >= 256)
574 switch (limits[ind].parameter)
576 case RLIMIT_FILESIZE:
577 #if !defined (HAVE_RESOURCE)
578 return (ulimit (2, newlim / 512L));
584 case RLIMIT_OPENFILES:
585 #if defined (HAVE_SETDTABLESIZE)
586 # if defined (__CYGWIN__)
587 /* Grrr... Cygwin declares setdtablesize as void. */
588 setdtablesize (newlim);
591 return (setdtablesize (newlim));
594 case RLIMIT_PIPESIZE:
596 case RLIMIT_MAXUPROC:
603 #if defined (HAVE_RESOURCE)
604 if (getrlimit (limits[ind].parameter, &limit) < 0)
607 if (limits[ind].parameter == RLIMIT_FILESIZE)
608 newlim /= 512; /* Ugh. */
610 val = (current_user.euid != 0 && newlim == RLIM_INFINITY &&
611 (mode & LIMIT_HARD) == 0 && /* XXX -- test */
612 (limit.rlim_cur <= limit.rlim_max))
613 ? limit.rlim_max : newlim;
614 if (mode & LIMIT_SOFT)
615 limit.rlim_cur = val;
616 if (mode & LIMIT_HARD)
617 limit.rlim_max = val;
619 return (setrlimit (limits[ind].parameter, &limit));
628 getmaxvm (softlim, hardlim)
629 RLIMTYPE *softlim, *hardlim;
631 #if defined (HAVE_RESOURCE)
632 struct rlimit datalim, stacklim;
634 if (getrlimit (RLIMIT_DATA, &datalim) < 0)
637 if (getrlimit (RLIMIT_STACK, &stacklim) < 0)
640 /* Protect against overflow. */
641 *softlim = (datalim.rlim_cur / 1024L) + (stacklim.rlim_cur / 1024L);
642 *hardlim = (datalim.rlim_max / 1024L) + (stacklim.rlim_max / 1024L);
647 #endif /* HAVE_RESOURCE */
654 #if !defined (HAVE_RESOURCE)
656 if ((result = ulimit (1, 0L)) < 0)
659 *valuep = (RLIMTYPE) result * 512;
671 #if defined (PIPE_BUF)
672 /* This is defined on Posix systems. */
673 *valuep = (RLIMTYPE) PIPE_BUF;
676 # if defined (_POSIX_PIPE_BUF)
677 *valuep = (RLIMTYPE) _POSIX_PIPE_BUF;
680 # if defined (PIPESIZE)
681 /* This is defined by running a program from the Makefile. */
682 *valuep = (RLIMTYPE) PIPESIZE;
687 # endif /* PIPESIZE */
688 # endif /* _POSIX_PIPE_BUF */
689 #endif /* PIPE_BUF */
698 maxchild = getmaxchild ();
706 *valuep = (RLIMTYPE) maxchild;
712 print_all_limits (mode)
716 RLIMTYPE softlim, hardlim;
721 for (i = 0; limits[i].option > 0; i++)
723 if (get_limit (i, &softlim, &hardlim) == 0)
724 printone (i, (mode & LIMIT_SOFT) ? softlim : hardlim, 1);
725 else if (errno != EINVAL)
726 builtin_error ("%s: cannot get limit: %s", limits[i].description,
732 printone (limind, curlim, pdesc)
740 factor = BLOCKSIZE(limits[limind].block_factor);
743 if (limits[limind].units)
744 sprintf (unitstr, "(%s, -%c) ", limits[limind].units, limits[limind].option);
746 sprintf (unitstr, "(-%c) ", limits[limind].option);
748 printf ("%-20s %20s", limits[limind].description, unitstr);
750 if (curlim == RLIM_INFINITY)
752 else if (curlim == RLIM_SAVED_MAX)
754 else if (curlim == RLIM_SAVED_CUR)
757 print_rlimtype ((curlim / factor), 1);
760 /* Set all limits to NEWLIM. NEWLIM currently must be RLIM_INFINITY, which
761 causes all limits to be set as high as possible depending on mode (like
762 csh `unlimit'). Returns -1 if NEWLIM is invalid, 0 if all limits
763 were set successfully, and 1 if at least one limit could not be set.
765 To raise all soft limits to their corresponding hard limits, use
766 ulimit -S -a unlimited
767 To attempt to raise all hard limits to infinity (superuser-only), use
768 ulimit -H -a unlimited
769 To attempt to raise all soft and hard limits to infinity, use
774 set_all_limits (mode, newlim)
781 if (newlim != RLIM_INFINITY)
788 mode = LIMIT_SOFT|LIMIT_HARD;
790 for (retval = i = 0; limits[i].option > 0; i++)
791 if (set_limit (i, newlim, mode) < 0)
793 builtin_error (_("%s: cannot modify limit: %s"), limits[i].description,