1.\" $NetBSD: getrlimit.2,v 1.8 1995/10/12 15:40:58 jtc 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. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)getrlimit.2 8.1 (Berkeley) 6/4/93 35.\" 36.Dd June 4, 1993 37.Dt GETRLIMIT 2 38.Os BSD 4 39.Sh NAME 40.Nm getrlimit , 41.Nm setrlimit 42.Nd control maximum system resource consumption 43.Sh SYNOPSIS 44.Fd #include <sys/resource.h> 45.Ft int 46.Fo getrlimit 47.Fa "int resource" 48.Fa "struct rlimit *rlp" 49.Fc 50.Ft int 51.Fo setrlimit 52.Fa "int resource" 53.Fa "const struct rlimit *rlp" 54.Fc 55.Sh DESCRIPTION 56Limits on the consumption of system resources by the current process 57and each process it creates may be obtained with the 58.Fn getrlimit 59call, and set with the 60.Fn setrlimit 61call. 62.Pp 63The 64.Fa resource 65parameter is one of the following: 66.Bl -tag -width RLIMIT_FSIZEAA 67.\" ======== 68.It Li RLIMIT_CORE 69The largest size (in bytes) 70.Xr core 71file that may be created. 72.\" ======== 73.It Li RLIMIT_CPU 74The maximum amount of cpu time (in seconds) to be used by 75each process. 76.\" ======== 77.It Li RLIMIT_DATA 78The maximum size (in bytes) of the data segment for a process; 79this defines how far a program may extend its break with the 80.Xr sbrk 2 81system call. 82.\" ======== 83.It Li RLIMIT_FSIZE 84The largest size (in bytes) file that may be created. 85.\" ======== 86.It Li RLIMIT_MEMLOCK 87The maximum size (in bytes) which a process may lock into memory 88using the 89.Xr mlock 2 90function. 91.\" ======== 92.It Li RLIMIT_NOFILE 93The maximum number of open files for this process. 94.\" ======== 95.It Li RLIMIT_NPROC 96The maximum number of simultaneous processes for this user id. 97.\" ======== 98.It Li RLIMIT_RSS 99The maximum size (in bytes) 100to which a process's resident set size may grow. 101This imposes a limit on the amount of physical memory 102to be given to a process; 103if memory is tight, the system will prefer to take memory 104from processes that are exceeding their declared resident set size. 105.\" ======== 106.It Li RLIMIT_STACK 107The maximum size (in bytes) of the stack segment for a process; 108this defines how far a program's stack segment may be extended. 109Stack extension is performed automatically by the system. 110.El 111.Pp 112A resource limit is specified as a soft limit and a hard limit. When a 113soft limit is exceeded a process may receive a signal (for example, if 114the cpu time or file size is exceeded), but it will be allowed to 115continue execution until it reaches the hard limit (or modifies 116its resource limit). The 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. Other users 127may 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 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; 137.Ic limit 138is thus a built-in command to 139.Xr csh 1 140and 141.Ic ulimit 142is the 143.Xr sh 1 144equivalent. 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 break 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. When 162the 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. A return value of -1 indicates 169that an error occurred, and an error code is stored in the global 170location 171.Va errno . 172.Sh ERRORS 173The 174.Fn getrlimit 175and 176.Fn setrlimit 177system calls will fail if: 178.Bl -tag -width Er 179.\" ========== 180.It Bq Er EFAULT 181The address specified for 182.Fa rlp 183is invalid. 184.\" ========== 185.It Bq Er EINVAL 186.Fa resource 187is invalid. 188.El 189.Pp 190The 191.Fn setrlimit 192call will fail if: 193.Bl -tag -width Er 194.\" ========== 195.It Bq Er EINVAL 196The specified limit is invalid 197(e.g., RLIM_INFINITY or lower than rlim_cur). 198.\" ========== 199.It Bq Er EPERM 200The limit specified would have raised the maximum limit value 201and the caller is not the super-user. 202.El 203.Sh LEGACY SYNOPSIS 204.Fd #include <sys/types.h> 205.Fd #include <sys/time.h> 206.Fd #include <sys/resource.h> 207.Pp 208The include files 209.In sys/types.h 210and 211.In sys/time.h 212are necessary. 213.Sh COMPATIBILITY 214.Fn setrlimit 215now returns with 216.Va errno 217set to EINVAL in places that historically succeeded. 218It no longer accepts "rlim_cur = RLIM_INFINITY" for RLIM_NOFILE. 219Use "rlim_cur = min(OPEN_MAX, rlim_max)". 220.Sh SEE ALSO 221.Xr csh 1 , 222.Xr sh 1 , 223.Xr quota 2 , 224.Xr sigaction 2 , 225.Xr sigaltstack 2 , 226.Xr sysctl 3 , 227.Xr compat 5 228.Sh HISTORY 229The 230.Fn getrlimit 231function call appeared in 232.Bx 4.2 . 233