1 This file is ulimit.def, from which is created ulimit.c.
2 It implements the builtin "ulimit" in Bash.
4 Copyright (C) 1987-2021 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 [-SHabcdefiklmnpqrstuvxPRT] [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 /* POSIX compatibility. If the last item in cmdlist does not have an option
432 argument, but there is an operand (list != 0), treat the operand as if
433 it were an option argument for that last command. */
434 if (list && list->word && cmdlist[ncmd - 1].arg == 0)
436 cmdlist[ncmd - 1].arg = list->word->word;
440 for (c = 0; c < ncmd; c++)
441 if (ulimit_internal (cmdlist[c].cmd, cmdlist[c].arg, mode, ncmd > 1) == EXECUTION_FAILURE)
442 return (EXECUTION_FAILURE);
444 return (EXECUTION_SUCCESS);
448 ulimit_internal (cmd, cmdarg, mode, multiple)
453 int opt, limind, setting;
455 RLIMTYPE soft_limit, hard_limit, real_limit, limit;
457 setting = cmdarg != 0;
458 limind = _findlim (cmd);
460 mode = setting ? (LIMIT_HARD|LIMIT_SOFT) : LIMIT_SOFT;
461 opt = get_limit (limind, &soft_limit, &hard_limit);
464 builtin_error (_("%s: cannot get limit: %s"), limits[limind].description,
466 return (EXECUTION_FAILURE);
469 if (setting == 0) /* print the value of the specified limit */
471 printone (limind, (mode & LIMIT_SOFT) ? soft_limit : hard_limit, multiple);
472 return (EXECUTION_SUCCESS);
475 /* Setting the limit. */
476 if (STREQ (cmdarg, "hard"))
477 real_limit = hard_limit;
478 else if (STREQ (cmdarg, "soft"))
479 real_limit = soft_limit;
480 else if (STREQ (cmdarg, "unlimited"))
481 real_limit = RLIM_INFINITY;
482 else if (all_digits (cmdarg))
484 limit = string_to_rlimtype (cmdarg);
485 block_factor = BLOCKSIZE(limits[limind].block_factor);
486 real_limit = limit * block_factor;
488 if ((real_limit / block_factor) != limit)
490 sh_erange (cmdarg, _("limit"));
491 return (EXECUTION_FAILURE);
496 sh_invalidnum (cmdarg);
497 return (EXECUTION_FAILURE);
500 if (set_limit (limind, real_limit, mode) < 0)
502 builtin_error (_("%s: cannot modify limit: %s"), limits[limind].description,
504 return (EXECUTION_FAILURE);
507 return (EXECUTION_SUCCESS);
511 get_limit (ind, softlim, hardlim)
513 RLIMTYPE *softlim, *hardlim;
516 #if defined (HAVE_RESOURCE)
520 if (limits[ind].parameter >= 256)
522 switch (limits[ind].parameter)
524 case RLIMIT_FILESIZE:
525 if (filesize (&value) < 0)
528 case RLIMIT_PIPESIZE:
529 if (pipesize (&value) < 0)
532 case RLIMIT_OPENFILES:
533 value = (RLIMTYPE)getdtablesize ();
536 return (getmaxvm (softlim, hardlim));
537 case RLIMIT_MAXUPROC:
538 if (getmaxuprc (&value) < 0)
545 *softlim = *hardlim = value;
550 #if defined (HAVE_RESOURCE)
551 if (getrlimit (limits[ind].parameter, &limit) < 0)
553 *softlim = limit.rlim_cur;
554 *hardlim = limit.rlim_max;
556 if (limits[ind].parameter == RLIMIT_FILESIZE)
559 *hardlim *= 512; /* Ugh. */
572 set_limit (ind, newlim, mode)
577 #if defined (HAVE_RESOURCE)
582 if (limits[ind].parameter >= 256)
583 switch (limits[ind].parameter)
585 case RLIMIT_FILESIZE:
586 #if !defined (HAVE_RESOURCE)
587 return (ulimit (2, newlim / 512L));
593 case RLIMIT_OPENFILES:
594 #if defined (HAVE_SETDTABLESIZE)
595 # if defined (__CYGWIN__)
596 /* Grrr... Cygwin declares setdtablesize as void. */
597 setdtablesize (newlim);
600 return (setdtablesize (newlim));
603 case RLIMIT_PIPESIZE:
605 case RLIMIT_MAXUPROC:
612 #if defined (HAVE_RESOURCE)
613 if (getrlimit (limits[ind].parameter, &limit) < 0)
616 if (limits[ind].parameter == RLIMIT_FILESIZE)
617 newlim /= 512; /* Ugh. */
619 val = (current_user.euid != 0 && newlim == RLIM_INFINITY &&
620 (mode & LIMIT_HARD) == 0 && /* XXX -- test */
621 (limit.rlim_cur <= limit.rlim_max))
622 ? limit.rlim_max : newlim;
623 if (mode & LIMIT_SOFT)
624 limit.rlim_cur = val;
625 if (mode & LIMIT_HARD)
626 limit.rlim_max = val;
628 return (setrlimit (limits[ind].parameter, &limit));
637 getmaxvm (softlim, hardlim)
638 RLIMTYPE *softlim, *hardlim;
640 #if defined (HAVE_RESOURCE)
641 struct rlimit datalim, stacklim;
643 if (getrlimit (RLIMIT_DATA, &datalim) < 0)
646 if (getrlimit (RLIMIT_STACK, &stacklim) < 0)
649 /* Protect against overflow. */
650 *softlim = (datalim.rlim_cur / 1024L) + (stacklim.rlim_cur / 1024L);
651 *hardlim = (datalim.rlim_max / 1024L) + (stacklim.rlim_max / 1024L);
656 #endif /* HAVE_RESOURCE */
663 #if !defined (HAVE_RESOURCE)
665 if ((result = ulimit (1, 0L)) < 0)
668 *valuep = (RLIMTYPE) result * 512;
680 #if defined (PIPE_BUF)
681 /* This is defined on Posix systems. */
682 *valuep = (RLIMTYPE) PIPE_BUF;
685 # if defined (_POSIX_PIPE_BUF)
686 *valuep = (RLIMTYPE) _POSIX_PIPE_BUF;
689 # if defined (PIPESIZE)
690 /* This is defined by running a program from the Makefile. */
691 *valuep = (RLIMTYPE) PIPESIZE;
696 # endif /* PIPESIZE */
697 # endif /* _POSIX_PIPE_BUF */
698 #endif /* PIPE_BUF */
707 maxchild = getmaxchild ();
715 *valuep = (RLIMTYPE) maxchild;
721 print_all_limits (mode)
725 RLIMTYPE softlim, hardlim;
730 for (i = 0; limits[i].option > 0; i++)
732 if (get_limit (i, &softlim, &hardlim) == 0)
733 printone (i, (mode & LIMIT_SOFT) ? softlim : hardlim, 1);
734 else if (errno != EINVAL)
735 builtin_error ("%s: cannot get limit: %s", limits[i].description,
741 printone (limind, curlim, pdesc)
749 factor = BLOCKSIZE(limits[limind].block_factor);
752 if (limits[limind].units)
753 sprintf (unitstr, "(%s, -%c) ", limits[limind].units, limits[limind].option);
755 sprintf (unitstr, "(-%c) ", limits[limind].option);
757 printf ("%-20s %20s", limits[limind].description, unitstr);
759 if (curlim == RLIM_INFINITY)
761 else if (curlim == RLIM_SAVED_MAX)
763 else if (curlim == RLIM_SAVED_CUR)
766 print_rlimtype ((curlim / factor), 1);
769 /* Set all limits to NEWLIM. NEWLIM currently must be RLIM_INFINITY, which
770 causes all limits to be set as high as possible depending on mode (like
771 csh `unlimit'). Returns -1 if NEWLIM is invalid, 0 if all limits
772 were set successfully, and 1 if at least one limit could not be set.
774 To raise all soft limits to their corresponding hard limits, use
775 ulimit -S -a unlimited
776 To attempt to raise all hard limits to infinity (superuser-only), use
777 ulimit -H -a unlimited
778 To attempt to raise all soft and hard limits to infinity, use
783 set_all_limits (mode, newlim)
790 if (newlim != RLIM_INFINITY)
797 mode = LIMIT_SOFT|LIMIT_HARD;
799 for (retval = i = 0; limits[i].option > 0; i++)
800 if (set_limit (i, newlim, mode) < 0)
802 builtin_error (_("%s: cannot modify limit: %s"), limits[i].description,