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