Debian Codegraph @

upstream/debian-trixie/man2/getrlimit.2

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