xref: /openbsd-src/lib/libc/sys/getrlimit.2 (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1.\"	$OpenBSD: getrlimit.2,v 1.22 2013/07/17 05:42:11 schwarze 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: July 17 2013 $
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.Fd #include <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 defines how far a program may extend its break with the
68.Xr sbrk 2
69system call.
70.It Li RLIMIT_FSIZE
71The largest size (in bytes) file that may be created.
72.It Li RLIMIT_MEMLOCK
73The maximum size (in bytes) which a process may lock into memory
74using the
75.Xr mlock 2
76function.
77.It Li RLIMIT_NOFILE
78The maximum number of open files for this process.
79.It Li RLIMIT_NPROC
80The maximum number of simultaneous processes for this user id.
81.It Li RLIMIT_RSS
82The maximum size (in bytes) to which a process's resident set size may
83grow.
84This imposes a limit on the amount of physical memory to be given to
85a process; if memory is tight, the system will prefer to take memory
86from processes that are exceeding their declared resident set size.
87.It Li RLIMIT_STACK
88The maximum size (in bytes) of the stack segment for a process,
89which defines how far a process's stack segment may be extended.
90Stack extension is performed automatically by the system,
91and is only used by the main thread of a process.
92.El
93.Pp
94A resource limit is specified as a soft limit and a hard limit.
95When a soft limit is exceeded a process may receive a signal (for example,
96if the CPU time or file size is exceeded), but it will be allowed to
97continue execution until it reaches the hard limit (or modifies
98its resource limit).
99The
100.Em rlimit
101structure is used to specify the hard and soft limits on a resource,
102.Bd -literal -offset indent
103struct rlimit {
104	rlim_t	rlim_cur;	/* current (soft) limit */
105	rlim_t	rlim_max;	/* hard limit */
106};
107.Ed
108.Pp
109Only the superuser may raise the maximum limits.
110Other users may only alter
111.Fa rlim_cur
112within the range from 0 to
113.Fa rlim_max
114or (irreversibly) lower
115.Fa rlim_max .
116.Pp
117An
118.Dq infinite
119value for a limit is defined as
120.Dv RLIM_INFINITY .
121.Pp
122A value of
123.Dv RLIM_SAVED_CUR
124or
125.Dv RLIM_SAVED_MAX
126will be stored in
127.Fa rlim_cur
128or
129.Fa rlim_max
130respectively by
131.Fn getrlimit
132if the value for the current or maximum resource limit cannot be stored in an
133.Li rlim_t .
134The values
135.Dv RLIM_SAVED_CUR
136and
137.Dv RLIM_SAVED_MAX
138should not be used in a call to
139.Fn setrlimit
140unless they were returned by a previous call to
141.Fn getrlimit .
142.Pp
143Because this information is stored in the per-process information,
144this system call must be executed directly by the shell if it
145is to affect all future processes created by the shell;
146.Ic limit
147is thus a built-in command to
148.Xr csh 1
149and
150.Ic ulimit
151is the
152.Xr sh 1
153equivalent.
154.Pp
155The system refuses to extend the data or stack space when the limits
156would be exceeded in the normal way: a
157.Xr brk 2
158call fails if the data space limit is reached.
159When the stack limit is reached, the process receives
160a segmentation fault
161.Pq Dv SIGSEGV ;
162if this signal is not
163caught by a handler using the signal stack, this signal
164will kill the process.
165.Pp
166A file I/O operation that would create a file larger than the process'
167soft limit will cause the write to fail and a signal
168.Dv SIGXFSZ
169to be
170generated; this normally terminates the process, but may be caught.
171When the soft CPU time limit is exceeded, a signal
172.Dv SIGXCPU
173is sent to the
174offending process.
175.Sh RETURN VALUES
176A 0 return value indicates that the call succeeded, changing
177or returning the resource limit.
178A return value of \-1 indicates that an error occurred, and an error code
179is stored in the global variable
180.Va errno .
181.Sh ERRORS
182.Fn getrlimit
183and
184.Fn setrlimit
185will fail if:
186.Bl -tag -width Er
187.It Bq Er EFAULT
188The address specified for
189.Fa rlp
190is invalid.
191.It Bq Er EINVAL
192An unrecognized value for
193.Fa resource
194was specified.
195.El
196.Pp
197In addition,
198.Fn setrlimit
199may return the following errors:
200.Bl -tag -width Er
201.It Bq Er EINVAL
202The new
203.Fa rlim_cur
204is greater than the new
205.Fa rlim_max .
206.It Bq Er EPERM
207The new
208.Fa rlim_max
209is greater than the current maximum limit value,
210and the caller is not the superuser.
211.El
212.Sh SEE ALSO
213.Xr csh 1 ,
214.Xr sh 1 ,
215.Xr quotactl 2 ,
216.Xr sigaction 2 ,
217.Xr sigaltstack 2 ,
218.Xr sysctl 3
219.Sh STANDARDS
220The
221.Fn getrlimit
222and
223.Fn setrlimit
224functions conform to
225.St -p1003.1-2008 .
226.Pp
227The
228.Dv RLIMIT_MEMLOCK ,
229.Dv RLIMIT_NPROC ,
230and
231.Dv RLIMIT_RSS
232resources are non-standard extensions.
233.Sh HISTORY
234The
235.Fn getrlimit
236and
237.Fn setrlimit
238system calls first appeared in
239.Bx 4.1c .
240.Sh BUGS
241The
242.Dv RLIMIT_AS
243resource is missing.
244