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