1.\" $NetBSD: getrlimit.2,v 1.36 2012/06/09 02:36:25 christos 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 June 8, 2012 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_NTHR 87The maximum number of simultaneous threads (Lightweight Processes) for this 88user id. 89Kernel threads and the first thread of each process are not counted against 90this limit. 91.It Li RLIMIT_RSS 92The maximum size (in bytes) to which a process's resident set size may 93grow. 94This imposes a limit on the amount of physical memory to be given to 95a process; if memory is tight, the system will prefer to take memory 96from processes that are exceeding their declared resident set size. 97.It Li RLIMIT_SBSIZE 98The maximum size (in bytes) of the socket buffers 99set by the 100.Xr setsockopt 2 101.Dv SO_RCVBUF 102and 103.Dv SO_SNDBUF 104options. 105.It Li RLIMIT_STACK 106The maximum size (in bytes) of the stack segment for a process; 107this defines how far a program's stack segment may be extended. 108Stack extension is performed automatically by the system. 109.El 110.Pp 111A resource limit is specified as a soft limit and a hard limit. 112When a soft limit is exceeded a process may receive a signal (for example, 113if the CPU time or file size is exceeded), but it will be allowed to 114continue execution until it reaches the hard limit (or modifies 115its resource limit). 116The 117.Em rlimit 118structure is used to specify the hard and soft limits on a resource, 119.Bd -literal -offset indent 120struct rlimit { 121 rlim_t rlim_cur; /* current (soft) limit */ 122 rlim_t rlim_max; /* hard limit */ 123}; 124.Ed 125.Pp 126Only the super-user may raise the maximum limits. 127Other users may only alter 128.Fa rlim_cur 129within the range from 0 to 130.Fa rlim_max 131or (irreversibly) lower 132.Fa rlim_max . 133.Pp 134An 135.Dq infinite 136value for a limit is defined as 137.Dv RLIM_INFINITY . 138.Pp 139Because this information is stored in the per-process information, 140this system call must be executed directly by the shell if it 141is to affect all future processes created by the shell. 142Thus, shells provide built-in commands to change the limits 143.Ic ( limit 144for 145.Xr csh 1 , 146or 147.Ic ulimit 148for 149.Xr sh 1 ) . 150.Pp 151The system refuses to extend the data or stack space when the limits 152would be exceeded in the normal way: a 153.Xr brk 2 154call fails if the data space limit is reached. 155When the stack limit is reached, the process receives 156a segmentation fault 157.Pq Dv SIGSEGV ; 158if this signal is not 159caught by a handler using the signal stack, this signal 160will kill the process. 161.Pp 162A file I/O operation that would create a file larger that the process' 163soft limit will cause the write to fail and a signal 164.Dv SIGXFSZ 165to be 166generated; this normally terminates the process, but may be caught. 167When the soft CPU time limit is exceeded, a signal 168.Dv SIGXCPU 169is sent to the 170offending process. 171.Sh RETURN VALUES 172A 0 return value indicates that the call succeeded, changing 173or returning the resource limit. 174Otherwise, \-1 is returned and the global variable 175.Va errno 176is set to indicate the error. 177.Sh ERRORS 178The 179.Fn getrlimit 180and 181.Fn setrlimit 182will fail if: 183.Bl -tag -width Er 184.It Bq Er EFAULT 185The address specified for 186.Fa rlp 187is invalid. 188.It Bq Er EINVAL 189Specified 190.Fa resource 191was invalid; or, in the 192.Fn setrlimit 193call, the specified 194.Fa rlim_cur 195exceeds the specified 196.Fa rlim_max . 197.It Bq Er EPERM 198The limit specified to 199.Fn setrlimit 200would have 201raised the maximum limit value, and the caller is not the super-user. 202.El 203.Pp 204The 205.Fn setrlimit 206function may fail if: 207.Bl -tag -width Er 208.It Bq Er EINVAL 209The limit specified to 210.Fn setrlimit 211cannot be lowered, because current usage is already higher than the limit. 212.El 213.Sh SEE ALSO 214.Xr csh 1 , 215.Xr sh 1 , 216.Xr mlock 2 , 217.Xr setsockopt 2 , 218.Xr sigaction 2 , 219.Xr sigaltstack 2 , 220.Xr libquota 3 , 221.Xr sysctl 3 222.\" Sh STANDARDS 223.\" With exception of 224.\" .Li RLIMIT_AS 225.\" (which is not currently supported), the 226.\" .Fn getrlimit 227.\" and 228.\" .Fn setrlimit 229.\" functions conform to 230.\" .St -susv2 . 231.Sh HISTORY 232The 233.Fn getrlimit 234function call appeared in 235.Bx 4.2 . 236.Sh BUGS 237The resource limit 238.Dv RLIMIT_RSS 239is not implemented in 240.Xr uvm 9 241which means that process memory size limits are not enforced. 242