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