getrlimit(2) System Calls Manual getrlimit(2) getrlimit, setrlimit, prlimit - / LIBRARY Standard C library (libc, -lc) #include int getrlimit(int resource, struct rlimit *rlim); int setrlimit(int resource, const struct rlimit *rlim); int prlimit(pid_t pid, int resource, const struct rlimit *_Nullable new_limit, struct rlimit *_Nullable old_limit); glibc (. feature_test_macros(7)): prlimit(): _GNU_SOURCE getrlimit() setrlimit() . , rlimit: struct rlimit { rlim_t rlim_cur; /* */ rlim_t rlim_max; /* ( rlim_cur) */ }; , . : 0 , . ( Linux: CAP_SYS_RESOURCE ) . RLIM_INFINITY ( , getrlimit() , setrlimit()). resource : RLIMIT_AS ( ) . . brk(2), mmap(2) mremap(2), ENOMEM, . ( SIGSEGV, , sigaltstack(2) ). long, 32- long 2 , . RLIMIT_CORE core ( core(5)) , . 0, core . , . RLIMIT_CPU . , SIGX CPU. . , , . , SIGXCPU , , SIGKILL ( Linux. . , , SIGXCPU). RLIMIT_DATA ( , , ). . brk(2), sbrk(2) ( Linux 4.7) mmap(2), ENOMEM . RLIMIT_FSIZE ( ) , . SIGXFSZ. , (, write(2), truncate(2)) EFBIG. RLIMIT_LOCKS (Linux 2.4.0 to Linux 2.4.24) flock(2) fcntl(2), . RLIMIT_MEMLOCK , . , . mlock(2), mlockall(2) mmap(2) MAP_LOCKED. Linux 2.6.9, shmctl(2) SHM_LOCK, ( shmget(2)), . SHM_LOCK shmctl(2) , mlock(2), mlockall(2) mmap(2) MAP_LOCKED; . Before Linux 2.6.9, this limit controlled the amount of memory that could be locked by a privileged process. Since Linux 2.6.9, no limits are placed on the amount of memory that a privileged process may lock, and this limit instead governs the amount of memory that an unprivileged process may lock. RLIMIT_MSGQUEUE ( Linux 2.6.8) , POSIX . mq_open(3). , , ( ) : Linux 3.5: bytes = attr.mq_maxmsg * sizeof(struct msg_msg) + MIN(attr.mq_maxmsg, MQ_PRIO_MAX) * sizeof(struct posix_msg_tree_node)+ /* */ attr.mq_maxmsg * attr.mq_msgsize; /* */ Linux 3.4 : bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) + /* */ attr.mq_maxmsg * attr.mq_msgsize; /* */ attr -- mq_attr, mq_open(3), msg_msg posix_msg_tree_node -- . <<>> (overhead) , ( , , ). RLIMIT_NICE ( Linux 2.6.12, . ) , setpriority(2) nice(2). : 20 - rlim_cur. 1 ( 19) 40 ( -20). - , , , , . , RLIM_INFINITY, -1. sched(7). RLIMIT_NOFILE , 1 , . (open(2), pipe(2), dup(2) ..) EMFILE ( BSD RLIMIT_OFILE). Linux 4.5, , ( CAP_SYS_RESOURCE) <<>> (in flight) UNIX. sendmsg(2). unix(7). RLIMIT_NPROC (extant) (, Linux, ), . , , fork(2) EAGAIN. The RLIMIT_NPROC limit is not enforced for processes that have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability, or run with real user ID 0. RLIMIT_RSS ( ) ( , ). Linux 2.4.x, x < 30, madvise(2) MADV_WILLNEED. RLIMIT_RTPRIO ( Linux 2.6.12, ) , sched_setscheduler(2) sched_setparam(2). sched(7). RLIMIT_RTTIME ( Linux 2.6.25) ( ) , . , . , , , sched_yield(2). SIGXCPU. , , SIGXCPU , , SIGKILL. . sched(7). RLIMIT_SIGPENDING ( Linux 2.6.8) , . . sigqueue(3); kill(2) , . RLIMIT_STACK . SIGSEGV. (sigaltstack(2)). Linux 2.6.23, , ; execve(2). prlimit() prlimit(), Linux setrlimit() getrlimit(). . resource setrlimit() getrlimit(). new_limit NULL, rlimit, , resource. old_limit NULL, prlimit() resource rlimit, old_limit. pid . pid 0, . , CAP_SYS_RESOURCE , , , . On success, these system calls return 0. On error, -1 is returned, and errno is set to indicate the error. EFAULT - . EINVAL resource; setrlimit() prlimit(): rlim->rlim_cur rlim->rlim_max. EPERM ; CAP_SYS_RESOURCE. EPERM RLIMIT_NOFILE, , /proc/sys/fs/nr_open ( proc(5)). EPERM , pid. ESRCH , pid. attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getrlimit(), setrlimit(), | | MT-Safe | |prlimit() | | | +----------------------------+----------------------------------------------------------+--------------------------+ getrlimit() setrlimit() POSIX.1-2008. prlimit() Linux. RLIMIT_MEMLOCK and RLIMIT_NPROC derive from BSD and are not specified in POSIX.1; they are present on the BSDs and Linux, but on few other implementations. RLIMIT_RSS derives from BSD and is not specified in POSIX.1; it is nevertheless present on most implementations. RLIMIT_MSGQUEUE, RLIMIT_NICE, RLIMIT_RTPRIO, RLIMIT_RTTIME, and RLIMIT_SIGPENDING are Linux-specific. getrlimit() setrlimit() POSIX.1-2001, SVr4, 4.3BSD. prlimit() Linux 2.6.36, glibc 2.13. , fork(2), . execve(2). , . ( ). ulimit (limit csh(1)). , . Linux 2.6.24, /proc/pid/limits; proc(5). vlimit() setrlimit() . glibc vlimit(). setrlimit(). C ABI Since glibc 2.13, the glibc getrlimit() and setrlimit() wrapper functions no longer invoke the corresponding system calls, but instead employ prlimit(), for the reasons described in BUGS. glibc prlimit(); prlimit64(). In older Linux kernels, the SIGXCPU and SIGKILL signals delivered when a process encountered the soft and hard RLIMIT_CPU limits were delivered one (CPU) second later than they should have been. This was fixed in Linux 2.6.8. In Linux 2.6.x kernels before Linux 2.6.17, a RLIMIT_CPU limit of 0 is wrongly treated as "no limit" (like RLIM_INFINITY). Since Linux 2.6.17, setting a limit of 0 does have an effect, but is actually treated as a limit of 1 second. A kernel bug means that RLIMIT_RTPRIO does not work in Linux 2.6.12; the problem is fixed in Linux 2.6.13. In Linux 2.6.12, there was an off-by-one mismatch between the priority ranges returned by getpriority(2) and RLIMIT_NICE. This had the effect that the actual ceiling for the nice value was calculated as 19 - rlim_cur. This was fixed in Linux 2.6.13. Linux 2.6.12, RLIMIT_CPU SIGXCPU, , , . , , , , . RLIMIT_CPU , Linux, , ; Linux. Linux RLIMIT_RTTIME , . Kernels before Linux 2.4.22 did not diagnose the error EINVAL for setrlimit() when rlim->rlim_cur was greater than rlim->rlim_max. , Linux RLIMIT_CPU. <<>> 32- The glibc getrlimit() and setrlimit() wrapper functions use a 64-bit rlim_t data type, even on 32-bit platforms. However, the rlim_t data type used in the getrlimit() and setrlimit() system calls is a (32-bit) unsigned long. Furthermore, in Linux, the kernel represents resource limits on 32-bit platforms as unsigned long. However, a 32-bit data type is not wide enough. The most pertinent limit here is RLIMIT_FSIZE, which specifies the maximum size to which a file can grow: to be useful, this limit must be represented using a type that is as wide as the type used to represent file offsets--that is, as wide as a 64-bit off_t (assuming a program compiled with _FILE_OFFSET_BITS=64). , 32- unsigned long, , , glibc setrlimit() RLIM_INFINITY. , . Since glibc 2.13, glibc works around the limitations of the getrlimit() and setrlimit() system calls by implementing setrlimit() and getrlimit() as wrapper functions that call prlimit(). prlimit(). #define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include int main(int argc, char *argv[]) { pid_t pid; struct rlimit old, new; struct rlimit *newp; if (!(argc == 2 || argc == 4)) { fprintf(stderr, "Usage: %s [ " "]\n", argv[0]); exit(EXIT_FAILURE); } pid = atoi(argv[1]); /* PID of target process */ newp = NULL; if (argc == 4) { new.rlim_cur = atoi(argv[2]); new.rlim_max = atoi(argv[3]); newp = &new; } /* Set CPU time limit of target process; retrieve and display previous limit */ if (prlimit(pid, RLIMIT_CPU, newp, &old) == -1) err(EXIT_FAILURE, "prlimit-1"); printf("Previous limits: soft=%jd; hard=%jd\n", (intmax_t) old.rlim_cur, (intmax_t) old.rlim_max); /* Retrieve and display new CPU time limit */ if (prlimit(pid, RLIMIT_CPU, NULL, &old) == -1) err(EXIT_FAILURE, "prlimit-2"); printf("New limits: soft=%jd; hard=%jd\n", (intmax_t) old.rlim_cur, (intmax_t) old.rlim_max); exit(EXIT_SUCCESS); } . prlimit(1), dup(2), fcntl(2), fork(2), getrusage(2), mlock(2), mmap(2), open(2), quotactl(2), sbrk(2), shmctl(2), malloc(3), sigqueue(3), ulimit(3), core(5), capabilities(7), cgroups(7), credentials(7), signal(7) Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . getrlimit(2)