1.\" $NetBSD: getrlimit.2,v 1.32 2009/07/01 11:25:50 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 July 1, 2009 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.Pp 57The 58.Fa resource 59parameter is one of the following: 60.Bl -tag -width RLIMIT_FSIZEAA 61.It Li RLIMIT_AS 62The maximum amount (in bytes) of virtual memory the process is allowed to map. 63.It Li RLIMIT_CORE 64The largest size (in bytes) 65.Pa core 66file that may be created. 67.It Li RLIMIT_CPU 68The maximum amount of CPU time (in seconds) to be used by 69each process. 70.It Li 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 74system call. 75.It Li RLIMIT_FSIZE 76The largest size (in bytes) file that may be created. 77.It Li RLIMIT_MEMLOCK 78The maximum size (in bytes) which a process may lock into memory 79using the 80.Xr mlock 2 81function. 82.It Li RLIMIT_NOFILE 83The maximum number of open files for this process. 84.It Li RLIMIT_NPROC 85The maximum number of simultaneous processes for this user id. 86.It Li RLIMIT_RSS 87The maximum size (in bytes) to which a process's resident set size may 88grow. 89This imposes a limit on the amount of physical memory to be given to 90a process; if memory is tight, the system will prefer to take memory 91from processes that are exceeding their declared resident set size. 92.It Li RLIMIT_SBSIZE 93The maximum size (in bytes) of the socket buffers 94set by the 95.Xr setsockopt 2 96.Dv SO_RCVBUF 97and 98.Dv SO_SNDBUF 99options. 100.It Li 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.El 105.Pp 106A resource limit is specified as a soft limit and a hard limit. 107When a soft limit is exceeded a process may receive a signal (for example, 108if the CPU time or file size is exceeded), but it will be allowed to 109continue execution until it reaches the hard limit (or modifies 110its resource limit). 111The 112.Em rlimit 113structure is used to specify the hard and soft limits on a resource, 114.Bd -literal -offset indent 115struct rlimit { 116 rlim_t rlim_cur; /* current (soft) limit */ 117 rlim_t rlim_max; /* hard limit */ 118}; 119.Ed 120.Pp 121Only the super-user may raise the maximum limits. 122Other users may only alter 123.Fa rlim_cur 124within the range from 0 to 125.Fa rlim_max 126or (irreversibly) lower 127.Fa rlim_max . 128.Pp 129An 130.Dq infinite 131value for a limit is defined as 132.Dv RLIM_INFINITY . 133.Pp 134Because this information is stored in the per-process information, 135this system call must be executed directly by the shell if it 136is to affect all future processes created by the shell. 137Thus, shells provide built-in commands to change the limits 138.Ic ( limit 139for 140.Xr csh 1 , 141or 142.Ic ulimit 143for 144.Xr sh 1 ) . 145.Pp 146The system refuses to extend the data or stack space when the limits 147would be exceeded in the normal way: a 148.Xr brk 2 149call fails if the data space limit is reached. 150When the stack limit is reached, the process receives 151a segmentation fault 152.Pq Dv SIGSEGV ; 153if this signal is not 154caught by a handler using the signal stack, this signal 155will kill the process. 156.Pp 157A file I/O operation that would create a file larger that the process' 158soft limit will cause the write to fail and a signal 159.Dv SIGXFSZ 160to be 161generated; this normally terminates the process, but may be caught. 162When the soft CPU time limit is exceeded, a signal 163.Dv SIGXCPU 164is sent to the 165offending process. 166.Sh RETURN VALUES 167A 0 return value indicates that the call succeeded, changing 168or returning the resource limit. 169Otherwise, \-1 is returned and the global variable 170.Va errno 171is set to indicate the error. 172.Sh ERRORS 173The 174.Fn getrlimit 175and 176.Fn setrlimit 177will fail if: 178.Bl -tag -width Er 179.It Bq Er EFAULT 180The address specified for 181.Fa rlp 182is invalid. 183.It Bq Er EINVAL 184Specified 185.Fa resource 186was invalid. 187.It Bq Er EINVAL 188In the 189.Fn setrlimit 190call, the specified 191.Fa rlim_cur 192exceeds the specified 193.Fa rlim_max . 194.It Bq Er EPERM 195The limit specified to 196.Fn setrlimit 197would have 198raised the maximum limit value, and the caller is not the super-user. 199.El 200.Pp 201The 202.Fn setrlimit 203function may fail if: 204.Bl -tag -width Er 205.It Bq Er EINVAL 206The limit specified to 207.Fn setrlimit 208cannot be lowered, because current usage is already higher than the limit. 209.El 210.Sh SEE ALSO 211.Xr csh 1 , 212.Xr sh 1 , 213.Xr mlock 2 , 214.Xr quotactl 2 , 215.Xr setsockopt 2 , 216.Xr sigaction 2 , 217.Xr sigaltstack 2 , 218.Xr sysctl 3 219.\" Sh STANDARDS 220.\" With exception of 221.\" .Li RLIMIT_AS 222.\" (which is not currently supported), the 223.\" .Fn getrlimit 224.\" and 225.\" .Fn setrlimit 226.\" functions conform to 227.\" .St -susv2 . 228.Sh HISTORY 229The 230.Fn getrlimit 231function call appeared in 232.Bx 4.2 . 233