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