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