ioctl_userfaultfd.2, madvise.2, memfd_create.2, migrate_pages.2, mmap.2, shmget.2...
[man-pages.git] / man2 / getrlimit.2
blobf488af488027b9ba99709cfe14e83f939e0b5233
1 .\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
2 .\" and Copyright (c) 2002, 2004, 2005, 2008, 2010 Michael Kerrisk
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
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.
13 .\"
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.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
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 2018-04-30 "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 .PP
72 .BI "int getrlimit(int " resource ", struct rlimit *" rlim );
73 .br
74 .BI "int setrlimit(int " resource ", const struct rlimit *" rlim );
75 .PP
76 .BI "int prlimit(pid_t "  pid ", int " resource \
77 ", const struct rlimit *" new_limit ,
78 .br
79 .BI "            struct rlimit *" old_limit );
80 .PP
81 .in -4n
82 Feature Test Macro Requirements for glibc (see
83 .BR feature_test_macros (7)):
84 .in
85 .PP
86 .BR prlimit ():
87 _GNU_SOURCE
88 .SH DESCRIPTION
89 The
90 .BR getrlimit ()
91 and
92 .BR setrlimit ()
93 system calls get and set resource limits.
94 Each resource has an associated soft and hard limit, as defined by the
95 .I rlimit
96 structure:
97 .PP
98 .in +4n
99 .EX
100 struct rlimit {
101     rlim_t rlim_cur;  /* Soft limit */
102     rlim_t rlim_max;  /* Hard limit (ceiling for rlim_cur) */
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:
110 an unprivileged process may set only its soft limit to a value in the
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 in the initial user namespace)
115 may make arbitrary changes to either limit value.
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 ()).
125 .I resource
126 argument must be one of:
128 .B RLIMIT_AS
129 This is the maximum size of the process's virtual memory
130 (address space).
131 The limit is specified in bytes, and is rounded down to the system page size.
132 .\" since 2.0.27 / 2.1.12
133 This limit affects calls to
134 .BR brk (2),
135 .BR mmap (2),
137 .BR mremap (2),
138 which fail with the error
139 .B ENOMEM
140 upon exceeding this limit.
141 In addition, automatic stack expansion fails
142 (and generates a
143 .B SIGSEGV
144 that kills the process if no alternate stack
145 has been made available via
146 .BR sigaltstack (2)).
147 Since the value is a \fIlong\fP, on machines with a 32-bit \fIlong\fP
148 either this limit is at most 2\ GiB, or this resource is unlimited.
150 .B RLIMIT_CORE
151 This is the maximum size of a
152 .I core
153 file (see
154 .BR core (5))
155 in bytes that the process may dump.
156 When 0 no core dump files are created.
157 When nonzero, larger dumps are truncated to this size.
159 .B RLIMIT_CPU
160 This is a limit, in seconds,
161 on the amount of CPU time that the process can consume.
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 .
173 (This latter point describes Linux behavior.
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
178 .BR SIGXCPU .)
180 .B RLIMIT_DATA
181 This is the maximum size
182 of the process's data segment (initialized data,
183 uninitialized data, and heap).
184 The limit is specified in bytes, and is rounded down to the system page size.
185 This limit affects calls to
186 .BR brk (2),
187 .BR sbrk (2),
188 and (since Linux 4.7)
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).
194 which fail with the error
195 .B ENOMEM
196 upon encountering the soft limit of this resource.
198 .B RLIMIT_FSIZE
199 This is the maximum size in bytes of files that the process may create.
200 Attempts to extend a file beyond this limit result in delivery of a
201 .B SIGXFSZ
202 signal.
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.,
205 .BR write (2),
206 .BR truncate (2))
207 fails with the error
208 .BR EFBIG .
210 .BR RLIMIT_LOCKS " (early Linux 2.4 only)"
211 .\" to be precise: Linux 2.4.0-test9; no longer in 2.4.25 / 2.5.65
212 This is a limit on the combined number of
213 .BR flock (2)
214 locks and
215 .BR fcntl (2)
216 leases that this process may establish.
218 .B RLIMIT_MEMLOCK
219 This is the maximum number of bytes of memory that may be locked
220 into RAM.
221 This limit is in effect rounded down to the nearest multiple
222 of the system page size.
223 This limit affects
224 .BR mlock (2),
225 .BR mlockall (2),
226 and the
227 .BR mmap (2)
228 .B MAP_LOCKED
229 operation.
230 Since Linux 2.6.9, it also affects the
231 .BR shmctl (2)
232 .B SHM_LOCK
233 operation, where it sets a maximum on the total bytes in
234 shared memory segments (see
235 .BR shmget (2))
236 that may be locked by the real user ID of the calling process.
238 .BR shmctl (2)
239 .B SHM_LOCK
240 locks are accounted for separately from the per-process memory
241 locks established by
242 .BR mlock (2),
243 .BR mlockall (2),
245 .BR mmap (2)
246 .BR MAP_LOCKED ;
247 a process can lock bytes up to this limit in each of these
248 two categories.
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.
256 .BR RLIMIT_MSGQUEUE " (since Linux 2.6.8)"
257 This is a limit on the number of bytes that can be allocated
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).
261 Each message queue that the user creates counts (until it is removed)
262 against this limit according to the formula:
264     Since Linux 3.5:
267         bytes = attr.mq_maxmsg * sizeof(struct msg_msg) +
268                 min(attr.mq_maxmsg, MQ_PRIO_MAX) *
269                       sizeof(struct posix_msg_tree_node)+
270                                 /* For overhead */
271                 attr.mq_maxmsg * attr.mq_msgsize;
272                                 /* For message data */
275     Linux 3.4 and earlier:
278         bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
279                                 /* For overhead */
280                 attr.mq_maxmsg * attr.mq_msgsize;
281                                 /* For message data */
284 where
285 .I attr
286 is the
287 .I mq_attr
288 structure specified as the fourth argument to
289 .BR mq_open (3),
290 and the
291 .I msg_msg
293 .I posix_msg_tree_node
294 structures are kernel-internal structures.
296 The "overhead" addend in the formula accounts for overhead
297 bytes required by the implementation
298 and ensures that the user cannot
299 create an unlimited number of zero-length messages (such messages
300 nevertheless each consume some system memory for bookkeeping overhead).
302 .BR RLIMIT_NICE " (since Linux 2.6.12, but see BUGS below)"
303 This specifies a ceiling to which the process's nice value can be raised using
304 .BR setpriority (2)
306 .BR nice (2).
307 The actual ceiling for the nice value is calculated as
308 .IR "20\ \-\ rlim_cur" .
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).
312 This unusual choice of range was necessary
313 because negative numbers cannot be specified
314 as resource limit values, since they typically have special meanings.
315 For example,
316 .B RLIM_INFINITY
317 typically is the same as \-1.
318 For more detail on the nice value, see
319 .BR sched (7).
321 .B RLIMIT_NOFILE
322 This specifies a value one greater than the maximum file descriptor number
323 that can be opened by this process.
324 Attempts
325 .RB ( open (2),
326 .BR pipe (2),
327 .BR dup (2),
328 etc.)
329 to exceed this limit yield the error
330 .BR EMFILE .
331 (Historically, this limit was named
332 .B RLIMIT_OFILE
333 on BSD.)
335 Since Linux 4.5,
336 this limit also defines the maximum number of file descriptors that
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).
347 .B RLIMIT_NPROC
348 This is a limit on the number of extant process
349 (or, more precisely on Linux, threads)
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,
353 .BR fork (2)
354 fails with the error
355 .BR EAGAIN .
358 .B RLIMIT_NPROC
359 limit is not enforced for processes that have either the
360 .B CAP_SYS_ADMIN
361 or the
362 .B CAP_SYS_RESOURCE
363 capability.
365 .B RLIMIT_RSS
366 This is a limit (in bytes) on the process's resident set
367 (the number of virtual pages resident in RAM).
368 This limit has effect only in Linux 2.4.x, x < 30, and there
369 affects only calls to
370 .BR madvise (2)
371 specifying
372 .BR MADV_WILLNEED .
373 .\" As at kernel 2.6.12, this limit still does nothing in 2.6 though
374 .\" talk of making it do something has surfaced from time to time in LKML
375 .\"       -- MTK, Jul 05
377 .BR RLIMIT_RTPRIO " (since Linux 2.6.12, but see BUGS)"
378 This specifies a ceiling on the real-time priority that may be set for
379 this process using
380 .BR sched_setscheduler (2)
382 .BR sched_setparam (2).
384 For further details on real-time scheduling policies, see
385 .BR sched (7)
387 .BR RLIMIT_RTTIME " (since Linux 2.6.25)"
388 This is a limit (in microseconds)
389 on the amount of CPU time that a process scheduled
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).
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.
410 The intended use of this limit is to stop a runaway
411 real-time process from locking up the system.
413 For further details on real-time scheduling policies, see
414 .BR sched (7)
416 .BR RLIMIT_SIGPENDING " (since Linux 2.6.8)"
417 This is a limit on the number of signals
418 that may be queued for the real user ID of the calling process.
419 Both standard and real-time signals are counted for the purpose of
420 checking this limit.
421 However, the limit is enforced only for
422 .BR sigqueue (3);
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.
427 .\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit
428 .\" that was present in kernels <= 2.6.7.  MTK Dec 04
430 .B RLIMIT_STACK
431 This is the maximum size of the process stack, in bytes.
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)).
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).
442 .SS prlimit()
443 .\" commit c022a0acad534fd5f5d5f17280f6d4d135e74e81
444 .\" Author: Jiri Slaby <jslaby@suse.cz>
445 .\" Date:   Tue May 4 18:03:50 2010 +0200
447 .\"     rlimits: implement prlimit64 syscall
449 .\" commit 6a1d5e2c85d06da35cdfd93f1a27675bfdc3ad8c
450 .\" Author: Jiri Slaby <jslaby@suse.cz>
451 .\" Date:   Wed Mar 24 17:06:58 2010 +0100
453 .\"     rlimits: add rlimit64 structure
455 The Linux-specific
456 .BR prlimit ()
457 system call combines and extends the functionality of
458 .BR setrlimit ()
460 .BR getrlimit ().
461 It can be used to both set and get the resource limits of an arbitrary process.
464 .I resource
465 argument has the same meaning as for
466 .BR setrlimit ()
468 .BR getrlimit ().
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
483 in the
484 .I rlimit
485 structure pointed to by
486 .IR old_limit .
489 .I pid
490 argument specifies the ID of the process on which the call is to operate.
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
497 capability in the user namespace of the process
498 whose resource limits are being changed, or the
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.
504 .\" FIXME . this permission check is strange
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.
509 On error, \-1 is returned, and
510 .I errno
511 is set appropriately.
512 .SH ERRORS
514 .B EFAULT
515 A pointer argument points to a location
516 outside the accessible address space.
518 .B EINVAL
519 The value specified in
520 .I resource
521 is not valid;
522 or, for
523 .BR setrlimit ()
525 .BR prlimit ():
526 .I rlim\->rlim_cur
527 was greater than
528 .IR rlim\->rlim_max .
530 .B EPERM
531 An unprivileged process tried to raise the hard limit; the
532 .B CAP_SYS_RESOURCE
533 capability is required to do this.
535 .B EPERM
536 The caller tried to increase the hard
537 .B RLIMIT_NOFILE
538 limit above the maximum defined by
539 .IR /proc/sys/fs/nr_open
540 (see
541 .BR proc (5))
543 .B EPERM
544 .RB ( prlimit ())
545 The calling process did not have permission to set limits
546 for the process specified by
547 .IR pid .
549 .B ESRCH
550 Could not find a process with the ID specified in
551 .IR pid .
552 .SH VERSIONS
554 .BR prlimit ()
555 system call is available since Linux 2.6.36.
556 Library support is available since glibc 2.13.
557 .SH ATTRIBUTES
558 For an explanation of the terms used in this section, see
559 .BR attributes (7).
561 allbox;
562 lbw35 lb lb
563 l l l.
564 Interface       Attribute       Value
566 .BR getrlimit (),
567 .BR setrlimit (),
568 .BR prlimit ()
569 T}      Thread safety   MT-Safe
571 .sp 1
572 .SH CONFORMING TO
573 .BR getrlimit (),
574 .BR setrlimit ():
575 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
577 .BR prlimit ():
578 Linux-specific.
580 .B RLIMIT_MEMLOCK
582 .B RLIMIT_NPROC
583 derive from BSD and are not specified in POSIX.1;
584 they are present on the BSDs and Linux, but on few other implementations.
585 .B RLIMIT_RSS
586 derives from BSD and is not specified in POSIX.1;
587 it is nevertheless present on most implementations.
588 .BR RLIMIT_MSGQUEUE ,
589 .BR RLIMIT_NICE ,
590 .BR RLIMIT_RTPRIO ,
591 .BR RLIMIT_RTTIME ,
593 .B RLIMIT_SIGPENDING
594 are Linux-specific.
595 .SH NOTES
596 A child process created via
597 .BR fork (2)
598 inherits its parent's resource limits.
599 Resource limits are preserved across
600 .BR execve (2).
602 Resource limits are per-process attributes that are shared
603 by all of the threads in a process.
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).
610 One can set the resource limits of the shell using the built-in
611 .IR ulimit
612 command
613 .RI ( limit
615 .BR csh (1)).
616 The shell's resource limits are inherited by the processes that
617 it creates to execute commands.
619 Since Linux 2.6.24, the resource limits of any process can be inspected via
620 .IR /proc/[pid]/limits ;
622 .BR proc (5).
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 ().
632 .SS C library/kernel ABI differences
633 Since version 2.13, the glibc
634 .BR getrlimit ()
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.
642 The name of the glibc wrapper function is
643 .BR prlimit ();
644 the underlying system call is
645 .BR prlimit64 ().
646 .SH BUGS
647 In older Linux kernels, the
648 .B SIGXCPU
650 .B SIGKILL
651 signals delivered when a process encountered the soft and hard
652 .B RLIMIT_CPU
653 limits were delivered one (CPU) second later than they should have been.
654 This was fixed in kernel 2.6.8.
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
659 .BR RLIM_INFINITY ).
660 Since Linux 2.6.17, setting a limit of 0 does have an effect,
661 but is actually treated as a limit of 1 second.
662 .\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=114008066530167&w=2
664 A kernel bug means that
665 .\" See https://lwn.net/Articles/145008/
666 .B RLIMIT_RTPRIO
667 does not work in kernel 2.6.12; the problem is fixed in kernel 2.6.13.
669 In kernel 2.6.12, there was an off-by-one mismatch
670 between the priority ranges returned by
671 .BR getpriority (2)
673 .BR RLIMIT_NICE .
674 This had the effect that the actual ceiling for the nice value
675 was calculated as
676 .IR "19\ \-\ rlim_cur" .
677 This was fixed in kernel 2.6.13.
678 .\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=112256338703880&w=2
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.
702 .\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=50951
703 The Linux-specific
704 .BR RLIMIT_RTTIME
705 limit exhibits the same behavior when the soft limit is encountered.
707 Kernels before 2.4.22 did not diagnose the error
708 .B EINVAL
710 .BR setrlimit ()
711 when
712 .I rlim\->rlim_cur
713 was greater than
714 .IR rlim\->rlim_max .
715 .\" d3561f78fd379a7110e46c87964ba7aa4120235c
717 Linux doesn't return an error when an attempt to set
718 .B RLIMIT_CPU
719 has failed, for compatibility reasons.
721 .SS Representation of """large""" resource limit values on 32-bit platforms
722 The glibc
723 .BR getrlimit ()
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 ()
734 .BR setrlimit ()
735 system calls is a (32-bit)
736 .IR "unsigned long" .
737 Furthermore, in Linux versions before 2.6.36,
738 the kernel represents resource limits on 32-bit platforms as
739 .IR "unsigned long" .
740 However, a 32-bit data type is not wide enough.
741 .\" https://bugzilla.kernel.org/show_bug.cgi?id=5042
742 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12201
743 The most pertinent limit here is
744 .BR RLIMIT_FSIZE ,
745 which specifies the maximum size to which a file can grow:
746 to be useful, this limit must be represented using a type
747 that is as wide as the type used to
748 represent file offsets\(emthat is, as wide as a 64-bit
749 .BR off_t
750 (assuming a program compiled with
751 .IR _FILE_OFFSET_BITS=64 ).
753 To work around this kernel limitation,
754 if a program tried to set a resource limit to a value larger than
755 can be represented in a 32-bit
756 .IR "unsigned long" ,
757 then the glibc
758 .BR setrlimit ()
759 wrapper function silently converted the limit value to
760 .BR RLIM_INFINITY .
761 In other words, the requested resource limit setting was silently ignored.
763 This problem was addressed in Linux 2.6.36 with two principal changes:
764 .IP * 3
765 the addition of a new kernel representation of resource limits that
766 uses 64 bits, even on 32-bit platforms;
767 .IP *
768 the addition of the
769 .BR prlimit ()
770 system call, which employs 64-bit values for its resource limit arguments.
772 Since version 2.13,
773 .\" https://www.sourceware.org/bugzilla/show_bug.cgi?id=12201
774 glibc works around the limitations of the
775 .BR getrlimit ()
777 .BR setrlimit ()
778 system calls by implementing
779 .BR setrlimit ()
781 .BR getrlimit ()
782 as wrapper functions that call
783 .BR prlimit ().
784 .SH EXAMPLE
785 The program below demonstrates the use of
786 .BR prlimit ().
789 #define _GNU_SOURCE
790 #define _FILE_OFFSET_BITS 64
791 #include <stdio.h>
792 #include <time.h>
793 #include <stdlib.h>
794 #include <unistd.h>
795 #include <sys/resource.h>
797 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
798                         } while (0)
801 main(int argc, char *argv[])
803     struct rlimit old, new;
804     struct rlimit *newp;
805     pid_t pid;
807     if (!(argc == 2 || argc == 4)) {
808         fprintf(stderr, "Usage: %s <pid> [<new\-soft\-limit> "
809                 "<new\-hard\-limit>]\\n", argv[0]);
810         exit(EXIT_FAILURE);
811     }
813     pid = atoi(argv[1]);        /* PID of target process */
815     newp = NULL;
816     if (argc == 4) {
817         new.rlim_cur = atoi(argv[2]);
818         new.rlim_max = atoi(argv[3]);
819         newp = &new;
820     }
822     /* Set CPU time limit of target process; retrieve and display
823        previous limit */
825     if (prlimit(pid, RLIMIT_CPU, newp, &old) == \-1)
826         errExit("prlimit\-1");
827     printf("Previous limits: soft=%lld; hard=%lld\\n",
828             (long long) old.rlim_cur, (long long) old.rlim_max);
830     /* Retrieve and display new CPU time limit */
832     if (prlimit(pid, RLIMIT_CPU, NULL, &old) == \-1)
833         errExit("prlimit\-2");
834     printf("New limits: soft=%lld; hard=%lld\\n",
835             (long long) old.rlim_cur, (long long) old.rlim_max);
837     exit(EXIT_SUCCESS);
840 .SH SEE ALSO
841 .BR prlimit (1),
842 .BR dup (2),
843 .BR fcntl (2),
844 .BR fork (2),
845 .BR getrusage (2),
846 .BR mlock (2),
847 .BR mmap (2),
848 .BR open (2),
849 .BR quotactl (2),
850 .BR sbrk (2),
851 .BR shmctl (2),
852 .BR malloc (3),
853 .BR sigqueue (3),
854 .BR ulimit (3),
855 .BR core (5),
856 .BR capabilities (7),
857 .BR cgroups (7),
858 .BR credentials (7),
859 .BR signal (7)