| 1 | /* Linux setrlimit64 implementation (64 bits off_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 setlimit64 to {__}setrlimit does not throw a type error. */ |
| 25 | #undef settrlimit |
| 26 | #undef __sttrlimit |
| 27 | #define setrlimit setrlimit_redirect |
| 28 | #define __setrlimit __setrlimit_redirect |
| 29 | #include <sys/resource.h> |
| 30 | #undef setrlimit |
| 31 | #undef __setrlimit |
| 32 | |
| 33 | /* Set the soft and hard limits for RESOURCE to *RLIMITS. |
| 34 | Only the super-user can increase hard limits. |
| 35 | Return 0 if successful, -1 if not (and sets errno). */ |
| 36 | int |
| 37 | __setrlimit64 (enum __rlimit_resource resource, const struct rlimit64 *rlimits) |
| 38 | { |
| 39 | int res; |
| 40 | |
| 41 | #ifdef __NR_prlimit64 |
| 42 | res = INLINE_SYSCALL_CALL (prlimit64, 0, resource, rlimits, NULL); |
| 43 | if (res == 0 || errno != ENOSYS) |
| 44 | return res; |
| 45 | #endif |
| 46 | |
| 47 | /* The fallback code only makes sense if the platform supports |
| 48 | __NR_setrlimit. */ |
| 49 | #ifdef __NR_setrlimit |
| 50 | # if !__RLIM_T_MATCHES_RLIM64_T |
| 51 | struct rlimit rlimits32; |
| 52 | |
| 53 | if (rlimits->rlim_cur >= RLIM_INFINITY) |
| 54 | rlimits32.rlim_cur = RLIM_INFINITY; |
| 55 | else |
| 56 | rlimits32.rlim_cur = rlimits->rlim_cur; |
| 57 | if (rlimits->rlim_max >= RLIM_INFINITY) |
| 58 | rlimits32.rlim_max = RLIM_INFINITY; |
| 59 | else |
| 60 | rlimits32.rlim_max = rlimits->rlim_max; |
| 61 | # else |
| 62 | # define rlimits32 (*rlimits) |
| 63 | # endif |
| 64 | |
| 65 | res = INLINE_SYSCALL_CALL (setrlimit, resource, &rlimits32); |
| 66 | #endif |
| 67 | |
| 68 | return res; |
| 69 | } |
| 70 | weak_alias (__setrlimit64, setrlimit64) |
| 71 | |
| 72 | #if __RLIM_T_MATCHES_RLIM64_T |
| 73 | strong_alias (__setrlimit64, __setrlimit) |
| 74 | weak_alias (__setrlimit64, setrlimit) |
| 75 | #endif |
| 76 | |