1 /*
2  * setrlimit() 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 setrlimit if the new usetrlimit is not present and setrlimit sucks */
15 
16 #if defined(__NR_usetrlimit)
17 /* just call usetrlimit() */
18 # define __NR___syscall_usetrlimit __NR_usetrlimit
19 static __always_inline
_syscall2(int,__syscall_usetrlimit,enum __rlimit_resource,resource,const struct rlimit *,rlim)20 _syscall2(int, __syscall_usetrlimit, enum __rlimit_resource, resource,
21           const struct rlimit *, rlim)
22 int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
23 {
24 	return __syscall_usetrlimit(resource, rlimits);
25 }
26 libc_hidden_def(setrlimit)
27 
28 #elif defined(__NR_prlimit64)
29 
30 /* Use prlimit64 if present, the prlimit64 syscall is free from a back
31    compatibility stuff for setrlimit */
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 setrlimit as an alias to setrlimit64, see setrlimit64.c */
36 int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
37 {
38 	struct rlimit64 rlimits64;
39 
40 	if (rlimits->rlim_cur == RLIM_INFINITY)
41 		rlimits64.rlim_cur = RLIM64_INFINITY;
42 	else
43 		rlimits64.rlim_cur = rlimits->rlim_cur;
44 	if (rlimits->rlim_max == RLIM_INFINITY)
45 		rlimits64.rlim_max = RLIM64_INFINITY;
46 	else
47 		rlimits64.rlim_max = rlimits->rlim_max;
48 
49 	return INLINE_SYSCALL (prlimit64, 4, 0, resource, &rlimits64, NULL);
50 }
51 libc_hidden_def(setrlimit)
52 # endif
53 
54 #else
55 
56 # if !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
57 
58 /* We don't need to wrap setrlimit() */
59 _syscall2(int, setrlimit, __rlimit_resource_t, resource,
60 		const struct rlimit *, rlim)
61 
62 # else
63 
64 #  define __need_NULL
65 #  include <stddef.h>
66 #  include <errno.h>
67 #  include <sys/param.h>
68 
69 /* we have to handle old style setrlimit() */
70 #  define __NR___syscall_setrlimit __NR_setrlimit
71 static __always_inline
72 _syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim)
73 
74 int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
75 {
76 	struct rlimit rlimits_small;
77 
78 	if (rlimits == NULL) {
79 		__set_errno(EINVAL);
80 		return -1;
81 	}
82 
83 	/* We might have to correct the limits values.  Since the old values
84 	 * were signed the new values might be too large.  */
85 	rlimits_small.rlim_cur = MIN((unsigned long int) rlimits->rlim_cur,
86 								  RLIM_INFINITY >> 1);
87 	rlimits_small.rlim_max = MIN((unsigned long int) rlimits->rlim_max,
88 								  RLIM_INFINITY >> 1);
89 	return __syscall_setrlimit(resource, &rlimits_small);
90 }
91 # endif
92 
93 libc_hidden_def(setrlimit)
94 #endif
95