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