xref: /csrg-svn/lib/libc/sys/getrlimit.2 (revision 61181)
1*61181Sbostic.\" Copyright (c) 1980, 1991, 1993
2*61181Sbostic.\"	The Regents of the University of California.  All rights reserved.
320157Smckusick.\"
443568Strent.\" %sccs.include.redist.man%
520157Smckusick.\"
6*61181Sbostic.\"     @(#)getrlimit.2	8.1 (Berkeley) 06/04/93
738053Sbostic.\"
847208Scael.Dd
947208Scael.Dt GETRLIMIT 2
1047208Scael.Os BSD 4
1147208Scael.Sh NAME
1247208Scael.Nm getrlimit ,
1347208Scael.Nm setrlimit
1447208Scael.Nd control maximum system resource consumption
1547208Scael.Sh SYNOPSIS
1661071Smckusick.Fd #include <sys/types.h>
1747208Scael.Fd #include <sys/time.h>
1847208Scael.Fd #include <sys/resource.h>
1947208Scael.Ft int
2047208Scael.Fn getrlimit "int resource" "struct rlimit *rlp"
2147208Scael.Ft int
2247208Scael.Fn setrlimit "int resource" "struct rlimit *rlp"
2347208Scael.Sh DESCRIPTION
2420158SmckusickLimits on the consumption of system resources by the current process
2520158Smckusickand each process it creates may be obtained with the
2647208Scael.Fn getrlimit
2720158Smckusickcall, and set with the
2847208Scael.Fn setrlimit
2920158Smckusickcall.
3047208Scael.Pp
3120158SmckusickThe
3247208Scael.Fa resource
3320158Smckusickparameter is one of the following:
3447208Scael.Bl -tag -width RLIMIT_FSIZEAA
3560298Sbostic.It Li RLIMIT_CORE
3660298SbosticThe largest size (in bytes)
3760298Sbostic.Xr core
3860298Sbosticfile that may be created.
3960298Sbostic.It Li RLIMIT_CPU
4060298SbosticThe maximum amount of cpu time (in seconds) to be used by
4120158Smckusickeach process.
4260298Sbostic.It Li RLIMIT_DATA
4360298SbosticThe maximum size (in bytes) of the data segment for a process;
4420158Smckusickthis defines how far a program may extend its break with the
4547208Scael.Xr sbrk 2
4620158Smckusicksystem call.
4760298Sbostic.It Li RLIMIT_FSIZE
4860298SbosticThe largest size (in bytes) file that may be created.
4960298Sbostic.It Li RLIMIT_MEMLOCK
5060298SbosticThe maximum size (in bytes) which a process may lock into memory
5160298Sbosticusing the
5260298Sbostic.Xr mlock 2
5360298Sbosticfunction.
5460298Sbostic.It Li RLIMIT_NOFILE
5560298SbosticThe maximum number of open files for this process.
5660298Sbostic.It Li RLIMIT_NPROC
5760298SbosticThe maximum number of simultaneous processes for this user id.
5860298Sbostic.It Li RLIMIT_RSS
5960298SbosticThe maximum size (in bytes) to which a process's resident set size may
6060298Sbosticgrow.
6160298SbosticThis imposes a limit on the amount of physical memory to be given to
6260298Sbostica process; if memory is tight, the system will prefer to take memory
6360298Sbosticfrom processes that are exceeding their declared resident set size.
6460298Sbostic.It Li RLIMIT_STACK
6560298SbosticThe maximum size (in bytes) of the stack segment for a process;
6628082Skarelsthis defines how far a program's stack segment may be extended.
6728082SkarelsStack extension is performed automatically by the system.
6847208Scael.El
6947208Scael.Pp
7030781SbosticA resource limit is specified as a soft limit and a hard limit.  When a
7130781Sbosticsoft limit is exceeded a process may receive a signal (for example, if
7230781Sbosticthe cpu time or file size is exceeded), but it will be allowed to
7330781Sbosticcontinue execution until it reaches the hard limit (or modifies
7420158Smckusickits resource limit).  The
7547208Scael.Em rlimit
7620158Smckusickstructure is used to specify the hard and soft limits on a resource,
7747208Scael.Bd -literal -offset indent
7820158Smckusickstruct rlimit {
7960298Sbostic	quad_t	rlim_cur;	/* current (soft) limit */
8060298Sbostic	quad_t	rlim_max;	/* hard limit */
8120158Smckusick};
8247208Scael.Ed
8347208Scael.Pp
8420158SmckusickOnly the super-user may raise the maximum limits.  Other users
8520158Smckusickmay only alter
8647208Scael.Fa rlim_cur
8720158Smckusickwithin the range from 0 to
8847208Scael.Fa rlim_max
8920158Smckusickor (irreversibly) lower
9047208Scael.Fa rlim_max .
9147208Scael.Pp
9247208ScaelAn
9347208Scael.Dq infinite
9447208Scaelvalue for a limit is defined as
9560298Sbostic.Dv RLIM_INFINITY .
9647208Scael.Pp
9720158SmckusickBecause this information is stored in the per-process information,
9820157Smckusickthis system call must be executed directly by the shell if it
9920157Smckusickis to affect all future processes created by the shell;
10047208Scael.Ic limit
10120157Smckusickis thus a built-in command to
10247208Scael.Xr csh 1 .
10347208Scael.Pp
10420157SmckusickThe system refuses to extend the data or stack space when the limits
10520158Smckusickwould be exceeded in the normal way: a
10647208Scael.Xr break
10728082Skarelscall fails if the data space limit is reached.
10828082SkarelsWhen the stack limit is reached, the process receives
10947208Scaela segmentation fault
11047208Scael.Pq Dv SIGSEGV ;
11147208Scaelif this signal is not
11228082Skarelscaught by a handler using the signal stack, this signal
11328082Skarelswill kill the process.
11447208Scael.Pp
11530781SbosticA file I/O operation that would create a file larger that the process'
11647208Scaelsoft limit will cause the write to fail and a signal
11747208Scael.Dv SIGXFSZ
11847208Scaelto be
11930781Sbosticgenerated; this normally terminates the process, but may be caught.  When
12047208Scaelthe soft cpu time limit is exceeded, a signal
12147208Scael.Dv SIGXCPU
12247208Scaelis sent to the
12320158Smckusickoffending process.
12447208Scael.Sh RETURN VALUES
12520158SmckusickA 0 return value indicates that the call succeeded, changing
12647208Scaelor returning the resource limit.   A return value of -1 indicates
12720158Smckusickthat an error occurred, and an error code is stored in the global
12847208Scaellocation
12947208Scael.Va errno .
13047208Scael.Sh ERRORS
13147208Scael.Fn Getrlimit
13247208Scaeland
13347208Scael.Fn setrlimit
13447208Scaelwill fail if:
13547208Scael.Bl -tag -width Er
13647208Scael.It Bq Er EFAULT
13747208ScaelThe address specified for
13847208Scael.Fa rlp
13947208Scaelis invalid.
14047208Scael.It Bq Er EPERM
14147208ScaelThe limit specified to
14247208Scael.Fn setrlimit
14347208Scaelwould have
14420158Smckusickraised the maximum limit value, and the caller is not the super-user.
14547208Scael.El
14647208Scael.Sh SEE ALSO
14747208Scael.Xr csh 1 ,
14847208Scael.Xr quota 2 ,
14953256Sbostic.Xr sigaltstack 2 ,
15060298Sbostic.Xr sigvec 2 ,
15160298Sbostic.Xr sysctl 3
15247208Scael.Sh BUGS
15320157SmckusickThere should be
15447208Scael.Ic limit
15520157Smckusickand
15647208Scael.Ic unlimit
15720157Smckusickcommands in
15847208Scael.Xr sh 1
15920157Smckusickas well as in
16047208Scael.Xr csh .
16147208Scael.Sh HISTORY
16247208ScaelThe
16347208Scael.Nm
16447208Scaelfunction call appeared in
16547208Scael.Bx 4.2 .
166