1 /* Get/set resource limits. Linux specific syscall.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <sys/resource.h>
20 #include <stddef.h> // needed for NULL to be defined
22 #if defined(__NR_prlimit64) && __WORDSIZE == 32 && !defined(__USE_FILE_OFFSET64)
24 prlimit (__pid_t pid
, enum __rlimit_resource resource
,
25 const struct rlimit
*new_rlimit
, struct rlimit
*old_rlimit
)
27 struct rlimit64 new_rlimit64
;
28 struct rlimit64
*new_rlimit64_ptr
= NULL
;
29 struct rlimit64 old_rlimit64
;
30 struct rlimit64
*old_rlimit64_ptr
= (old_rlimit
!= NULL
? &old_rlimit64
: NULL
);
33 if (new_rlimit
!= NULL
) {
34 if (new_rlimit
->rlim_cur
== RLIM_INFINITY
)
35 new_rlimit64
.rlim_cur
= RLIM64_INFINITY
;
37 new_rlimit64
.rlim_cur
= new_rlimit
->rlim_cur
;
38 if (new_rlimit
->rlim_max
== RLIM_INFINITY
)
39 new_rlimit64
.rlim_max
= RLIM64_INFINITY
;
41 new_rlimit64
.rlim_max
= new_rlimit
->rlim_max
;
42 new_rlimit64_ptr
= &new_rlimit64
;
45 res
= INLINE_SYSCALL (prlimit64
, 4, pid
, resource
, new_rlimit64_ptr
,
48 if (res
== 0 && old_rlimit
!= NULL
) {
49 /* If the syscall succeeds but the values do not fit into a
50 rlimit structure set EOVERFLOW errno and retrun -1.
51 With current Linux implementation of the prlimit64 syscall,
52 overflow can't happen. An extra condition has been added to get
53 the same behavior as in glibc for future potential overflows. */
54 old_rlimit
->rlim_cur
= old_rlimit64
.rlim_cur
;
55 if (old_rlimit64
.rlim_cur
!= old_rlimit
->rlim_cur
) {
56 if (new_rlimit
== NULL
&&
57 old_rlimit64
.rlim_cur
!= RLIM64_INFINITY
) {
58 __set_errno(EOVERFLOW
);
61 old_rlimit
->rlim_cur
= RLIM_INFINITY
;
63 old_rlimit
->rlim_max
= old_rlimit64
.rlim_max
;
64 if (old_rlimit64
.rlim_max
!= old_rlimit
->rlim_max
) {
65 if (new_rlimit
== NULL
&&
66 old_rlimit64
.rlim_max
!= RLIM64_INFINITY
) {
67 __set_errno(EOVERFLOW
);
70 old_rlimit
->rlim_max
= RLIM_INFINITY
;