1GETRLIMIT(2) Linux Programmer's Manual GETRLIMIT(2)
2
3
4
6 getrlimit, setrlimit - get/set resource limits
7
9 #include <sys/time.h>
10 #include <sys/resource.h>
11
12 int getrlimit(int resource, struct rlimit *rlim);
13 int setrlimit(int resource, const struct rlimit *rlim);
14
16 getrlimit() and setrlimit() get and set resource limits respectively.
17 Each resource has an associated soft and hard limit, as defined by the
18 rlimit structure (the rlim argument to both getrlimit() and setr‐
19 limit()):
20
21 struct rlimit {
22 rlim_t rlim_cur; /* Soft limit */
23 rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
24 };
25
26 The soft limit is the value that the kernel enforces for the corre‐
27 sponding resource. The hard limit acts as a ceiling for the soft
28 limit: an unprivileged process may only set its soft limit to a value
29 in the range from 0 up to the hard limit, and (irreversibly) lower its
30 hard limit. A privileged process (under Linux: one with the
31 CAP_SYS_RESOURCE capability) may make arbitrary changes to either limit
32 value.
33
34 The value RLIM_INFINITY denotes no limit on a resource (both in the
35 structure returned by getrlimit() and in the structure passed to setr‐
36 limit()).
37
38 resource must be one of:
39
40 RLIMIT_AS
41 The maximum size of the process's virtual memory (address space)
42 in bytes. This limit affects calls to brk(2), mmap(2) and
43 mremap(2), which fail with the error ENOMEM upon exceeding this
44 limit. Also automatic stack expansion will fail (and generate a
45 SIGSEGV that kills the process if no alternate stack has been
46 made available via sigaltstack(2)). Since the value is a long,
47 on machines with a 32-bit long either this limit is at most 2
48 GiB, or this resource is unlimited.
49
50 RLIMIT_CORE
51 Maximum size of core file. When 0 no core dump files are cre‐
52 ated. When non-zero, larger dumps are truncated to this size.
53
54 RLIMIT_CPU
55 CPU time limit in seconds. When the process reaches the soft
56 limit, it is sent a SIGXCPU signal. The default action for this
57 signal is to terminate the process. However, the signal can be
58 caught, and the handler can return control to the main program.
59 If the process continues to consume CPU time, it will be sent
60 SIGXCPU once per second until the hard limit is reached, at
61 which time it is sent SIGKILL. (This latter point describes
62 Linux 2.2 through 2.6 behaviour. Implementations vary in how
63 they treat processes which continue to consume CPU time after
64 reaching the soft limit. Portable applications that need to
65 catch this signal should perform an orderly termination upon
66 first receipt of SIGXCPU.)
67
68 RLIMIT_DATA
69 The maximum size of the process's data segment (initialized
70 data, uninitialized data, and heap). This limit affects calls
71 to brk() and sbrk(), which fail with the error ENOMEM upon
72 encountering the soft limit of this resource.
73
74 RLIMIT_FSIZE
75 The maximum size of files that the process may create. Attempts
76 to extend a file beyond this limit result in delivery of a
77 SIGXFSZ signal. By default, this signal terminates a process,
78 but a process can catch this signal instead, in which case the
79 relevant system call (e.g., write() truncate()) fails with the
80 error EFBIG.
81
82 RLIMIT_LOCKS (Early Linux 2.4 only)
83 A limit on the combined number of flock() locks and fcntl()
84 leases that this process may establish.
85
86 RLIMIT_MEMLOCK
87 The maximum number of bytes of memory that may be locked into
88 RAM. In effect this limit is rounded down to the nearest multi‐
89 ple of the system page size. This limit affects mlock(2) and
90 mlockall(2) and the mmap(2) MAP_LOCKED operation. Since Linux
91 2.6.9 it also affects the shmctl(2) SHM_LOCK operation, where it
92 sets a maximum on the total bytes in shared memory segments (see
93 shmget(2)) that may be locked by the real user ID of the calling
94 process. The shmctl(2) SHM_LOCK locks are accounted for sepa‐
95 rately from the per-process memory locks established by
96 mlock(2), mlockall(2), and mmap(2) MAP_LOCKED; a process can
97 lock bytes up to this limit in each of these two categories. In
98 Linux kernels before 2.6.9, this limit controlled the amount of
99 memory that could be locked by a privileged process. Since
100 Linux 2.6.9, no limits are placed on the amount of memory that a
101 privileged process may lock, and this limit instead governs the
102 amount of memory that an unprivileged process may lock.
103
104 RLIMIT_MSGQUEUE (Since Linux 2.6.8)
105 Specifies the limit on the number of bytes that can be allocated
106 for POSIX message queues for the real user ID of the calling
107 process. This limit is enforced for mq_open(3). Each message
108 queue that the user creates counts (until it is removed) against
109 this limit according to the formula:
110
111 bytes = attr.mq_maxmsg * sizeof(struct msg_msg *) +
112 attr.mq_maxmsg * attr.mq_msgsize
113
114 where attr is the mq_attr structure specified as the fourth
115 argument to mq_open().
116
117 The first addend in the formula, which includes sizeof(struct
118 msg_msg *) (4 bytes on Linux/x86), ensures that the user cannot
119 create an unlimited number of zero-length messages (such mes‐
120 sages nevertheless each consume some system memory for bookkeep‐
121 ing overhead).
122
123 RLIMIT_NICE (since kernel 2.6.12, but see BUGS below)
124 Specifies a ceiling to which the process's nice value can be
125 raised using setpriority(2) or nice(2). The actual ceiling for
126 the nice value is calculated as 20 - rlim_cur. (This strange‐
127 ness occurs because negative numbers cannot be specified as
128 resource limit values, since they typically have special mean‐
129 ings. For example, RLIM_INFINITY typically is the same as -1.)
130
131 RLIMIT_NOFILE
132 Specifies a value one greater than the maximum file descriptor
133 number that can be opened by this process. Attempts (open(),
134 pipe(), dup(), etc.) to exceed this limit yield the error
135 EMFILE.
136
137 RLIMIT_NPROC
138 The maximum number of processes (or, more precisely on Linux,
139 threads) that can be created for the real user ID of the calling
140 process. Upon encountering this limit, fork() fails with the
141 error EAGAIN.
142
143 RLIMIT_RSS
144 Specifies the limit (in pages) of the process's resident set
145 (the number of virtual pages resident in RAM). This limit only
146 has effect in Linux 2.4.x, x < 30, and there only affects calls
147 to madvise() specifying MADV_WILLNEED.
148
149 RLIMIT_RTPRIO (Since Linux 2.6.12, but see BUGS)
150 Specifies a ceiling on the real-time priority that may be set
151 for this process using sched_setscheduler(2) and sched_set‐
152 param(2).
153
154 RLIMIT_SIGPENDING (Since Linux 2.6.8)
155 Specifies the limit on the number of signals that may be queued
156 for the real user ID of the calling process. Both standard and
157 real-time signals are counted for the purpose of checking this
158 limit. However, the limit is only enforced for sigqueue(2); it
159 is always possible to use kill(2) to queue one instance of any
160 of the signals that are not already queued to the process.
161
162 RLIMIT_STACK
163 The maximum size of the process stack, in bytes. Upon reaching
164 this limit, a SIGSEGV signal is generated. To handle this sig‐
165 nal, a process must employ an alternate signal stack (sigalt‐
166 stack(2)).
167
168 RLIMIT_OFILE is the BSD name for RLIMIT_NOFILE.
169
171 On success, zero is returned. On error, -1 is returned, and errno is
172 set appropriately.
173
175 EFAULT rlim points outside the accessible address space.
176
177 EINVAL resource is not valid; or, for setrlimit(): rlim->rlim_cur was
178 greater than rlim->rlim_max.
179
180 EPERM An unprivileged process tried to use setrlimit() to increase a
181 soft or hard limit above the current hard limit; the
182 CAP_SYS_RESOURCE capability is required to do this. Or, the
183 process tried to use setrlimit() to increase the soft or hard
184 RLIMIT_NOFILE limit above the current kernel maximum (NR_OPEN).
185
187 In older Linux kernels, the SIGXCPU and SIGKILL signals delivered when
188 a process encountered the soft and hard RLIMIT_CPU limits were deliv‐
189 ered one (CPU) second later than they should have been. This was fixed
190 in kernel 2.6.8.
191
192 In 2.6.x kernels before 2.6.17, a RLIMIT_CPU limit of 0 is wrongly
193 treated as "no limit" (like RLIM_INFINITY). Since kernel 2.6.17, set‐
194 ting a limit of 0 does have an effect, but is actually treated as a
195 limit of 1 second.
196
197 A kernel bug means that RLIMIT_RTPRIO does not work in kernel 2.6.12;
198 the problem is fixed in kernel 2.6.13.
199
200 In kernel 2.6.12, there was an off-by-one mismatch between the priority
201 ranges returned by getpriority(2) and RLIMIT_NICE. This had the effect
202 that actual ceiling for the nice value was calculated as 19 - rlim_cur.
203 This was fixed in kernel 2.6.13.
204
205 Kernels before 2.4.22 did not diagnose the error EINVAL for setrlimit()
206 when rlim->rlim_cur was greater than rlim->rlim_max.
207
209 A child process created via fork(2) inherits its parents resource lim‐
210 its. Resource limits are preserved across execve(2).
211
213 SVr4, 4.3BSD, POSIX.1-2001. RLIMIT_MEMLOCK and RLIMIT_NPROC derive
214 from BSD and are not specified in POSIX.1-2001; they are present on the
215 BSDs and Linux, but on few other implementations. RLIMIT_RSS derives
216 from BSD and is not specified in POSIX.1-2001; it is nevertheless
217 present on most implementations. RLIMIT_MSGQUEUE, RLIMIT_NICE,
218 RLIMIT_RTPRIO, and RLIMIT_SIGPENDING are Linux specific.
219
221 dup(2), fcntl(2), fork(2), getrusage(2), mlock(2), mmap(2), open(2),
222 quotactl(2), sbrk(2), shmctl(2), sigqueue(2), malloc(3), ulimit(3),
223 core(5), capabilities(7), signal(7)
224
225
226
227Linux 2.6.13 2005-09-20 GETRLIMIT(2)