Blame man2/getrlimit.2

Packit 7cfc04
.\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
Packit 7cfc04
.\" and Copyright (c) 2002, 2004, 2005, 2008, 2010 Michael Kerrisk
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified by Michael Haardt <michael@moria.de>
Packit 7cfc04
.\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified 1996-01-13 by Arnt Gulbrandsen <agulbra@troll.no>
Packit 7cfc04
.\" Modified 1996-01-22 by aeb, following a remark by
Packit 7cfc04
.\"          Tigran Aivazian <tigran@sco.com>
Packit 7cfc04
.\" Modified 1996-04-14 by aeb, following a remark by
Packit 7cfc04
.\"          Robert Bihlmeyer <robbe@orcus.ping.at>
Packit 7cfc04
.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" Modified 2001-05-04 by aeb, following a remark by
Packit 7cfc04
.\"          HÃ¥vard Lygre <hklygre@online.no>
Packit 7cfc04
.\" Modified 2001-04-17 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" Modified 2002-06-13 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"     Added note on nonstandard behavior when SIGCHLD is ignored.
Packit 7cfc04
.\" Modified 2002-07-09 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"	Enhanced descriptions of 'resource' values
Packit 7cfc04
.\" Modified 2003-11-28 by aeb, added RLIMIT_CORE
Packit 7cfc04
.\" Modified 2004-03-26 by aeb, added RLIMIT_AS
Packit 7cfc04
.\" Modified 2004-06-16 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"     Added notes on CAP_SYS_RESOURCE
Packit 7cfc04
.\"
Packit 7cfc04
.\" 2004-11-16 -- mtk: the getrlimit.2 page, which formally included
Packit 7cfc04
.\" coverage of getrusage(2), has been split, so that the latter
Packit 7cfc04
.\" is now covered in its own getrusage.2.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified 2004-11-16, mtk: A few other minor changes
Packit 7cfc04
.\" Modified 2004-11-23, mtk
Packit 7cfc04
.\"	Added notes on RLIMIT_MEMLOCK, RLIMIT_NPROC, and RLIMIT_RSS
Packit 7cfc04
.\"		to "CONFORMING TO"
Packit 7cfc04
.\" Modified 2004-11-25, mtk
Packit 7cfc04
.\"	Rewrote discussion on RLIMIT_MEMLOCK to incorporate kernel
Packit 7cfc04
.\"		2.6.9 changes.
Packit 7cfc04
.\"	Added note on RLIMIT_CPU error in older kernels
Packit 7cfc04
.\" 2004-11-03, mtk, Added RLIMIT_SIGPENDING
Packit 7cfc04
.\" 2005-07-13, mtk, documented RLIMIT_MSGQUEUE limit.
Packit 7cfc04
.\" 2005-07-28, mtk, Added descriptions of RLIMIT_NICE and RLIMIT_RTPRIO
Packit 7cfc04
.\" 2008-05-07, mtk / Peter Zijlstra, Added description of RLIMIT_RTTIME
Packit 7cfc04
.\" 2010-11-06, mtk: Added documentation of prlimit()
Packit 7cfc04
.\"
Packit 7cfc04
.TH GETRLIMIT 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
getrlimit, setrlimit, prlimit \- get/set resource limits
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <sys/time.h>
Packit 7cfc04
.br
Packit 7cfc04
.B #include <sys/resource.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int getrlimit(int " resource ", struct rlimit *" rlim );
Packit 7cfc04
.br
Packit 7cfc04
.BI "int setrlimit(int " resource ", const struct rlimit *" rlim );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int prlimit(pid_t "  pid ", int " resource \
Packit 7cfc04
", const struct rlimit *" new_limit ,
Packit 7cfc04
.br
Packit 7cfc04
.BI "            struct rlimit *" old_limit );
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR prlimit ():
Packit 7cfc04
_GNU_SOURCE
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR getrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
system calls get and set resource limits respectively.
Packit 7cfc04
Each resource has an associated soft and hard limit, as defined by the
Packit 7cfc04
.I rlimit
Packit 7cfc04
structure:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct rlimit {
Packit 7cfc04
    rlim_t rlim_cur;  /* Soft limit */
Packit 7cfc04
    rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The soft limit is the value that the kernel enforces for the
Packit 7cfc04
corresponding resource.
Packit 7cfc04
The hard limit acts as a ceiling for the soft limit:
Packit 7cfc04
an unprivileged process may set only its soft limit to a value in the
Packit 7cfc04
range from 0 up to the hard limit, and (irreversibly) lower its hard limit.
Packit 7cfc04
A privileged process (under Linux: one with the
Packit 7cfc04
.B CAP_SYS_RESOURCE
Packit 7cfc04
capability) may make arbitrary changes to either limit value.
Packit 7cfc04
.PP
Packit 7cfc04
The value
Packit 7cfc04
.B RLIM_INFINITY
Packit 7cfc04
denotes no limit on a resource (both in the structure returned by
Packit 7cfc04
.BR getrlimit ()
Packit 7cfc04
and in the structure passed to
Packit 7cfc04
.BR setrlimit ()).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I resource
Packit 7cfc04
argument must be one of:
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_AS
Packit 7cfc04
This is the maximum size of the process's virtual memory
Packit 7cfc04
(address space).
Packit 7cfc04
The limit is specified in bytes, and is rounded down to the system page size.
Packit 7cfc04
.\" since 2.0.27 / 2.1.12
Packit 7cfc04
This limit affects calls to
Packit 7cfc04
.BR brk (2),
Packit 7cfc04
.BR mmap (2),
Packit 7cfc04
and
Packit 7cfc04
.BR mremap (2),
Packit 7cfc04
which fail with the error
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
upon exceeding this limit.
Packit 7cfc04
In addition, automatic stack expansion fails
Packit 7cfc04
(and generates a
Packit 7cfc04
.B SIGSEGV
Packit 7cfc04
that kills the process if no alternate stack
Packit 7cfc04
has been made available via
Packit 7cfc04
.BR sigaltstack (2)).
Packit 7cfc04
Since the value is a \fIlong\fP, on machines with a 32-bit \fIlong\fP
Packit 7cfc04
either this limit is at most 2\ GiB, or this resource is unlimited.
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_CORE
Packit 7cfc04
This is the maximum size of a
Packit 7cfc04
.I core
Packit 7cfc04
file (see
Packit 7cfc04
.BR core (5))
Packit 7cfc04
in bytes that the process may dump.
Packit 7cfc04
When 0 no core dump files are created.
Packit 7cfc04
When nonzero, larger dumps are truncated to this size.
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_CPU
Packit 7cfc04
This is a limit, in seconds,
Packit 7cfc04
on the amount of CPU time that the process can consume.
Packit 7cfc04
When the process reaches the soft limit, it is sent a
Packit 7cfc04
.B SIGXCPU
Packit 7cfc04
signal.
Packit 7cfc04
The default action for this signal is to terminate the process.
Packit 7cfc04
However, the signal can be caught, and the handler can return control to
Packit 7cfc04
the main program.
Packit 7cfc04
If the process continues to consume CPU time, it will be sent
Packit 7cfc04
.B SIGXCPU
Packit 7cfc04
once per second until the hard limit is reached, at which time
Packit 7cfc04
it is sent
Packit 7cfc04
.BR SIGKILL .
Packit 7cfc04
(This latter point describes Linux behavior.
Packit 7cfc04
Implementations vary in how they treat processes which continue to
Packit 7cfc04
consume CPU time after reaching the soft limit.
Packit 7cfc04
Portable applications that need to catch this signal should
Packit 7cfc04
perform an orderly termination upon first receipt of
Packit 7cfc04
.BR SIGXCPU .)
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_DATA
Packit 7cfc04
This is the maximum size
Packit 7cfc04
of the process's data segment (initialized data,
Packit 7cfc04
uninitialized data, and heap).
Packit 7cfc04
The limit is specified in bytes, and is rounded down to the system page size.
Packit 7cfc04
This limit affects calls to
Packit 7cfc04
.BR brk (2),
Packit 7cfc04
.BR sbrk (2),
Packit 7cfc04
and (since Linux 4.7)
Packit 7cfc04
.BR mmap (2),
Packit 7cfc04
.\" commits 84638335900f1995495838fe1bd4870c43ec1f67
Packit 7cfc04
.\" ("mm: rework virtual memory accounting"),
Packit 7cfc04
.\" f4fcd55841fc9e46daac553b39361572453c2b88
Packit 7cfc04
.\" (mm: enable RLIMIT_DATA by default with workaround for valgrind).
Packit 7cfc04
which fail with the error
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
upon encountering the soft limit of this resource.
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_FSIZE
Packit 7cfc04
This is the maximum size in bytes of files that the process may create.
Packit 7cfc04
Attempts to extend a file beyond this limit result in delivery of a
Packit 7cfc04
.B SIGXFSZ
Packit 7cfc04
signal.
Packit 7cfc04
By default, this signal terminates a process, but a process can
Packit 7cfc04
catch this signal instead, in which case the relevant system call (e.g.,
Packit 7cfc04
.BR write (2),
Packit 7cfc04
.BR truncate (2))
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR EFBIG .
Packit 7cfc04
.TP
Packit 7cfc04
.BR RLIMIT_LOCKS " (early Linux 2.4 only)"
Packit 7cfc04
.\" to be precise: Linux 2.4.0-test9; no longer in 2.4.25 / 2.5.65
Packit 7cfc04
This is a limit on the combined number of
Packit 7cfc04
.BR flock (2)
Packit 7cfc04
locks and
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
leases that this process may establish.
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_MEMLOCK
Packit 7cfc04
This is the maximum number of bytes of memory that may be locked
Packit 7cfc04
into RAM.
Packit 7cfc04
This limit is in effect rounded down to the nearest multiple
Packit 7cfc04
of the system page size.
Packit 7cfc04
This limit affects
Packit 7cfc04
.BR mlock (2),
Packit 7cfc04
.BR mlockall (2),
Packit 7cfc04
and the
Packit 7cfc04
.BR mmap (2)
Packit 7cfc04
.B MAP_LOCKED
Packit 7cfc04
operation.
Packit 7cfc04
Since Linux 2.6.9, it also affects the
Packit 7cfc04
.BR shmctl (2)
Packit 7cfc04
.B SHM_LOCK
Packit 7cfc04
operation, where it sets a maximum on the total bytes in
Packit 7cfc04
shared memory segments (see
Packit 7cfc04
.BR shmget (2))
Packit 7cfc04
that may be locked by the real user ID of the calling process.
Packit 7cfc04
The
Packit 7cfc04
.BR shmctl (2)
Packit 7cfc04
.B SHM_LOCK
Packit 7cfc04
locks are accounted for separately from the per-process memory
Packit 7cfc04
locks established by
Packit 7cfc04
.BR mlock (2),
Packit 7cfc04
.BR mlockall (2),
Packit 7cfc04
and
Packit 7cfc04
.BR mmap (2)
Packit 7cfc04
.BR MAP_LOCKED ;
Packit 7cfc04
a process can lock bytes up to this limit in each of these
Packit 7cfc04
two categories.
Packit 7cfc04
.IP
Packit 7cfc04
In Linux kernels before 2.6.9, this limit controlled the amount of
Packit 7cfc04
memory that could be locked by a privileged process.
Packit 7cfc04
Since Linux 2.6.9, no limits are placed on the amount of memory
Packit 7cfc04
that a privileged process may lock, and this limit instead governs
Packit 7cfc04
the amount of memory that an unprivileged process may lock.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RLIMIT_MSGQUEUE " (since Linux 2.6.8)"
Packit 7cfc04
This is a limit on the number of bytes that can be allocated
Packit 7cfc04
for POSIX message queues for the real user ID of the calling process.
Packit 7cfc04
This limit is enforced for
Packit 7cfc04
.BR mq_open (3).
Packit 7cfc04
Each message queue that the user creates counts (until it is removed)
Packit 7cfc04
against this limit according to the formula:
Packit 7cfc04
.IP
Packit 7cfc04
    Since Linux 3.5:
