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