]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/getrlimit.2
getrlimit.2: Make it clear RLIMIT_NPROC is a limit on current number of processes
[thirdparty/man-pages.git] / man2 / getrlimit.2
CommitLineData
fea681da 1.\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
658ea3ee 2.\" and Copyright (c) 2002, 2004, 2005, 2008, 2010 Michael Kerrisk
fea681da 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
c13182ef 13.\"
fea681da
MK
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da
MK
25.\"
26.\" Modified by Michael Haardt <michael@moria.de>
27.\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
28.\" Modified 1996-01-13 by Arnt Gulbrandsen <agulbra@troll.no>
29.\" Modified 1996-01-22 by aeb, following a remark by
30.\" Tigran Aivazian <tigran@sco.com>
31.\" Modified 1996-04-14 by aeb, following a remark by
32.\" Robert Bihlmeyer <robbe@orcus.ping.at>
33.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
34.\" Modified 2001-05-04 by aeb, following a remark by
e00c3a07 35.\" HÃ¥vard Lygre <hklygre@online.no>
c11b1abf
MK
36.\" Modified 2001-04-17 by Michael Kerrisk <mtk.manpages@gmail.com>
37.\" Modified 2002-06-13 by Michael Kerrisk <mtk.manpages@gmail.com>
c8f2dd47 38.\" Added note on nonstandard behavior when SIGCHLD is ignored.
c11b1abf 39.\" Modified 2002-07-09 by Michael Kerrisk <mtk.manpages@gmail.com>
1546fe19 40.\" Enhanced descriptions of 'resource' values
fea681da
MK
41.\" Modified 2003-11-28 by aeb, added RLIMIT_CORE
42.\" Modified 2004-03-26 by aeb, added RLIMIT_AS
c11b1abf 43.\" Modified 2004-06-16 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
44.\" Added notes on CAP_SYS_RESOURCE
45.\"
c13182ef 46.\" 2004-11-16 -- mtk: the getrlimit.2 page, which formally included
0fc46b5a
MK
47.\" coverage of getrusage(2), has been split, so that the latter
48.\" is now covered in its own getrusage.2.
49.\"
50.\" Modified 2004-11-16, mtk: A few other minor changes
b4c0e1cb
MK
51.\" Modified 2004-11-23, mtk
52.\" Added notes on RLIMIT_MEMLOCK, RLIMIT_NPROC, and RLIMIT_RSS
53.\" to "CONFORMING TO"
54.\" Modified 2004-11-25, mtk
55.\" Rewrote discussion on RLIMIT_MEMLOCK to incorporate kernel
56.\" 2.6.9 changes.
57.\" Added note on RLIMIT_CPU error in older kernels
1bf844f1 58.\" 2004-11-03, mtk, Added RLIMIT_SIGPENDING
9d8b1d5f 59.\" 2005-07-13, mtk, documented RLIMIT_MSGQUEUE limit.
1bf844f1 60.\" 2005-07-28, mtk, Added descriptions of RLIMIT_NICE and RLIMIT_RTPRIO
23ce0537 61.\" 2008-05-07, mtk / Peter Zijlstra, Added description of RLIMIT_RTTIME
1546fe19 62.\" 2010-11-06, mtk: Added documentation of prlimit()
0fc46b5a 63.\"
4b8c67d9 64.TH GETRLIMIT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
fea681da 65.SH NAME
1546fe19 66getrlimit, setrlimit, prlimit \- get/set resource limits
fea681da
MK
67.SH SYNOPSIS
68.B #include <sys/time.h>
69.br
70.B #include <sys/resource.h>
68e4db0a 71.PP
fea681da
MK
72.BI "int getrlimit(int " resource ", struct rlimit *" rlim );
73.br
fea681da 74.BI "int setrlimit(int " resource ", const struct rlimit *" rlim );
68e4db0a 75.PP
1546fe19
MK
76.BI "int prlimit(pid_t " pid ", int " resource \
77", const struct rlimit *" new_limit ,
78.br
79.BI " struct rlimit *" old_limit );
68e4db0a 80.PP
1546fe19
MK
81.in -4n
82Feature Test Macro Requirements for glibc (see
83.BR feature_test_macros (7)):
84.in
68e4db0a 85.PP
1546fe19 86.BR prlimit ():
abb3258d 87_GNU_SOURCE
fea681da 88.SH DESCRIPTION
1546fe19 89The
0fc46b5a 90.BR getrlimit ()
fea681da 91and
0fc46b5a 92.BR setrlimit ()
0ef19275 93system calls get and set resource limits respectively.
c13182ef 94Each resource has an associated soft and hard limit, as defined by the
8478ee02 95.I rlimit
c805532e 96structure:
fea681da 97.PP
a08ea57c 98.in +4n
20ae960c 99.EX
fea681da 100struct rlimit {
0fc46b5a
MK
101 rlim_t rlim_cur; /* Soft limit */
102 rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
fea681da 103};
20ae960c 104.EE
a08ea57c 105.in
20ae960c 106.PP
fea681da
MK
107The soft limit is the value that the kernel enforces for the
108corresponding resource.
109The hard limit acts as a ceiling for the soft limit:
33a0ccb2 110an unprivileged process may set only its soft limit to a value in the
fea681da
MK
111range from 0 up to the hard limit, and (irreversibly) lower its hard limit.
112A privileged process (under Linux: one with the
113.B CAP_SYS_RESOURCE
114capability) may make arbitrary changes to either limit value.
115.PP
116The value
117.B RLIM_INFINITY
118denotes no limit on a resource (both in the structure returned by
119.BR getrlimit ()
120and in the structure passed to
121.BR setrlimit ()).
122.PP
0ef19275 123The
fea681da 124.I resource
0ef19275 125argument must be one of:
fea681da
MK
126.TP
127.B RLIMIT_AS
ee3d7b3b 128This is the maximum size of the process's virtual memory
8a351f84
MK
129(address space).
130The limit is specified in bytes, and is rounded down to the system page size.
fea681da
MK
131.\" since 2.0.27 / 2.1.12
132This limit affects calls to
133.BR brk (2),
9af134cd 134.BR mmap (2),
fea681da
MK
135and
136.BR mremap (2),
137which fail with the error
138.B ENOMEM
1c44bd5b
MK
139upon exceeding this limit.
140Also automatic stack expansion will fail
0fc46b5a
MK
141(and generate a
142.B SIGSEGV
143that kills the process if no alternate stack
144has been made available via
145.BR sigaltstack (2)).
4a3f7c5f 146Since the value is a \fIlong\fP, on machines with a 32-bit \fIlong\fP
c4b7e5ac 147either this limit is at most 2\ GiB, or this resource is unlimited.
fea681da
MK
148.TP
149.B RLIMIT_CORE
ee3d7b3b 150This is the maximum size of a
fea681da 151.I core
4711f722 152file (see
ee3d7b3b 153.BR core (5))
4a63f626 154in bytes that the process may dump.
1c44bd5b 155When 0 no core dump files are created.
c7094399 156When nonzero, larger dumps are truncated to this size.
fea681da
MK
157.TP
158.B RLIMIT_CPU
ee3d7b3b
MK
159This is a limit, in seconds,
160on the amount of CPU time that the process can consume.
fea681da
MK
161When the process reaches the soft limit, it is sent a
162.B SIGXCPU
163signal.
164The default action for this signal is to terminate the process.
165However, the signal can be caught, and the handler can return control to
166the main program.
167If the process continues to consume CPU time, it will be sent
168.B SIGXCPU
169once per second until the hard limit is reached, at which time
170it is sent
171.BR SIGKILL .
4f96e450 172(This latter point describes Linux behavior.
fea681da
MK
173Implementations vary in how they treat processes which continue to
174consume CPU time after reaching the soft limit.
175Portable applications that need to catch this signal should
176perform an orderly termination upon first receipt of
e6c5832f 177.BR SIGXCPU .)
fea681da
MK
178.TP
179.B RLIMIT_DATA
8a351f84 180This is the maximum size
b50667ac 181of the process's data segment (initialized data,
fea681da 182uninitialized data, and heap).
8a351f84 183The limit is specified in bytes, and is rounded down to the system page size.
fea681da 184This limit affects calls to
c87d084b 185.BR brk (2),
0bfa087b 186.BR sbrk (2),
c0d3d8fe 187and (since Linux 4.7)
c87d084b
JG
188.BR mmap (2),
189.\" commits 84638335900f1995495838fe1bd4870c43ec1f67
190.\" ("mm: rework virtual memory accounting"),
191.\" f4fcd55841fc9e46daac553b39361572453c2b88
192.\" (mm: enable RLIMIT_DATA by default with workaround for valgrind).
fea681da
MK
193which fail with the error
194.B ENOMEM
195upon encountering the soft limit of this resource.
196.TP
197.B RLIMIT_FSIZE
4a63f626 198This is the maximum size in bytes of files that the process may create.
fea681da
MK
199Attempts to extend a file beyond this limit result in delivery of a
200.B SIGXFSZ
201signal.
c13182ef
MK
202By default, this signal terminates a process, but a process can
203catch this signal instead, in which case the relevant system call (e.g.,
2e42dfb3 204.BR write (2),
0bfa087b 205.BR truncate (2))
fea681da
MK
206fails with the error
207.BR EFBIG .
208.TP
0dfc1b01 209.BR RLIMIT_LOCKS " (early Linux 2.4 only)"
9d8b1d5f 210.\" to be precise: Linux 2.4.0-test9; no longer in 2.4.25 / 2.5.65
ee3d7b3b 211This is a limit on the combined number of
0bfa087b 212.BR flock (2)
c13182ef 213locks and
0bfa087b 214.BR fcntl (2)
fea681da 215leases that this process may establish.
fea681da
MK
216.TP
217.B RLIMIT_MEMLOCK
ee3d7b3b 218This is the maximum number of bytes of memory that may be locked
b4c0e1cb 219into RAM.
ee3d7b3b 220This limit is in effect rounded down to the nearest multiple
b4c0e1cb 221of the system page size.
c13182ef 222This limit affects
28ab42e4
MK
223.BR mlock (2),
224.BR mlockall (2),
b4c0e1cb
MK
225and the
226.BR mmap (2)
227.B MAP_LOCKED
228operation.
28ab42e4 229Since Linux 2.6.9, it also affects the
b4c0e1cb
MK
230.BR shmctl (2)
231.B SHM_LOCK
c13182ef 232operation, where it sets a maximum on the total bytes in
b4c0e1cb
MK
233shared memory segments (see
234.BR shmget (2))
235that may be locked by the real user ID of the calling process.
c13182ef 236The
b4c0e1cb
MK
237.BR shmctl (2)
238.B SHM_LOCK
239locks are accounted for separately from the per-process memory
c13182ef
MK
240locks established by
241.BR mlock (2),
e1d6264d 242.BR mlockall (2),
b4c0e1cb
MK
243and
244.BR mmap (2)
245.BR MAP_LOCKED ;
246a process can lock bytes up to this limit in each of these
e6c5832f 247two categories.
efeece04 248.IP
b4c0e1cb
MK
249In Linux kernels before 2.6.9, this limit controlled the amount of
250memory that could be locked by a privileged process.
251Since Linux 2.6.9, no limits are placed on the amount of memory
252that a privileged process may lock, and this limit instead governs
253the amount of memory that an unprivileged process may lock.
9d8b1d5f 254.TP
31c1f2b0 255.BR RLIMIT_MSGQUEUE " (since Linux 2.6.8)"
ee3d7b3b 256This is a limit on the number of bytes that can be allocated
9d8b1d5f
MK
257for POSIX message queues for the real user ID of the calling process.
258This limit is enforced for
259.BR mq_open (3).
9d8b1d5f
MK
260Each message queue that the user creates counts (until it is removed)
261against this limit according to the formula:
c7885256 262.IP
e15dc338 263 Since Linux 3.5:
c7885256
MK
264.IP
265.EX
e15dc338
MK
266 bytes = attr.mq_maxmsg * sizeof(struct msg_msg) +
267 min(attr.mq_maxmsg, MQ_PRIO_MAX) *
6f9e0e57 268 sizeof(struct posix_msg_tree_node)+
e15dc338
MK
269 /* For overhead */
270 attr.mq_maxmsg * attr.mq_msgsize;
271 /* For message data */
c7885256
MK
272.EE
273.IP
e15dc338 274 Linux 3.4 and earlier:
c7885256
MK
275.IP
276.EX
e15dc338
MK
277 bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
278 /* For overhead */
279 attr.mq_maxmsg * attr.mq_msgsize;
280 /* For message data */
b76974c1 281.EE
c7885256 282.IP
c13182ef 283where
9d8b1d5f 284.I attr
c13182ef 285is the
9d8b1d5f
MK
286.I mq_attr
287structure specified as the fourth argument to
e15dc338
MK
288.BR mq_open (3),
289and the
290.I msg_msg
291and
292.I posix_msg_tree_node
293structures are kernel-internal structures.
efeece04 294.IP
e15dc338
MK
295The "overhead" addend in the formula accounts for overhead
296bytes required by the implementation
297and ensures that the user cannot
9d8b1d5f
MK
298create an unlimited number of zero-length messages (such messages
299nevertheless each consume some system memory for bookkeeping overhead).
a23bf8a3 300.TP
64d6219c 301.BR RLIMIT_NICE " (since Linux 2.6.12, but see BUGS below)"
ee3d7b3b 302This specifies a ceiling to which the process's nice value can be raised using
a23bf8a3
MK
303.BR setpriority (2)
304or
305.BR nice (2).
306The actual ceiling for the nice value is calculated as
307.IR "20\ \-\ rlim_cur" .
d17de833
MK
308The useful range for this limit is thus from 1
309(corresponding to a nice value of 19) to 40
310(corresponding to a nice value of -20).
4e66320e 311This unusual choice of range was necessary
bbc069dd 312because negative numbers cannot be specified
cedd678f 313as resource limit values, since they typically have special meanings.
682edefb
MK
314For example,
315.B RLIM_INFINITY
bbc069dd 316typically is the same as \-1.
dfc3c7de
MK
317For more detail on the nice value, see
318.BR sched (7).
1bf844f1 319.TP
fea681da 320.B RLIMIT_NOFILE
ee3d7b3b 321This specifies a value one greater than the maximum file descriptor number
fea681da
MK
322that can be opened by this process.
323Attempts
0bfa087b
MK
324.RB ( open (2),
325.BR pipe (2),
326.BR dup (2),
4a04cd9a 327etc.)
fea681da
MK
328to exceed this limit yield the error
329.BR EMFILE .
00e8730f
MK
330(Historically, this limit was named
331.B RLIMIT_OFILE
332on BSD.)
efeece04 333.IP
ececfc73 334Since Linux 4.5,
afa27faa 335this limit also defines the maximum number of file descriptors that
ececfc73
MK
336an unprivileged process (one without the
337.BR CAP_SYS_RESOURCE
338capability) may have "in flight" to other processes,
339by being passed across UNIX domain sockets.
340This limit applies to the
341.BR sendmsg (2)
342system call.
343For further details, see
344.BR unix (7).
fea681da
MK
345.TP
346.B RLIMIT_NPROC
da06433e 347This is a limit on the number of extant process
ee3d7b3b 348(or, more precisely on Linux, threads)
da06433e
MK
349for the real user ID of the calling process.
350So long as the current number of processes belonging to this
351process's real user ID is greater than or equal to this limit,
0bfa087b 352.BR fork (2)
fea681da
MK
353fails with the error
354.BR EAGAIN .
63345f22
MK
355.IP
356The
357.B RLIMIT_NPROC
358limit is not enforced for processes that have either the
f703b9e1
MK
359.B CAP_SYS_ADMIN
360or the
361.B CAP_SYS_RESOURCE
362capability.
fea681da
MK
363.TP
364.B RLIMIT_RSS
ee3d7b3b 365This is a limit (in bytes) on the process's resident set
fea681da 366(the number of virtual pages resident in RAM).
33a0ccb2
MK
367This limit has effect only in Linux 2.4.x, x < 30, and there
368affects only calls to
0bfa087b 369.BR madvise (2)
fea681da 370specifying
9d8b1d5f
MK
371.BR MADV_WILLNEED .
372.\" As at kernel 2.6.12, this limit still does nothing in 2.6 though
c13182ef 373.\" talk of making it do something has surfaced from time to time in LKML
9426c9dd 374.\" -- MTK, Jul 05
fea681da 375.TP
31c1f2b0 376.BR RLIMIT_RTPRIO " (since Linux 2.6.12, but see BUGS)"
ee3d7b3b 377This specifies a ceiling on the real-time priority that may be set for
cedd678f 378this process using
1bf844f1
MK
379.BR sched_setscheduler (2)
380and
381.BR sched_setparam (2).
efeece04 382.IP
384c705b
MK
383For further details on real-time scheduling policies, see
384.BR sched (7)
1bf844f1 385.TP
31c1f2b0 386.BR RLIMIT_RTTIME " (since Linux 2.6.25)"
ee3d7b3b 387This is a limit (in microseconds)
c43b0ac7 388on the amount of CPU time that a process scheduled
23ce0537
MK
389under a real-time scheduling policy may consume without making a blocking
390system call.
391For the purpose of this limit,
392each time a process makes a blocking system call,
393the count of its consumed CPU time is reset to zero.
394The CPU time count is not reset if the process continues trying to
395use the CPU but is preempted, its time slice expires, or it calls
396.BR sched_yield (2).
efeece04 397.IP
23ce0537
MK
398Upon reaching the soft limit, the process is sent a
399.B SIGXCPU
400signal.
401If the process catches or ignores this signal and
402continues consuming CPU time, then
403.B SIGXCPU
404will be generated once each second until the hard limit is reached,
405at which point the process is sent a
406.B SIGKILL
407signal.
efeece04 408.IP
23ce0537
MK
409The intended use of this limit is to stop a runaway
410real-time process from locking up the system.
efeece04 411.IP
384c705b
MK
412For further details on real-time scheduling policies, see
413.BR sched (7)
23ce0537 414.TP
31c1f2b0 415.BR RLIMIT_SIGPENDING " (since Linux 2.6.8)"
ee3d7b3b 416This is a limit on the number of signals
e6c5832f 417that may be queued for the real user ID of the calling process.
8b6aacb0
MK
418Both standard and real-time signals are counted for the purpose of
419checking this limit.
33a0ccb2 420However, the limit is enforced only for
485ab701 421.BR sigqueue (3);
8b6aacb0
MK
422it is always possible to use
423.BR kill (2)
424to queue one instance of any of the signals that are not already
425queued to the process.
e6c5832f
MK
426.\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit
427.\" that was present in kernels <= 2.6.7. MTK Dec 04
428.TP
fea681da 429.B RLIMIT_STACK
ee3d7b3b 430This is the maximum size of the process stack, in bytes.
fea681da
MK
431Upon reaching this limit, a
432.B SIGSEGV
433signal is generated.
434To handle this signal, a process must employ an alternate signal stack
435.RB ( sigaltstack (2)).
efeece04 436.IP
374af67a
MK
437Since Linux 2.6.23,
438this limit also determines the amount of space used for the process's
439command-line arguments and environment variables; for details, see
440.BR execve (2).
1546fe19
MK
441.SS prlimit()
442.\" commit c022a0acad534fd5f5d5f17280f6d4d135e74e81
443.\" Author: Jiri Slaby <jslaby@suse.cz>
444.\" Date: Tue May 4 18:03:50 2010 +0200
9bd51977
MK
445.\"
446.\" rlimits: implement prlimit64 syscall
ef4f4031 447.\"
9bd51977
MK
448.\" commit 6a1d5e2c85d06da35cdfd93f1a27675bfdc3ad8c
449.\" Author: Jiri Slaby <jslaby@suse.cz>
450.\" Date: Wed Mar 24 17:06:58 2010 +0100
ef4f4031 451.\"
9bd51977
MK
452.\" rlimits: add rlimit64 structure
453.\"
1546fe19
MK
454The Linux-specific
455.BR prlimit ()
456system call combines and extends the functionality of
457.BR setrlimit ()
458and
459.BR getrlimit ().
460It can be used to both set and get the resource limits of an arbitrary process.
efeece04 461.PP
1546fe19
MK
462The
463.I resource
464argument has the same meaning as for
465.BR setrlimit ()
466and
467.BR getrlimit ().
efeece04 468.PP
1546fe19
MK
469If the
470.IR new_limit
471argument is a not NULL, then the
472.I rlimit
473structure to which it points is used to set new values for
474the soft and hard limits for
475.IR resource .
476If the
477.IR old_limit
478argument is a not NULL, then a successful call to
479.BR prlimit ()
480places the previous soft and hard limits for
481.I resource
98b43b57 482in the
1546fe19
MK
483.I rlimit
484structure pointed to by
485.IR old_limit .
efeece04 486.PP
1546fe19
MK
487The
488.I pid
489argument specifies the ID of the process on which the call is to operate.
490If
491.I pid
492is 0, then the call applies to the calling process.
493To set or get the resources of a process other than itself,
494the caller must have the
495.B CAP_SYS_RESOURCE
32dbbd64
MK
496capability in the user namespace of the process
497whose resource limits are being changed, or the
1546fe19
MK
498real, effective, and saved set user IDs of the target process
499must match the real user ID of the caller
500.I and
501the real, effective, and saved set group IDs of the target process
502must match the real group ID of the caller.
bea08fec 503.\" FIXME . this permission check is strange
1546fe19
MK
504.\" Asked about this on LKML, 7 Nov 2010
505.\" "Inconsistent credential checking in prlimit() syscall"
506.SH RETURN VALUE
507On success, these system calls return 0.
c13182ef 508On error, \-1 is returned, and
fea681da
MK
509.I errno
510is set appropriately.
511.SH ERRORS
512.TP
513.B EFAULT
1546fe19
MK
514A pointer argument points to a location
515outside the accessible address space.
fea681da
MK
516.TP
517.B EINVAL
1546fe19 518The value specified in
0fc46b5a 519.I resource
b270eba9
MK
520is not valid;
521or, for
1546fe19
MK
522.BR setrlimit ()
523or
524.BR prlimit ():
94e9d9fe 525.I rlim\->rlim_cur
b270eba9 526was greater than
94e9d9fe 527.IR rlim\->rlim_max .
fea681da
MK
528.TP
529.B EPERM
1546fe19 530An unprivileged process tried to raise the hard limit; the
fea681da
MK
531.B CAP_SYS_RESOURCE
532capability is required to do this.
f7bd810d
MK
533.TP
534.B EPERM
535The caller tried to increase the hard
682edefb 536.B RLIMIT_NOFILE
625b5f5a
MK
537limit above the maximum defined by
538.IR /proc/sys/fs/nr_open
539(see
540.BR proc (5))
f7bd810d
MK
541.TP
542.B EPERM
543.RB ( prlimit ())
544The calling process did not have permission to set limits
1546fe19
MK
545for the process specified by
546.IR pid .
547.TP
548.B ESRCH
549Could not find a process with the ID specified in
550.IR pid .
551.SH VERSIONS
010eefd7 552The
1546fe19
MK
553.BR prlimit ()
554system call is available since Linux 2.6.36.
555Library support is available since glibc 2.13.
57ba9747
ZL
556.SH ATTRIBUTES
557For an explanation of the terms used in this section, see
558.BR attributes (7).
559.TS
560allbox;
561lbw35 lb lb
562l l l.
563Interface Attribute Value
564T{
565.BR getrlimit (),
566.BR setrlimit (),
567.BR prlimit ()
568T} Thread safety MT-Safe
569.TE
efeece04 570.sp 1
a1d5f77c 571.SH CONFORMING TO
1546fe19
MK
572.BR getrlimit (),
573.BR setrlimit ():
ac17f435 574POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
168c21d4 575.PP
1546fe19
MK
576.BR prlimit ():
577Linux-specific.
efeece04 578.PP
0daa9e92 579.B RLIMIT_MEMLOCK
a1d5f77c 580and
0daa9e92 581.B RLIMIT_NPROC
ac17f435 582derive from BSD and are not specified in POSIX.1;
a1d5f77c 583they are present on the BSDs and Linux, but on few other implementations.
0daa9e92 584.B RLIMIT_RSS
ac17f435 585derives from BSD and is not specified in POSIX.1;
a1d5f77c
MK
586it is nevertheless present on most implementations.
587.BR RLIMIT_MSGQUEUE ,
588.BR RLIMIT_NICE ,
589.BR RLIMIT_RTPRIO ,
23ce0537 590.BR RLIMIT_RTTIME ,
a1d5f77c
MK
591and
592.B RLIMIT_SIGPENDING
8382f16d 593are Linux-specific.
a1d5f77c
MK
594.SH NOTES
595A child process created via
596.BR fork (2)
2c0cfe3c 597inherits its parent's resource limits.
a1d5f77c
MK
598Resource limits are preserved across
599.BR execve (2).
efeece04 600.PP
1d3050c0
MK
601Lowering the soft limit for a resource below the process's
602current consumption of that resource will succeed
603(but will prevent the process from further increasing
604its consumption of the resource).
efeece04 605.PP
835363b2
MK
606One can set the resource limits of the shell using the built-in
607.IR ulimit
608command
609.RI ( limit
610in
611.BR csh (1)).
612The shell's resource limits are inherited by the processes that
613it creates to execute commands.
efeece04 614.PP
6d0620d8
MK
615Since Linux 2.6.24, the resource limits of any process can be inspected via
616.IR /proc/[pid]/limits ;
617see
618.BR proc (5).
efeece04 619.PP
e1695dec
MK
620Ancient systems provided a
621.BR vlimit ()
622function with a similar purpose to
623.BR setrlimit ().
624For backward compatibility, glibc also provides
625.BR vlimit ().
626All new applications should be written using
627.BR setrlimit ().
93a3b5ca
MK
628.SS C library/ kernel ABI differences
629Since version 2.13, the glibc
630.BR getrlimit ()
631and
632.BR setrlimit ()
633wrapper functions no longer invoke the corresponding system calls,
634but instead employ
635.BR prlimit (),
636for the reasons described in BUGS.
efeece04 637.PP
28633770
MK
638The name of the glibc wrapper function is
639.BR prlimit ();
a467eeaa
MK
640the underlying system call is
641.BR prlimit64 ().
b4c0e1cb
MK
642.SH BUGS
643In older Linux kernels, the
644.B SIGXCPU
645and
646.B SIGKILL
647signals delivered when a process encountered the soft and hard
9a8a1136 648.B RLIMIT_CPU
b4c0e1cb
MK
649limits were delivered one (CPU) second later than they should have been.
650This was fixed in kernel 2.6.8.
efeece04 651.PP
c13182ef
MK
652In 2.6.x kernels before 2.6.17, a
653.B RLIMIT_CPU
654limit of 0 is wrongly treated as "no limit" (like
6057e7a9 655.BR RLIM_INFINITY ).
64d6219c 656Since Linux 2.6.17, setting a limit of 0 does have an effect,
6057e7a9 657but is actually treated as a limit of 1 second.
a31272fe 658.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=114008066530167&w=2
efeece04 659.PP
1bf844f1 660A kernel bug means that
ceee84ba 661.\" See https://lwn.net/Articles/145008/
1bf844f1
MK
662.B RLIMIT_RTPRIO
663does not work in kernel 2.6.12; the problem is fixed in kernel 2.6.13.
efeece04 664.PP
c13182ef 665In kernel 2.6.12, there was an off-by-one mismatch
b5cc2ffb
MK
666between the priority ranges returned by
667.BR getpriority (2)
668and
6151ea9a 669.BR RLIMIT_NICE .
11532b16 670This had the effect that the actual ceiling for the nice value
6151ea9a
MK
671was calculated as
672.IR "19\ \-\ rlim_cur" .
cedd678f 673This was fixed in kernel 2.6.13.
6151ea9a 674.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=112256338703880&w=2
efeece04 675.PP
27bada1f
MK
676Since Linux 2.6.12,
677.\" The relevant patch, sent to LKML, seems to be
678.\" http://thread.gmane.org/gmane.linux.kernel/273462
679.\" From: Roland McGrath <roland <at> redhat.com>
680.\" Subject: [PATCH 7/7] make RLIMIT_CPU/SIGXCPU per-process
681.\" Date: 2005-01-23 23:27:46 GMT
682if a process reaches its soft
683.BR RLIMIT_CPU
684limit and has a handler installed for
685.BR SIGXCPU ,
686then, in addition to invoking the signal handler,
687the kernel increases the soft limit by one second.
688This behavior repeats if the process continues to consume CPU time,
689until the hard limit is reached,
690at which point the process is killed.
691Other implementations
692.\" Tested Solaris 10, FreeBSD 9, OpenBSD 5.0
693do not change the
694.BR RLIMIT_CPU
695soft limit in this manner,
696and the Linux behavior is probably not standards conformant;
697portable applications should avoid relying on this Linux-specific behavior.
bea08fec 698.\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=50951
27bada1f
MK
699The Linux-specific
700.BR RLIMIT_RTTIME
701limit exhibits the same behavior when the soft limit is encountered.
efeece04 702.PP
b270eba9
MK
703Kernels before 2.4.22 did not diagnose the error
704.B EINVAL
c13182ef 705for
b270eba9
MK
706.BR setrlimit ()
707when
94e9d9fe 708.I rlim\->rlim_cur
b270eba9 709was greater than
94e9d9fe 710.IR rlim\->rlim_max .
7add6ac9
MK
711.\"
712.SS Representation of """large""" resource limit values on 32-bit platforms
713The glibc
714.BR getrlimit ()
715and
716.BR setrlimit ()
717wrapper functions use a 64-bit
718.IR rlim_t
719data type, even on 32-bit platforms.
720However, the
721.I rlim_t
722data type used in the
723.BR getrlimit ()
724and
725.BR setrlimit ()
726system calls is a (32-bit)
727.IR "unsigned long" .
728Furthermore, in Linux versions before 2.6.36,
729the kernel represents resource limits on 32-bit platforms as
730.IR "unsigned long" .
731However, a 32-bit data type is not wide enough.
732.\" https://bugzilla.kernel.org/show_bug.cgi?id=5042
bea08fec 733.\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12201
7add6ac9
MK
734The most pertinent limit here is
735.BR RLIMIT_FSIZE ,
736which specifies the maximum size to which a file can grow:
737to be useful, this limit must be represented using a type
ef4f4031 738that is as wide as the type used to
7add6ac9
MK
739represent file offsets\(emthat is, as wide as a 64-bit
740.BR off_t
741(assuming a program compiled with
742.IR _FILE_OFFSET_BITS=64 ).
efeece04 743.PP
7add6ac9
MK
744To work around this kernel limitation,
745if a program tried to set a resource limit to a value larger than
746can be represented in a 32-bit
747.IR "unsigned long" ,
748then the glibc
749.BR setrlimit ()
750wrapper function silently converted the limit value to
751.BR RLIM_INFINITY .
752In other words, the requested resource limit setting was silently ignored.
efeece04 753.PP
7add6ac9
MK
754This problem was addressed in Linux 2.6.36 with two principal changes:
755.IP * 3
756the addition of a new kernel representation of resource limits that
757uses 64 bits, even on 32-bit platforms;
758.IP *
759the addition of the
760.BR prlimit ()
761system call, which employs 64-bit values for its resource limit arguments.
762.PP
763Since version 2.13,
764.\" https://www.sourceware.org/bugzilla/show_bug.cgi?id=12201
765glibc works around the limitations of the
766.BR getrlimit ()
767and
768.BR setrlimit ()
769system calls by implementing
770.BR setrlimit ()
771and
772.BR getrlimit ()
773as wrapper functions that call
774.BR prlimit ().
7484d5a7
MK
775.SH EXAMPLE
776The program below demonstrates the use of
777.BR prlimit ().
778.PP
b76974c1 779.EX
7484d5a7
MK
780#define _GNU_SOURCE
781#define _FILE_OFFSET_BITS 64
782#include <stdio.h>
783#include <time.h>
784#include <stdlib.h>
785#include <unistd.h>
786#include <sys/resource.h>
787
788#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
789 } while (0)
790
791int
792main(int argc, char *argv[])
793{
794 struct rlimit old, new;
795 struct rlimit *newp;
796 pid_t pid;
797
798 if (!(argc == 2 || argc == 4)) {
799 fprintf(stderr, "Usage: %s <pid> [<new\-soft\-limit> "
800 "<new\-hard\-limit>]\\n", argv[0]);
801 exit(EXIT_FAILURE);
802 }
803
804 pid = atoi(argv[1]); /* PID of target process */
805
806 newp = NULL;
807 if (argc == 4) {
808 new.rlim_cur = atoi(argv[2]);
809 new.rlim_max = atoi(argv[3]);
810 newp = &new;
811 }
812
813 /* Set CPU time limit of target process; retrieve and display
814 previous limit */
815
816 if (prlimit(pid, RLIMIT_CPU, newp, &old) == \-1)
817 errExit("prlimit\-1");
818 printf("Previous limits: soft=%lld; hard=%lld\\n",
819 (long long) old.rlim_cur, (long long) old.rlim_max);
820
821 /* Retrieve and display new CPU time limit */
822
823 if (prlimit(pid, RLIMIT_CPU, NULL, &old) == \-1)
824 errExit("prlimit\-2");
825 printf("New limits: soft=%lld; hard=%lld\\n",
826 (long long) old.rlim_cur, (long long) old.rlim_max);
827
bc32c626 828 exit(EXIT_SUCCESS);
7484d5a7 829}
b9c93deb 830.EE
47297adb 831.SH SEE ALSO
94315587 832.BR prlimit (1),
fea681da
MK
833.BR dup (2),
834.BR fcntl (2),
835.BR fork (2),
0fc46b5a 836.BR getrusage (2),
fea681da 837.BR mlock (2),
fea681da
MK
838.BR mmap (2),
839.BR open (2),
840.BR quotactl (2),
841.BR sbrk (2),
b4c0e1cb 842.BR shmctl (2),
fea681da 843.BR malloc (3),
485ab701 844.BR sigqueue (3),
fea681da 845.BR ulimit (3),
e1a9bc1b 846.BR core (5),
fea681da 847.BR capabilities (7),
99851d8b 848.BR cgroups (7),
dc5186fe 849.BR credentials (7),
fea681da 850.BR signal (7)