#define RLIMIT_CORE 1 // Size of core files, 0 for disabling. #define RLIMIT_CPU 2 // CPU time limit in seconds. #define RLIMIT_FSIZE 4 // Maximum file size in bytes. #define RLIMIT_NOFILE 5 // Maximum number of open file descriptors. #define RLIMIT_STACK 6 // Maximum stack size in bytes. #define RLIMIT_AS 7 // Maximum memory size in bytes. uint64_t getrlimit(int resource); int setrlimit(int resource, uint64_t limit);
This syscall fetches and sets current limits for a specified resource, limits can only be lowered, are inherited from parent to children, and start maxed out.
When a limit is reached, the operation that would reach or exceed it will fail like the following:
RLIMIT_CORE: Core files exceeding this size will be truncated, with 0,
core files are not generated.
RLIMIT_CPU: Once the limit is passed, the process is killed.
RLIMIT_FSIZE: System call growing the file fails with EFBIG.
RLIMIT_NOFILE: Adding a new file descriptor fails with EMFILE.
RLIMIT_STACK: The value is used for the size of created stacks, thus
there is no failure condition.
RLIMIT_AS: mmap or other virtual memory allocation syscalls will
fail with ENOMEM.
setrlimit returns 0 on success or -1 on failure. For
getrlimit, 0 is a valid return so checking errno is necessary.
Both report the following errno:
EINVAL: Invalid value for resource.
EPERM: MAC did not allow the operation.