xref: /dragonfly/lib/libc/sys/getrlimit.2 (revision e5a92d33)
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: src/lib/libc/sys/getrlimit.2,v 1.10.2.5 2001/12/14 18:34:00 ru Exp $
30.\"
31.Dd March 1, 2019
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
52call, and set with the
53.Fn setrlimit
54call.
55.Pp
56The
57.Fa resource
58parameter is one of the following:
59.Bl -tag -width ".Dv RLIMIT_POSIXLOCKS"
60.It Dv RLIMIT_AS
61The maximum size (in bytes) of a process' total available memory.
62If this limit is exceeded, the
63.Xr malloc 3
64and
65.Xr mmap 2
66functions will fail and
67.Va errno
68set to
69.Er ENOMEM .
70Moreover, the automatic stack growth will fail as well.
71.It Dv RLIMIT_CORE
72The largest size (in bytes)
73.Xr core 5
74file that may be created.
75.It Dv RLIMIT_CPU
76The maximum amount of cpu time (in seconds) to be used by
77each process.
78.It Dv RLIMIT_DATA
79The maximum size (in bytes) of the data segment for a process;
80this defines how far a program may extend its break with the
81.Xr sbrk 2
82system call.
83.It Dv RLIMIT_FSIZE
84The largest size (in bytes) file that may be created.
85.It Dv RLIMIT_MEMLOCK
86The maximum size (in bytes) which a process may lock into memory
87using the
88.Xr mlock 2
89function.
90.It Dv RLIMIT_NOFILE
91The maximum number of open files for this process.
92.It Dv RLIMIT_NPROC
93The maximum number of simultaneous processes for this user id.
94.It Dv RLIMIT_RSS
95The maximum size (in bytes) to which a process's resident set size may
96grow.
97This imposes a limit on the amount of physical memory to be given to
98a process; if memory is tight, the system will prefer to take memory
99from processes that are exceeding their declared resident set size.
100.It Dv RLIMIT_STACK
101The maximum size (in bytes) of the stack segment for a process;
102this defines how far a program's stack segment may be extended.
103Stack extension is performed automatically by the system.
104.It Dv RLIMIT_SBSIZE
105The maximum size (in bytes) of socket buffer usage for this user.
106This limits the amount of network memory, and hence the amount of
107mbufs, that this user may hold at any time.
108.It Dv RLIMIT_POSIXLOCKS
109The maximum number of POSIX-type advisory-mode locks available to
110this user.
111.El
112.Pp
113A resource limit is specified as a soft limit and a hard limit.
114When a soft limit is exceeded a process may receive a signal (for example, if
115the cpu time or file size is exceeded), but it will be allowed to
116continue execution until it reaches the hard limit (or modifies
117its resource limit).
118The
119.Em rlimit
120structure is used to specify the hard and soft limits on a resource,
121.Bd -literal -offset indent
122struct rlimit {
123	rlim_t	rlim_cur;	/* current (soft) limit */
124	rlim_t	rlim_max;	/* maximum value for rlim_cur */
125};
126.Ed
127.Pp
128Only the super-user may raise the maximum limits.
129Other users may only alter
130.Fa rlim_cur
131within the range from 0 to
132.Fa rlim_max
133or (irreversibly) lower
134.Fa rlim_max .
135.Pp
136An
137.Dq infinite
138value for a limit is defined as
139.Dv RLIM_INFINITY .
140.Pp
141Because this information is stored in the per-process information,
142this system call must be executed directly by the shell if it
143is to affect all future processes created by the shell;
144.Ic limit
145is thus a built-in command to
146.Xr csh 1 .
147.Pp
148The system refuses to extend the data or stack space when the limits
149would be exceeded in the normal way: an
150.Xr sbrk 2
151call fails if the data space limit is reached.
152When the stack limit is reached, the process receives
153a segmentation fault
154.Pq Dv SIGSEGV ;
155if this signal is not
156caught by a handler using the signal stack, this signal
157will kill the process.
158.Pp
159A file I/O operation that would create a file larger than the process'
160soft limit will cause the write to fail and a signal
161.Dv SIGXFSZ
162to be
163generated; this normally terminates the process, but may be caught.
164When the soft cpu time limit is exceeded, a signal
165.Dv SIGXCPU
166is sent to the
167offending process.
168.Sh RETURN VALUES
169.Rv -std
170.Sh ERRORS
171.Fn Getrlimit
172and
173.Fn setrlimit
174will fail if:
175.Bl -tag -width Er
176.It Bq Er EFAULT
177The address specified for
178.Fa rlp
179is invalid.
180.It Bq Er EPERM
181The limit specified to
182.Fn setrlimit
183would have
184raised the maximum limit value, and the caller is not the super-user.
185.El
186.Sh SEE ALSO
187.Xr csh 1 ,
188.Xr quota 1 ,
189.Xr quotactl 2 ,
190.Xr sigaction 2 ,
191.Xr sigaltstack 2 ,
192.Xr sysctl 3 ,
193.Xr ulimit 3
194.Sh HISTORY
195The
196.Fn getrlimit
197function call appeared in
198.Bx 4.2 .
199