Add INLINE_SYSCALL_RETURN/INLINE_SYSCALL_ERROR_RETURN
[glibc.git] / sysdeps / unix / sysv / linux / prlimit.c
blobad164c4b2cba73042ec14d76aba0c343018d9c09
1 /* Copyright (C) 2010-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <sys/resource.h>
20 #include <sys/syscall.h>
23 #ifdef __NR_prlimit64
24 int
25 prlimit (__pid_t pid, enum __rlimit_resource resource,
26 const struct rlimit *new_rlimit, struct rlimit *old_rlimit)
28 struct rlimit64 new_rlimit64_mem;
29 struct rlimit64 *new_rlimit64 = NULL;
30 struct rlimit64 old_rlimit64_mem;
31 struct rlimit64 *old_rlimit64 = (old_rlimit != NULL
32 ? &old_rlimit64_mem : NULL);
34 if (new_rlimit != NULL)
36 if (new_rlimit->rlim_cur == RLIM_INFINITY)
37 new_rlimit64_mem.rlim_cur = RLIM64_INFINITY;
38 else
39 new_rlimit64_mem.rlim_cur = new_rlimit->rlim_cur;
40 if (new_rlimit->rlim_max == RLIM_INFINITY)
41 new_rlimit64_mem.rlim_max = RLIM64_INFINITY;
42 else
43 new_rlimit64_mem.rlim_max = new_rlimit->rlim_max;
44 new_rlimit64 = &new_rlimit64_mem;
47 INTERNAL_SYSCALL_DECL (err);
48 int res = INTERNAL_SYSCALL (prlimit64, err, 4, pid, resource,
49 new_rlimit64, old_rlimit64);
51 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (res, err)))
52 return INLINE_SYSCALL_ERROR_RETURN (-INTERNAL_SYSCALL_ERRNO (res,
53 err),
54 int, -1);
55 else if (old_rlimit != NULL)
57 /* The prlimit64 syscall is ill-designed for 32-bit machines.
58 We have to provide a 32-bit variant since otherwise the LFS
59 system would not work. But what shall we do if the syscall
60 succeeds but the old values do not fit into a rlimit
61 structure? We cannot return an error because the operation
62 itself worked. Best is perhaps to return RLIM_INFINITY. */
63 old_rlimit->rlim_cur = old_rlimit64_mem.rlim_cur;
64 if (old_rlimit->rlim_cur != old_rlimit64_mem.rlim_cur)
66 if (new_rlimit == NULL)
67 return INLINE_SYSCALL_ERROR_RETURN (-EOVERFLOW, int, -1);
68 old_rlimit->rlim_cur = RLIM_INFINITY;
70 old_rlimit->rlim_max = old_rlimit64_mem.rlim_max;
71 if (old_rlimit->rlim_max != old_rlimit64_mem.rlim_max)
73 if (new_rlimit == NULL)
74 return INLINE_SYSCALL_ERROR_RETURN (-EOVERFLOW, int, -1);
75 old_rlimit->rlim_max = RLIM_INFINITY;
79 return res;
81 #else
82 int
83 prlimit (__pid_t pid, enum __rlimit_resource resource,
84 const struct rlimit *new_rlimit, struct rlimit *old_rlimit)
86 return INLINE_SYSCALL_ERROR_RETURN (-ENOSYS, int, -1);
88 stub_warning (prlimit)
89 #endif