setrlimit() — Control maximum resource consumption
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4.2 |
both |
Format
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/resource.h>
int setrlimit(int resource, const struct rlimit *rlp);
General description
The setrlimit() function sets resource limits for the calling process. A resource limit is a pair of values; one specifying the current (soft) limit, the other a maximum (hard) limit.
The soft limit may be modified to any value that is less than or equal to the hard limit. For certain resource values, (RLIMIT_CPU, RLIMIT_NOFILE, RLIMIT_AS), the soft limit cannot be set lower than the existing usage.
The hard limit may be lowered to any value that is greater than or equal to the soft limit. The hard limit can be raised only by a process which has superuser authority. Both the soft limit and hard limit can be changed by a single call to setrlimit().
The value RLIM_INFINITY defined in <sys/resource.h>, is considered to be larger than any other limit value. If a call to getrlimit() returns RLIM_INFINITY for a resource, it means the implementation does not enforce limits on that resource. Specifying RLIM_INFINITY as any resource limit values on a successful call to setrlimit() inhibits enforcement of that resource limit.
- RLIMIT_CORE
- The maximum size of a dump of memory (in bytes) allowed for the process. A value of 0 (zero) prevents file creation. Dump file creation will stop at this limit. .
- RLIMIT_CPU
- The maximum amount of CPU time (in seconds) allowed for the process. If the limit is exceeded, a SIGXCPU signal is sent to the process and the process is granted a small CPU time extension to allow for signal generation and delivery. If the extension is used up, the process is terminated with a SIGKILL signal. An attempt to set the CPU limit lower than that already used will result in an EINVAL errno.
- RLIMIT_DATA
- The maximum size of the break value for the process, in bytes. In this implementation, this resource always has a hard and soft limit value of RLIM_INFINITY. A call to setrlimit() to set this resource to any value other than RLIM_INFINITY will fail with an errno of EINVAL.
- RLIMIT_FSIZE
- The maximum file size (in bytes) allowed for the process. A value of 0 (zero) prevents file creation. If the size is exceeded, a SIGXFSZ signal is sent to the process. If the process is blocking, catching, or ignoring SIGXFSZ, continued attempts to increase the size of a file beyond the limit will fail with an errno of EFBIG.
- RLIMIT_MEMLIMIT
- The maximum amount of usable storage above the 2 gigabyte bar (in 1 megabyte segments) that can be allocated. Any attempt to extend the usable amount of virtual storage above the 2 gigabyte bar fails.
- RLIMIT_NOFILE
- The maximum number of open file descriptors allowed for the process.
This number is one greater than the maximum value that may be assigned
to a newly created descriptor. (That is, it is one-based.) Any function
that attempts to create a new file descriptor beyond the limit will
fail with an EMFILE errno. An attempt to set the open file descriptors
limit lower than that already used will result in an EINVAL errno. Restrictions: This value may not exceed 524288.
- RLIMIT_STACK
- The maximum size of the stack for a process, in bytes. Note that in z/OS® UNIX services, the stack is a per-thread resource. In this implementation, this resource always has a hard and soft limit value of RLIM_INFINITY. A call to setrlimit() to set this resource to any value other than RLIM_INFINITY will fail with an errno of EINVAL.
- RLIMIT_AS
- The maximum address space size for the process, in bytes. If the limit is exceeded, malloc() and mmap() functions will fail with an errno of ENOMEM. Also, automatic stack growth will fail.
- rlim_cur
- The current (soft) limit
- rlim_max
- The maximum (hard) limit
Refer to the <sys/resource.h> header for more detail.
The resource limit values are propagated across exec and fork.
Special behavior for z/OS UNIX Services: An exception exists for exec processing in conjunction with daemon support. If a daemon process invokes exec and it had previously invoked setuid() before exec, the RLIMIT_CPU, RLIMIT_AS, RLIMIT_CORE, RLIMIT_FSIZE, and RLIMIT_NOFILE limit values are set based on the limit values specified in the kernel parmlib member BPXPRMxx.
For processes which are not the only process within an address space, the RLIMIT_CPU and RLIMIT_AS limits are shared with all the processes within the address space. For RLIMIT_CPU, when the soft limit is exceeded, action will be taken on the first process within the address space. If the action is termination, all processes within the address space will be terminated.
In addition to the RLIMIT_CORE limit values, the dump file defaults are set by SYSMDUMP defaults. Refer to z/OS MVS Initialization and Tuning Reference for more information on setting up SYSMDUMP defaults using the IEADMR00 parmlib member.
Dumps of memory are taken in 4160 byte increments. Therefore, RLIMIT_CORE values affect the size of memory dumps in 4160 byte increments. For example, if the RLIMIT_CORE soft limit value is 4000, the dump will contain no data. If the RLIMIT_CORE soft limit value is 8000, the maximum size of a memory dump is 4160 bytes.
When setting RLIMIT_NOFILE, the hard limit cannot exceed the system defined limit of 524288.
Large file support for z/OS UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well.
Returned value
If successful, setrlimit() returns 0.
- Error Code
- Description
- EINVAL
- An invalid resource was specified, or the soft limit to set exceeds the hard limit to set, the soft limit to set is below the current usage, or the resource does not allow any value other than RLIM_INFINITY.
- EPERM
- The limit specified to setrlimit() would have raised the maximum limit value, and the calling process does not have appropriate privileges.
Related information
- stropts.h
- sys/resource.h
- brk() — Change space allocation
- fork() — Create a new process
- getdtablesize() — Get the file descriptor table size
- getrlimit() — Get current or maximum resource consumption
- malloc() — Reserve storage block
- open() — Open a file
- rexec() — Execute commands one at a time on a remote host
- sigaltstack() — Set or get signal alternate stack context
- sysconf() — Determine system configuration options
- ulimit() — Get or set process file size limits