1 This file is ulimit.def, from which is created ulimit.c.
2 It implements the builtin "ulimit" in Bash.
4 Copyright (C) 1987-2005 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION ulimit_builtin
27 $SHORT_DOC ulimit [-SHacdfilmnpqstuvx] [limit]
28 Ulimit provides control over the resources available to processes
29 started by the shell, on systems that allow such control. If an
30 option is given, it is interpreted as follows:
32 -S use the `soft' resource limit
33 -H use the `hard' resource limit
34 -a all current limits are reported
35 -c the maximum size of core files created
36 -d the maximum size of a process's data segment
37 -f the maximum size of files created by the shell
38 -i the maximum number of pending signals
39 -l the maximum size a process may lock into memory
40 -m the maximum resident set size
41 -n the maximum number of open file descriptors
42 -p the pipe buffer size
43 -q the maximum number of bytes in POSIX message queues
44 -s the maximum stack size
45 -t the maximum amount of cpu time in seconds
46 -u the maximum number of user processes
47 -v the size of virtual memory
48 -x the maximum number of file locks
50 If LIMIT is given, it is the new value of the specified resource;
51 the special LIMIT values `soft', `hard', and `unlimited' stand for
52 the current soft limit, the current hard limit, and no limit, respectively.
53 Otherwise, the current value of the specified resource is printed.
54 If no option is given, then -f is assumed. Values are in 1024-byte
55 increments, except for -t, which is in seconds, -p, which is in
56 increments of 512 bytes, and -u, which is an unscaled number of
64 #include "../bashtypes.h"
66 # include <sys/param.h>
69 #if defined (HAVE_UNISTD_H)
76 #include "../bashintl.h"
80 #include "bashgetopt.h"
87 /* For some reason, HPUX chose to make these definitions visible only if
88 _KERNEL is defined, so we define _KERNEL before including <sys/resource.h>
89 and #undef it afterward. */
90 #if defined (HAVE_RESOURCE)
91 # include <sys/time.h>
92 # if defined (HPUX) && defined (RLIMIT_NEEDS_KERNEL)
95 # include <sys/resource.h>
96 # if defined (HPUX) && defined (RLIMIT_NEEDS_KERNEL)
100 # include <sys/times.h>
103 #if defined (HAVE_LIMITS_H)
107 /* Check for the most basic symbols. If they aren't present, this
108 system's <sys/resource.h> isn't very useful to us. */
109 #if !defined (RLIMIT_FSIZE) || !defined (HAVE_GETRLIMIT)
110 # undef HAVE_RESOURCE
113 #if !defined (RLIMTYPE)
114 # define RLIMTYPE long
115 # define string_to_rlimtype(s) strtol(s, (char **)NULL, 10)
116 # define print_rlimtype(num, nl) printf ("%ld%s", num, nl ? "\n" : "")
119 /* Some systems use RLIMIT_NOFILE, others use RLIMIT_OFILE */
120 #if defined (HAVE_RESOURCE) && defined (RLIMIT_OFILE) && !defined (RLIMIT_NOFILE)
121 # define RLIMIT_NOFILE RLIMIT_OFILE
122 #endif /* HAVE_RESOURCE && RLIMIT_OFILE && !RLIMIT_NOFILE */
124 /* Some systems have these, some do not. */
126 # define RLIMIT_FILESIZE RLIMIT_FSIZE
128 # define RLIMIT_FILESIZE 256
131 #define RLIMIT_PIPESIZE 257
134 # define RLIMIT_OPENFILES RLIMIT_NOFILE
136 # define RLIMIT_OPENFILES 258
140 # define RLIMIT_VIRTMEM RLIMIT_VMEM
141 # define RLIMIT_VMBLKSZ 1024
144 # define RLIMIT_VIRTMEM RLIMIT_AS
145 # define RLIMIT_VMBLKSZ 1024
147 # define RLIMIT_VIRTMEM 259
148 # define RLIMIT_VMBLKSZ 1
153 # define RLIMIT_MAXUPROC RLIMIT_NPROC
155 # define RLIMIT_MAXUPROC 260
158 #if !defined (RLIM_INFINITY)
159 # define RLIM_INFINITY 0x7fffffff
162 #if !defined (RLIM_SAVED_CUR)
163 # define RLIM_SAVED_CUR RLIM_INFINITY
166 #if !defined (RLIM_SAVED_MAX)
167 # define RLIM_SAVED_MAX RLIM_INFINITY
170 #define LIMIT_HARD 0x01
171 #define LIMIT_SOFT 0x02
173 static int _findlim __P((int));
175 static int ulimit_internal __P((int, char *, int, int));
177 static int get_limit __P((int, RLIMTYPE *, RLIMTYPE *));
178 static int set_limit __P((int, RLIMTYPE, int));
180 static void printone __P((int, RLIMTYPE, int));
181 static void print_all_limits __P((int));
183 static int set_all_limits __P((int, RLIMTYPE));
185 static int filesize __P((RLIMTYPE *));
186 static int pipesize __P((RLIMTYPE *));
187 static int getmaxuprc __P((RLIMTYPE *));
188 static int getmaxvm __P((RLIMTYPE *, RLIMTYPE *));
191 int option; /* The ulimit option for this limit. */
192 int parameter; /* Parameter to pass to get_limit (). */
193 int block_factor; /* Blocking factor for specific limit. */
194 char *description; /* Descriptive string to output. */
195 char *units; /* scale */
198 static RESOURCE_LIMITS limits[] = {
200 { 'c', RLIMIT_CORE, 1024, "core file size", "blocks" },
203 { 'd', RLIMIT_DATA, 1024, "data seg size", "kbytes" },
205 { 'f', RLIMIT_FILESIZE, 1024, "file size", "blocks" },
206 #ifdef RLIMIT_SIGPENDING
207 { 'i', RLIMIT_SIGPENDING, 1, "pending signals", (char *)NULL },
209 #ifdef RLIMIT_MEMLOCK
210 { 'l', RLIMIT_MEMLOCK, 1024, "max locked memory", "kbytes" },
213 { 'm', RLIMIT_RSS, 1024, "max memory size", "kbytes" },
214 #endif /* RLIMIT_RSS */
215 { 'n', RLIMIT_OPENFILES, 1, "open files", (char *)NULL},
216 { 'p', RLIMIT_PIPESIZE, 512, "pipe size", "512 bytes" },
217 #ifdef RLIMIT_MSGQUEUE
218 { 'q', RLIMIT_MSGQUEUE, 1, "POSIX message queues", "bytes" },
221 { 's', RLIMIT_STACK, 1024, "stack size", "kbytes" },
224 { 't', RLIMIT_CPU, 1, "cpu time", "seconds" },
225 #endif /* RLIMIT_CPU */
226 { 'u', RLIMIT_MAXUPROC, 1, "max user processes", (char *)NULL },
227 #if defined (HAVE_RESOURCE)
228 { 'v', RLIMIT_VIRTMEM, RLIMIT_VMBLKSZ, "virtual memory", "kbytes" },
231 { 'w', RLIMIT_SWAP, 1024, "swap size", "kbytes" },
234 { 'x', RLIMIT_LOCKS, 1, "file locks", (char *)NULL },
236 { -1, -1, -1, (char *)NULL, (char *)NULL }
238 #define NCMDS (sizeof(limits) / sizeof(limits[0]))
240 typedef struct _cmd {
245 static ULCMD *cmdlist;
247 static int cmdlistsz;
249 #if !defined (HAVE_RESOURCE) && !defined (HAVE_ULIMIT)
258 #endif /* !HAVE_RESOURCE && !HAVE_ULIMIT */
266 for (i = 0; limits[i].option > 0; i++)
267 if (limits[i].option == opt)
272 static char optstring[4 + 2 * NCMDS];
274 /* Report or set limits associated with certain per-process resources.
275 See the help documentation in builtins.c for a full description. */
277 ulimit_builtin (list)
278 register WORD_LIST *list;
281 int c, limind, mode, opt, all_limits;
287 /* Idea stolen from pdksh -- build option string the first time called. */
288 if (optstring[0] == 0)
291 *s++ = 'a'; *s++ = 'S'; *s++ = 'H';
292 for (c = 0; limits[c].option > 0; c++)
294 *s++ = limits[c].option;
300 /* Initialize the command list. */
302 cmdlist = (ULCMD *)xmalloc ((cmdlistsz = 16) * sizeof (ULCMD));
305 reset_internal_getopt ();
306 while ((opt = internal_getopt (list, optstring)) != -1)
314 /* -S and -H are modifiers, not real options. */
328 if (ncmd >= cmdlistsz)
329 cmdlist = (ULCMD *)xrealloc (cmdlist, (cmdlistsz *= 2) * sizeof (ULCMD));
330 cmdlist[ncmd].cmd = opt;
331 cmdlist[ncmd++].arg = list_optarg;
340 if (list) /* setting */
342 if (STREQ (list->word->word, "unlimited") == 0)
344 builtin_error (_("%s: invalid limit argument"), list->word->word);
345 return (EXECUTION_FAILURE);
347 return (set_all_limits (mode == 0 ? LIMIT_SOFT|LIMIT_HARD : mode, RLIM_INFINITY));
350 print_all_limits (mode == 0 ? LIMIT_SOFT : mode);
351 return (EXECUTION_SUCCESS);
354 /* default is `ulimit -f' */
357 cmdlist[ncmd].cmd = 'f';
358 /* `ulimit something' is same as `ulimit -f something' */
359 cmdlist[ncmd++].arg = list ? list->word->word : (char *)NULL;
364 /* verify each command in the list. */
365 for (c = 0; c < ncmd; c++)
367 limind = _findlim (cmdlist[c].cmd);
370 builtin_error (_("`%c': bad command"), cmdlist[c].cmd);
375 for (c = 0; c < ncmd; c++)
376 if (ulimit_internal (cmdlist[c].cmd, cmdlist[c].arg, mode, ncmd > 1) == EXECUTION_FAILURE)
377 return (EXECUTION_FAILURE);
379 return (EXECUTION_SUCCESS);
383 ulimit_internal (cmd, cmdarg, mode, multiple)
388 int opt, limind, setting;
390 RLIMTYPE soft_limit, hard_limit, real_limit, limit;
392 setting = cmdarg != 0;
393 limind = _findlim (cmd);
395 mode = setting ? (LIMIT_HARD|LIMIT_SOFT) : LIMIT_SOFT;
396 opt = get_limit (limind, &soft_limit, &hard_limit);
399 builtin_error (_("%s: cannot get limit: %s"), limits[limind].description,
401 return (EXECUTION_FAILURE);
404 if (setting == 0) /* print the value of the specified limit */
406 printone (limind, (mode & LIMIT_SOFT) ? soft_limit : hard_limit, multiple);
407 return (EXECUTION_SUCCESS);
410 /* Setting the limit. */
411 if (STREQ (cmdarg, "hard"))
412 real_limit = hard_limit;
413 else if (STREQ (cmdarg, "soft"))
414 real_limit = soft_limit;
415 else if (STREQ (cmdarg, "unlimited"))
416 real_limit = RLIM_INFINITY;
417 else if (all_digits (cmdarg))
419 limit = string_to_rlimtype (cmdarg);
420 block_factor = limits[limind].block_factor;
421 real_limit = limit * block_factor;
423 if ((real_limit / block_factor) != limit)
425 sh_erange (cmdarg, "limit");
426 return (EXECUTION_FAILURE);
431 sh_invalidnum (cmdarg);
432 return (EXECUTION_FAILURE);
435 if (set_limit (limind, real_limit, mode) < 0)
437 builtin_error (_("%s: cannot modify limit: %s"), limits[limind].description,
439 return (EXECUTION_FAILURE);
442 return (EXECUTION_SUCCESS);
446 get_limit (ind, softlim, hardlim)
448 RLIMTYPE *softlim, *hardlim;
451 #if defined (HAVE_RESOURCE)
455 if (limits[ind].parameter >= 256)
457 switch (limits[ind].parameter)
459 case RLIMIT_FILESIZE:
460 if (filesize (&value) < 0)
463 case RLIMIT_PIPESIZE:
464 if (pipesize (&value) < 0)
467 case RLIMIT_OPENFILES:
468 value = (RLIMTYPE)getdtablesize ();
471 return (getmaxvm (softlim, hardlim));
472 case RLIMIT_MAXUPROC:
473 if (getmaxuprc (&value) < 0)
480 *softlim = *hardlim = value;
485 #if defined (HAVE_RESOURCE)
486 if (getrlimit (limits[ind].parameter, &limit) < 0)
488 *softlim = limit.rlim_cur;
489 *hardlim = limit.rlim_max;
491 if (limits[ind].parameter == RLIMIT_FILESIZE)
494 *hardlim *= 512; /* Ugh. */
507 set_limit (ind, newlim, mode)
512 #if defined (HAVE_RESOURCE)
517 if (limits[ind].parameter >= 256)
518 switch (limits[ind].parameter)
520 case RLIMIT_FILESIZE:
521 #if !defined (HAVE_RESOURCE)
522 return (ulimit (2, newlim / 512L));
528 case RLIMIT_OPENFILES:
529 #if defined (HAVE_SETDTABLESIZE)
530 # if defined (__CYGWIN__)
531 /* Grrr... Cygwin declares setdtablesize as void. */
532 setdtablesize (newlim);
535 return (setdtablesize (newlim));
538 case RLIMIT_PIPESIZE:
540 case RLIMIT_MAXUPROC:
547 #if defined (HAVE_RESOURCE)
548 if (getrlimit (limits[ind].parameter, &limit) < 0)
551 if (limits[ind].parameter == RLIMIT_FILESIZE)
552 newlim /= 512; /* Ugh. */
554 val = (current_user.euid != 0 && newlim == RLIM_INFINITY &&
555 (mode & LIMIT_HARD) == 0 && /* XXX -- test */
556 (limit.rlim_cur <= limit.rlim_max))
557 ? limit.rlim_max : newlim;
558 if (mode & LIMIT_SOFT)
559 limit.rlim_cur = val;
560 if (mode & LIMIT_HARD)
561 limit.rlim_max = val;
563 return (setrlimit (limits[ind].parameter, &limit));
572 getmaxvm (softlim, hardlim)
573 RLIMTYPE *softlim, *hardlim;
575 #if defined (HAVE_RESOURCE)
576 struct rlimit datalim, stacklim;
578 if (getrlimit (RLIMIT_DATA, &datalim) < 0)
581 if (getrlimit (RLIMIT_STACK, &stacklim) < 0)
584 /* Protect against overflow. */
585 *softlim = (datalim.rlim_cur / 1024L) + (stacklim.rlim_cur / 1024L);
586 *hardlim = (datalim.rlim_max / 1024L) + (stacklim.rlim_max / 1024L);
591 #endif /* HAVE_RESOURCE */
598 #if !defined (HAVE_RESOURCE)
600 if ((result = ulimit (1, 0L)) < 0)
603 *valuep = (RLIMTYPE) result * 512;
615 #if defined (PIPE_BUF)
616 /* This is defined on Posix systems. */
617 *valuep = (RLIMTYPE) PIPE_BUF;
620 # if defined (_POSIX_PIPE_BUF)
621 *valuep = (RLIMTYPE) _POSIX_PIPE_BUF;
624 # if defined (PIPESIZE)
625 /* This is defined by running a program from the Makefile. */
626 *valuep = (RLIMTYPE) PIPESIZE;
631 # endif /* PIPESIZE */
632 # endif /* _POSIX_PIPE_BUF */
633 #endif /* PIPE_BUF */
642 maxchild = getmaxchild ();
650 *valuep = (RLIMTYPE) maxchild;
656 print_all_limits (mode)
660 RLIMTYPE softlim, hardlim;
665 for (i = 0; limits[i].option > 0; i++)
667 if (get_limit (i, &softlim, &hardlim) == 0)
668 printone (i, (mode & LIMIT_SOFT) ? softlim : hardlim, 1);
669 else if (errno != EINVAL)
670 builtin_error ("%s: cannot get limit: %s", limits[i].description,
676 printone (limind, curlim, pdesc)
685 if (limits[limind].units)
686 sprintf (unitstr, "(%s, -%c) ", limits[limind].units, limits[limind].option);
688 sprintf (unitstr, "(-%c) ", limits[limind].option);
690 printf ("%-20s %16s", limits[limind].description, unitstr);
692 if (curlim == RLIM_INFINITY)
694 else if (curlim == RLIM_SAVED_MAX)
696 else if (curlim == RLIM_SAVED_CUR)
699 print_rlimtype ((curlim / limits[limind].block_factor), 1);
702 /* Set all limits to NEWLIM. NEWLIM currently must be RLIM_INFINITY, which
703 causes all limits to be set as high as possible depending on mode (like
704 csh `unlimit'). Returns -1 if NEWLIM is invalid, 0 if all limits
705 were set successfully, and 1 if at least one limit could not be set.
707 To raise all soft limits to their corresponding hard limits, use
708 ulimit -S -a unlimited
709 To attempt to raise all hard limits to infinity (superuser-only), use
710 ulimit -H -a unlimited
711 To attempt to raise all soft and hard limits to infinity, use
716 set_all_limits (mode, newlim)
723 if (newlim != RLIM_INFINITY)
730 mode = LIMIT_SOFT|LIMIT_HARD;
732 for (retval = i = 0; limits[i].option > 0; i++)
733 if (set_limit (i, newlim, mode) < 0)
735 builtin_error ("%s: cannot modify limit: %s", limits[i].description,