.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
.\" and Copyright (c) 2002, 2004, 2005, 2008 Michael Kerrisk
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Modified by Michael Haardt <michael@moria.de>
.\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
.\" Modified 1996-01-13 by Arnt Gulbrandsen <agulbra@troll.no>
.\" Modified 1996-01-22 by aeb, following a remark by
.\"          Tigran Aivazian <tigran@sco.com>
.\" Modified 1996-04-14 by aeb, following a remark by
.\"          Robert Bihlmeyer <robbe@orcus.ping.at>
.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 2001-05-04 by aeb, following a remark by
.\"          H?vard Lygre <hklygre@online.no>
.\" Modified 2001-04-17 by Michael Kerrisk <mtk.manpages@gmail.com>
.\" Modified 2002-06-13 by Michael Kerrisk <mtk.manpages@gmail.com>
.\"     Added note on non-standard behavior when SIGCHLD is ignored.
.\" Modified 2002-07-09 by Michael Kerrisk <mtk.manpages@gmail.com>
.\"	Enhanced descriptions of 'resource' values for [gs]etrlimit()
.\" Modified 2003-11-28 by aeb, added RLIMIT_CORE
.\" Modified 2004-03-26 by aeb, added RLIMIT_AS
.\" Modified 2004-06-16 by Michael Kerrisk <mtk.manpages@gmail.com>
.\"     Added notes on CAP_SYS_RESOURCE
.\"
.\" 2004-11-16 -- mtk: the getrlimit.2 page, which formally included
.\" coverage of getrusage(2), has been split, so that the latter
.\" is now covered in its own getrusage.2.
.\"
.\" Modified 2004-11-16, mtk: A few other minor changes
.\" Modified 2004-11-23, mtk
.\"	Added notes on RLIMIT_MEMLOCK, RLIMIT_NPROC, and RLIMIT_RSS
.\"		to "CONFORMING TO"
.\" Modified 2004-11-25, mtk
.\"	Rewrote discussion on RLIMIT_MEMLOCK to incorporate kernel
.\"		2.6.9 changes.
.\"	Added note on RLIMIT_CPU error in older kernels
.\" 2004-11-03, mtk, Added RLIMIT_SIGPENDING
.\" 2005-07-13, mtk, documented RLIMIT_MSGQUEUE limit.
.\" 2005-07-28, mtk, Added descriptions of RLIMIT_NICE and RLIMIT_RTPRIO
.\" 2008-05-07, mtk / Peter Zijlstra, Added description of RLIMIT_RTTIME
.\"
.TH GETRLIMIT 2 2008-10-06 "Linux" "Linux Programmer's Manual"
.SH NAME
getrlimit, setrlimit \- get/set resource limits
.SH SYNOPSIS
.B #include <sys/time.h>
.br
.B #include <sys/resource.h>
.sp
.BI "int getrlimit(int " resource ", struct rlimit *" rlim );
.br
.BI "int setrlimit(int " resource ", const struct rlimit *" rlim );
.SH DESCRIPTION
.BR getrlimit ()
and
.BR setrlimit ()
get and set resource limits respectively.
Each resource has an associated soft and hard limit, as defined by the
.I rlimit
structure (the
.I rlim
argument to both
.BR getrlimit ()
and
.BR setrlimit ()):
.PP
.in +4n
.nf
struct rlimit {
    rlim_t rlim_cur;  /* Soft limit */
    rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
};

