-
Notifications
You must be signed in to change notification settings - Fork 0
/
prlimit64.html
777 lines (613 loc) · 26.9 KB
/
prlimit64.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:26:10 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>GETRLIMIT</title>
</head>
<body>
<h1 align="center">GETRLIMIT</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#VERSIONS">VERSIONS</a><br>
<a href="#ATTRIBUTES">ATTRIBUTES</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">getrlimit,
setrlimit, prlimit - get/set resource limits</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<sys/time.h> <br>
#include <sys/resource.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
getrlimit(int</b> <i>resource</i><b>, struct rlimit
*</b><i>rlim</i><b>); <br>
int setrlimit(int</b> <i>resource</i><b>, const struct
rlimit *</b><i>rlim</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
prlimit(pid_t</b> <i>pid</i><b>, int</b> <i>resource</i><b>,
const struct rlimit *</b><i>new_limit</i><b>, <br>
struct rlimit *</b><i>old_limit</i><b>);</b></p>
<p style="margin-left:5%; margin-top: 1em">Feature Test
Macro Requirements for glibc (see
<b>feature_test_macros</b>(7)):</p>
<p style="margin-left:11%; margin-top: 1em"><b>prlimit</b>():
_GNU_SOURCE</p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The
<b>getrlimit</b>() and <b>setrlimit</b>() system calls get
and set resource limits. Each resource has an associated
soft and hard limit, as defined by the <i>rlimit</i>
structure:</p>
<p style="margin-left:17%; margin-top: 1em">struct rlimit {
<br>
rlim_t rlim_cur; /* Soft limit */ <br>
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
<br>
};</p>
<p style="margin-left:11%; margin-top: 1em">The soft limit
is the value that the kernel enforces for the corresponding
resource. The hard limit acts as a ceiling for the soft
limit: an unprivileged process may set only its soft limit
to a value in the range from 0 up to the hard limit, and
(irreversibly) lower its hard limit. A privileged process
(under Linux: one with the <b>CAP_SYS_RESOURCE</b>
capability in the initial user namespace) may make arbitrary
changes to either limit value.</p>
<p style="margin-left:11%; margin-top: 1em">The value
<b>RLIM_INFINITY</b> denotes no limit on a resource (both in
the structure returned by <b>getrlimit</b>() and in the
structure passed to <b>setrlimit</b>()).</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>resource</i> argument must be one of: <b><br>
RLIMIT_AS</b></p>
<p style="margin-left:22%;">This is the maximum size of the
process’s virtual memory (address space). The limit is
specified in bytes, and is rounded down to the system page
size. This limit affects calls to <b>brk</b>(2),
<b>mmap</b>(2), and <b>mremap</b>(2), which fail with the
error <b>ENOMEM</b> upon exceeding this limit. In addition,
automatic stack expansion fails (and generates a
<b>SIGSEGV</b> that kills the process if no alternate stack
has been made available via <b>sigaltstack</b>(2)). Since
the value is a <i>long</i>, on machines with a 32-bit
<i>long</i> either this limit is at most 2 GiB, or this
resource is unlimited.</p>
<p style="margin-left:11%;"><b>RLIMIT_CORE</b></p>
<p style="margin-left:22%;">This is the maximum size of a
<i>core</i> file (see <b>core</b>(5)) in bytes that the
process may dump. When 0 no core dump files are created.
When nonzero, larger dumps are truncated to this size.</p>
<p style="margin-left:11%;"><b>RLIMIT_CPU</b></p>
<p style="margin-left:22%;">This is a limit, in seconds, on
the amount of CPU time that the process can consume. When
the process reaches the soft limit, it is sent a
<b>SIGXCPU</b> signal. The default action for this signal is
to terminate the process. However, the signal can be caught,
and the handler can return control to the main program. If
the process continues to consume CPU time, it will be sent
<b>SIGXCPU</b> once per second until the hard limit is
reached, at which time it is sent <b>SIGKILL</b>. (This
latter point describes Linux behavior. Implementations vary
in how they treat processes which continue to consume CPU
time after reaching the soft limit. Portable applications
that need to catch this signal should perform an orderly
termination upon first receipt of <b>SIGXCPU</b>.)</p>
<p style="margin-left:11%;"><b>RLIMIT_DATA</b></p>
<p style="margin-left:22%;">This is the maximum size of the
process’s data segment (initialized data,
uninitialized data, and heap). The limit is specified in
bytes, and is rounded down to the system page size. This
limit affects calls to <b>brk</b>(2), <b>sbrk</b>(2), and
(since Linux 4.7) <b>mmap</b>(2), which fail with the error
<b>ENOMEM</b> upon encountering the soft limit of this
resource.</p>
<p style="margin-left:11%;"><b>RLIMIT_FSIZE</b></p>
<p style="margin-left:22%;">This is the maximum size in
bytes of files that the process may create. Attempts to
extend a file beyond this limit result in delivery of a
<b>SIGXFSZ</b> signal. By default, this signal terminates a
process, but a process can catch this signal instead, in
which case the relevant system call (e.g., <b>write</b>(2),
<b>truncate</b>(2)) fails with the error <b>EFBIG</b>.</p>
<p style="margin-left:11%;"><b>RLIMIT_LOCKS</b> (early
Linux 2.4 only)</p>
<p style="margin-left:22%;">This is a limit on the combined
number of <b>flock</b>(2) locks and <b>fcntl</b>(2) leases
that this process may establish.</p>
<p style="margin-left:11%;"><b>RLIMIT_MEMLOCK</b></p>
<p style="margin-left:22%;">This is the maximum number of
bytes of memory that may be locked into RAM. This limit is
in effect rounded down to the nearest multiple of the system
page size. This limit affects <b>mlock</b>(2),
<b>mlockall</b>(2), and the <b>mmap</b>(2) <b>MAP_LOCKED</b>
operation. Since Linux 2.6.9, it also affects the
<b>shmctl</b>(2) <b>SHM_LOCK</b> operation, where it sets a
maximum on the total bytes in shared memory segments (see
<b>shmget</b>(2)) that may be locked by the real user ID of
the calling process. The <b>shmctl</b>(2) <b>SHM_LOCK</b>
locks are accounted for separately from the per-process
memory locks established by <b>mlock</b>(2),
<b>mlockall</b>(2), and <b>mmap</b>(2) <b>MAP_LOCKED</b>; a
process can lock bytes up to this limit in each of these two
categories.</p>
<p style="margin-left:22%; margin-top: 1em">In Linux
kernels before 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.</p>
<p style="margin-left:11%;"><b>RLIMIT_MSGQUEUE</b> (since
Linux 2.6.8)</p>
<p style="margin-left:22%;">This is a limit on the number
of bytes that can be allocated for POSIX message queues for
the real user ID of the calling process. This limit is
enforced for <b>mq_open</b>(3). Each message queue that the
user creates counts (until it is removed) against this limit
according to the formula:</p>
<p style="margin-left:22%; margin-top: 1em">Since Linux
3.5:</p>
<p style="margin-left:22%; margin-top: 1em">bytes =
attr.mq_maxmsg * sizeof(struct msg_msg) + <br>
min(attr.mq_maxmsg, MQ_PRIO_MAX) * <br>
sizeof(struct posix_msg_tree_node)+ <br>
/* For overhead */ <br>
attr.mq_maxmsg * attr.mq_msgsize; <br>
/* For message data */</p>
<p style="margin-left:22%; margin-top: 1em">Linux 3.4 and
earlier:</p>
<p style="margin-left:22%; margin-top: 1em">bytes =
attr.mq_maxmsg * sizeof(struct msg_msg *) + <br>
/* For overhead */ <br>
attr.mq_maxmsg * attr.mq_msgsize; <br>
/* For message data */</p>
<p style="margin-left:22%; margin-top: 1em">where
<i>attr</i> is the <i>mq_attr</i> structure specified as the
fourth argument to <b>mq_open</b>(3), and the <i>msg_msg</i>
and <i>posix_msg_tree_node</i> structures are
kernel-internal structures.</p>
<p style="margin-left:22%; margin-top: 1em">The
"overhead" addend in the formula accounts for
overhead bytes required by the implementation and ensures
that the user cannot create an unlimited number of
zero-length messages (such messages nevertheless each
consume some system memory for bookkeeping overhead).</p>
<p style="margin-left:11%;"><b>RLIMIT_NICE</b> (since Linux
2.6.12, but see BUGS below)</p>
<p style="margin-left:22%;">This specifies a ceiling to
which the process’s nice value can be raised using
<b>setpriority</b>(2) or <b>nice</b>(2). The actual ceiling
for the nice value is calculated as
<i>20 - rlim_cur</i>. The useful range for this
limit is thus from 1 (corresponding to a nice value of 19)
to 40 (corresponding to a nice value of -20). This unusual
choice of range was necessary because negative numbers
cannot be specified as resource limit values, since they
typically have special meanings. For example,
<b>RLIM_INFINITY</b> typically is the same as -1. For more
detail on the nice value, see <b>sched</b>(7).</p>
<p style="margin-left:11%;"><b>RLIMIT_NOFILE</b></p>
<p style="margin-left:22%;">This specifies a value one
greater than the maximum file descriptor number that can be
opened by this process. Attempts (<b>open</b>(2),
<b>pipe</b>(2), <b>dup</b>(2), etc.) to exceed this limit
yield the error <b>EMFILE</b>. (Historically, this limit was
named <b>RLIMIT_OFILE</b> on BSD.)</p>
<p style="margin-left:22%; margin-top: 1em">Since Linux
4.5, this limit also defines the maximum number of file
descriptors that an unprivileged process (one without the
<b>CAP_SYS_RESOURCE</b> capability) may have "in
flight" to other processes, by being passed across UNIX
domain sockets. This limit applies to the <b>sendmsg</b>(2)
system call. For further details, see <b>unix</b>(7).</p>
<p style="margin-left:11%;"><b>RLIMIT_NPROC</b></p>
<p style="margin-left:22%;">This is a limit on the number
of extant process (or, more precisely on Linux, threads) for
the real user ID of the calling process. So long as the
current number of processes belonging to this
process’s real user ID is greater than or equal to
this limit, <b>fork</b>(2) fails with the error
<b>EAGAIN</b>.</p>
<p style="margin-left:22%; margin-top: 1em">The
<b>RLIMIT_NPROC</b> limit is not enforced for processes that
have either the <b>CAP_SYS_ADMIN</b> or the
<b>CAP_SYS_RESOURCE</b> capability.</p>
<p style="margin-left:11%;"><b>RLIMIT_RSS</b></p>
<p style="margin-left:22%;">This is a limit (in bytes) on
the process’s resident set (the number of virtual
pages resident in RAM). This limit has effect only in Linux
2.4.x, x < 30, and there affects only calls to
<b>madvise</b>(2) specifying <b>MADV_WILLNEED</b>.</p>
<p style="margin-left:11%;"><b>RLIMIT_RTPRIO</b> (since
Linux 2.6.12, but see BUGS)</p>
<p style="margin-left:22%;">This specifies a ceiling on the
real-time priority that may be set for this process using
<b>sched_setscheduler</b>(2) and
<b>sched_setparam</b>(2).</p>
<p style="margin-left:22%; margin-top: 1em">For further
details on real-time scheduling policies, see
<b>sched</b>(7)</p>
<p style="margin-left:11%;"><b>RLIMIT_RTTIME</b> (since
Linux 2.6.25)</p>
<p style="margin-left:22%;">This is a limit (in
microseconds) on the amount of CPU time that a process
scheduled under a real-time scheduling policy may consume
without making a blocking system call. For the purpose of
this limit, each time a process makes a blocking system
call, the count of its consumed CPU time is reset to zero.
The CPU time count is not reset if the process continues
trying to use the CPU but is preempted, its time slice
expires, or it calls <b>sched_yield</b>(2).</p>
<p style="margin-left:22%; margin-top: 1em">Upon reaching
the soft limit, the process is sent a <b>SIGXCPU</b> signal.
If the process catches or ignores this signal and continues
consuming CPU time, then <b>SIGXCPU</b> will be generated
once each second until the hard limit is reached, at which
point the process is sent a <b>SIGKILL</b> signal.</p>
<p style="margin-left:22%; margin-top: 1em">The intended
use of this limit is to stop a runaway real-time process
from locking up the system.</p>
<p style="margin-left:22%; margin-top: 1em">For further
details on real-time scheduling policies, see
<b>sched</b>(7)</p>
<p style="margin-left:11%;"><b>RLIMIT_SIGPENDING</b> (since
Linux 2.6.8)</p>
<p style="margin-left:22%;">This is a limit on the number
of signals that may be queued for the real user ID of the
calling process. Both standard and real-time signals are
counted for the purpose of checking this limit. However, the
limit is enforced only for <b>sigqueue</b>(3); it is always
possible to use <b>kill</b>(2) to queue one instance of any
of the signals that are not already queued to the
process.</p>
<p style="margin-left:11%;"><b>RLIMIT_STACK</b></p>
<p style="margin-left:22%;">This is the maximum size of the
process stack, in bytes. Upon reaching this limit, a
<b>SIGSEGV</b> signal is generated. To handle this signal, a
process must employ an alternate signal stack
(<b>sigaltstack</b>(2)).</p>
<p style="margin-left:22%; margin-top: 1em">Since Linux
2.6.23, this limit also determines the amount of space used
for the process’s command-line arguments and
environment variables; for details, see
<b>execve</b>(2).</p>
<p style="margin-left:11%; margin-top: 1em"><b>prlimit()</b>
<br>
The Linux-specific <b>prlimit</b>() system call combines and
extends the functionality of <b>setrlimit</b>() and
<b>getrlimit</b>(). It can be used to both set and get the
resource limits of an arbitrary process.</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>resource</i> argument has the same meaning as for
<b>setrlimit</b>() and <b>getrlimit</b>().</p>
<p style="margin-left:11%; margin-top: 1em">If the
<i>new_limit</i> argument is a not NULL, then the
<i>rlimit</i> structure to which it points is used to set
new values for the soft and hard limits for <i>resource</i>.
If the <i>old_limit</i> argument is a not NULL, then a
successful call to <b>prlimit</b>() places the previous soft
and hard limits for <i>resource</i> in the <i>rlimit</i>
structure pointed to by <i>old_limit</i>.</p>
<p style="margin-left:11%; margin-top: 1em">The <i>pid</i>
argument specifies the ID of the process on which the call
is to operate. If <i>pid</i> is 0, then the call applies to
the calling process. To set or get the resources of a
process other than itself, the caller must have the
<b>CAP_SYS_RESOURCE</b> capability in the user namespace of
the process whose resource limits are being changed, or the
real, effective, and saved set user IDs of the target
process must match the real user ID of the caller <i>and</i>
the real, effective, and saved set group IDs of the target
process must match the real group ID of the caller.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success,
these system calls return 0. On error, -1 is returned, and
<i>errno</i> is set appropriately.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">A pointer argument points to a
location outside the accessible address space.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The value specified in <i>resource</i> is not valid; or,
for <b>setrlimit</b>() or <b>prlimit</b>():
<i>rlim->rlim_cur</i> was greater than
<i>rlim->rlim_max</i>.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EPERM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>An unprivileged process tried to raise the hard limit;
the <b>CAP_SYS_RESOURCE</b> capability is required to do
this.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EPERM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The caller tried to increase the hard
<b>RLIMIT_NOFILE</b> limit above the maximum defined by
<i>/proc/sys/fs/nr_open</i> (see <b>proc</b>(5))</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EPERM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>(<b>prlimit</b>()) The calling process did not have
permission to set limits for the process specified by
<i>pid</i>.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ESRCH</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Could not find a process with the ID specified in
<i>pid</i>.</p> </td></tr>
</table>
<h2>VERSIONS
<a name="VERSIONS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The
<b>prlimit</b>() system call is available since Linux
2.6.36. Library support is available since glibc 2.13.</p>
<h2>ATTRIBUTES
<a name="ATTRIBUTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">For an
explanation of the terms used in this section, see
<b>attributes</b>(7).</p>
<p align="center" style="margin-top: 1em"><img src="grohtml-509381.png" alt="Image grohtml-509381.png"></p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>getrlimit</b>(),
<b>setrlimit</b>(): POSIX.1-2001, POSIX.1-2008, SVr4,
4.3BSD.</p>
<p style="margin-left:11%; margin-top: 1em"><b>prlimit</b>():
Linux-specific.</p>
<p style="margin-left:11%; margin-top: 1em"><b>RLIMIT_MEMLOCK</b>
and <b>RLIMIT_NPROC</b> derive from BSD and are not
specified in POSIX.1; they are present on the BSDs and
Linux, but on few other implementations. <b>RLIMIT_RSS</b>
derives from BSD and is not specified in POSIX.1; it is
nevertheless present on most implementations.
<b>RLIMIT_MSGQUEUE</b>, <b>RLIMIT_NICE</b>,
<b>RLIMIT_RTPRIO</b>, <b>RLIMIT_RTTIME</b>, and
<b>RLIMIT_SIGPENDING</b> are Linux-specific.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">A child process
created via <b>fork</b>(2) inherits its parent’s
resource limits. Resource limits are preserved across
<b>execve</b>(2).</p>
<p style="margin-left:11%; margin-top: 1em">Resource limits
are per-process attributes that are shared by all of the
threads in a process.</p>
<p style="margin-left:11%; margin-top: 1em">Lowering the
soft limit for a resource below the process’s current
consumption of that resource will succeed (but will prevent
the process from further increasing its consumption of the
resource).</p>
<p style="margin-left:11%; margin-top: 1em">One can set the
resource limits of the shell using the built-in
<i>ulimit</i> command (<i>limit</i> in <b>csh</b>(1)). The
shell’s resource limits are inherited by the processes
that it creates to execute commands.</p>
<p style="margin-left:11%; margin-top: 1em">Since Linux
2.6.24, the resource limits of any process can be inspected
via <i>/proc/[pid]/limits</i>; see <b>proc</b>(5).</p>
<p style="margin-left:11%; margin-top: 1em">Ancient systems
provided a <b>vlimit</b>() function with a similar purpose
to <b>setrlimit</b>(). For backward compatibility, glibc
also provides <b>vlimit</b>(). All new applications should
be written using <b>setrlimit</b>().</p>
<p style="margin-left:11%; margin-top: 1em"><b>C
library/kernel ABI differences</b> <br>
Since version 2.13, the glibc <b>getrlimit</b>() and
<b>setrlimit</b>() wrapper functions no longer invoke the
corresponding system calls, but instead employ
<b>prlimit</b>(), for the reasons described in BUGS.</p>
<p style="margin-left:11%; margin-top: 1em">The name of the
glibc wrapper function is <b>prlimit</b>(); the underlying
system call is <b>prlimit64</b>().</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">In older Linux
kernels, the <b>SIGXCPU</b> and <b>SIGKILL</b> signals
delivered when a process encountered the soft and hard
<b>RLIMIT_CPU</b> limits were delivered one (CPU) second
later than they should have been. This was fixed in kernel
2.6.8.</p>
<p style="margin-left:11%; margin-top: 1em">In 2.6.x
kernels before 2.6.17, a <b>RLIMIT_CPU</b> limit of 0 is
wrongly treated as "no limit" (like
<b>RLIM_INFINITY</b>). Since Linux 2.6.17, setting a limit
of 0 does have an effect, but is actually treated as a limit
of 1 second.</p>
<p style="margin-left:11%; margin-top: 1em">A kernel bug
means that <b>RLIMIT_RTPRIO</b> does not work in kernel
2.6.12; the problem is fixed in kernel 2.6.13.</p>
<p style="margin-left:11%; margin-top: 1em">In kernel
2.6.12, there was an off-by-one mismatch between the
priority ranges returned by <b>getpriority</b>(2) and
<b>RLIMIT_NICE</b>. This had the effect that the actual
ceiling for the nice value was calculated as
<i>19 - rlim_cur</i>. This was fixed in kernel
2.6.13.</p>
<p style="margin-left:11%; margin-top: 1em">Since Linux
2.6.12, if a process reaches its soft <b>RLIMIT_CPU</b>
limit and has a handler installed for <b>SIGXCPU</b>, then,
in addition to invoking the signal handler, the kernel
increases the soft limit by one second. This behavior
repeats if the process continues to consume CPU time, until
the hard limit is reached, at which point the process is
killed. Other implementations do not change the
<b>RLIMIT_CPU</b> soft limit in this manner, and the Linux
behavior is probably not standards conformant; portable
applications should avoid relying on this Linux-specific
behavior. The Linux-specific <b>RLIMIT_RTTIME</b> limit
exhibits the same behavior when the soft limit is
encountered.</p>
<p style="margin-left:11%; margin-top: 1em">Kernels before
2.4.22 did not diagnose the error <b>EINVAL</b> for
<b>setrlimit</b>() when <i>rlim->rlim_cur</i> was greater
than <i>rlim->rlim_max</i>.</p>
<p style="margin-left:11%; margin-top: 1em">Linux
doesn’t return an error when an attempt to set
<b>RLIMIT_CPU</b> has failed, for compatibility reasons.</p>
<p style="margin-left:11%; margin-top: 1em"><b>Representation
of "large" resource limit values on 32-bit
platforms</b> <br>
The glibc <b>getrlimit</b>() and <b>setrlimit</b>() wrapper
functions use a 64-bit <i>rlim_t</i> data type, even on
32-bit platforms. However, the <i>rlim_t</i> data type used
in the <b>getrlimit</b>() and <b>setrlimit</b>() system
calls is a (32-bit) <i>unsigned long</i>. Furthermore, in
Linux, the kernel represents resource limits on 32-bit
platforms as <i>unsigned long</i>. However, a 32-bit data
type is not wide enough. The most pertinent limit here is
<b>RLIMIT_FSIZE</b>, 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
<b>off_t</b> (assuming a program compiled with
<i>_FILE_OFFSET_BITS=64</i>).</p>
<p style="margin-left:11%; margin-top: 1em">To work around
this kernel limitation, if a program tried to set a resource
limit to a value larger than can be represented in a 32-bit
<i>unsigned long</i>, then the glibc <b>setrlimit</b>()
wrapper function silently converted the limit value to
<b>RLIM_INFINITY</b>. In other words, the requested resource
limit setting was silently ignored.</p>
<p style="margin-left:11%; margin-top: 1em">Since version
2.13, glibc works around the limitations of the
<b>getrlimit</b>() and <b>setrlimit</b>() system calls by
implementing <b>setrlimit</b>() and <b>getrlimit</b>() as
wrapper functions that call <b>prlimit</b>().</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The program
below demonstrates the use of <b>prlimit</b>().</p>
<p style="margin-left:11%; margin-top: 1em">#define
_GNU_SOURCE <br>
#define _FILE_OFFSET_BITS 64 <br>
#include <stdio.h> <br>
#include <time.h> <br>
#include <stdlib.h> <br>
#include <unistd.h> <br>
#include <sys/resource.h></p>
<p style="margin-left:11%; margin-top: 1em">#define
errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ <br>
} while (0)</p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char *argv[]) <br>
{ <br>
struct rlimit old, new; <br>
struct rlimit *newp; <br>
pid_t pid;</p>
<p style="margin-left:11%; margin-top: 1em">if (!(argc == 2
|| argc == 4)) { <br>
fprintf(stderr, "Usage: %s <pid>
[<new-soft-limit> " <br>
"<new-hard-limit>]\n", argv[0]); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">pid =
atoi(argv[1]); /* PID of target process */</p>
<p style="margin-left:11%; margin-top: 1em">newp = NULL;
<br>
if (argc == 4) { <br>
new.rlim_cur = atoi(argv[2]); <br>
new.rlim_max = atoi(argv[3]); <br>
newp = &new; <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">/* Set CPU time
limit of target process; retrieve and display <br>
previous limit */</p>
<p style="margin-left:11%; margin-top: 1em">if
(prlimit(pid, RLIMIT_CPU, newp, &old) == -1) <br>
errExit("prlimit-1"); <br>
printf("Previous limits: soft=%lld; hard=%lld\n",
<br>
(long long) old.rlim_cur, (long long) old.rlim_max);</p>
<p style="margin-left:11%; margin-top: 1em">/* Retrieve and
display new CPU time limit */</p>
<p style="margin-left:11%; margin-top: 1em">if
(prlimit(pid, RLIMIT_CPU, NULL, &old) == -1) <br>
errExit("prlimit-2"); <br>
printf("New limits: soft=%lld; hard=%lld\n", <br>
(long long) old.rlim_cur, (long long) old.rlim_max);</p>
<p style="margin-left:11%; margin-top: 1em">exit(EXIT_SUCCESS);
<br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>prlimit</b>(1),
<b>dup</b>(2), <b>fcntl</b>(2), <b>fork</b>(2),
<b>getrusage</b>(2), <b>mlock</b>(2), <b>mmap</b>(2),
<b>open</b>(2), <b>quotactl</b>(2), <b>sbrk</b>(2),
<b>shmctl</b>(2), <b>malloc</b>(3), <b>sigqueue</b>(3),
<b>ulimit</b>(3), <b>core</b>(5), <b>capabilities</b>(7),
<b>cgroups</b>(7), <b>credentials</b>(7),
<b>signal</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>