Home

setrlimit

Setrlimit is a POSIX-compatible API that sets resource limits for the calling process. It updates the soft limit (rlim_cur) and hard limit (rlim_max) for a specified resource, using a struct rlimit. The rlimit structure contains rlim_cur and rlim_max, both of type rlim_t. The resource parameter selects which limit to adjust, with common examples including RLIMIT_CPU (CPU time), RLIMIT_FSIZE (file size), RLIMIT_DATA (data segment size), RLIMIT_STACK (stack size), RLIMIT_CORE (core file size), RLIMIT_AS (address space), RLIMIT_NOFILE (number of open files), RLIMIT_NPROC (number of processes), and RLIMIT_MEMLOCK or RLIMIT_RSS on some systems.

Operation and effects: The soft limit is the enforcement threshold seen by the kernel at runtime, while

Behavior on limit breach: If a process attempts to exceed a limit, the kernel may take an

Usage notes: setrlimit is declared in header <sys/resource.h> and is commonly used for resource protection, sandboxing,

the
hard
limit
is
the
ceiling
that
the
soft
limit
may
be
raised
to.
A
process
can
increase
its
soft
limit
up
to
the
hard
limit
using
setrlimit,
but
increasing
the
hard
limit
generally
requires
elevated
privileges
(such
as
appropriate
capabilities).
A
process
can
lower
either
limit
without
special
privileges.
Limits
are
inherited
by
child
processes,
which
may
independently
adjust
them
within
allowed
bounds.
action
such
as
sending
a
signal
or
terminating
the
process,
depending
on
the
resource
and
system
policy
(for
example,
CPU
time
exceeding
RLIMIT_CPU
may
trigger
a
SIGXCPU).
or
daemon
startup
to
bound
resource
consumption.
The
function
returns
0
on
success
and
-1
on
error,
with
errno
describing
the
failure.
Related
functionality
includes
getrlimit,
which
retrieves
current
limits.