#include <sys/time.h> #include <sys/resource.h>
int getrlimit(int resource, struct rlimit *rlp);
int getrlimit64(int resource, struct rlimit64 *rlp);
int setrlimit(int resource, const struct rlimit *rlp);
int setrlimit64(int resource, const struct rlimit64 *rlp);
Limits on the consumption of a variety of system resources by a process and each process it creates may be obtained with getrlimit and set with setrlimit.
Each call to either getrlimit or 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 is less than or equal to the hard limit. A process may (irreversibly) lower its hard limit to any value that is greater than or equal to the soft limit. Only a process with appropriate privilege (P_SYSOPS) 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.
For getrlimit, if a resource limit can be represented correctly in an object of type rlim_t then its representation is returned. Otherwise, if the value of the resource limit is equal to that of the corresponding saved hard limit, the value returned is RLIM_SAVED_MAX, otherwise the value returned is RLIM_SAVED_CUR.
For setrlimit, if the requested new limit is RLIM_INFINITY the new limit will be ``no limit'', otherwise if the requested new limit is RLIM_SAVED_MAX, the new limit will be the corresponding saved hard limit. Otherwise, if the requested new limit is RLIM_SAVED_CUR the new limit will be the corresponding saved soft limit, otherwise the new limit will be the requested value. In addition, if the corresponding saved limit can be represented correctly in an object of type rlim_t then it will be 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 that can be represented correctly in an object of type rlim_t is either ``no limit'', which is represented with RLIM_INFINITY, or RLIM_SAVED_MAX or RLIM_SAVED_CUR and which can be represented correctly in an object of type rlim_t and which meets any additional implementation-specific criteria for correct representation.
The determination of whether a limit can be correctly represented in an object of type rlim_t is implementation-dependent. For example, some implementations permit a limit whose value is greater than RLIM_INFINITY and others do not.
Limits may have an infinite value of RLIM_INFINITY.
For getrlimit and setrlimit, rlp is a pointer to struct rlimit that includes the following members:
rlim_t rlim_cur; /* current (soft) limit */ rlim_t rlim_max; /* hard limit */
For getrlimit64 and setrlimit64, rlp is a pointer to struct rlimit64 that includes the following members:
rlim64_t rlim_cur; /* current (soft) limit */ rlim64_t rlim_max; /* hard limit */
In addition, getrlimit64 and setrlimit64 return the following values, which are defined in the sys/resource.h header.
rlim_t is an arithmetic data type to which objects of type
int, size_t, and off_t can be cast without loss of information.
The possible resources, their descriptions, and the actions taken when current limit is exceeded, are summarized in the table below:
|RLIMIT_CORE||The maximum size of a core file in bytes that may be created by a process. A limit of 0 will prevent the creation of a core file.||The writing of a core file will terminate at this size.|
|RLIMIT_CPU||The maximum amount of CPU time in seconds used by a process.||SIGXCPU is sent to the process. If the process is holding or ignoring SIGXCPU, the behavior is scheduling class defined.|
|RLIMIT_DATA||The maximum size of a process's heap in bytes.||brk(S) will fail with errno set to ENOMEM.|
|RLIMIT_FSIZE||The maximum size of a file in bytes that may be created by a process.||SIGXFSZ 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 will fail with errno set to EFBIG.|
|RLIMIT_NOFILE||The maximum number of open file descriptors that the process can have.||Functions that create new file descriptors will fail with errno set to EMFILE.|
|RLIMIT_STACK||The maximum size of a process's single ``autogrow'' stack in bytes. The system will not automatically grow the stack beyond this limit.||SIGSEGV is sent to the offending thread. If that thread is holding SIGSEGV or if the containing process is ignoring SIGSEGV or is catching SIGSEGV and the underlying LWP has not made arrangements to use an alternate stack [see sigaltstack(S)], the disposition of SIGSEGV will be set to SIG_DFL before it is sent. Consequently, the containing process is terminated.|
|RLIMIT_VMEM||The maximum size of a process's mapped address space in bytes.||brk(S) and mmap(S) functions will fail with errno set to ENOMEM.|
Because limit information is stored in the per-process information, the shell builtin ulimit must directly execute this system call if it is to affect all future processes created by the shell.
The value of the current limit of the following resources affect these implementation defined constants:
|Limit||Implementation Defined Constant|
rlim_curexceeds the new
Also, see description of RLIMIT_STACK above.
Large files (larger than 2GB) are supported in this release. However, file size limits are only discretely settable up to 2GB (more precisely, 2GB - 1 byte). Setting a file size limit greater than or equal to 2GB - 1 has the equivalent effect to setting the file size limit to unlimited. unlimited is currently defined as RLIM_INFINITY (see resource.h).