1/* Linux getrlimit64 implementation (64 bits rlim_t).
2 Copyright (C) 2010-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <sys/types.h>
21#include <shlib-compat.h>
22
23/* Add this redirection so the strong_alias for __RLIM_T_MATCHES_RLIM64_T
24 linking getlimit64 to {__}getrlimit does not throw a type error. */
25#undef getrlimit
26#undef __getrlimit
27#define getrlimit getrlimit_redirect
28#define __getrlimit __getrlimit_redirect
29#include <sys/resource.h>
30#undef getrlimit
31#undef __getrlimit
32
33/* Put the soft and hard limits for RESOURCE in *RLIMITS.
34 Returns 0 if successful, -1 if not (and sets errno). */
35int
36__getrlimit64 (enum __rlimit_resource resource, struct rlimit64 *rlimits)
37{
38#ifdef __NR_prlimit64
39 int res = INLINE_SYSCALL_CALL (prlimit64, 0, resource, NULL, rlimits);
40 if (res == 0 || errno != ENOSYS)
41 return res;
42#endif
43
44/* The fallback code only makes sense if the platform supports either
45 __NR_ugetrlimit and/or __NR_getrlimit. */
46#if defined (__NR_ugetrlimit) || defined (__NR_getrlimit)
47# ifndef __NR_ugetrlimit
48# define __NR_ugetrlimit __NR_getrlimit
49# endif
50# if __RLIM_T_MATCHES_RLIM64_T
51# define rlimits32 (*rlimits)
52# else
53 struct rlimit rlimits32;
54# endif
55
56 if (INLINE_SYSCALL_CALL (ugetrlimit, resource, &rlimits32) < 0)
57 return -1;
58
59# if !__RLIM_T_MATCHES_RLIM64_T
60 if (rlimits32.rlim_cur == RLIM_INFINITY)
61 rlimits->rlim_cur = RLIM64_INFINITY;
62 else
63 rlimits->rlim_cur = rlimits32.rlim_cur;
64 if (rlimits32.rlim_max == RLIM_INFINITY)
65 rlimits->rlim_max = RLIM64_INFINITY;
66 else
67 rlimits->rlim_max = rlimits32.rlim_max;
68# endif /* !__RLIM_T_MATCHES_RLIM64_T */
69#endif /* defined (__NR_ugetrlimit) || defined (__NR_getrlimit) */
70
71 return 0;
72}
73libc_hidden_def (__getrlimit64)
74
75#if __RLIM_T_MATCHES_RLIM64_T
76/* If both rlim_t and rlimt64_t are essentially the same type we can use
77 alias both interfaces. */
78strong_alias (__getrlimit64, __GI_getrlimit)
79strong_alias (__getrlimit64, __GI___getrlimit)
80strong_alias (__getrlimit64, __getrlimit)
81weak_alias (__getrlimit64, getrlimit)
82/* And there is no need for compat symbols. */
83# undef SHLIB_COMPAT
84# define SHLIB_COMPAT(a, b, c) 0
85#endif
86
87#if SHLIB_COMPAT (libc, GLIBC_2_1, GLIBC_2_2)
88/* Back compatible 2GiB limited rlimit. */
89extern int __new_getrlimit (enum __rlimit_resource, struct rlimit *);
90
91int
92attribute_compat_text_section
93__old_getrlimit64 (enum __rlimit_resource resource, struct rlimit64 *rlimits)
94{
95# if __RLIM_T_MATCHES_RLIM64_T
96# define rlimits32 (*rlimits)
97# else
98 struct rlimit rlimits32;
99# endif
100
101 if (__new_getrlimit (resource, &rlimits32) < 0)
102 return -1;
103
104 if (rlimits32.rlim_cur == RLIM_INFINITY)
105 rlimits->rlim_cur = RLIM64_INFINITY >> 1;
106 else
107 rlimits->rlim_cur = rlimits32.rlim_cur;
108 if (rlimits32.rlim_max == RLIM_INFINITY)
109 rlimits->rlim_max = RLIM64_INFINITY >> 1;
110 else
111 rlimits->rlim_max = rlimits32.rlim_max;
112
113 return 0;
114}
115versioned_symbol (libc, __getrlimit64, getrlimit64, GLIBC_2_2);
116compat_symbol (libc, __old_getrlimit64, getrlimit64, GLIBC_2_1);
117#else
118weak_alias (__getrlimit64, getrlimit64)
119libc_hidden_weak (getrlimit64)
120#endif
121