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