xref: /freebsd/lib/libsys/getrlimit.2 (revision b59017c5cad90d0f09a59e68c00457b7faf93e7c)
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.Dd July 25, 2024
29.Dt GETRLIMIT 2
30.Os
31.Sh NAME
32.Nm getrlimit ,
33.Nm setrlimit
34.Nd control maximum system resource consumption
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In sys/types.h
39.In sys/time.h
40.In sys/resource.h
41.Ft int
42.Fn getrlimit "int resource" "struct rlimit *rlp"
43.Ft int
44.Fn setrlimit "int resource" "const struct rlimit *rlp"
45.Sh DESCRIPTION
46Limits on the consumption of system resources by the current process
47and each process it creates may be obtained with the
48.Fn getrlimit
49system call, and set with the
50.Fn setrlimit
51system call.
52.Pp
53The
54.Fa resource
55argument is one of the following:
56.Bl -tag -width RLIMIT_FSIZEAA
57.It Dv RLIMIT_AS
58The maximum amount (in bytes) of virtual memory the process is
59allowed to map.
60.It Dv RLIMIT_CORE
61The largest size (in bytes)
62.Xr core 5
63file that may be created.
64.It Dv RLIMIT_CPU
65The maximum amount of cpu time (in seconds) to be used by
66each process.
67.It Dv RLIMIT_DATA
68The maximum size (in bytes) of the data segment for a process;
69this defines how far a program may extend its break with the
70.Xr sbrk 2
71function.
72.It Dv RLIMIT_FSIZE
73The largest size (in bytes) file that may be created.
74.It Dv RLIMIT_KQUEUES
75The maximum number of kqueues this user id is allowed to create.
76.It Dv RLIMIT_MEMLOCK
77The maximum size (in bytes) which a process may lock into memory
78using the
79.Xr mlock 2
80system call.
81.It Dv RLIMIT_NOFILE
82The maximum number of open files for this process.
83.It Dv RLIMIT_NPROC
84The maximum number of simultaneous processes for this user id.
85.It Dv RLIMIT_NPTS
86The maximum number of pseudo-terminals this user id is allowed to create.
87.It Dv RLIMIT_PIPEBUF
88The maximum total size of in-kernel buffers for bi-directional pipes/fifos
89that this user id is allowed to consume.
90The buffers for kernel FIFOs created on the first open of a filesystem
91object created by
92.Pq Xr mkfifo 2
93are also charged to the user ID of the process opening it,
94not the FIFO's filesystem owner.
95Despite somewhat unexpected, this is in fact fair, since user of the fifo
96is not necessary its creator.
97.It Dv RLIMIT_RSS
98When there is memory pressure and swap is available, prioritize eviction of
99a process' resident pages beyond this amount (in bytes).
100When memory is not under pressure, this rlimit is effectively ignored.
101.Pp
102Processes that exceed their set
103.Dv RLIMIT_RSS
104are not signalled or halted.
105The limit is merely a hint to the VM daemon to prefer to deactivate pages from
106processes that have exceeded their set
107.Dv RLIMIT_RSS .
108.It Dv RLIMIT_SBSIZE
109The maximum size (in bytes) of socket buffer usage for this user.
110This limits the amount of network memory, and hence the amount of
111mbufs, that this user may hold at any time.
112.It Dv RLIMIT_STACK
113The maximum size (in bytes) of the stack segment for a process;
114this defines how far a program's stack segment may be extended.
115Stack extension is performed automatically by the system.
116.It Dv RLIMIT_SWAP
117The maximum size (in bytes) of the swap space that may be reserved or
118used by all of this user id's processes.
119This limit is enforced only if bit 1 of the
120.Va vm.overcommit
121sysctl is set.
122Please see
123.Xr tuning 7
124for a complete description of this sysctl.
125.It Dv RLIMIT_UMTXP
126The limit of the number of process-shared posix thread library objects
127allocated by user id.
128.It Dv RLIMIT_VMEM
129An alias for
130.Dv RLIMIT_AS .
131.El
132.Pp
133A resource limit is specified as a soft limit and a hard limit.
134When a soft limit is exceeded, a process might or might not receive a signal.
135For example, signals are generated when the cpu time or file size is exceeded,
136but not if the address space or RSS limit is exceeded.
137A program that exceeds the soft limit is allowed to continue execution until it
138reaches the hard limit, or modifies its own resource limit.
139Even reaching the hard limit does not necessarily halt a process.
140For example, if the RSS hard limit is exceeded, nothing happens.
141.Pp
142The
143.Vt rlimit
144structure is used to specify the hard and soft limits on a resource.
145.Bd -literal -offset indent
146struct rlimit {
147	rlim_t	rlim_cur;	/* current (soft) limit */
148	rlim_t	rlim_max;	/* maximum value for rlim_cur */
149};
150.Ed
151.Pp
152Only the super-user may raise the maximum limits.
153Other users
154may only alter
155.Fa rlim_cur
156within the range from 0 to
157.Fa rlim_max
158or (irreversibly) lower
159.Fa rlim_max .
160.Pp
161An
162.Dq infinite
163value for a limit is defined as
164.Dv RLIM_INFINITY .
165.Pp
166Because this information is stored in the per-process information,
167this system call must be executed directly by the shell if it
168is to affect all future processes created by the shell;
169.Ic limit
170is thus a built-in command to
171.Xr csh 1 .
172.Pp
173The system refuses to extend the data or stack space when the limits
174would be exceeded in the normal way: a
175.Xr brk 2
176function fails if the data space limit is reached.
177When the stack limit is reached, the process receives
178a segmentation fault
179.Pq Dv SIGSEGV ;
180if this signal is not
181caught by a handler using the signal stack, this signal
182will kill the process.
183.Pp
184A file I/O operation that would create a file larger that the process'
185soft limit will cause the write to fail and a signal
186.Dv SIGXFSZ
187to be
188generated; this normally terminates the process, but may be caught.
189When
190the soft cpu time limit is exceeded, a
191.Dv SIGXCPU
192signal is sent to the
193offending process.
194.Pp
195When most operations would allocate more virtual memory than allowed by the
196soft limit of
197.Dv RLIMIT_AS ,
198the operation fails with
199.Dv ENOMEM
200and no signal is raised.
201A notable exception is stack extension, described above.
202If stack extension would allocate more virtual memory than allowed by the soft
203limit of
204.Dv RLIMIT_AS ,
205a
206.Dv SIGSEGV
207signal will be delivered.
208The caller is free to raise the soft address space limit up to the hard limit
209and retry the allocation.
210.Sh RETURN VALUES
211.Rv -std
212.Sh ERRORS
213The
214.Fn getrlimit
215and
216.Fn setrlimit
217system calls
218will fail if:
219.Bl -tag -width Er
220.It Bq Er EFAULT
221The address specified for
222.Fa rlp
223is invalid.
224.It Bq Er EPERM
225The limit specified to
226.Fn setrlimit
227would have
228raised the maximum limit value, and the caller is not the super-user.
229.El
230.Sh SEE ALSO
231.Xr csh 1 ,
232.Xr quota 1 ,
233.Xr quotactl 2 ,
234.Xr sigaction 2 ,
235.Xr sigaltstack 2 ,
236.Xr sysctl 3 ,
237.Xr ulimit 3
238.Sh HISTORY
239The
240.Fn getrlimit
241system call appeared in
242.Bx 4.2 .
243