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