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