xref: /openbsd/lib/libc/sys/getrlimit.2 (revision 4cfece93)
1.\"	$OpenBSD: getrlimit.2,v 1.28 2018/01/12 04:36:44 deraadt Exp $
2.\"	$NetBSD: getrlimit.2,v 1.8 1995/10/12 15:40:58 jtc Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)getrlimit.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: January 12 2018 $
34.Dt GETRLIMIT 2
35.Os
36.Sh NAME
37.Nm getrlimit ,
38.Nm setrlimit
39.Nd control maximum system resource consumption
40.Sh SYNOPSIS
41.In sys/resource.h
42.Ft int
43.Fn getrlimit "int resource" "struct rlimit *rlp"
44.Ft int
45.Fn setrlimit "int resource" "const struct rlimit *rlp"
46.Sh DESCRIPTION
47Limits on the consumption of system resources by the current process
48and each process it creates may be obtained with the
49.Fn getrlimit
50call, and set with the
51.Fn setrlimit
52call.
53.Pp
54The
55.Fa resource
56parameter is one of the following:
57.Bl -tag -width RLIMIT_FSIZEAA
58.It Li RLIMIT_CORE
59The largest size (in bytes)
60.Pa core
61file that may be created.
62.It Li RLIMIT_CPU
63The maximum amount of CPU time (in seconds) to be used by
64each process.
65.It Li RLIMIT_DATA
66The maximum size (in bytes) of the data segment for a process;
67this includes memory allocated via
68.Xr malloc 3
69and all other anonymous memory mapped via
70.Xr mmap 2 .
71.It Li RLIMIT_FSIZE
72The largest size (in bytes) file that may be created.
73.It Li RLIMIT_MEMLOCK
74The maximum size (in bytes) which a process may lock into memory
75using the
76.Xr mlock 2
77function.
78.It Li RLIMIT_NOFILE
79The maximum number of open files for this process.
80.It Li RLIMIT_NPROC
81The maximum number of simultaneous processes for this user id.
82.It Li RLIMIT_RSS
83The maximum size (in bytes) to which a process's resident set size may
84grow.
85This setting is no longer enforced, but retained for compatibility.
86.It Li RLIMIT_STACK
87The maximum size (in bytes) of the stack segment for a process,
88which defines how far a process's stack segment may be extended.
89Stack extension is performed automatically by the system,
90and is only used by the main thread of a process.
91.El
92.Pp
93A resource limit is specified as a soft limit and a hard limit.
94When a soft limit is exceeded a process may receive a signal (for example,
95if the CPU time or file size is exceeded), but it will be allowed to
96continue execution until it reaches the hard limit (or modifies
97its resource limit).
98The
99.Em rlimit
100structure is used to specify the hard and soft limits on a resource,
101.Bd -literal -offset indent
102struct rlimit {
103	rlim_t	rlim_cur;	/* current (soft) limit */
104	rlim_t	rlim_max;	/* hard limit */
105};
106.Ed
107.Pp
108Only the superuser may raise the maximum limits.
109Other users may only alter
110.Fa rlim_cur
111within the range from 0 to
112.Fa rlim_max
113or (irreversibly) lower
114.Fa rlim_max .
115.Pp
116An
117.Dq infinite
118value for a limit is defined as
119.Dv RLIM_INFINITY .
120.Pp
121A value of
122.Dv RLIM_SAVED_CUR
123or
124.Dv RLIM_SAVED_MAX
125will be stored in
126.Fa rlim_cur
127or
128.Fa rlim_max
129respectively by
130.Fn getrlimit
131if the value for the current or maximum resource limit cannot be stored in an
132.Li rlim_t .
133The values
134.Dv RLIM_SAVED_CUR
135and
136.Dv RLIM_SAVED_MAX
137should not be used in a call to
138.Fn setrlimit
139unless they were returned by a previous call to
140.Fn getrlimit .
141.Pp
142Because this information is stored in the per-process information,
143this system call must be executed directly by the shell if it
144is to affect all future processes created by the shell;
145.Ic limit
146is thus a built-in command to
147.Xr csh 1
148and
149.Ic ulimit
150is the
151.Xr sh 1
152equivalent.
153.Pp
154The system refuses to extend the data or stack space when the limits
155would be exceeded in the normal way: a
156.Xr brk 2
157call fails if the data space limit is reached.
158When the stack limit is reached, the process receives
159a segmentation fault
160.Pq Dv SIGSEGV ;
161if this signal is not
162caught by a handler using the signal stack, this signal
163will kill the process.
164.Pp
165A file I/O operation that would create a file larger than the process'
166soft limit will cause the write to fail and a signal
167.Dv SIGXFSZ
168to be
169generated; this normally terminates the process, but may be caught.
170When the soft CPU time limit is exceeded, a signal
171.Dv SIGXCPU
172is sent to the
173offending process.
174.Sh RETURN VALUES
175.Rv -std
176.Sh ERRORS
177.Fn getrlimit
178and
179.Fn setrlimit
180will fail if:
181.Bl -tag -width Er
182.It Bq Er EFAULT
183The address specified for
184.Fa rlp
185is invalid.
186.It Bq Er EINVAL
187An unrecognized value for
188.Fa resource
189was specified.
190.El
191.Pp
192In addition,
193.Fn setrlimit
194may return the following errors:
195.Bl -tag -width Er
196.It Bq Er EINVAL
197The new
198.Fa rlim_cur
199is greater than the new
200.Fa rlim_max .
201.It Bq Er EPERM
202The new
203.Fa rlim_max
204is greater than the current maximum limit value,
205and the caller is not the superuser.
206.El
207.Sh SEE ALSO
208.Xr csh 1 ,
209.Xr sh 1 ,
210.Xr quotactl 2 ,
211.Xr sigaction 2 ,
212.Xr sigaltstack 2 ,
213.Xr sysctl 2
214.Sh STANDARDS
215The
216.Fn getrlimit
217and
218.Fn setrlimit
219functions conform to
220.St -p1003.1-2008 .
221.Pp
222The
223.Dv RLIMIT_MEMLOCK ,
224.Dv RLIMIT_NPROC ,
225and
226.Dv RLIMIT_RSS
227resources are non-standard extensions.
228.Sh HISTORY
229The
230.Fn getrlimit
231and
232.Fn setrlimit
233system calls first appeared in
234.Bx 4.1c .
235.Sh BUGS
236The
237.Dv RLIMIT_AS
238resource is missing.
239