GETRLIMIT(2)

πŸ’– GETRLIMIT(2) β€” the getrlimit / setrlimit page, but make it make sense πŸ’–

okay bestie, so this is a System Calls Manual page, and it's about two little functions that control how much of the system's stuff your process is allowed to eat. welcome to resource limits 101, you're gonna slay this 🫢


✨ NAME ✨

getrlimit, setrlimit β€” control maximum system resource consumption

like, literally. these two are the gatekeepers of how much CPU, memory, files, etc. your process gets to consume. main character energy fr fr πŸ‘‘


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)


πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

int
getrlimit(int resource, struct rlimit *rlp);

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

🌸 DESCRIPTION 🌸

So here's the vibe: every process has limits on how much of the system's resources it (and every process it spawns, lowkey) is allowed to consume. You read them with the getrlimit() system call, and you change them with setrlimit(). that's the whole plot bestie πŸ’…

The resource argument is one of these, and no, you can't just pick vibes β€” pick a real one:

  • RLIMIT_AS β€” the max amount (in bytes) of virtual memory the process is allowed to map.
  • RLIMIT_CORE β€” the largest size (in bytes) core(5) file that may be created.
  • RLIMIT_CPU β€” the max amount of cpu time (in seconds) each process gets to use.
  • RLIMIT_DATA β€” the max size (in bytes) of the data segment for a process; this is literally what defines how far a program may extend its break with the sbrk(2) function.
  • RLIMIT_FSIZE β€” the largest size (in bytes) file that may be created.
  • RLIMIT_KQUEUES β€” the max number of kqueues this user id is allowed to create.
  • RLIMIT_MEMLOCK β€” the max size (in bytes) a process may lock into memory using the mlock(2) system call.
  • RLIMIT_NOFILE β€” the max number of open files for this process.
  • RLIMIT_NPROC β€” the max number of simultaneous processes for this user id.
  • RLIMIT_NPTS β€” the max number of pseudo-terminals this user id is allowed to create.
  • RLIMIT_RSS β€” when there's memory pressure and swap is available, prioritize eviction of a process' resident pages beyond this amount (in bytes). when memory is NOT under pressure, this rlimit is effectively ignored, like it said "not today". even when there IS memory pressure, the amount of available swap space and some sysctl settings like vm.swap_enabled and vm.swap_idle_enabled can affect what happens to processes that have exceeded this size.

Processes that exceed their set RLIMIT_RSS are not signalled or halted. it's just a hint to the VM daemon to prefer to deactivate pages from processes that have exceeded their set RLIMIT_RSS. no drama, no consequences, just a polite suggestion πŸ’…

  • RLIMIT_SBSIZE β€” the max size (in bytes) of socket buffer usage for this user. this limits the amount of network memory, and therefore the amount of mbufs, that this user may hold at any time.
  • RLIMIT_STACK β€” the max size (in bytes) of the stack segment for a process; this defines how far a program's stack segment may be extended. stack extension is performed automatically by the system, she does it herself ✨
  • RLIMIT_SWAP β€” the max size (in bytes) of the swap space that may be reserved or used by all of this user id's processes. heads up: this limit is enforced only if bit 1 of the vm.overcommit sysctl is set. please see tuning(7) for a complete description of this sysctl.
  • RLIMIT_VMEM β€” an alias for RLIMIT_AS. same girl, different name πŸŽ€

🌈 soft limit / hard limit lore 🌈

A resource limit is specified as a soft limit and a hard limit. when a soft limit is exceeded, a process might or might not receive a signal β€” it's giving mystery, it's giving "depends". for example, signals are generated when the cpu time or file size is exceeded, but not if the address space or RSS limit is exceeded.

A program that exceeds the soft limit is allowed to continue execution until it reaches the hard limit, or modifies its own resource limit. and even reaching the hard limit does not necessarily halt a process β€” for example, if the RSS hard limit is exceeded, nothing happens. iconic, honestly 😌


🧚 the rlimit struct 🧚

The rlimit structure is used to specify the hard and soft limits on a resource.

struct rlimit {
        rlim_t  rlim_cur;       /* current (soft) limit */
        rlim_t  rlim_max;       /* maximum value for rlim_cur */
};

πŸ‘‘ who's allowed to do what πŸ‘‘

Only the super-user may raise the maximum limits. everyone else may only alter rlim_cur within the range from 0 to rlim_max, or (and this part is irreversible, so be careful bestie) lower rlim_max.

An "infinite" value for a limit is defined as RLIM_INFINITY. no cap, literally 🌌


🐚 shell girlies, this one's for you 🐚

Because this information is stored in the per-process information, this system call must be executed directly by the shell if it is to affect all future processes created by the shell; limit is thus a built-in command to csh(1). iykyk πŸ’…


😭 what actually happens when you hit the limit 😭

The system refuses to extend the data or stack space when the limits would be exceeded in the normal way: a brk(2) function fails if the data space limit is reached.

When the stack limit is reached, the process receives a segmentation fault (SIGSEGV); if this signal is not caught by a handler using the signal stack, this signal will kill the process. like, actually kill it. rip πŸͺ¦

A file I/O operation that would create a file larger that the process' soft limit will cause the write to fail and a signal SIGXFSZ to be generated; this normally terminates the process, but may be caught.

When the soft cpu time limit is exceeded, a SIGXCPU signal is sent to the offending process. caught in 4k πŸ“Έ


🫢 the RLIMIT_AS exception pipeline 🫢

When most operations would allocate more virtual memory than allowed by the soft limit of RLIMIT_AS, the operation fails with ENOMEM and no signal is raised. a notable exception is stack extension, described above. if stack extension would allocate more virtual memory than allowed by the soft limit of RLIMIT_AS, a SIGSEGV signal will be delivered.

The caller is free to raise the soft address space limit up to the hard limit and retry the allocation. so like... you can try again, bestie, just aim higher πŸ’–


πŸ’€ RETURN VALUES πŸ’€

Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error. classic.


⭐ ERRORS ⭐

The getrlimit() and setrlimit() system calls will fail if:

  • [EFAULT] β€” The address specified for rlp is invalid.
  • [EPERM] β€” The limit specified to setrlimit() would have raised the maximum limit value, and the caller is not the super-user.

🌸 SEE ALSO 🌸

csh(1), quota(1), quotactl(2), sigaction(2), sigaltstack(2), sysctl(3), ulimit(3)


πŸ¦‹ HISTORY πŸ¦‹

The getrlimit() system call appeared in 4.2BSD. a true OG ✨


okay that's the whole page bestie, you ate it, no crumbs left πŸ“ this one's a Debian classic, dated September 30, 2016, and it's the GETRLIMIT(2) page β€” go forth and set your limits responsibly, mwah πŸ’œπŸ‘‘

πŸ“¦ where this page comes from