blob: ed773f7aec249a3f5c6885d8b394062170431629 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* vi: set sw=4 ts=4: */
2/*
3 * setrlimit() for uClibc
4 *
5 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
6 *
7 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
8 */
9
10#define setrlimit64 __hide_setrlimit64
11#include <sys/syscall.h>
12#include <unistd.h>
13#include <sys/resource.h>
14#undef setrlimit64
15
16
17/* Only wrap setrlimit if the new usetrlimit is not present and setrlimit sucks */
18
19#if defined(__NR_usetrlimit)
20
21/* just call usetrlimit() */
22# define __NR___syscall_usetrlimit __NR_usetrlimit
23static __always_inline
24_syscall2(int, __syscall_usetrlimit, enum __rlimit_resource, resource,
25 const struct rlimit *, rlim)
26int setrlimit(__rlimit_resource_t resource, struct rlimit *rlimits)
27{
28 return (__syscall_usetrlimit(resource, rlimits));
29}
30
31#elif !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
32
33/* We don't need to wrap setrlimit() */
34_syscall2(int, setrlimit, __rlimit_resource_t, resource,
35 const struct rlimit *, rlim)
36
37#else
38
39/* we have to handle old style setrlimit() */
40# define __NR___syscall_setrlimit __NR_setrlimit
41static __always_inline
42_syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim)
43
44int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
45{
46 struct rlimit rlimits_small;
47
48 if (rlimits == NULL) {
49 __set_errno(EINVAL);
50 return -1;
51 }
52
53 /* We might have to correct the limits values. Since the old values
54 * were signed the new values might be too large. */
55# define RMIN(x, y) ((x) < (y) ? (x) : (y))
56 rlimits_small.rlim_cur = RMIN((unsigned long int) rlimits->rlim_cur,
57 RLIM_INFINITY >> 1);
58 rlimits_small.rlim_max = RMIN((unsigned long int) rlimits->rlim_max,
59 RLIM_INFINITY >> 1);
60#undef RMIN
61 return (__syscall_setrlimit(resource, &rlimits_small));
62}
63#endif
64
65libc_hidden_def(setrlimit)
66
67#if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
68strong_alias(setrlimit, setrlimit64)
69#endif