.fi
.in
The soft limit is the value that the kernel enforces for the
corresponding resource.
The hard limit acts as a ceiling for the soft limit:
an unprivileged process may only set its soft limit to a value in the
range from 0 up to the hard limit, and (irreversibly) lower its hard limit.
A privileged process (under Linux: one with the
.B CAP_SYS_RESOURCE
capability) may make arbitrary changes to either limit value.
.PP
The value
.B RLIM_INFINITY
denotes no limit on a resource (both in the structure returned by
.BR getrlimit ()
and in the structure passed to
.BR setrlimit ()).
.PP
.I resource
must be one of:
.TP
.B RLIMIT_AS
The maximum size of the process's virtual memory (address space) in bytes.
.\" since 2.0.27 / 2.1.12
This limit affects calls to
.BR brk (2),
.BR mmap (2)
and
.BR mremap (2),
which fail with the error
.B ENOMEM
upon exceeding this limit.
Also automatic stack expansion will fail
(and generate a
.B SIGSEGV
that kills the process if no alternate stack
has been made available via
.BR sigaltstack (2)).
Since the value is a \fIlong\fP, on machines with a 32-bit \fIlong\fP
either this limit is at most 2 GiB, or this resource is unlimited.
.TP
.B RLIMIT_CORE
Maximum size of
.I core
file.
When 0 no core dump files are created.
When non-zero, larger dumps are truncated to this size.
.TP
.B RLIMIT_CPU
CPU time limit in seconds.
When the process reaches the soft limit, it is sent a
.B SIGXCPU
signal.
The default action for this signal is to terminate the process.
However, the signal can be caught, and the handler can return control to
the main program.
If the process continues to consume CPU time, it will be sent
.B SIGXCPU
once per second until the hard limit is reached, at which time
it is sent
.BR SIGKILL .
(This latter point describes Linux 2.2 through 2.6 behavior.
Implementations vary in how they treat processes which continue to
consume CPU time after reaching the soft limit.
Portable applications that need to catch this signal should
perform an orderly termination upon first receipt of
.BR SIGXCPU .)
.TP
.B RLIMIT_DATA
The maximum size of the process's data segment (initialized data,
uninitialized data, and heap).
This limit affects calls to
.BR brk (2)
and
.BR sbrk (2),
which fail with the error
.B ENOMEM
upon encountering the soft limit of this resource.
.TP
.B RLIMIT_FSIZE
The maximum size of files that the process may create.
Attempts to extend a file beyond this limit result in delivery of a
.B SIGXFSZ
signal.
By default, this signal terminates a process, but a process can
catch this signal instead, in which case the relevant system call (e.g.,
.BR write (2),
.BR truncate (2))
fails with the error
.BR EFBIG .
.TP
.BR RLIMIT_LOCKS " (Early Linux 2.4 only)"
.\" to be precise: Linux 2.4.0-test9; no longer in 2.4.25 / 2.5.65
A limit on the combined number of
.BR flock (2)
locks and
.BR fcntl (2)
leases that this process may establish.
.TP
.B RLIMIT_MEMLOCK
The maximum number of bytes of memory that may be locked
into RAM.
In effect this limit is rounded down to the nearest multiple
of the system page size.
This limit affects
.BR mlock (2)
and
.BR mlockall (2)
and the
.BR mmap (2)
.B MAP_LOCKED
operation.
Since Linux 2.6.9 it also affects the
.BR shmctl (2)
.B SHM_LOCK
operation, where it sets a maximum on the total bytes in
shared memory segments (see
.BR shmget (2))
that may be locked by the real user ID of the calling process.
The
.BR shmctl (2)
.B SHM_LOCK
locks are accounted for separately from the per-process memory
locks established by
.BR mlock (2),
.BR mlockall (2),
and
.BR mmap (2)
.BR MAP_LOCKED ;
a process can lock bytes up to this limit in each of these
two categories.
In Linux kernels before 2.6.9, this limit controlled the amount of
memory that could be locked by a privileged process.
Since Linux 2.6.9, no limits are placed on the amount of memory
that a privileged process may lock, and this limit instead governs
the amount of memory that an unprivileged process may lock.
.TP
.BR RLIMIT_MSGQUEUE " (Since Linux 2.6.8)"
Specifies the limit on the number of bytes that can be allocated
for POSIX message queues for the real user ID of the calling process.
This limit is enforced for
.BR mq_open (3).
Each message queue that the user creates counts (until it is removed)
against this limit according to the formula:
.nf

    bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
            attr.mq_maxmsg * attr.mq_msgsize

