1 /*
2 * getrlimit() for uClibc
3 *
4 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
5 *
6 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
7 */
8
9 #include <sys/syscall.h>
10 #include <sys/resource.h>
11 #include <bits/wordsize.h>
12 #include <stddef.h> // needed for NULL to be defined
13
14 /* Only wrap getrlimit if the new ugetrlimit is not present and getrlimit sucks */
15
16 #if defined(__NR_ugetrlimit)
17
18 /* just call ugetrlimit() */
19 # define __NR___syscall_ugetrlimit __NR_ugetrlimit
20 static __always_inline
_syscall2(int,__syscall_ugetrlimit,enum __rlimit_resource,resource,struct rlimit *,rlim)21 _syscall2(int, __syscall_ugetrlimit, enum __rlimit_resource, resource,
22 struct rlimit *, rlim)
23 int getrlimit(__rlimit_resource_t resource, struct rlimit *rlimits)
24 {
25 return __syscall_ugetrlimit(resource, rlimits);
26 }
27 libc_hidden_def(getrlimit)
28
29 #elif defined(__NR_prlimit64)
30 /* Use prlimit64 if present, the prlimit64 syscall is free from a back
31 compatibility stuff for an old getrlimit */
32
33 # if __WORDSIZE == 32 && !defined(__USE_FILE_OFFSET64)
34 /* If struct rlimit has 64-bit fields (if __WORDSIZE == 64 or __USE_FILE_OFFSET64
35 is defined), then use getrlimit as an alias to getrlimit64, see getrlimit64.c */
36 int getrlimit(__rlimit_resource_t resource, struct rlimit *rlimits)
37 {
38 struct rlimit64 rlimits64;
39 int res = INLINE_SYSCALL (prlimit64, 4, 0, resource, NULL, &rlimits64);
40
41 if (res == 0) {
42 /* If the syscall succeeds but the values do not fit into a
43 rlimit structure set EOVERFLOW errno and retrun -1. */
44 rlimits->rlim_cur = rlimits64.rlim_cur;
45 if (rlimits64.rlim_cur != rlimits->rlim_cur) {
46 if (rlimits64.rlim_cur != RLIM64_INFINITY) {
47 __set_errno(EOVERFLOW);
48 return -1;
49 }
50 rlimits->rlim_cur = RLIM_INFINITY;
51 }
52
53 rlimits->rlim_max = rlimits64.rlim_max;
54 if (rlimits64.rlim_max != rlimits->rlim_max) {
55 if (rlimits64.rlim_max != RLIM64_INFINITY) {
56 __set_errno(EOVERFLOW);
57 return -1;
58 }
59 rlimits->rlim_max = RLIM_INFINITY;
60 }
61 }
62 return res;
63 }
64 libc_hidden_def(getrlimit)
65 # endif
66
67 #else
68
69 # if !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
70
71 /* We don't need to wrap getrlimit() */
72 _syscall2(int, getrlimit, __rlimit_resource_t, resource,
73 struct rlimit *, rlim)
74
75 # else
76
77 /* we have to handle old style getrlimit() */
78 # define __NR___syscall_getrlimit __NR_getrlimit
79 static __always_inline
80 _syscall2(int, __syscall_getrlimit, int, resource, struct rlimit *, rlim)
81
82 int getrlimit(__rlimit_resource_t resource, struct rlimit *rlimits)
83 {
84 int result;
85
86 result = __syscall_getrlimit(resource, rlimits);
87
88 if (result == -1)
89 return result;
90
91 /* We might have to correct the limits values. Since the old values
92 * were signed the infinity value is too small. */
93 if (rlimits->rlim_cur == RLIM_INFINITY >> 1)
94 rlimits->rlim_cur = RLIM_INFINITY;
95 if (rlimits->rlim_max == RLIM_INFINITY >> 1)
96 rlimits->rlim_max = RLIM_INFINITY;
97 return result;
98 }
99 # endif
100
101 libc_hidden_def(getrlimit)
102 #endif
103