setrlimit(), setrlimit64()

Updated: April 19, 2023

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 );

Arguments:

resource
The resource whose limit you want to set; one of the following:
  • RLIMIT_AS
  • RLIMIT_CORE
  • RLIMIT_CPU
  • RLIMIT_DATA
  • RLIMIT_FREEMEM
  • RLIMIT_NOCONN_NP
  • RLIMIT_NOFILE
  • RLIMIT_NPROC
  • RLIMIT_NTHR
  • RLIMIT_OFILE
  • RLIMIT_RSS
  • RLIMIT_SIGEVENT_NP
  • RLIMIT_STACK
  • RLIMIT_TIMERS_NP
  • RLIMIT_VMEM
For descriptions and the actions taken when the current limit is exceeded, see prlimit().
rlp
A pointer to a rlimit or rlimit64 structure that specifies the limit that you want to set for the resource. The rlimit and rlimit64 structures include 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.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The setrlimit() and setrlimit64() functions set the limits on the consumption of a variety of system resources by a process and each process it creates. The setrlimit64() function is a large-file support version of setrlimit().

Note: In QNX Neutrino 6.6 or later, the large-file support functions and data types appear in the name space only if you define _LARGEFILE64_SOURCE when you compile your code. For more information, see Classification in What's in a Function Description?

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. The call to setrlimit() in turn calls prlimit() with the old_rlp argument set to NULL.

A process can change soft limits 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 the PROCMGR_AID_RLIMIT ability enabled (see procmgr_ability()) 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.

Note: RLIM_INFINITY is a special value, and its actual numerical value doesn't represent a valid size of virtual memory or the address space.

Because limit information is stored in the per-process information, the shell builtin ulimit command (see the entry for ksh in the Utilities Reference) 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_NOFILE OPEN_MAX

When you're 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 undefined 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 have raised the maximum limit value, and the calling process doesn't have the required permission; see procmgr_ability().

Classification:

setrlimit() is POSIX 1003.1; setrlimit64() is Large-file support

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