.fi
where
.I attr
is the
.I mq_attr
structure specified as the fourth argument to
.BR mq_open (3).

The first addend in the formula, which includes
.I "sizeof(struct msg_msg *)"
(4 bytes on Linux/i386), ensures that the user cannot
create an unlimited number of zero-length messages (such messages
nevertheless each consume some system memory for bookkeeping overhead).
.TP
.BR RLIMIT_NICE " (since Linux 2.6.12, but see BUGS below)"
Specifies a ceiling to which the process's nice value can be raised using
.BR setpriority (2)
or
.BR nice (2).
The actual ceiling for the nice value is calculated as
.IR "20\ \-\ rlim_cur" .
(This strangeness occurs because negative numbers cannot be specified
as resource limit values, since they typically have special meanings.
For example,
.B RLIM_INFINITY
typically is the same as \-1.)
.TP
.B RLIMIT_NOFILE
Specifies a value one greater than the maximum file descriptor number
that can be opened by this process.
Attempts
.RB ( open (2),
.BR pipe (2),
.BR dup (2),
etc.)
to exceed this limit yield the error
.BR EMFILE .
(Historically, this limit was named
.B RLIMIT_OFILE
on BSD.)
.TP
.B RLIMIT_NPROC
The maximum number of processes (or, more precisely on Linux, threads)
that can be created for the real user ID of the calling process.
Upon encountering this limit,
.BR fork (2)
fails with the error
.BR EAGAIN .
.TP
.B RLIMIT_RSS
Specifies the limit (in pages) of the process's resident set
(the number of virtual pages resident in RAM).
This limit only has effect in Linux 2.4.x, x < 30, and there only
affects calls to
.BR madvise (2)
specifying
.BR MADV_WILLNEED .
.\" As at kernel 2.6.12, this limit still does nothing in 2.6 though
.\" talk of making it do something has surfaced from time to time in LKML
.\"       -- MTK, Jul 05
.TP
.BR RLIMIT_RTPRIO " (Since Linux 2.6.12, but see BUGS)"
Specifies a ceiling on the real-time priority that may be set for
this process using
.BR sched_setscheduler (2)
and
.BR sched_setparam (2).
.TP
.BR RLIMIT_RTTIME " (Since Linux 2.6.25)"
Specifies a limit on the amount of CPU time that a process scheduled
under a real-time scheduling policy may consume without making a blocking
system call.
For the purpose of this limit,
each time a process makes a blocking system call,
the count of its consumed CPU time is reset to zero.
The CPU time count is not reset if the process continues trying to
use the CPU but is preempted, its time slice expires, or it calls
.BR sched_yield (2).

Upon reaching the soft limit, the process is sent a
.B SIGXCPU
signal.
If the process catches or ignores this signal and
continues consuming CPU time, then
.B SIGXCPU
will be generated once each second until the hard limit is reached,
at which point the process is sent a
.B SIGKILL
signal.

The intended use of this limit is to stop a runaway
real-time process from locking up the system.
.TP
.BR RLIMIT_SIGPENDING " (Since Linux 2.6.8)"
Specifies the limit on the number of signals
that may be queued for the real user ID of the calling process.
Both standard and real-time signals are counted for the purpose of
checking this limit.
However, the limit is only enforced for
.BR sigqueue (2);
it is always possible to use
.BR kill (2)
to queue one instance of any of the signals that are not already
queued to the process.
.\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit
.\" that was present in kernels <= 2.6.7.  MTK Dec 04
.TP
.B RLIMIT_STACK
The maximum size of the process stack, in bytes.
Upon reaching this limit, a
.B SIGSEGV
signal is generated.
To handle this signal, a process must employ an alternate signal stack
.RB ( sigaltstack (2)).

