xref: /original-bsd/lib/libc/sys/getrlimit.2 (revision 89e46f9f)
1.\" Copyright (c) 1980, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)getrlimit.2	6.8 (Berkeley) 04/23/92
7.\"
8.Dd
9.Dt GETRLIMIT 2
10.Os BSD 4
11.Sh NAME
12.Nm getrlimit ,
13.Nm setrlimit
14.Nd control maximum system resource consumption
15.Sh SYNOPSIS
16.Fd #include <sys/time.h>
17.Fd #include <sys/resource.h>
18.Ft int
19.Fn getrlimit "int resource" "struct rlimit *rlp"
20.Ft int
21.Fn setrlimit "int resource" "struct rlimit *rlp"
22.Sh DESCRIPTION
23Limits on the consumption of system resources by the current process
24and each process it creates may be obtained with the
25.Fn getrlimit
26call, and set with the
27.Fn setrlimit
28call.
29.Pp
30The
31.Fa resource
32parameter is one of the following:
33.Bl -tag -width RLIMIT_FSIZEAA
34.It Dv RLIMIT_CPU
35the maximum amount of cpu time (in seconds) to be used by
36each process.
37.It Dv RLIMIT_FSIZE
38the largest size, in bytes, of any single file that may be created.
39.It Dv RLIMIT_DATA
40the maximum size, in bytes, of the data segment for a process;
41this defines how far a program may extend its break with the
42.Xr sbrk 2
43system call.
44.It Dv RLIMIT_STACK
45the maximum size, in bytes, of the stack segment for a process;
46this defines how far a program's stack segment may be extended.
47Stack extension is performed automatically by the system.
48.It Dv RLIMIT_CORE
49the largest size, in bytes, of a
50.Xr core
51file that may be created.
52.It Dv RLIMIT_RSS
53the maximum size, in bytes, to which a process's resident set size may
54grow.  This imposes a limit on the amount of physical memory
55to be given to a process; if memory is tight, the system will
56prefer to take memory from processes that are exceeding their
57declared resident set size.
58.El
59.Pp
60A resource limit is specified as a soft limit and a hard limit.  When a
61soft limit is exceeded a process may receive a signal (for example, if
62the cpu time or file size is exceeded), but it will be allowed to
63continue execution until it reaches the hard limit (or modifies
64its resource limit).  The
65.Em rlimit
66structure is used to specify the hard and soft limits on a resource,
67.Bd -literal -offset indent
68struct rlimit {
69	int	rlim_cur;	/* current (soft) limit */
70	int	rlim_max;	/* hard limit */
71};
72.Ed
73.Pp
74Only the super-user may raise the maximum limits.  Other users
75may only alter
76.Fa rlim_cur
77within the range from 0 to
78.Fa rlim_max
79or (irreversibly) lower
80.Fa rlim_max .
81.Pp
82An
83.Dq infinite
84value for a limit is defined as
85.Dv RLIM_INFINITY
86(0x7\&f\&f\&f\&f\&f\&f\&f).
87.Pp
88Because this information is stored in the per-process information,
89this system call must be executed directly by the shell if it
90is to affect all future processes created by the shell;
91.Ic limit
92is thus a built-in command to
93.Xr csh 1 .
94.Pp
95The system refuses to extend the data or stack space when the limits
96would be exceeded in the normal way: a
97.Xr break
98call fails if the data space limit is reached.
99When the stack limit is reached, the process receives
100a segmentation fault
101.Pq Dv SIGSEGV ;
102if this signal is not
103caught by a handler using the signal stack, this signal
104will kill the process.
105.Pp
106A file I/O operation that would create a file larger that the process'
107soft limit will cause the write to fail and a signal
108.Dv SIGXFSZ
109to be
110generated; this normally terminates the process, but may be caught.  When
111the soft cpu time limit is exceeded, a signal
112.Dv SIGXCPU
113is sent to the
114offending process.
115.Sh RETURN VALUES
116A 0 return value indicates that the call succeeded, changing
117or returning the resource limit.   A return value of -1 indicates
118that an error occurred, and an error code is stored in the global
119location
120.Va errno .
121.Sh ERRORS
122.Fn Getrlimit
123and
124.Fn setrlimit
125will fail if:
126.Bl -tag -width Er
127.It Bq Er EFAULT
128The address specified for
129.Fa rlp
130is invalid.
131.It Bq Er EPERM
132The limit specified to
133.Fn setrlimit
134would have
135raised the maximum limit value, and the caller is not the super-user.
136.El
137.Sh SEE ALSO
138.Xr csh 1 ,
139.Xr quota 2 ,
140.Xr sigaltstack 2 ,
141.Xr sigvec 2
142.Sh BUGS
143There should be
144.Ic limit
145and
146.Ic unlimit
147commands in
148.Xr sh 1
149as well as in
150.Xr csh .
151.Sh HISTORY
152The
153.Nm
154function call appeared in
155.Bx 4.2 .
156