GETRLIMIT(3POSIX)

✨ GETRLIMIT(3POSIX) ✨

okay bestie, welcome to the POSIX Programmer's Manual page for getrlimit and setrlimit β€” it's giving resource-control realness πŸ’…

🌸 PROLOG 🌸

okay so first things first, this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so like, consult the corresponding Linux manual page for the deets on Linux behavior, bestie), or honestly the interface may not even be implemented on Linux at all. πŸ™ˆ


πŸ‘‘ NAME πŸ‘‘

getrlimit, setrlimit β€” control maximum resource consumption. periodt.

🌈 SYNOPSIS 🌈

#include <sys/resource.h>

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

πŸ’– DESCRIPTION πŸ’–

so getrlimit() shall get, and setrlimit() shall set, limits on the consumption of a whole variety of resources. iconic behavior honestly.

every call to either getrlimit() or setrlimit() identifies a specific resource to be operated upon, plus a resource limit. a resource limit is represented by an rlimit structure, and inside it:

  • the rlim_cur member specifies the current or soft limit
  • the rlim_max member specifies the maximum or 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 privileges 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, obviously. 🫢

the value RLIM_INFINITY, defined in <sys/resource.h>, shall be considered to be larger than any other limit value. if a call to getrlimit() returns RLIM_INFINITY for a resource, it means the implementation shall not enforce limits on that resource. specifying RLIM_INFINITY as any resource limit value on a successful call to setrlimit() shall inhibit enforcement of that resource limit. no cap. πŸ’€

πŸ¦‹ the resources, defined πŸ¦‹

  • RLIMIT_CORE β€” this is the maximum size of a core file, in bytes, that may be created by a process. a limit of 0 shall prevent the creation of a core file. if this limit is exceeded, the writing of a core file shall terminate at this size.
  • RLIMIT_CPU β€” this is the maximum amount of CPU time, in seconds, used by a process. if this limit is exceeded, SIGXCPU shall be generated for the process. if the process is catching or ignoring SIGXCPU, or all threads belonging to that process are blocking SIGXCPU, the behavior is unspecified. 😭
  • RLIMIT_DATA β€” this is the maximum size of a data segment of the process, in bytes. if this limit is exceeded, the malloc() function shall fail with errno set to [ENOMEM].
  • RLIMIT_FSIZE β€” this is the maximum size of a file, in bytes, that may be created by a process. if a write or truncate operation would cause this limit to be exceeded, SIGXFSZ shall be generated for the thread. if the thread is blocking, or the process is catching or ignoring SIGXFSZ, continued attempts to increase the size of a file from end-of-file to beyond the limit shall fail with errno set to [EFBIG].
  • RLIMIT_NOFILE β€” this is a number one greater than the maximum value that the system may assign to a newly-created descriptor. if this limit is exceeded, functions that allocate a file descriptor shall fail with errno set to [EMFILE]. this limit constrains the number of file descriptors that a process may allocate.
  • RLIMIT_STACK β€” this is the maximum size of the initial thread's stack, in bytes. the implementation does not automatically grow the stack beyond this limit. if this limit is exceeded, SIGSEGV shall be generated for the thread. if the thread is blocking SIGSEGV, or the process is ignoring or catching SIGSEGV and has not made arrangements to use an alternate stack, the disposition of SIGSEGV shall be set to SIG_DFL before it is generated.
  • RLIMIT_AS β€” this is the maximum size of total available memory of the process, in bytes. if this limit is exceeded, the malloc() and mmap() functions shall fail with errno set to [ENOMEM]. in addition, the automatic stack growth fails with the effects outlined above.

when using the getrlimit() function, 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 shall be RLIM_SAVED_MAX; otherwise, the value returned shall be RLIM_SAVED_CUR. πŸŽ€

when using the setrlimit() function:

  • if the requested new limit is RLIM_INFINITY, the new limit shall be "no limit"
  • otherwise, if the requested new limit is RLIM_SAVED_MAX, the new limit shall be the corresponding saved hard limit
  • otherwise, if the requested new limit is RLIM_SAVED_CUR, the new limit shall be the corresponding saved soft limit
  • otherwise, the new limit shall be the requested value

in addition, if the corresponding saved limit can be represented correctly in an object of type rlim_t then it shall 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. so like, don't do that, bestie. 🫠

the determination of whether a limit can be correctly represented in an object of type rlim_t is implementation-defined. for example, some implementations permit a limit whose value is greater than RLIM_INFINITY and others do not. it's giving chaos, honestly. πŸ’€

the exec family of functions shall cause resource limits to be saved.

⭐ RETURN VALUE ⭐

upon successful completion, getrlimit() and setrlimit() shall return 0. otherwise, these functions shall return -1 and set errno to indicate the error. slay when it works, sad when it doesn't. 😌

πŸ“ ERRORS πŸ“

the getrlimit() and setrlimit() functions shall fail if:

  • EINVAL β€” an invalid resource was specified; or in a setrlimit() call, the new rlim_cur exceeds the new rlim_max.
  • EPERM β€” the limit specified to setrlimit() would have raised the maximum limit value, and the calling process does not have appropriate privileges.

the setrlimit() function may fail if:

  • EINVAL β€” the limit specified cannot be lowered because current usage is already higher than the limit.
heads up, the following sections are informative. iykyk πŸ’…

🧚 EXAMPLES 🧚

none. yeah bestie, we got nothing. πŸ’€

πŸ’œ APPLICATION USAGE πŸ’œ

  • if a process attempts to set the hard limit or soft limit for RLIMIT_NOFILE to less than the value of {_POSIX_OPEN_MAX} from <limits.h>, unexpected behavior may occur.
  • if a process attempts to set the hard limit or soft limit for RLIMIT_NOFILE to less than the highest currently open file descriptor +1, unexpected behavior may occur.

🌸 RATIONALE 🌸

it should be noted that RLIMIT_STACK applies "at least" to the stack of the initial thread in the process, and not to the sum of all the stacks in the process β€” because that would be very limiting unless the value is so big as to provide no value at all with a single thread. and like, the way that would be so useless? no thank you. πŸ™…

πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

none.

✨ SEE ALSO ✨

exec, fork(), malloc(), open(), sigaltstack(), sysconf(), ulimit()

the Base Definitions volume of POSIX.1‐2017, <stropts.h>, <sys_resource.h>

portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


okay that's the whole page bestie, ily, go set some limits responsibly 🫢✨ this was IEEE/The Open Group, 2017, and the page is GETRLIMIT(3POSIX) β€” bye pookie, drink water πŸ’–πŸŽ€