xref: /freebsd-13.1/lib/libc/sys/getrlimit.2 (revision fbbd9655)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)getrlimit.2	8.1 (Berkeley) 6/4/93
29.\" $FreeBSD$
30.\"
31.Dd September 30, 2016
32.Dt GETRLIMIT 2
33.Os
34.Sh NAME
35.Nm getrlimit ,
36.Nm setrlimit
37.Nd control maximum system resource consumption
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In sys/types.h
42.In sys/time.h
43.In sys/resource.h
44.Ft int
45.Fn getrlimit "int resource" "struct rlimit *rlp"
46.Ft int
47.Fn setrlimit "int resource" "const struct rlimit *rlp"
48.Sh DESCRIPTION
49Limits on the consumption of system resources by the current process
50and each process it creates may be obtained with the
51.Fn getrlimit
52system call, and set with the
53.Fn setrlimit
54system call.
55.Pp
56The
57.Fa resource
58argument is one of the following:
59.Bl -tag -width RLIMIT_FSIZEAA
60.It Dv RLIMIT_AS
61The maximum amount (in bytes) of virtual memory the process is
62allowed to map.
63.It Dv RLIMIT_CORE
64The largest size (in bytes)
65.Xr core 5
66file that may be created.
67.It Dv RLIMIT_CPU
68The maximum amount of cpu time (in seconds) to be used by
69each process.
70.It Dv RLIMIT_DATA
71The maximum size (in bytes) of the data segment for a process;
72this defines how far a program may extend its break with the
73.Xr sbrk 2
74function.
75.It Dv RLIMIT_FSIZE
76The largest size (in bytes) file that may be created.
77.It Dv RLIMIT_KQUEUES
78The maximum number of kqueues this user id is allowed to create.
79.It Dv RLIMIT_MEMLOCK
80The maximum size (in bytes) which a process may lock into memory
81using the
82.Xr mlock 2
83system call.
84.It Dv RLIMIT_NOFILE
85The maximum number of open files for this process.
86.It Dv RLIMIT_NPROC
87The maximum number of simultaneous processes for this user id.
88.It Dv RLIMIT_NPTS
89The maximum number of pseudo-terminals this user id is allowed to create.
90.It Dv RLIMIT_RSS
91When there is memory pressure and swap is available, prioritize eviction of
92a process' resident pages beyond this amount (in bytes).
93When memory is not under pressure, this rlimit is effectively ignored.
94Even when there is memory pressure, the amount of available swap space and some
95sysctl settings like
96.Xr vm.swap_enabled
97and
98.Xr vm.swap_idle_enabled
99can affect what happens to processes that have exceeded this size.
100.Pp
101Processes that exceed their set
102.Dv RLIMIT_RSS
103are not signalled or halted.
104The limit is merely a hint to the VM daemon to prefer to deactivate pages from
105processes that have exceeded their set
106.Dv RLIMIT_RSS .
107.It Dv RLIMIT_SBSIZE
108The maximum size (in bytes) of socket buffer usage for this user.
109This limits the amount of network memory, and hence the amount of
110mbufs, that this user may hold at any time.
111.It Dv RLIMIT_STACK
112The maximum size (in bytes) of the stack segment for a process;
113this defines how far a program's stack segment may be extended.
114Stack extension is performed automatically by the system.
115.It Dv RLIMIT_SWAP
116The maximum size (in bytes) of the swap space that may be reserved or
117used by all of this user id's processes.
118This limit is enforced only if bit 1 of the
119.Va vm.overcommit
120sysctl is set.
121Please see
122.Xr tuning 7
123for a complete description of this sysctl.
124.It Dv RLIMIT_VMEM
125An alias for
126.Dv RLIMIT_AS .
127.El
128.Pp
129A resource limit is specified as a soft limit and a hard limit.
130When a soft limit is exceeded, a process might or might not receive a signal.
131For example, signals are generated when the cpu time or file size is exceeded,
132but not if the address space or RSS limit is exceeded.
133A program that exceeds the soft limit is allowed to continue execution until it
134reaches the hard limit, or modifies its own resource limit.
135Even reaching the hard limit does not necessarily halt a process.
136For example, if the RSS hard limit is exceeded, nothing happens.
137.Pp
138The
139.Vt rlimit
140structure is used to specify the hard and soft limits on a resource.
141.Bd -literal -offset indent
142struct rlimit {
143	rlim_t	rlim_cur;	/* current (soft) limit */
144	rlim_t	rlim_max;	/* maximum value for rlim_cur */
145};
146.Ed
147.Pp
148Only the super-user may raise the maximum limits.
149Other users
150may only alter
151.Fa rlim_cur
152within the range from 0 to
153.Fa rlim_max
154or (irreversibly) lower
155.Fa rlim_max .
156.Pp
157An
158.Dq infinite
159value for a limit is defined as
160.Dv RLIM_INFINITY .
161.Pp
162Because this information is stored in the per-process information,
163this system call must be executed directly by the shell if it
164is to affect all future processes created by the shell;
165.Ic limit
166is thus a built-in command to
167.Xr csh 1 .
168.Pp
169The system refuses to extend the data or stack space when the limits
170would be exceeded in the normal way: a
171.Xr brk 2
172function fails if the data space limit is reached.
173When the stack limit is reached, the process receives
174a segmentation fault
175.Pq Dv SIGSEGV ;
176if this signal is not
177caught by a handler using the signal stack, this signal
178will kill the process.
179.Pp
180A file I/O operation that would create a file larger that the process'
181soft limit will cause the write to fail and a signal
182.Dv SIGXFSZ
183to be
184generated; this normally terminates the process, but may be caught.
185When
186the soft cpu time limit is exceeded, a
187.Dv SIGXCPU
188signal is sent to the
189offending process.
190.Pp
191When most operations would allocate more virtual memory than allowed by the
192soft limit of
193.Dv RLIMIT_AS ,
194the operation fails with
195.Dv ENOMEM
196and no signal is raised.
197A notable exception is stack extension, described above.
198If stack extension would allocate more virtual memory than allowed by the soft
199limit of
200.Dv RLIMIT_AS ,
201a
202.Dv SIGSEGV
203signal will be delivered.
204The caller is free to raise the soft address space limit up to the hard limit
205and retry the allocation.
206.Sh RETURN VALUES
207.Rv -std
208.Sh ERRORS
209The
210.Fn getrlimit
211and
212.Fn setrlimit
213system calls
214will fail if:
215.Bl -tag -width Er
216.It Bq Er EFAULT
217The address specified for
218.Fa rlp
219is invalid.
220.It Bq Er EPERM
221The limit specified to
222.Fn setrlimit
223would have
224raised the maximum limit value, and the caller is not the super-user.
225.El
226.Sh SEE ALSO
227.Xr csh 1 ,
228.Xr quota 1 ,
229.Xr quotactl 2 ,
230.Xr sigaction 2 ,
231.Xr sigaltstack 2 ,
232.Xr sysctl 3 ,
233.Xr ulimit 3
234.Sh HISTORY
235The
236.Fn getrlimit
237system call appeared in
238.Bx 4.2 .
239