Since Linux 2.6.23,
this limit also determines the amount of space used for the process's
command-line arguments and environment variables; for details, see
.BR execve (2).
.SH "RETURN VALUE"
On success, zero is returned.
On error, \-1 is returned, and
.I errno
is set appropriately.
.SH ERRORS
.TP
.B EFAULT
.I rlim
points outside the accessible address space.
.TP
.B EINVAL
.I resource
is not valid;
or, for
.BR setrlimit ():
.I rlim\->rlim_cur
was greater than
.IR rlim\->rlim_max .
.TP
.B EPERM
An unprivileged process tried to use
.BR setrlimit ()
to
increase a soft or hard limit above the current hard limit; the
.B CAP_SYS_RESOURCE
capability is required to do this.
Or, the process tried to use
.BR setrlimit ()
to increase
the soft or hard
.B RLIMIT_NOFILE
limit above the current kernel
maximum
.RB ( NR_OPEN ).
.SH CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001.
.B RLIMIT_MEMLOCK
and
.B RLIMIT_NPROC
derive from BSD and are not specified in POSIX.1-2001;
they are present on the BSDs and Linux, but on few other implementations.
.B RLIMIT_RSS
derives from BSD and is not specified in POSIX.1-2001;
it is nevertheless present on most implementations.
.BR RLIMIT_MSGQUEUE ,
.BR RLIMIT_NICE ,
.BR RLIMIT_RTPRIO ,
.BR RLIMIT_RTTIME ,
and
.B RLIMIT_SIGPENDING
are Linux-specific.
.SH NOTES
A child process created via
.BR fork (2)
inherits its parent's resource limits.
Resource limits are preserved across
.BR execve (2).

One can set the resource limits of the shell using the built-in
.IR ulimit
command
.RI ( limit
in
.BR csh (1)).
The shell's resource limits are inherited by the processes that
it creates to execute commands.
.SH BUGS
In older Linux kernels, the
.B SIGXCPU
and
.B SIGKILL
signals delivered when a process encountered the soft and hard
.B RLIMIT_CPU
limits were delivered one (CPU) second later than they should have been.
This was fixed in kernel 2.6.8.

In 2.6.x kernels before 2.6.17, a
.B RLIMIT_CPU
limit of 0 is wrongly treated as "no limit" (like
.BR RLIM_INFINITY ).
Since Linux 2.6.17, setting a limit of 0 does have an effect,
but is actually treated as a limit of 1 second.
.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=114008066530167&w=2

A kernel bug means that
.B RLIMIT_RTPRIO
does not work in kernel 2.6.12; the problem is fixed in kernel 2.6.13.

In kernel 2.6.12, there was an off-by-one mismatch
between the priority ranges returned by
.BR getpriority (2)
and
.BR RLIMIT_NICE .
This had the effect that actual ceiling for the nice value
was calculated as
.IR "19\ \-\ rlim_cur" .
This was fixed in kernel 2.6.13.
.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=112256338703880&w=2

Kernels before 2.4.22 did not diagnose the error
.B EINVAL
for
.BR setrlimit ()
when
.I rlim\->rlim_cur
was greater than
.IR rlim\->rlim_max .
.SH "SEE ALSO"
.BR dup (2),
.BR fcntl (2),
.BR fork (2),
.BR getrusage (2),
.BR mlock (2),
.BR mmap (2),
.BR open (2),
.BR quotactl (2),
.BR sbrk (2),
.BR shmctl (2),
.BR sigqueue (2),
.BR malloc (3),
.BR ulimit (3),
.BR core (5),
.BR capabilities (7),
.BR signal (7)
.SH COLOPHON
This page is part of release 3.22 of the Linux
.I man-pages
project.
A description of the project,
and information about reporting bugs,
can be found at
http://www.kernel.org/doc/man-pages/.
