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