1GETRLIMIT(2)               Linux Programmer's Manual              GETRLIMIT(2)
2
3
4

NAME

6       getrlimit, setrlimit - get/set resource limits
7

SYNOPSIS

9       #include <sys/time.h>
10       #include <sys/resource.h>
11
12       int getrlimit(int resource, struct rlimit *rlim);
13       int setrlimit(int resource, const struct rlimit *rlim);
14

DESCRIPTION

16       getrlimit()  and  setrlimit() get and set resource limits respectively.
17       Each resource has an associated soft and hard limit, as defined by  the
18       rlimit  structure  (the  rlim  argument  to  both getrlimit() and setr‐
19       limit()):
20
21           struct rlimit {
22               rlim_t rlim_cur;  /* Soft limit */
23               rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
24           };
25
26       The soft limit is the value that the kernel  enforces  for  the  corre‐
27       sponding  resource.   The  hard  limit  acts  as a ceiling for the soft
28       limit: an unprivileged process may only set its soft limit to  a  value
29       in  the range from 0 up to the hard limit, and (irreversibly) lower its
30       hard  limit.   A  privileged  process  (under  Linux:  one   with   the
31       CAP_SYS_RESOURCE capability) may make arbitrary changes to either limit
32       value.
33
34       The value RLIM_INFINITY denotes no limit on a  resource  (both  in  the
35       structure  returned by getrlimit() and in the structure passed to setr‐
36       limit()).
37
38       resource must be one of:
39
40       RLIMIT_AS
41              The maximum size of the process's virtual memory (address space)
42              in  bytes.   This  limit  affects  calls  to brk(2), mmap(2) and
43              mremap(2), which fail with the error ENOMEM upon exceeding  this
44              limit.  Also automatic stack expansion will fail (and generate a
45              SIGSEGV that kills the process if no alternate  stack  has  been
46              made  available via sigaltstack(2)).  Since the value is a long,
47              on machines with a 32-bit long either this limit is  at  most  2
48              GiB, or this resource is unlimited.
49
50       RLIMIT_CORE
51              Maximum  size  of core file.  When 0 no core dump files are cre‐
52              ated.  When non-zero, larger dumps are truncated to this size.
53
54       RLIMIT_CPU
55              CPU time limit in seconds.  When the process  reaches  the  soft
56              limit, it is sent a SIGXCPU signal.  The default action for this
57              signal is to terminate the process.  However, the signal can  be
58              caught,  and the handler can return control to the main program.
59              If the process continues to consume CPU time, it  will  be  sent
60              SIGXCPU  once  per  second  until  the hard limit is reached, at
61              which time it is sent SIGKILL.   (This  latter  point  describes
62              Linux  2.2  through  2.6  behavior.  Implementations vary in how
63              they treat processes which continue to consume  CPU  time  after
64              reaching  the  soft  limit.   Portable applications that need to
65              catch this signal should perform  an  orderly  termination  upon
66              first receipt of SIGXCPU.)
67
68       RLIMIT_DATA
69              The  maximum  size  of  the  process's data segment (initialized
70              data, uninitialized data, and heap).  This limit  affects  calls
71              to  brk(2)  and  sbrk(2),  which fail with the error ENOMEM upon
72              encountering the soft limit of this resource.
73
74       RLIMIT_FSIZE
75              The maximum size of files that the process may create.  Attempts
76              to  extend  a  file  beyond  this  limit result in delivery of a
77              SIGXFSZ signal.  By default, this signal terminates  a  process,
78              but  a  process can catch this signal instead, in which case the
79              relevant system call (e.g., write(2),  truncate(2))  fails  with
80              the error EFBIG.
81
82       RLIMIT_LOCKS (Early Linux 2.4 only)
83              A  limit  on  the combined number of flock(2) locks and fcntl(2)
84              leases that this process may establish.
85
86       RLIMIT_MEMLOCK
87              The maximum number of bytes of memory that may  be  locked  into
88              RAM.  In effect this limit is rounded down to the nearest multi‐
89              ple of the system page size.  This limit  affects  mlock(2)  and
90              mlockall(2)  and  the mmap(2) MAP_LOCKED operation.  Since Linux
91              2.6.9 it also affects the shmctl(2) SHM_LOCK operation, where it
92              sets a maximum on the total bytes in shared memory segments (see
93              shmget(2)) that may be locked by the real user ID of the calling
94              process.   The  shmctl(2) SHM_LOCK locks are accounted for sepa‐
95              rately  from  the  per-process  memory  locks   established   by
96              mlock(2),  mlockall(2),  and  mmap(2)  MAP_LOCKED; a process can
97              lock bytes up to this limit in each of these two categories.  In
98              Linux  kernels before 2.6.9, this limit controlled the amount of
99              memory that could be locked  by  a  privileged  process.   Since
100              Linux 2.6.9, no limits are placed on the amount of memory that a
101              privileged process may lock, and this limit instead governs  the
102              amount of memory that an unprivileged process may lock.
103
104       RLIMIT_MSGQUEUE (Since Linux 2.6.8)
105              Specifies the limit on the number of bytes that can be allocated
106              for POSIX message queues for the real user  ID  of  the  calling
107              process.   This  limit is enforced for mq_open(3).  Each message
108              queue that the user creates counts (until it is removed) against
109              this limit according to the formula:
110
111                  bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
112                          attr.mq_maxmsg * attr.mq_msgsize
113
114              where  attr  is  the  mq_attr  structure specified as the fourth
115              argument to mq_open(3).
116
117              The first addend in the formula,  which  includes  sizeof(struct
118              msg_msg *) (4 bytes on Linux/i386), ensures that the user cannot
119              create an unlimited number of zero-length  messages  (such  mes‐
120              sages nevertheless each consume some system memory for bookkeep‐
121              ing overhead).
122
123       RLIMIT_NICE (since Linux 2.6.12, but see BUGS below)
124              Specifies a ceiling to which the process's  nice  value  can  be
125              raised  using setpriority(2) or nice(2).  The actual ceiling for
126              the nice value is calculated as 20 - rlim_cur.   (This  strange‐
127              ness  occurs  because  negative  numbers  cannot be specified as
128              resource limit values, since they typically have  special  mean‐
129              ings.  For example, RLIM_INFINITY typically is the same as -1.)
130
131       RLIMIT_NOFILE
132              Specifies  a  value one greater than the maximum file descriptor
133              number that can be opened by this process.   Attempts  (open(2),
134              pipe(2),  dup(2),  etc.)   to  exceed this limit yield the error
135              EMFILE.  (Historically, this limit  was  named  RLIMIT_OFILE  on
136              BSD.)
137
138       RLIMIT_NPROC
139              The  maximum  number  of processes (or, more precisely on Linux,
140              threads) that can be created for the real user ID of the calling
141              process.   Upon  encountering this limit, fork(2) fails with the
142              error EAGAIN.
143
144       RLIMIT_RSS
145              Specifies the limit (in pages) of  the  process's  resident  set
146              (the  number of virtual pages resident in RAM).  This limit only
147              has effect in Linux 2.4.x, x < 30, and there only affects  calls
148              to madvise(2) specifying MADV_WILLNEED.
149
150       RLIMIT_RTPRIO (Since Linux 2.6.12, but see BUGS)
151              Specifies  a  ceiling  on the real-time priority that may be set
152              for this  process  using  sched_setscheduler(2)  and  sched_set‐
153              param(2).
154
155       RLIMIT_RTTIME (Since Linux 2.6.25)
156              Specifies  a  limit  on  the  amount  of CPU time that a process
157              scheduled under a real-time scheduling policy may consume  with‐
158              out  making  a  blocking  system  call.  For the purpose of this
159              limit, each time a process makes a  blocking  system  call,  the
160              count  of  its consumed CPU time is reset to zero.  The CPU time
161              count is not reset if the process continues trying  to  use  the
162              CPU  but  is  preempted,  its  time  slice  expires, or it calls
163              sched_yield(2).
164
165              Upon reaching the soft limit, the process is sent a SIGXCPU sig‐
166              nal.   If the process catches or ignores this signal and contin‐
167              ues consuming CPU time, then SIGXCPU will be generated once each
168              second  until  the  hard  limit  is  reached, at which point the
169              process is sent a SIGKILL signal.
170
171              The intended use of this limit is to stop  a  runaway  real-time
172              process from locking up the system.
173
174       RLIMIT_SIGPENDING (Since Linux 2.6.8)
175              Specifies  the limit on the number of signals that may be queued
176              for the real user ID of the calling process.  Both standard  and
177              real-time  signals  are counted for the purpose of checking this
178              limit.  However, the limit is only enforced for sigqueue(2);  it
179              is  always  possible to use kill(2) to queue one instance of any
180              of the signals that are not already queued to the process.
181
182       RLIMIT_STACK
183              The maximum size of the process stack, in bytes.  Upon  reaching
184              this  limit, a SIGSEGV signal is generated.  To handle this sig‐
185              nal, a process must employ an alternate  signal  stack  (sigalt‐
186              stack(2)).
187
188              Since  Linux  2.6.23,  this  limit also determines the amount of
189              space used for the process's command-line arguments and environ‐
190              ment variables; for details, see execve(2).
191