Packit 7cfc04
.IP
Packit 7cfc04
.EX
Packit 7cfc04
        bytes = attr.mq_maxmsg * sizeof(struct msg_msg) +
Packit 7cfc04
                min(attr.mq_maxmsg, MQ_PRIO_MAX) *
Packit 7cfc04
                      sizeof(struct posix_msg_tree_node)+
Packit 7cfc04
                                /* For overhead */
Packit 7cfc04
                attr.mq_maxmsg * attr.mq_msgsize;
Packit 7cfc04
                                /* For message data */
Packit 7cfc04
.EE
Packit 7cfc04
.IP
Packit 7cfc04
    Linux 3.4 and earlier:
Packit 7cfc04
.IP
Packit 7cfc04
.EX
Packit 7cfc04
        bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
Packit 7cfc04
                                /* For overhead */
Packit 7cfc04
                attr.mq_maxmsg * attr.mq_msgsize;
Packit 7cfc04
                                /* For message data */
Packit 7cfc04
.EE
Packit 7cfc04
.IP
Packit 7cfc04
where
Packit 7cfc04
.I attr
Packit 7cfc04
is the
Packit 7cfc04
.I mq_attr
Packit 7cfc04
structure specified as the fourth argument to
Packit 7cfc04
.BR mq_open (3),
Packit 7cfc04
and the
Packit 7cfc04
.I msg_msg
Packit 7cfc04
and
Packit 7cfc04
.I posix_msg_tree_node
Packit 7cfc04
structures are kernel-internal structures.
Packit 7cfc04
.IP
Packit 7cfc04
The "overhead" addend in the formula accounts for overhead
Packit 7cfc04
bytes required by the implementation
Packit 7cfc04
and ensures that the user cannot
Packit 7cfc04
create an unlimited number of zero-length messages (such messages
Packit 7cfc04
nevertheless each consume some system memory for bookkeeping overhead).
Packit 7cfc04
.TP
Packit 7cfc04
.BR RLIMIT_NICE " (since Linux 2.6.12, but see BUGS below)"
Packit 7cfc04
This specifies a ceiling to which the process's nice value can be raised using
Packit 7cfc04
.BR setpriority (2)
Packit 7cfc04
or
Packit 7cfc04
.BR nice (2).
Packit 7cfc04
The actual ceiling for the nice value is calculated as
Packit 7cfc04
.IR "20\ \-\ rlim_cur" .
Packit 7cfc04
The useful range for this limit is thus from 1
Packit 7cfc04
(corresponding to a nice value of 19) to 40
Packit 7cfc04
(corresponding to a nice value of -20).
Packit 7cfc04
This unusual choice of range was necessary
Packit 7cfc04
because negative numbers cannot be specified
Packit 7cfc04
as resource limit values, since they typically have special meanings.
Packit 7cfc04
For example,
Packit 7cfc04
.B RLIM_INFINITY
Packit 7cfc04
typically is the same as \-1.
Packit 7cfc04
For more detail on the nice value, see
Packit 7cfc04
.BR sched (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_NOFILE
Packit 7cfc04
This specifies a value one greater than the maximum file descriptor number
Packit 7cfc04
that can be opened by this process.
Packit 7cfc04
Attempts
Packit 7cfc04
.RB ( open (2),
Packit 7cfc04
.BR pipe (2),
Packit 7cfc04
.BR dup (2),
Packit 7cfc04
etc.)
Packit 7cfc04
to exceed this limit yield the error
Packit 7cfc04
.BR EMFILE .
Packit 7cfc04
(Historically, this limit was named
Packit 7cfc04
.B RLIMIT_OFILE
Packit 7cfc04
on BSD.)
Packit 7cfc04
.IP
Packit 7cfc04
Since Linux 4.5,
Packit 7cfc04
this limit also defines the maximum number of file descriptors that
Packit 7cfc04
an unprivileged process (one without the
Packit 7cfc04
.BR CAP_SYS_RESOURCE
Packit 7cfc04
capability) may have "in flight" to other processes,
Packit 7cfc04
by being passed across UNIX domain sockets.
Packit 7cfc04
This limit applies to the
Packit 7cfc04
.BR sendmsg (2)
Packit 7cfc04
system call.
Packit 7cfc04
For further details, see
Packit 7cfc04
.BR unix (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_NPROC
Packit 7cfc04
This is a limit on the number of extant process
Packit 7cfc04
(or, more precisely on Linux, threads)
Packit 7cfc04
for the real user ID of the calling process.
Packit 7cfc04
So long as the current number of processes belonging to this
Packit 7cfc04
process's real user ID is greater than or equal to this limit,
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR EAGAIN .
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.B RLIMIT_NPROC
Packit 7cfc04
limit is not enforced for processes that have either the
Packit 7cfc04
.B CAP_SYS_ADMIN
Packit 7cfc04
or the
Packit 7cfc04
.B CAP_SYS_RESOURCE
Packit 7cfc04
capability.
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_RSS
Packit 7cfc04
This is a limit (in bytes) on the process's resident set
Packit 7cfc04
(the number of virtual pages resident in RAM).
Packit 7cfc04
This limit has effect only in Linux 2.4.x, x < 30, and there
Packit 7cfc04
affects only calls to
Packit 7cfc04
.BR madvise (2)
Packit 7cfc04
specifying
Packit 7cfc04
.BR MADV_WILLNEED .
Packit 7cfc04
.\" As at kernel 2.6.12, this limit still does nothing in 2.6 though
Packit 7cfc04
.\" talk of making it do something has surfaced from time to time in LKML
Packit 7cfc04
.\"       -- MTK, Jul 05
Packit 7cfc04
.TP
Packit 7cfc04
.BR RLIMIT_RTPRIO " (since Linux 2.6.12, but see BUGS)"
Packit 7cfc04
This specifies a ceiling on the real-time priority that may be set for
Packit 7cfc04
this process using
Packit 7cfc04
.BR sched_setscheduler (2)
Packit 7cfc04
and
Packit 7cfc04
.BR sched_setparam (2).
Packit 7cfc04
.IP
Packit 7cfc04
For further details on real-time scheduling policies, see
Packit 7cfc04
.BR sched (7)
Packit 7cfc04
.TP
Packit 7cfc04
.BR RLIMIT_RTTIME " (since Linux 2.6.25)"
Packit 7cfc04
This is a limit (in microseconds)
Packit 7cfc04
on the amount of CPU time that a process scheduled
Packit 7cfc04
under a real-time scheduling policy may consume without making a blocking
Packit 7cfc04
system call.
Packit 7cfc04
For the purpose of this limit,
Packit 7cfc04
each time a process makes a blocking system call,
Packit 7cfc04
the count of its consumed CPU time is reset to zero.
Packit 7cfc04
The CPU time count is not reset if the process continues trying to
Packit 7cfc04
use the CPU but is preempted, its time slice expires, or it calls
Packit 7cfc04
.BR sched_yield (2).
Packit 7cfc04
.IP
Packit 7cfc04
Upon reaching the soft limit, the process is sent a
Packit 7cfc04
.B SIGXCPU
Packit 7cfc04
signal.
Packit 7cfc04
If the process catches or ignores this signal and
Packit 7cfc04
continues consuming CPU time, then
Packit 7cfc04
.B SIGXCPU
Packit 7cfc04
will be generated once each second until the hard limit is reached,
Packit 7cfc04
at which point the process is sent a
Packit 7cfc04
.B SIGKILL
Packit 7cfc04
signal.
Packit 7cfc04
.IP
Packit 7cfc04
The intended use of this limit is to stop a runaway
Packit 7cfc04
real-time process from locking up the system.
Packit 7cfc04
.IP
Packit 7cfc04
For further details on real-time scheduling policies, see
Packit 7cfc04
.BR sched (7)
Packit 7cfc04
.TP
Packit 7cfc04
.BR RLIMIT_SIGPENDING " (since Linux 2.6.8)"
Packit 7cfc04
This is a limit on the number of signals
Packit 7cfc04
that may be queued for the real user ID of the calling process.
Packit 7cfc04
Both standard and real-time signals are counted for the purpose of
Packit 7cfc04
checking this limit.
Packit 7cfc04
However, the limit is enforced only for
Packit 7cfc04
.BR sigqueue (3);
Packit 7cfc04
it is always possible to use
Packit 7cfc04
.BR kill (2)
Packit 7cfc04
to queue one instance of any of the signals that are not already
Packit 7cfc04
queued to the process.
Packit 7cfc04
.\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit
Packit 7cfc04
.\" that was present in kernels <= 2.6.7.  MTK Dec 04
Packit 7cfc04
.TP
Packit 7cfc04
.B RLIMIT_STACK
Packit 7cfc04
This is the maximum size of the process stack, in bytes.
Packit 7cfc04
Upon reaching this limit, a
Packit 7cfc04
.B SIGSEGV
Packit 7cfc04
signal is generated.
Packit 7cfc04
To handle this signal, a process must employ an alternate signal stack
Packit 7cfc04
.RB ( sigaltstack (2)).
Packit 7cfc04
.IP
Packit 7cfc04
Since Linux 2.6.23,
Packit 7cfc04
this limit also determines the amount of space used for the process's
Packit 7cfc04
command-line arguments and environment variables; for details, see
Packit 7cfc04
.BR execve (2).
Packit 7cfc04
.SS prlimit()
Packit 7cfc04
.\" commit c022a0acad534fd5f5d5f17280f6d4d135e74e81
Packit 7cfc04
.\" Author: Jiri Slaby <jslaby@suse.cz>
Packit 7cfc04
.\" Date:   Tue May 4 18:03:50 2010 +0200
Packit 7cfc04
.\"
Packit 7cfc04
.\"     rlimits: implement prlimit64 syscall
Packit 7cfc04
.\"
Packit 7cfc04
.\" commit 6a1d5e2c85d06da35cdfd93f1a27675bfdc3ad8c
Packit 7cfc04
.\" Author: Jiri Slaby <jslaby@suse.cz>
Packit 7cfc04
.\" Date:   Wed Mar 24 17:06:58 2010 +0100
Packit 7cfc04
.\"
Packit 7cfc04
.\"     rlimits: add rlimit64 structure
Packit 7cfc04
.\"
Packit 7cfc04
The Linux-specific
Packit 7cfc04
.BR prlimit ()
Packit 7cfc04
system call combines and extends the functionality of
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR getrlimit ().
Packit 7cfc04
It can be used to both set and get the resource limits of an arbitrary process.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I resource
Packit 7cfc04
argument has the same meaning as for
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR getrlimit ().
Packit 7cfc04
.PP
Packit 7cfc04
If the
Packit 7cfc04
.IR new_limit
Packit 7cfc04
argument is a not NULL, then the
Packit 7cfc04
.I rlimit
Packit 7cfc04
structure to which it points is used to set new values for
Packit 7cfc04
the soft and hard limits for
Packit 7cfc04
.IR resource .
Packit 7cfc04
If the
Packit 7cfc04
.IR old_limit
Packit 7cfc04
argument is a not NULL, then a successful call to
Packit 7cfc04
.BR prlimit ()
Packit 7cfc04
places the previous soft and hard limits for
Packit 7cfc04
.I resource
Packit 7cfc04
in the
Packit 7cfc04
.I rlimit
Packit 7cfc04
structure pointed to by
Packit 7cfc04
.IR old_limit .
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I pid
Packit 7cfc04
argument specifies the ID of the process on which the call is to operate.
Packit 7cfc04
If
Packit 7cfc04
.I pid
Packit 7cfc04
is 0, then the call applies to the calling process.
Packit 7cfc04
To set or get the resources of a process other than itself,
Packit 7cfc04
the caller must have the
Packit 7cfc04
.B CAP_SYS_RESOURCE
Packit 7cfc04
capability in the user namespace of the process
Packit 7cfc04
whose resource limits are being changed, or the
Packit 7cfc04
real, effective, and saved set user IDs of the target process
Packit 7cfc04
must match the real user ID of the caller
Packit 7cfc04
.I and
Packit 7cfc04
the real, effective, and saved set group IDs of the target process
Packit 7cfc04
must match the real group ID of the caller.
Packit 7cfc04
.\" FIXME . this permission check is strange
Packit 7cfc04
.\" Asked about this on LKML, 7 Nov 2010
Packit 7cfc04
.\"     "Inconsistent credential checking in prlimit() syscall"
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, these system calls return 0.
Packit 7cfc04
On error, \-1 is returned, and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
A pointer argument points to a location
Packit 7cfc04
outside the accessible address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The value specified in
Packit 7cfc04
.I resource
Packit 7cfc04
is not valid;
Packit 7cfc04
or, for
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
or
Packit 7cfc04
.BR prlimit ():
Packit 7cfc04
.I rlim\->rlim_cur
Packit 7cfc04
was greater than
Packit 7cfc04
.IR rlim\->rlim_max .
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
An unprivileged process tried to raise the hard limit; the
Packit 7cfc04
.B CAP_SYS_RESOURCE
Packit 7cfc04
capability is required to do this.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The caller tried to increase the hard
Packit 7cfc04
.B RLIMIT_NOFILE
Packit 7cfc04
limit above the maximum defined by
Packit 7cfc04
.IR /proc/sys/fs/nr_open
Packit 7cfc04
(see
Packit 7cfc04
.BR proc (5))
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
.RB ( prlimit ())
Packit 7cfc04
The calling process did not have permission to set limits
Packit 7cfc04
for the process specified by
Packit 7cfc04
.IR pid .
Packit 7cfc04
.TP
Packit 7cfc04
.B ESRCH
Packit 7cfc04
Could not find a process with the ID specified in
Packit 7cfc04
.IR pid .
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
The
Packit 7cfc04
.BR prlimit ()
Packit 7cfc04
system call is available since Linux 2.6.36.
Packit 7cfc04
Library support is available since glibc 2.13.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lbw35 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR getrlimit (),
Packit 7cfc04
.BR setrlimit (),
Packit 7cfc04
.BR prlimit ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.sp 1
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR getrlimit (),
Packit 7cfc04
.BR setrlimit ():
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
Packit 7cfc04
.PP
Packit 7cfc04
.BR prlimit ():
Packit 7cfc04
Linux-specific.
Packit 7cfc04
.PP
Packit 7cfc04
.B RLIMIT_MEMLOCK
Packit 7cfc04
and
Packit 7cfc04
.B RLIMIT_NPROC
Packit 7cfc04
derive from BSD and are not specified in POSIX.1;
Packit 7cfc04
they are present on the BSDs and Linux, but on few other implementations.
Packit 7cfc04
.B RLIMIT_RSS
Packit 7cfc04
derives from BSD and is not specified in POSIX.1;
Packit 7cfc04
it is nevertheless present on most implementations.
Packit 7cfc04
.BR RLIMIT_MSGQUEUE ,
Packit 7cfc04
.BR RLIMIT_NICE ,
Packit 7cfc04
.BR RLIMIT_RTPRIO ,
Packit 7cfc04
.BR RLIMIT_RTTIME ,
Packit 7cfc04
and
Packit 7cfc04
.B RLIMIT_SIGPENDING
Packit 7cfc04
are Linux-specific.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
A child process created via
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
inherits its parent's resource limits.
Packit 7cfc04
Resource limits are preserved across
Packit 7cfc04
.BR execve (2).
Packit 7cfc04
.PP
Packit 7cfc04
Lowering the soft limit for a resource below the process's
Packit 7cfc04
current consumption of that resource will succeed
Packit 7cfc04
(but will prevent the process from further increasing
Packit 7cfc04
its consumption of the resource).
Packit 7cfc04
.PP
Packit 7cfc04
One can set the resource limits of the shell using the built-in
Packit 7cfc04
.IR ulimit
Packit 7cfc04
command
Packit 7cfc04
.RI ( limit
Packit 7cfc04
in
Packit 7cfc04
.BR csh (1)).
Packit 7cfc04
The shell's resource limits are inherited by the processes that
Packit 7cfc04
it creates to execute commands.
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 2.6.24, the resource limits of any process can be inspected via
Packit 7cfc04
.IR /proc/[pid]/limits ;
Packit 7cfc04
see
Packit 7cfc04
.BR proc (5).
Packit 7cfc04
.PP
Packit 7cfc04
Ancient systems provided a
Packit 7cfc04
.BR vlimit ()
Packit 7cfc04
function with a similar purpose to
Packit 7cfc04
.BR setrlimit ().
Packit 7cfc04
For backward compatibility, glibc also provides
Packit 7cfc04
.BR vlimit ().
Packit 7cfc04
All new applications should be written using
Packit 7cfc04
.BR setrlimit ().
Packit 7cfc04
.SS C library/kernel ABI differences
Packit 7cfc04
Since version 2.13, the glibc
Packit 7cfc04
.BR getrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
wrapper functions no longer invoke the corresponding system calls,
Packit 7cfc04
but instead employ
Packit 7cfc04
.BR prlimit (),
Packit 7cfc04
for the reasons described in BUGS.
Packit 7cfc04
.PP
Packit 7cfc04
The name of the glibc wrapper function is
Packit 7cfc04
.BR prlimit ();
Packit 7cfc04
the underlying system call is
Packit 7cfc04
.BR prlimit64 ().
Packit 7cfc04
.SH BUGS
Packit 7cfc04
In older Linux kernels, the
Packit 7cfc04
.B SIGXCPU
Packit 7cfc04
and
Packit 7cfc04
.B SIGKILL
Packit 7cfc04
signals delivered when a process encountered the soft and hard
Packit 7cfc04
.B RLIMIT_CPU
Packit 7cfc04
limits were delivered one (CPU) second later than they should have been.
Packit 7cfc04
This was fixed in kernel 2.6.8.
Packit 7cfc04
.PP
Packit 7cfc04
In 2.6.x kernels before 2.6.17, a
Packit 7cfc04
.B RLIMIT_CPU
Packit 7cfc04
limit of 0 is wrongly treated as "no limit" (like
Packit 7cfc04
.BR RLIM_INFINITY ).
Packit 7cfc04
Since Linux 2.6.17, setting a limit of 0 does have an effect,
Packit 7cfc04
but is actually treated as a limit of 1 second.
Packit 7cfc04
.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=114008066530167&w=2
Packit 7cfc04
.PP
Packit 7cfc04
A kernel bug means that
Packit 7cfc04
.\" See https://lwn.net/Articles/145008/
Packit 7cfc04
.B RLIMIT_RTPRIO
Packit 7cfc04
does not work in kernel 2.6.12; the problem is fixed in kernel 2.6.13.
Packit 7cfc04
.PP
Packit 7cfc04
In kernel 2.6.12, there was an off-by-one mismatch
Packit 7cfc04
between the priority ranges returned by
Packit 7cfc04
.BR getpriority (2)
Packit 7cfc04
and
Packit 7cfc04
.BR RLIMIT_NICE .
Packit 7cfc04
This had the effect that the actual ceiling for the nice value
Packit 7cfc04
was calculated as
Packit 7cfc04
.IR "19\ \-\ rlim_cur" .
Packit 7cfc04
This was fixed in kernel 2.6.13.
Packit 7cfc04
.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=112256338703880&w=2
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 2.6.12,
Packit 7cfc04
.\" The relevant patch, sent to LKML, seems to be
Packit 7cfc04
.\" http://thread.gmane.org/gmane.linux.kernel/273462
Packit 7cfc04
.\" From: Roland McGrath <roland <at> redhat.com>
Packit 7cfc04
.\" Subject: [PATCH 7/7] make RLIMIT_CPU/SIGXCPU per-process
Packit 7cfc04
.\" Date: 2005-01-23 23:27:46 GMT
Packit 7cfc04
if a process reaches its soft
Packit 7cfc04
.BR RLIMIT_CPU
Packit 7cfc04
limit and has a handler installed for
Packit 7cfc04
.BR SIGXCPU ,
Packit 7cfc04
then, in addition to invoking the signal handler,
Packit 7cfc04
the kernel increases the soft limit by one second.
Packit 7cfc04
This behavior repeats if the process continues to consume CPU time,
Packit 7cfc04
until the hard limit is reached,
Packit 7cfc04
at which point the process is killed.
Packit 7cfc04
Other implementations
Packit 7cfc04
.\" Tested Solaris 10, FreeBSD 9, OpenBSD 5.0
Packit 7cfc04
do not change the
Packit 7cfc04
.BR RLIMIT_CPU
Packit 7cfc04
soft limit in this manner,
Packit 7cfc04
and the Linux behavior is probably not standards conformant;
Packit 7cfc04
portable applications should avoid relying on this Linux-specific behavior.
Packit 7cfc04
.\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=50951
Packit 7cfc04
The Linux-specific
Packit 7cfc04
.BR RLIMIT_RTTIME
Packit 7cfc04
limit exhibits the same behavior when the soft limit is encountered.
Packit 7cfc04
.PP
Packit 7cfc04
Kernels before 2.4.22 did not diagnose the error
Packit 7cfc04
.B EINVAL
Packit 7cfc04
for
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
when
Packit 7cfc04
.I rlim\->rlim_cur
Packit 7cfc04
was greater than
Packit 7cfc04
.IR rlim\->rlim_max .
Packit 7cfc04
.\"
Packit 7cfc04
.SS Representation of """large""" resource limit values on 32-bit platforms
Packit 7cfc04
The glibc
Packit 7cfc04
.BR getrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
wrapper functions use a 64-bit
Packit 7cfc04
.IR rlim_t
Packit 7cfc04
data type, even on 32-bit platforms.
Packit 7cfc04
However, the
Packit 7cfc04
.I rlim_t
Packit 7cfc04
data type used in the
Packit 7cfc04
.BR getrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
system calls is a (32-bit)
Packit 7cfc04
.IR "unsigned long" .
Packit 7cfc04
Furthermore, in Linux versions before 2.6.36,
Packit 7cfc04
the kernel represents resource limits on 32-bit platforms as
Packit 7cfc04
.IR "unsigned long" .
Packit 7cfc04
However, a 32-bit data type is not wide enough.
Packit 7cfc04
.\" https://bugzilla.kernel.org/show_bug.cgi?id=5042
Packit 7cfc04
.\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12201
Packit 7cfc04
The most pertinent limit here is
Packit 7cfc04
.BR RLIMIT_FSIZE ,
Packit 7cfc04
which specifies the maximum size to which a file can grow:
Packit 7cfc04
to be useful, this limit must be represented using a type
Packit 7cfc04
that is as wide as the type used to
Packit 7cfc04
represent file offsets\(emthat is, as wide as a 64-bit
Packit 7cfc04
.BR off_t
Packit 7cfc04
(assuming a program compiled with
Packit 7cfc04
.IR _FILE_OFFSET_BITS=64 ).
Packit 7cfc04
.PP
Packit 7cfc04
To work around this kernel limitation,
Packit 7cfc04
if a program tried to set a resource limit to a value larger than
Packit 7cfc04
can be represented in a 32-bit
Packit 7cfc04
.IR "unsigned long" ,
Packit 7cfc04
then the glibc
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
wrapper function silently converted the limit value to
Packit 7cfc04
.BR RLIM_INFINITY .
Packit 7cfc04
In other words, the requested resource limit setting was silently ignored.
Packit 7cfc04
.PP
Packit 7cfc04
This problem was addressed in Linux 2.6.36 with two principal changes:
Packit 7cfc04
.IP * 3
Packit 7cfc04
the addition of a new kernel representation of resource limits that
Packit 7cfc04
uses 64 bits, even on 32-bit platforms;
Packit 7cfc04
.IP *
Packit 7cfc04
the addition of the
Packit 7cfc04
.BR prlimit ()
Packit 7cfc04
system call, which employs 64-bit values for its resource limit arguments.
Packit 7cfc04
.PP
Packit 7cfc04
Since version 2.13,
Packit 7cfc04
.\" https://www.sourceware.org/bugzilla/show_bug.cgi?id=12201
Packit 7cfc04
glibc works around the limitations of the
Packit 7cfc04
.BR getrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
system calls by implementing
Packit 7cfc04
.BR setrlimit ()
Packit 7cfc04
and
Packit 7cfc04
.BR getrlimit ()
Packit 7cfc04
as wrapper functions that call
Packit 7cfc04
.BR prlimit ().
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The program below demonstrates the use of
Packit 7cfc04
.BR prlimit ().
Packit 7cfc04
.PP
Packit 7cfc04
.EX
Packit 7cfc04
#define _GNU_SOURCE
Packit 7cfc04
#define _FILE_OFFSET_BITS 64
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <time.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
#include <sys/resource.h>
Packit 7cfc04
Packit 7cfc04
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
Packit 7cfc04
                        } while (0)
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    struct rlimit old, new;
Packit 7cfc04
    struct rlimit *newp;
Packit 7cfc04
    pid_t pid;
Packit 7cfc04
Packit 7cfc04
    if (!(argc == 2 || argc == 4)) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s <pid> [<new\-soft\-limit> "
Packit 7cfc04
                "<new\-hard\-limit>]\\n", argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    pid = atoi(argv[1]);        /* PID of target process */
Packit 7cfc04
Packit 7cfc04
    newp = NULL;
Packit 7cfc04
    if (argc == 4) {
Packit 7cfc04
        new.rlim_cur = atoi(argv[2]);
Packit 7cfc04
        new.rlim_max = atoi(argv[3]);
Packit 7cfc04
        newp = &new;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Set CPU time limit of target process; retrieve and display
Packit 7cfc04
       previous limit */
Packit 7cfc04
Packit 7cfc04
    if (prlimit(pid, RLIMIT_CPU, newp, &old) == \-1)
Packit 7cfc04
        errExit("prlimit\-1");
Packit 7cfc04
    printf("Previous limits: soft=%lld; hard=%lld\\n",
Packit 7cfc04
            (long long) old.rlim_cur, (long long) old.rlim_max);
Packit 7cfc04
Packit 7cfc04
    /* Retrieve and display new CPU time limit */
Packit 7cfc04
Packit 7cfc04
    if (prlimit(pid, RLIMIT_CPU, NULL, &old) == \-1)
Packit 7cfc04
        errExit("prlimit\-2");
Packit 7cfc04
    printf("New limits: soft=%lld; hard=%lld\\n",
Packit 7cfc04
            (long long) old.rlim_cur, (long long) old.rlim_max);
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR prlimit (1),
Packit 7cfc04
.BR dup (2),
Packit 7cfc04
.BR fcntl (2),
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
.BR getrusage (2),
Packit 7cfc04
.BR mlock (2),
Packit 7cfc04
.BR mmap (2),
Packit 7cfc04
.BR open (2),
Packit 7cfc04
.BR quotactl (2),
Packit 7cfc04
.BR sbrk (2),
Packit 7cfc04
.BR shmctl (2),
Packit 7cfc04
.BR malloc (3),
Packit 7cfc04
.BR sigqueue (3),
Packit 7cfc04
.BR ulimit (3),
Packit 7cfc04
.BR core (5),
Packit 7cfc04
.BR capabilities (7),
Packit 7cfc04
.BR cgroups (7),
Packit 7cfc04
.BR credentials (7),
Packit 7cfc04
.BR signal (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.