OSDN Git Service

(split) LDP man-pages の original/ を v3.31 に更新。
[linuxjm/LDP_man-pages.git] / original / man2 / getrlimit.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
4 .\" and Copyright (c) 2002, 2004, 2005, 2008. 2010 Michael Kerrisk
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
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
35 .\"          Håvard Lygre <hklygre@online.no>
36 .\" Modified 2001-04-17 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" Modified 2002-06-13 by Michael Kerrisk <mtk.manpages@gmail.com>
38 .\"     Added note on nonstandard behavior when SIGCHLD is ignored.
39 .\" Modified 2002-07-09 by Michael Kerrisk <mtk.manpages@gmail.com>
40 .\"     Enhanced descriptions of 'resource' values
41 .\" Modified 2003-11-28 by aeb, added RLIMIT_CORE
42 .\" Modified 2004-03-26 by aeb, added RLIMIT_AS
43 .\" Modified 2004-06-16 by Michael Kerrisk <mtk.manpages@gmail.com>
44 .\"     Added notes on CAP_SYS_RESOURCE
45 .\"
46 .\" 2004-11-16 -- mtk: the getrlimit.2 page, which formally included
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
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
58 .\" 2004-11-03, mtk, Added RLIMIT_SIGPENDING
59 .\" 2005-07-13, mtk, documented RLIMIT_MSGQUEUE limit.
60 .\" 2005-07-28, mtk, Added descriptions of RLIMIT_NICE and RLIMIT_RTPRIO
61 .\" 2008-05-07, mtk / Peter Zijlstra, Added description of RLIMIT_RTTIME
62 .\" 2010-11-06, mtk: Added documentation of prlimit()
63 .\"
64 .TH GETRLIMIT 2 2010-11-06 "Linux" "Linux Programmer's Manual"
65 .SH NAME
66 getrlimit, setrlimit, prlimit \- get/set resource limits
67 .SH SYNOPSIS
68 .B #include <sys/time.h>
69 .br
70 .B #include <sys/resource.h>
71 .sp
72 .BI "int getrlimit(int " resource ", struct rlimit *" rlim );
73 .br
74 .BI "int setrlimit(int " resource ", const struct rlimit *" rlim );
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 ():
87 _GNU_SOURCE
88 .SH DESCRIPTION
89 The
90 the
91 .BR getrlimit ()
92 and
93 .BR setrlimit ()
94 system calls get and set resource limits respectively.
95 Each resource has an associated soft and hard limit, as defined by the
96 .I rlimit
97 structure:
98 .PP
99 .in +4n
100 .nf
101 struct rlimit {
102     rlim_t rlim_cur;  /* Soft limit */
103     rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
104 };
105
106 .fi
107 .in
108 The soft limit is the value that the kernel enforces for the
109 corresponding resource.
110 The hard limit acts as a ceiling for the soft limit:
111 an unprivileged process may only set its soft limit to a value in the
112 range from 0 up to the hard limit, and (irreversibly) lower its hard limit.
113 A privileged process (under Linux: one with the
114 .B CAP_SYS_RESOURCE
115 capability) may make arbitrary changes to either limit value.
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
124 The
125 .I resource
126 argument must be one of:
127 .TP
128 .B RLIMIT_AS
129 The maximum size of the process's virtual memory (address space) in bytes.
130 .\" since 2.0.27 / 2.1.12
131 This limit affects calls to
132 .BR brk (2),
133 .BR mmap (2)
134 and
135 .BR mremap (2),
136 which fail with the error
137 .B ENOMEM
138 upon exceeding this limit.
139 Also automatic stack expansion will fail
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)).
145 Since the value is a \fIlong\fP, on machines with a 32-bit \fIlong\fP
146 either this limit is at most 2 GiB, or this resource is unlimited.
147 .TP
148 .B RLIMIT_CORE
149 Maximum size of
150 .I core
151 file.
152 When 0 no core dump files are created.
153 When nonzero, larger dumps are truncated to this size.
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 .
168 (This latter point describes Linux 2.2 through 2.6 behavior.
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
173 .BR SIGXCPU .)
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
179 .BR brk (2)
180 and
181 .BR sbrk (2),
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.
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.,
193 .BR write (2),
194 .BR truncate (2))
195 fails with the error
196 .BR EFBIG .
197 .TP
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
200 A limit on the combined number of
201 .BR flock (2)
202 locks and
203 .BR fcntl (2)
204 leases that this process may establish.
205 .TP
206 .B RLIMIT_MEMLOCK
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.
211 This limit affects
212 .BR mlock (2)
213 and
214 .BR mlockall (2)
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
222 operation, where it sets a maximum on the total bytes in
223 shared memory segments (see
224 .BR shmget (2))
225 that may be locked by the real user ID of the calling process.
226 The
227 .BR shmctl (2)
228 .B SHM_LOCK
229 locks are accounted for separately from the per-process memory
230 locks established by
231 .BR mlock (2),
232 .BR mlockall (2),
233 and
234 .BR mmap (2)
235 .BR MAP_LOCKED ;
236 a process can lock bytes up to this limit in each of these
237 two categories.
238 In Linux kernels before 2.6.9, this limit controlled the amount of
239 memory that could be locked by a privileged process.
240 Since Linux 2.6.9, no limits are placed on the amount of memory
241 that a privileged process may lock, and this limit instead governs
242 the amount of memory that an unprivileged process may lock.
243 .TP
244 .BR RLIMIT_MSGQUEUE " (Since Linux 2.6.8)"
245 Specifies the limit on the number of bytes that can be allocated
246 for POSIX message queues for the real user ID of the calling process.
247 This limit is enforced for
248 .BR mq_open (3).
249 Each message queue that the user creates counts (until it is removed)
250 against this limit according to the formula:
251 .nf
252
253     bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
254             attr.mq_maxmsg * attr.mq_msgsize
255
256 .fi
257 where
258 .I attr
259 is the
260 .I mq_attr
261 structure specified as the fourth argument to
262 .BR mq_open (3).
263
264 The first addend in the formula, which includes
265 .I "sizeof(struct msg_msg *)"
266 (4 bytes on Linux/i386), ensures that the user cannot
267 create an unlimited number of zero-length messages (such messages
268 nevertheless each consume some system memory for bookkeeping overhead).
269 .TP
270 .BR RLIMIT_NICE " (since Linux 2.6.12, but see BUGS below)"
271 Specifies a ceiling to which the process's nice value can be raised using
272 .BR setpriority (2)
273 or
274 .BR nice (2).
275 The actual ceiling for the nice value is calculated as
276 .IR "20\ \-\ rlim_cur" .
277 (This strangeness occurs because negative numbers cannot be specified
278 as resource limit values, since they typically have special meanings.
279 For example,
280 .B RLIM_INFINITY
281 typically is the same as \-1.)
282 .TP
283 .B RLIMIT_NOFILE
284 Specifies a value one greater than the maximum file descriptor number
285 that can be opened by this process.
286 Attempts
287 .RB ( open (2),
288 .BR pipe (2),
289 .BR dup (2),
290 etc.)
291 to exceed this limit yield the error
292 .BR EMFILE .
293 (Historically, this limit was named
294 .B RLIMIT_OFILE
295 on BSD.)
296 .TP
297 .B RLIMIT_NPROC
298 The maximum number of processes (or, more precisely on Linux, threads)
299 that can be created for the real user ID of the calling process.
300 Upon encountering this limit,
301 .BR fork (2)
302 fails with the error
303 .BR EAGAIN .
304 .TP
305 .B RLIMIT_RSS
306 Specifies the limit (in pages) of the process's resident set
307 (the number of virtual pages resident in RAM).
308 This limit only has effect in Linux 2.4.x, x < 30, and there only
309 affects calls to
310 .BR madvise (2)
311 specifying
312 .BR MADV_WILLNEED .
313 .\" As at kernel 2.6.12, this limit still does nothing in 2.6 though
314 .\" talk of making it do something has surfaced from time to time in LKML
315 .\"       -- MTK, Jul 05
316 .TP
317 .BR RLIMIT_RTPRIO " (Since Linux 2.6.12, but see BUGS)"
318 Specifies a ceiling on the real-time priority that may be set for
319 this process using
320 .BR sched_setscheduler (2)
321 and
322 .BR sched_setparam (2).
323 .TP
324 .BR RLIMIT_RTTIME " (Since Linux 2.6.25)"
325 Specifies a limit (in microseconds)
326 on the amount of CPU time that a process scheduled
327 under a real-time scheduling policy may consume without making a blocking
328 system call.
329 For the purpose of this limit,
330 each time a process makes a blocking system call,
331 the count of its consumed CPU time is reset to zero.
332 The CPU time count is not reset if the process continues trying to
333 use the CPU but is preempted, its time slice expires, or it calls
334 .BR sched_yield (2).
335
336 Upon reaching the soft limit, the process is sent a
337 .B SIGXCPU
338 signal.
339 If the process catches or ignores this signal and
340 continues consuming CPU time, then
341 .B SIGXCPU
342 will be generated once each second until the hard limit is reached,
343 at which point the process is sent a
344 .B SIGKILL
345 signal.
346
347 The intended use of this limit is to stop a runaway
348 real-time process from locking up the system.
349 .TP
350 .BR RLIMIT_SIGPENDING " (Since Linux 2.6.8)"
351 Specifies the limit on the number of signals
352 that may be queued for the real user ID of the calling process.
353 Both standard and real-time signals are counted for the purpose of
354 checking this limit.
355 However, the limit is only enforced for
356 .BR sigqueue (2);
357 it is always possible to use
358 .BR kill (2)
359 to queue one instance of any of the signals that are not already
360 queued to the process.
361 .\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit
362 .\" that was present in kernels <= 2.6.7.  MTK Dec 04
363 .TP
364 .B RLIMIT_STACK
365 The maximum size of the process stack, in bytes.
366 Upon reaching this limit, a
367 .B SIGSEGV
368 signal is generated.
369 To handle this signal, a process must employ an alternate signal stack
370 .RB ( sigaltstack (2)).
371
372 Since Linux 2.6.23,
373 this limit also determines the amount of space used for the process's
374 command-line arguments and environment variables; for details, see
375 .BR execve (2).
376 .SS prlimit()
377 .\" commit c022a0acad534fd5f5d5f17280f6d4d135e74e81
378 .\" Author: Jiri Slaby <jslaby@suse.cz>
379 .\" Date:   Tue May 4 18:03:50 2010 +0200
380 The Linux-specific
381 .BR prlimit ()
382 system call combines and extends the functionality of
383 .BR setrlimit ()
384 and
385 .BR getrlimit ().
386 It can be used to both set and get the resource limits of an arbitrary process.
387
388 The
389 .I resource
390 argument has the same meaning as for
391 .BR setrlimit ()
392 and
393 .BR getrlimit ().
394
395 If the
396 .IR new_limit
397 argument is a not NULL, then the
398 .I rlimit
399 structure to which it points is used to set new values for
400 the soft and hard limits for
401 .IR resource .
402 If the
403 .IR old_limit
404 argument is a not NULL, then a successful call to
405 .BR prlimit ()
406 places the previous soft and hard limits for
407 .I resource
408 in the
409 .I rlimit
410 structure pointed to by
411 .IR old_limit .
412
413 The
414 .I pid
415 argument specifies the ID of the process on which the call is to operate.
416 If
417 .I pid
418 is 0, then the call applies to the calling process.
419 To set or get the resources of a process other than itself,
420 the caller must have the
421 .B CAP_SYS_RESOURCE
422 capability, or the
423 real, effective, and saved set user IDs of the target process
424 must match the real user ID of the caller
425 .I and
426 the real, effective, and saved set group IDs of the target process
427 must match the real group ID of the caller.
428 .\" FIXME this permission check is strange
429 .\" Asked about this on LKML, 7 Nov 2010
430 .\"     "Inconsistent credential checking in prlimit() syscall"
431 .SH RETURN VALUE
432 On success, these system calls return 0.
433 On error, \-1 is returned, and
434 .I errno
435 is set appropriately.
436 .SH ERRORS
437 .TP
438 .B EFAULT
439 A pointer argument points to a location
440 outside the accessible address space.
441 .TP
442 .B EINVAL
443 The value specified in
444 .I resource
445 is not valid;
446 The value specified in
447 or, for
448 .BR setrlimit ()
449 or
450 .BR prlimit ():
451 .I rlim\->rlim_cur
452 was greater than
453 .IR rlim\->rlim_max .
454 .TP
455 .B EPERM
456 An unprivileged process tried to raise the hard limit; the
457 .B CAP_SYS_RESOURCE
458 capability is required to do this.
459 Or, the the caller tried to increase the hard
460 .B RLIMIT_NOFILE
461 limit above the current kernel maximum
462 .RB ( NR_OPEN ).
463 Or, the calling process did not have permission to set limits
464 for the process specified by
465 .IR pid .
466 .TP
467 .B ESRCH
468 Could not find a process with the ID specified in
469 .IR pid .
470 .SH VERSIONS
471 .BR The
472 .BR prlimit ()
473 system call is available since Linux 2.6.36.
474 Library support is available since glibc 2.13.
475 .SH CONFORMING TO
476 .BR getrlimit (),
477 .BR setrlimit ():
478 SVr4, 4.3BSD, POSIX.1-2001.
479 .br
480 .BR prlimit ():
481 Linux-specific.
482
483 .B RLIMIT_MEMLOCK
484 and
485 .B RLIMIT_NPROC
486 derive from BSD and are not specified in POSIX.1-2001;
487 they are present on the BSDs and Linux, but on few other implementations.
488 .B RLIMIT_RSS
489 derives from BSD and is not specified in POSIX.1-2001;
490 it is nevertheless present on most implementations.
491 .BR RLIMIT_MSGQUEUE ,
492 .BR RLIMIT_NICE ,
493 .BR RLIMIT_RTPRIO ,
494 .BR RLIMIT_RTTIME ,
495 and
496 .B RLIMIT_SIGPENDING
497 are Linux-specific.
498 .SH NOTES
499 A child process created via
500 .BR fork (2)
501 inherits its parent's resource limits.
502 Resource limits are preserved across
503 .BR execve (2).
504
505 One can set the resource limits of the shell using the built-in
506 .IR ulimit
507 command
508 .RI ( limit
509 in
510 .BR csh (1)).
511 The shell's resource limits are inherited by the processes that
512 it creates to execute commands.
513
514 Ancient systems provided a
515 .BR vlimit ()
516 function with a similar purpose to
517 .BR setrlimit ().
518 For backward compatibility, glibc also provides
519 .BR vlimit ().
520 All new applications should be written using
521 .BR setrlimit ().
522 .SH BUGS
523 .\" FIXME prlimit() does not suffer
524 .\" https://bugzilla.kernel.org/show_bug.cgi?id=5042
525 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12201
526 In older Linux kernels, the
527 .B SIGXCPU
528 and
529 .B SIGKILL
530 signals delivered when a process encountered the soft and hard
531 .B RLIMIT_CPU
532 limits were delivered one (CPU) second later than they should have been.
533 This was fixed in kernel 2.6.8.
534
535 In 2.6.x kernels before 2.6.17, a
536 .B RLIMIT_CPU
537 limit of 0 is wrongly treated as "no limit" (like
538 .BR RLIM_INFINITY ).
539 Since Linux 2.6.17, setting a limit of 0 does have an effect,
540 but is actually treated as a limit of 1 second.
541 .\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=114008066530167&w=2
542
543 A kernel bug means that
544 .B RLIMIT_RTPRIO
545 does not work in kernel 2.6.12; the problem is fixed in kernel 2.6.13.
546
547 In kernel 2.6.12, there was an off-by-one mismatch
548 between the priority ranges returned by
549 .BR getpriority (2)
550 and
551 .BR RLIMIT_NICE .
552 This had the effect that actual ceiling for the nice value
553 was calculated as
554 .IR "19\ \-\ rlim_cur" .
555 This was fixed in kernel 2.6.13.
556 .\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=112256338703880&w=2
557
558 Kernels before 2.4.22 did not diagnose the error
559 .B EINVAL
560 for
561 .BR setrlimit ()
562 when
563 .I rlim\->rlim_cur
564 was greater than
565 .IR rlim\->rlim_max .
566 .SH "SEE ALSO"
567 .BR dup (2),
568 .BR fcntl (2),
569 .BR fork (2),
570 .BR getrusage (2),
571 .BR mlock (2),
572 .BR mmap (2),
573 .BR open (2),
574 .BR quotactl (2),
575 .BR sbrk (2),
576 .BR shmctl (2),
577 .BR sigqueue (2),
578 .BR malloc (3),
579 .BR ulimit (3),
580 .BR core (5),
581 .BR capabilities (7),
582 .BR signal (7)