RETURN VALUE

193       On  success,  zero is returned.  On error, -1 is returned, and errno is
194       set appropriately.
195

ERRORS

197       EFAULT rlim points outside the accessible address space.
198
199       EINVAL resource is not valid; or, for setrlimit():  rlim->rlim_cur  was
200              greater than rlim->rlim_max.
201
202       EPERM  An  unprivileged  process tried to use setrlimit() to increase a
203              soft  or  hard  limit  above  the  current   hard   limit;   the
204              CAP_SYS_RESOURCE  capability  is  required  to do this.  Or, the
205              process tried to use setrlimit() to increase the  soft  or  hard
206              RLIMIT_NOFILE limit above the current kernel maximum (NR_OPEN).
207

CONFORMING TO

209       SVr4,  4.3BSD,  POSIX.1-2001.   RLIMIT_MEMLOCK  and RLIMIT_NPROC derive
210       from BSD and are not specified in POSIX.1-2001; they are present on the
211       BSDs  and  Linux, but on few other implementations.  RLIMIT_RSS derives
212       from BSD and is not  specified  in  POSIX.1-2001;  it  is  nevertheless
213       present   on   most   implementations.   RLIMIT_MSGQUEUE,  RLIMIT_NICE,
214       RLIMIT_RTPRIO, RLIMIT_RTTIME, and RLIMIT_SIGPENDING are Linux-specific.
215

