]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/getrlimit.2
capabilities.7: CAP_SYS_ADMIN allows overriding RLIMIT_NPROC
[thirdparty/man-pages.git] / man2 / getrlimit.2
CommitLineData
fea681da 1.\" Copyright (c) 1992 Drew Eckhardt, March 28, 1992
658ea3ee 2.\" and Copyright (c) 2002, 2004, 2005, 2008, 2010 Michael Kerrisk
fea681da 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
fea681da
MK
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
e00c3a07 35.\" HÃ¥vard Lygre <hklygre@online.no>
c11b1abf
MK
36.\" Modified 2001-04-17 by Michael Kerrisk <mtk.manpages@gmail.com>
37.\" Modified 2002-06-13 by Michael Kerrisk <mtk.manpages@gmail.com>
c8f2dd47 38.\" Added note on nonstandard behavior when SIGCHLD is ignored.
c11b1abf 39.\" Modified 2002-07-09 by Michael Kerrisk <mtk.manpages@gmail.com>
1546fe19 40.\" Enhanced descriptions of 'resource' values
fea681da
MK
41.\" Modified 2003-11-28 by aeb, added RLIMIT_CORE
42.\" Modified 2004-03-26 by aeb, added RLIMIT_AS
c11b1abf 43.\" Modified 2004-06-16 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
44.\" Added notes on CAP_SYS_RESOURCE
45.\"
c13182ef 46.\" 2004-11-16 -- mtk: the getrlimit.2 page, which formally included
0fc46b5a
MK
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
b4c0e1cb
MK
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
1bf844f1 58.\" 2004-11-03, mtk, Added RLIMIT_SIGPENDING
9d8b1d5f 59.\" 2005-07-13, mtk, documented RLIMIT_MSGQUEUE limit.
1bf844f1 60.\" 2005-07-28, mtk, Added descriptions of RLIMIT_NICE and RLIMIT_RTPRIO
23ce0537 61.\" 2008-05-07, mtk / Peter Zijlstra, Added description of RLIMIT_RTTIME
1546fe19 62.\" 2010-11-06, mtk: Added documentation of prlimit()
0fc46b5a 63.\"
1d3050c0 64.TH GETRLIMIT 2 2014-01-22 "Linux" "Linux Programmer's Manual"
fea681da 65.SH NAME
1546fe19 66getrlimit, setrlimit, prlimit \- get/set resource limits
fea681da
MK
67.SH SYNOPSIS
68.B #include <sys/time.h>
69.br
70.B #include <sys/resource.h>
fea681da
MK
71.sp
72.BI "int getrlimit(int " resource ", struct rlimit *" rlim );
73.br
fea681da 74.BI "int setrlimit(int " resource ", const struct rlimit *" rlim );
1546fe19
MK
75.sp
76.BI "int prlimit(pid_t " pid ", int " resource \
77", const struct rlimit *" new_limit ,
78.br
79.BI " struct rlimit *" old_limit );
80.sp
81.in -4n
82Feature Test Macro Requirements for glibc (see
83.BR feature_test_macros (7)):
84.in
85.sp
86.BR prlimit ():
74da838c 87_GNU_SOURCE && _FILE_OFFSET_BITS == 64
fea681da 88.SH DESCRIPTION
1546fe19 89The
0fc46b5a 90.BR getrlimit ()
fea681da 91and
0fc46b5a 92.BR setrlimit ()
0ef19275 93system calls get and set resource limits respectively.
c13182ef 94Each resource has an associated soft and hard limit, as defined by the
8478ee02 95.I rlimit
c805532e 96structure:
fea681da 97.PP
a08ea57c 98.in +4n
fea681da
MK
99.nf
100struct rlimit {
0fc46b5a
MK
101 rlim_t rlim_cur; /* Soft limit */
102 rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
fea681da
MK
103};
104
105.fi
a08ea57c 106.in
fea681da
MK
107The soft limit is the value that the kernel enforces for the
108corresponding resource.
109The hard limit acts as a ceiling for the soft limit:
33a0ccb2 110an unprivileged process may set only its soft limit to a value in the
fea681da
MK
111range from 0 up to the hard limit, and (irreversibly) lower its hard limit.
112A privileged process (under Linux: one with the
113.B CAP_SYS_RESOURCE
114capability) may make arbitrary changes to either limit value.
115.PP
116The value
117.B RLIM_INFINITY
118denotes no limit on a resource (both in the structure returned by
119.BR getrlimit ()
120and in the structure passed to
121.BR setrlimit ()).
122.PP
0ef19275 123The
fea681da 124.I resource
0ef19275 125argument must be one of:
fea681da
MK
126.TP
127.B RLIMIT_AS
128The maximum size of the process's virtual memory (address space) in bytes.
129.\" since 2.0.27 / 2.1.12
130This limit affects calls to
131.BR brk (2),
132.BR mmap (2)
133and
134.BR mremap (2),
135which fail with the error
136.B ENOMEM
1c44bd5b
MK
137upon exceeding this limit.
138Also automatic stack expansion will fail
0fc46b5a
MK
139(and generate a
140.B SIGSEGV
141that kills the process if no alternate stack
142has been made available via
143.BR sigaltstack (2)).
4a3f7c5f 144Since the value is a \fIlong\fP, on machines with a 32-bit \fIlong\fP
fea681da
MK
145either this limit is at most 2 GiB, or this resource is unlimited.
146.TP
147.B RLIMIT_CORE
148Maximum size of
149.I core
1c44bd5b
MK
150file.
151When 0 no core dump files are created.
c7094399 152When nonzero, larger dumps are truncated to this size.
fea681da
MK
153.TP
154.B RLIMIT_CPU
155CPU time limit in seconds.
156When the process reaches the soft limit, it is sent a
157.B SIGXCPU
158signal.
159The default action for this signal is to terminate the process.
160However, the signal can be caught, and the handler can return control to
161the main program.
162If the process continues to consume CPU time, it will be sent
163.B SIGXCPU
164once per second until the hard limit is reached, at which time
165it is sent
166.BR SIGKILL .
4f96e450 167(This latter point describes Linux behavior.
fea681da
MK
168Implementations vary in how they treat processes which continue to
169consume CPU time after reaching the soft limit.
170Portable applications that need to catch this signal should
171perform an orderly termination upon first receipt of
e6c5832f 172.BR SIGXCPU .)
fea681da
MK
173.TP
174.B RLIMIT_DATA
175The maximum size of the process's data segment (initialized data,
176uninitialized data, and heap).
177This limit affects calls to
0bfa087b 178.BR brk (2)
c13182ef 179and
0bfa087b 180.BR sbrk (2),
fea681da
MK
181which fail with the error
182.B ENOMEM
183upon encountering the soft limit of this resource.
184.TP
185.B RLIMIT_FSIZE
186The maximum size of files that the process may create.
187Attempts to extend a file beyond this limit result in delivery of a
188.B SIGXFSZ
189signal.
c13182ef
MK
190By default, this signal terminates a process, but a process can
191catch this signal instead, in which case the relevant system call (e.g.,
2e42dfb3 192.BR write (2),
0bfa087b 193.BR truncate (2))
fea681da
MK
194fails with the error
195.BR EFBIG .
196.TP
9d8b1d5f
MK
197.BR RLIMIT_LOCKS " (Early Linux 2.4 only)"
198.\" to be precise: Linux 2.4.0-test9; no longer in 2.4.25 / 2.5.65
fea681da 199A limit on the combined number of
0bfa087b 200.BR flock (2)
c13182ef 201locks and
0bfa087b 202.BR fcntl (2)
fea681da 203leases that this process may establish.
fea681da
MK
204.TP
205.B RLIMIT_MEMLOCK
b4c0e1cb
MK
206The maximum number of bytes of memory that may be locked
207into RAM.
208In effect this limit is rounded down to the nearest multiple
209of the system page size.
c13182ef 210This limit affects
e1d6264d 211.BR mlock (2)
c13182ef 212and
e1d6264d 213.BR mlockall (2)
b4c0e1cb
MK
214and the
215.BR mmap (2)
216.B MAP_LOCKED
217operation.
218Since Linux 2.6.9 it also affects the
219.BR shmctl (2)
220.B SHM_LOCK
c13182ef 221operation, where it sets a maximum on the total bytes in
b4c0e1cb
MK
222shared memory segments (see
223.BR shmget (2))
224that may be locked by the real user ID of the calling process.
c13182ef 225The
b4c0e1cb
MK
226.BR shmctl (2)
227.B SHM_LOCK
228locks are accounted for separately from the per-process memory
c13182ef
MK
229locks established by
230.BR mlock (2),
e1d6264d 231.BR mlockall (2),
b4c0e1cb
MK
232and
233.BR mmap (2)
234.BR MAP_LOCKED ;
235a process can lock bytes up to this limit in each of these
e6c5832f 236two categories.
b4c0e1cb
MK
237In Linux kernels before 2.6.9, this limit controlled the amount of
238memory that could be locked by a privileged process.
239Since Linux 2.6.9, no limits are placed on the amount of memory
240that a privileged process may lock, and this limit instead governs
241the amount of memory that an unprivileged process may lock.
9d8b1d5f 242.TP
31c1f2b0 243.BR RLIMIT_MSGQUEUE " (since Linux 2.6.8)"
9d8b1d5f
MK
244Specifies the limit on the number of bytes that can be allocated
245for POSIX message queues for the real user ID of the calling process.
246This limit is enforced for
247.BR mq_open (3).
9d8b1d5f
MK
248Each message queue that the user creates counts (until it is removed)
249against this limit according to the formula:
250.nf
251
252 bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
4a04cd9a 253 attr.mq_maxmsg * attr.mq_msgsize
9d8b1d5f 254
c13182ef
MK
255.fi
256where
9d8b1d5f 257.I attr
c13182ef 258is the
9d8b1d5f
MK
259.I mq_attr
260structure specified as the fourth argument to
0bfa087b 261.BR mq_open (3).
9d8b1d5f
MK
262
263The first addend in the formula, which includes
5049da5b 264.I "sizeof(struct msg_msg\ *)"
34ccb744 265(4 bytes on Linux/i386), ensures that the user cannot
9d8b1d5f
MK
266create an unlimited number of zero-length messages (such messages
267nevertheless each consume some system memory for bookkeeping overhead).
a23bf8a3 268.TP
64d6219c 269.BR RLIMIT_NICE " (since Linux 2.6.12, but see BUGS below)"
cedd678f 270Specifies a ceiling to which the process's nice value can be raised using
a23bf8a3
MK
271.BR setpriority (2)
272or
273.BR nice (2).
274The actual ceiling for the nice value is calculated as
275.IR "20\ \-\ rlim_cur" .
cedd678f
MK
276(This strangeness occurs because negative numbers cannot be specified
277as resource limit values, since they typically have special meanings.
682edefb
MK
278For example,
279.B RLIM_INFINITY
280typically is the same as \-1.)
1bf844f1 281.TP
fea681da
MK
282.B RLIMIT_NOFILE
283Specifies a value one greater than the maximum file descriptor number
284that can be opened by this process.
285Attempts
0bfa087b
MK
286.RB ( open (2),
287.BR pipe (2),
288.BR dup (2),
4a04cd9a 289etc.)
fea681da
MK
290to exceed this limit yield the error
291.BR EMFILE .
00e8730f
MK
292(Historically, this limit was named
293.B RLIMIT_OFILE
294on BSD.)
fea681da
MK
295.TP
296.B RLIMIT_NPROC
c13182ef 297The maximum number of processes (or, more precisely on Linux, threads)
ee930c49 298that can be created for the real user ID of the calling process.
c13182ef 299Upon encountering this limit,
0bfa087b 300.BR fork (2)
fea681da
MK
301fails with the error
302.BR EAGAIN .
303.TP
304.B RLIMIT_RSS
305Specifies the limit (in pages) of the process's resident set
306(the number of virtual pages resident in RAM).
33a0ccb2
MK
307This limit has effect only in Linux 2.4.x, x < 30, and there
308affects only calls to
0bfa087b 309.BR madvise (2)
fea681da 310specifying
9d8b1d5f
MK
311.BR MADV_WILLNEED .
312.\" As at kernel 2.6.12, this limit still does nothing in 2.6 though
c13182ef 313.\" talk of making it do something has surfaced from time to time in LKML
9426c9dd 314.\" -- MTK, Jul 05
fea681da 315.TP
31c1f2b0 316.BR RLIMIT_RTPRIO " (since Linux 2.6.12, but see BUGS)"
c13182ef 317Specifies a ceiling on the real-time priority that may be set for
cedd678f 318this process using
1bf844f1
MK
319.BR sched_setscheduler (2)
320and
321.BR sched_setparam (2).
322.TP
31c1f2b0 323.BR RLIMIT_RTTIME " (since Linux 2.6.25)"
c43b0ac7
MK
324Specifies a limit (in microseconds)
325on the amount of CPU time that a process scheduled
23ce0537
MK
326under a real-time scheduling policy may consume without making a blocking
327system call.
328For the purpose of this limit,
329each time a process makes a blocking system call,
330the count of its consumed CPU time is reset to zero.
331The CPU time count is not reset if the process continues trying to
332use the CPU but is preempted, its time slice expires, or it calls
333.BR sched_yield (2).
334
335Upon reaching the soft limit, the process is sent a
336.B SIGXCPU
337signal.
338If the process catches or ignores this signal and
339continues consuming CPU time, then
340.B SIGXCPU
341will be generated once each second until the hard limit is reached,
342at which point the process is sent a
343.B SIGKILL
344signal.
345
346The intended use of this limit is to stop a runaway
347real-time process from locking up the system.
348.TP
31c1f2b0 349.BR RLIMIT_SIGPENDING " (since Linux 2.6.8)"
8b6aacb0 350Specifies the limit on the number of signals
e6c5832f 351that may be queued for the real user ID of the calling process.
8b6aacb0
MK
352Both standard and real-time signals are counted for the purpose of
353checking this limit.
33a0ccb2 354However, the limit is enforced only for
485ab701 355.BR sigqueue (3);
8b6aacb0
MK
356it is always possible to use
357.BR kill (2)
358to queue one instance of any of the signals that are not already
359queued to the process.
e6c5832f
MK
360.\" This replaces the /proc/sys/kernel/rtsig-max system-wide limit
361.\" that was present in kernels <= 2.6.7. MTK Dec 04
362.TP
fea681da
MK
363.B RLIMIT_STACK
364The maximum size of the process stack, in bytes.
365Upon reaching this limit, a
366.B SIGSEGV
367signal is generated.
368To handle this signal, a process must employ an alternate signal stack
369.RB ( sigaltstack (2)).
374af67a
MK
370
371Since Linux 2.6.23,
372this limit also determines the amount of space used for the process's
373command-line arguments and environment variables; for details, see
374.BR execve (2).
1546fe19
MK
375.SS prlimit()
376.\" commit c022a0acad534fd5f5d5f17280f6d4d135e74e81
377.\" Author: Jiri Slaby <jslaby@suse.cz>
378.\" Date: Tue May 4 18:03:50 2010 +0200
379The Linux-specific
380.BR prlimit ()
381system call combines and extends the functionality of
382.BR setrlimit ()
383and
384.BR getrlimit ().
385It can be used to both set and get the resource limits of an arbitrary process.
386
387The
388.I resource
389argument has the same meaning as for
390.BR setrlimit ()
391and
392.BR getrlimit ().
393
394If the
395.IR new_limit
396argument is a not NULL, then the
397.I rlimit
398structure to which it points is used to set new values for
399the soft and hard limits for
400.IR resource .
401If the
402.IR old_limit
403argument is a not NULL, then a successful call to
404.BR prlimit ()
405places the previous soft and hard limits for
406.I resource
98b43b57 407in the
1546fe19
MK
408.I rlimit
409structure pointed to by
410.IR old_limit .
411
412The
413.I pid
414argument specifies the ID of the process on which the call is to operate.
415If
416.I pid
417is 0, then the call applies to the calling process.
418To set or get the resources of a process other than itself,
419the caller must have the
420.B CAP_SYS_RESOURCE
421capability, or the
422real, effective, and saved set user IDs of the target process
423must match the real user ID of the caller
424.I and
425the real, effective, and saved set group IDs of the target process
426must match the real group ID of the caller.
427.\" FIXME this permission check is strange
428.\" Asked about this on LKML, 7 Nov 2010
429.\" "Inconsistent credential checking in prlimit() syscall"
430.SH RETURN VALUE
431On success, these system calls return 0.
c13182ef 432On error, \-1 is returned, and
fea681da
MK
433.I errno
434is set appropriately.
435.SH ERRORS
436.TP
437.B EFAULT
1546fe19
MK
438A pointer argument points to a location
439outside the accessible address space.
fea681da
MK
440.TP
441.B EINVAL
1546fe19 442The value specified in
0fc46b5a 443.I resource
b270eba9
MK
444is not valid;
445or, for
1546fe19
MK
446.BR setrlimit ()
447or
448.BR prlimit ():
94e9d9fe 449.I rlim\->rlim_cur
b270eba9 450was greater than
94e9d9fe 451.IR rlim\->rlim_max .
fea681da
MK
452.TP
453.B EPERM
1546fe19 454An unprivileged process tried to raise the hard limit; the
fea681da
MK
455.B CAP_SYS_RESOURCE
456capability is required to do this.
bbc67398 457Or, the caller tried to increase the hard
682edefb 458.B RLIMIT_NOFILE
1546fe19 459limit above the current kernel maximum
2f0af33b 460.RB ( NR_OPEN ).
1546fe19
MK
461Or, the calling process did not have permission to set limits
462for the process specified by
463.IR pid .
464.TP
465.B ESRCH
466Could not find a process with the ID specified in
467.IR pid .
468.SH VERSIONS
010eefd7 469The
1546fe19
MK
470.BR prlimit ()
471system call is available since Linux 2.6.36.
472Library support is available since glibc 2.13.
a1d5f77c 473.SH CONFORMING TO
1546fe19
MK
474.BR getrlimit (),
475.BR setrlimit ():
a1d5f77c 476SVr4, 4.3BSD, POSIX.1-2001.
1546fe19
MK
477.br
478.BR prlimit ():
479Linux-specific.
480
0daa9e92 481.B RLIMIT_MEMLOCK
a1d5f77c 482and
0daa9e92 483.B RLIMIT_NPROC
a1d5f77c
MK
484derive from BSD and are not specified in POSIX.1-2001;
485they are present on the BSDs and Linux, but on few other implementations.
0daa9e92 486.B RLIMIT_RSS
a1d5f77c
MK
487derives from BSD and is not specified in POSIX.1-2001;
488it is nevertheless present on most implementations.
489.BR RLIMIT_MSGQUEUE ,
490.BR RLIMIT_NICE ,
491.BR RLIMIT_RTPRIO ,
23ce0537 492.BR RLIMIT_RTTIME ,
a1d5f77c
MK
493and
494.B RLIMIT_SIGPENDING
8382f16d 495are Linux-specific.
a1d5f77c
MK
496.SH NOTES
497A child process created via
498.BR fork (2)
2c0cfe3c 499inherits its parent's resource limits.
a1d5f77c
MK
500Resource limits are preserved across
501.BR execve (2).
835363b2 502
1d3050c0
MK
503Lowering the soft limit for a resource below the process's
504current consumption of that resource will succeed
505(but will prevent the process from further increasing
506its consumption of the resource).
507
835363b2
MK
508One can set the resource limits of the shell using the built-in
509.IR ulimit
510command
511.RI ( limit
512in
513.BR csh (1)).
514The shell's resource limits are inherited by the processes that
515it creates to execute commands.
e1695dec 516
6d0620d8
MK
517Since Linux 2.6.24, the resource limits of any process can be inspected via
518.IR /proc/[pid]/limits ;
519see
520.BR proc (5).
521
e1695dec
MK
522Ancient systems provided a
523.BR vlimit ()
524function with a similar purpose to
525.BR setrlimit ().
526For backward compatibility, glibc also provides
527.BR vlimit ().
528All new applications should be written using
529.BR setrlimit ().
b4c0e1cb 530.SH BUGS
1546fe19
MK
531.\" FIXME prlimit() does not suffer
532.\" https://bugzilla.kernel.org/show_bug.cgi?id=5042
533.\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12201
cf6ea96a
MK
534.\" Since versions 2.13, glibc has library implementations of
535.\" getrlimit() and setrlimit() that use prlimit() to work around
536.\" this bug.
b4c0e1cb
MK
537In older Linux kernels, the
538.B SIGXCPU
539and
540.B SIGKILL
541signals delivered when a process encountered the soft and hard
9a8a1136 542.B RLIMIT_CPU
b4c0e1cb
MK
543limits were delivered one (CPU) second later than they should have been.
544This was fixed in kernel 2.6.8.
1bf844f1 545
c13182ef
MK
546In 2.6.x kernels before 2.6.17, a
547.B RLIMIT_CPU
548limit of 0 is wrongly treated as "no limit" (like
6057e7a9 549.BR RLIM_INFINITY ).
64d6219c 550Since Linux 2.6.17, setting a limit of 0 does have an effect,
6057e7a9 551but is actually treated as a limit of 1 second.
a31272fe 552.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=114008066530167&w=2
6057e7a9 553
1bf844f1 554A kernel bug means that
ceee84ba 555.\" See https://lwn.net/Articles/145008/
1bf844f1
MK
556.B RLIMIT_RTPRIO
557does not work in kernel 2.6.12; the problem is fixed in kernel 2.6.13.
6151ea9a 558
c13182ef 559In kernel 2.6.12, there was an off-by-one mismatch
b5cc2ffb
MK
560between the priority ranges returned by
561.BR getpriority (2)
562and
6151ea9a 563.BR RLIMIT_NICE .
11532b16 564This had the effect that the actual ceiling for the nice value
6151ea9a
MK
565was calculated as
566.IR "19\ \-\ rlim_cur" .
cedd678f 567This was fixed in kernel 2.6.13.
6151ea9a 568.\" see http://marc.theaimsgroup.com/?l=linux-kernel&m=112256338703880&w=2
b270eba9 569
27bada1f
MK
570Since Linux 2.6.12,
571.\" The relevant patch, sent to LKML, seems to be
572.\" http://thread.gmane.org/gmane.linux.kernel/273462
573.\" From: Roland McGrath <roland <at> redhat.com>
574.\" Subject: [PATCH 7/7] make RLIMIT_CPU/SIGXCPU per-process
575.\" Date: 2005-01-23 23:27:46 GMT
576if a process reaches its soft
577.BR RLIMIT_CPU
578limit and has a handler installed for
579.BR SIGXCPU ,
580then, in addition to invoking the signal handler,
581the kernel increases the soft limit by one second.
582This behavior repeats if the process continues to consume CPU time,
583until the hard limit is reached,
584at which point the process is killed.
585Other implementations
586.\" Tested Solaris 10, FreeBSD 9, OpenBSD 5.0
587do not change the
588.BR RLIMIT_CPU
589soft limit in this manner,
590and the Linux behavior is probably not standards conformant;
591portable applications should avoid relying on this Linux-specific behavior.
592.\" FIXME https://bugzilla.kernel.org/show_bug.cgi?id=50951
593The Linux-specific
594.BR RLIMIT_RTTIME
595limit exhibits the same behavior when the soft limit is encountered.
596
b270eba9
MK
597Kernels before 2.4.22 did not diagnose the error
598.B EINVAL
c13182ef 599for
b270eba9
MK
600.BR setrlimit ()
601when
94e9d9fe 602.I rlim\->rlim_cur
b270eba9 603was greater than
94e9d9fe 604.IR rlim\->rlim_max .
7484d5a7
MK
605.SH EXAMPLE
606The program below demonstrates the use of
607.BR prlimit ().
608.PP
609.nf
610#define _GNU_SOURCE
611#define _FILE_OFFSET_BITS 64
612#include <stdio.h>
613#include <time.h>
614#include <stdlib.h>
615#include <unistd.h>
616#include <sys/resource.h>
617
618#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
619 } while (0)
620
621int
622main(int argc, char *argv[])
623{
624 struct rlimit old, new;
625 struct rlimit *newp;
626 pid_t pid;
627
628 if (!(argc == 2 || argc == 4)) {
629 fprintf(stderr, "Usage: %s <pid> [<new\-soft\-limit> "
630 "<new\-hard\-limit>]\\n", argv[0]);
631 exit(EXIT_FAILURE);
632 }
633
634 pid = atoi(argv[1]); /* PID of target process */
635
636 newp = NULL;
637 if (argc == 4) {
638 new.rlim_cur = atoi(argv[2]);
639 new.rlim_max = atoi(argv[3]);
640 newp = &new;
641 }
642
643 /* Set CPU time limit of target process; retrieve and display
644 previous limit */
645
646 if (prlimit(pid, RLIMIT_CPU, newp, &old) == \-1)
647 errExit("prlimit\-1");
648 printf("Previous limits: soft=%lld; hard=%lld\\n",
649 (long long) old.rlim_cur, (long long) old.rlim_max);
650
651 /* Retrieve and display new CPU time limit */
652
653 if (prlimit(pid, RLIMIT_CPU, NULL, &old) == \-1)
654 errExit("prlimit\-2");
655 printf("New limits: soft=%lld; hard=%lld\\n",
656 (long long) old.rlim_cur, (long long) old.rlim_max);
657
658 exit(EXIT_FAILURE);
659}
660.fi
47297adb 661.SH SEE ALSO
94315587 662.BR prlimit (1),
fea681da
MK
663.BR dup (2),
664.BR fcntl (2),
665.BR fork (2),
0fc46b5a 666.BR getrusage (2),
fea681da 667.BR mlock (2),
fea681da
MK
668.BR mmap (2),
669.BR open (2),
670.BR quotactl (2),
671.BR sbrk (2),
b4c0e1cb 672.BR shmctl (2),
fea681da 673.BR malloc (3),
485ab701 674.BR sigqueue (3),
fea681da 675.BR ulimit (3),
e1a9bc1b 676.BR core (5),
fea681da
MK
677.BR capabilities (7),
678.BR signal (7)