[Previous] [Contents] [Index] [Next]

setrlimit(), setrlimit64()

Set the limit on a system resource

Synopsis:

#include <sys/resource.h>

int setrlimit( int resource,
               const struct rlimit * rlp );

int setrlimit64( int resource,
                 const struct rlimit64 * rlp );

Library:

libc

Description:

The setrlimit() function sets the limits on the consumption of a variety of system resources by a process and each process it creates. The setrlimit64() function is a 64-bit version of setrlimit().

Each call to setrlimit() identifies a specific resource to be operated upon as well as a resource limit. A resource limit is a pair of values: one specifying the current (soft) limit, the other a maximum (hard) limit. Soft limits may be changed by a process to any value that's less than or equal to the hard limit. A process may (irreversibly) lower its hard limit to any value that's greater than or equal to the soft limit. Only a process with an effective user ID of superuser can raise a hard limit. Both hard and soft limits can be changed in a single call to setrlimit() subject to the constraints described above. Limits may have an "infinite" value of RLIM_INFINITY. The rlp argument is a pointer to an rlimit or rlimit64 structure that includes at least the following members:

rlim_t rlim_cur; /* current (soft) limit */
rlim_t rlim_max; /* hard limit */

The rlim_t type is an arithmetic data type to which you can cast objects of type int, size_t, and off_t without loss of information.

The possible resources, their descriptions, and the actions taken when the current limit is exceeded are summarized below:

Resource Description Action
RLIMIT_CORE The maximum size, in bytes, of a core file that may be created by a process. A limit of 0 prevents the creation of a core file. The writing of a core file terminates at this size.
RLIMIT_CPU The maximum amount of CPU time, in seconds, used by a process. This is a soft limit only. SIGXCPU is sent to the process. If the process is holding or ignoring SIGXCPU, the behavior is defined by the scheduling class.
RLIMIT_DATA The maximum size of a process's heap in bytes The brk() function fails with errno set to ENOMEM.
RLIMIT_FSIZE The maximum size of a file in bytes that may be created by a process. A limit of 0 prevents the creation of a file. The SIGXFSZ signal is sent to the process. If the process is holding or ignoring SIGXFSZ, continued attempts to increase the size of a file beyond the limit fail with errno set to EFBIG.
RLIMIT_NOFILE One more than the maximum value that the system may assign to a newly created descriptor. This limit constrains the number of file descriptors that a process may create.
RLIMIT_STACK The maximum size of a process's stack in bytes. The system will not automatically grow the stack beyond this limit.

Within a process, setrlimit() increases the limit on the size of your stack, but doesn't move current memory segments to allow for that growth. To guarantee that the process stack can grow to the limit, the limit must be altered prior to the execution of the process in which the new stack size is to be used.

Within a multithreaded process, setrlimit() has no impact on the stack size limit for the calling thread if the calling thread isn't the main thread. A call to setrlimit() for RLIMIT_STACK impacts only the main thread's stack, and should be made only from the main thread, if at all.

The SIGSEGV signal is sent to the process. If the process is holding or ignoring SIGSEGV, or is catching SIGSEGV and hasn't made arrangements to use an alternate stack, the disposition of SIGSEGV is set to SIG_DFL before it's sent.
RLIMIT_VMEM The maximum size of a process's mapped address space in bytes. The brk() and mmap() functions fail with errno set to ENOMEM. In addition, the automatic stack growth fails with the effects outlined above.
RLIMIT_AS The maximum size of a process's total available memory, in bytes. If this limit is exceeded, brk(), malloc(), mmap(), and sbrk() fail with errno set to ENOMEM. In addition, the automatic stack growth will fail with the effects outlined above.

Because limit information is stored in the per-process information, the shell builtin ulimit command must directly execute this system call if it's to affect all future processes created by the shell.

The values of the current limit of the following resources affect these parameters:

Resource Parameter
RLIMIT_FSIZE FCHR_MAX
RLIMIT_NOFILE OPEN_MAX

When using the setrlimit() function, if the requested new limit is RLIM_INFINITY, there's no new limit; otherwise, if the requested new limit is RLIM_SAVED_MAX, the new limit is the corresponding saved hard limit; otherwise, if the requested new limit is RLIM_SAVED_CUR, the new limit is the corresponding saved soft limit; otherwise, the new limit is the requested value. In addition, if the corresponding saved limit can be represented correctly in an object of type rlim_t, then it's overwritten with the new limit.

The result of setting a limit to RLIM_SAVED_MAX or RLIM_SAVED_CUR is unspecified unless a previous call to getrlimit() returned that value as the soft or hard limit for the corresponding resource limit.

A limit whose value is greater than RLIM_INFINITY is permitted.

The exec* family of functions also cause resource limits to be saved.

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

EFAULT
The rlp argument points to an illegal address.
EINVAL
An invalid resource was specified, the new rlim_cur exceeds the new rlim_max, or the limit specified can't be lowered because current usage is already higher than the limit.
EPERM
The limit specified to setrlimit() would've raised the maximum limit value, and the effective user of the calling process isn't the superuser.

Classification:

setrlimit() is standard Unix; setrlimit64() is for large-file support

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

brk(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fork(), getdtablesize(), getrlimit(), getrlimit64(), malloc(), open(), signal(), sysconf()


[Previous] [Contents] [Index] [Next]