]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/prlimit.c
2fa0642c766bab4f16e79ab7e53133fd14f69fb1
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / prlimit.c
1 /* Copyright (C) 2010-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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 <http://www.gnu.org/licenses/>. */
17
18 #include <errno.h>
19 #include <sys/resource.h>
20 #include <sys/syscall.h>
21
22
23 int
24 prlimit (__pid_t pid, enum __rlimit_resource resource,
25 const struct rlimit *new_rlimit, struct rlimit *old_rlimit)
26 {
27 struct rlimit64 new_rlimit64_mem;
28 struct rlimit64 *new_rlimit64 = NULL;
29 struct rlimit64 old_rlimit64_mem;
30 struct rlimit64 *old_rlimit64 = (old_rlimit != NULL
31 ? &old_rlimit64_mem : NULL);
32
33 if (new_rlimit != NULL)
34 {
35 if (new_rlimit->rlim_cur == RLIM_INFINITY)
36 new_rlimit64_mem.rlim_cur = RLIM64_INFINITY;
37 else
38 new_rlimit64_mem.rlim_cur = new_rlimit->rlim_cur;
39 if (new_rlimit->rlim_max == RLIM_INFINITY)
40 new_rlimit64_mem.rlim_max = RLIM64_INFINITY;
41 else
42 new_rlimit64_mem.rlim_max = new_rlimit->rlim_max;
43 new_rlimit64 = &new_rlimit64_mem;
44 }
45
46 int res = INLINE_SYSCALL (prlimit64, 4, pid, resource, new_rlimit64,
47 old_rlimit64);
48
49 if (res == 0 && old_rlimit != NULL)
50 {
51 /* The prlimit64 syscall is ill-designed for 32-bit machines.
52 We have to provide a 32-bit variant since otherwise the LFS
53 system would not work. The infinity value can be translated,
54 but otherwise what shall we do if the syscall succeeds but the
55 old values do not fit into a rlimit structure? We cannot return
56 an error because the operation itself worked. Best is perhaps
57 to return RLIM_INFINITY. */
58 old_rlimit->rlim_cur = old_rlimit64_mem.rlim_cur;
59 if (old_rlimit->rlim_cur != old_rlimit64_mem.rlim_cur)
60 {
61 if ((new_rlimit == NULL)
62 && (old_rlimit64_mem.rlim_cur != RLIM64_INFINITY))
63 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
64 old_rlimit->rlim_cur = RLIM_INFINITY;
65 }
66 old_rlimit->rlim_max = old_rlimit64_mem.rlim_max;
67 if (old_rlimit->rlim_max != old_rlimit64_mem.rlim_max)
68 {
69 if ((new_rlimit == NULL)
70 && (old_rlimit64_mem.rlim_max != RLIM64_INFINITY))
71 return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
72 old_rlimit->rlim_max = RLIM_INFINITY;
73 }
74 }
75
76 return res;
77 }