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