NOTES

217       A child process created via fork(2) inherits its parent's resource lim‐
218       its.  Resource limits are preserved across execve(2).
219
220       One  can set the resource limits of the shell using the built-in ulimit
221       command (limit in csh(1)).  The shell's resource limits  are  inherited
222       by the processes that it creates to execute commands.
223

BUGS

225       In  older Linux kernels, the SIGXCPU and SIGKILL signals delivered when
226       a process encountered the soft and hard RLIMIT_CPU limits  were  deliv‐
227       ered one (CPU) second later than they should have been.  This was fixed
228       in kernel 2.6.8.
229
230       In 2.6.x kernels before 2.6.17, a RLIMIT_CPU  limit  of  0  is  wrongly
231       treated  as  "no limit" (like RLIM_INFINITY).  Since Linux 2.6.17, set‐
232       ting a limit of 0 does have an effect, but is  actually  treated  as  a
233       limit of 1 second.
234
235       A  kernel  bug means that RLIMIT_RTPRIO does not work in kernel 2.6.12;
236       the problem is fixed in kernel 2.6.13.
237
238       In kernel 2.6.12, there was an off-by-one mismatch between the priority
239       ranges returned by getpriority(2) and RLIMIT_NICE.  This had the effect
240       that actual ceiling for the nice value was calculated as 19 - rlim_cur.
241       This was fixed in kernel 2.6.13.
242
243       Kernels before 2.4.22 did not diagnose the error EINVAL for setrlimit()
244       when rlim->rlim_cur was greater than rlim->rlim_max.
245

SEE ALSO

247       dup(2), fcntl(2), fork(2), getrusage(2),  mlock(2),  mmap(2),  open(2),
248       quotactl(2),  sbrk(2),  shmctl(2),  sigqueue(2),  malloc(3), ulimit(3),
249       core(5), capabilities(7), signal(7)
250

COLOPHON

252       This page is part of release 3.22 of the Linux  man-pages  project.   A
253       description  of  the project, and information about reporting bugs, can
254       be found at http://www.kernel.org/doc/man-pages/.
255
256
257
258Linux                             2008-10-06                      GETRLIMIT(2)
Impressum