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