| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
| Elliott Hughes | a932513 | 2015-04-16 17:56:12 -0700 | [diff] [blame] | 29 | #undef _FORTIFY_SOURCE | 
|  | 30 |  | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 31 | #include <errno.h> | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 32 | #include <fcntl.h> | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 33 | #include <stdarg.h> | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 34 | #include <sys/resource.h> | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 35 | #include <sys/types.h> | 
| Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 36 | #include <sys/uio.h> | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 37 | #include <sys/vfs.h> | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 38 | #include <unistd.h> | 
|  | 39 |  | 
| Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 40 | #include "private/bionic_fdtrack.h" | 
|  | 41 |  | 
| Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 42 | #if defined(__LP64__) | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 43 | #error This code is only needed on 32-bit systems! | 
|  | 44 | #endif | 
|  | 45 |  | 
|  | 46 | // System calls we need. | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 47 | extern "C" int __llseek(int, unsigned long, unsigned long, off64_t*, int); | 
| Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 48 | extern "C" int __preadv64(int, const struct iovec*, int, long, long); | 
|  | 49 | extern "C" int __pwritev64(int, const struct iovec*, int, long, long); | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 50 |  | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 51 | // For lseek64 we need to use the llseek system call which splits the off64_t in two and | 
|  | 52 | // returns the off64_t result via a pointer because 32-bit kernels can't return 64-bit results. | 
|  | 53 | off64_t lseek64(int fd, off64_t off, int whence) { | 
|  | 54 | off64_t result; | 
|  | 55 | unsigned long off_hi = static_cast<unsigned long>(off >> 32); | 
|  | 56 | unsigned long off_lo = static_cast<unsigned long>(off); | 
|  | 57 | if (__llseek(fd, off_hi, off_lo, &result, whence) < 0) { | 
|  | 58 | return -1; | 
|  | 59 | } | 
|  | 60 | return result; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | // There is no pread for 32-bit off_t, so we need to widen and call pread64. | 
|  | 64 | ssize_t pread(int fd, void* buf, size_t byte_count, off_t offset) { | 
|  | 65 | return pread64(fd, buf, byte_count, static_cast<off64_t>(offset)); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // There is no pwrite for 32-bit off_t, so we need to widen and call pwrite64. | 
|  | 69 | ssize_t pwrite(int fd, const void* buf, size_t byte_count, off_t offset) { | 
|  | 70 | return pwrite64(fd, buf, byte_count, static_cast<off64_t>(offset)); | 
|  | 71 | } | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 72 |  | 
| Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 73 | // On LP32, there is no off_t preadv/pwritev, and even the 64-bit preadv/pwritev | 
|  | 74 | // don't use off64_t (see SYSCALLS.TXT for more). Here, this means that we need | 
|  | 75 | // to implement all four functions because the two system calls don't match any | 
|  | 76 | // of the userspace functions. Unlike llseek, the pair is split lo-hi, not hi-lo. | 
|  | 77 | ssize_t preadv(int fd, const struct iovec* ios, int count, off_t offset) { | 
| Jerry Zhang | f55dbc0 | 2018-03-06 15:27:07 -0800 | [diff] [blame] | 78 | return preadv64(fd, ios, count, offset); | 
| Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 79 | } | 
|  | 80 | ssize_t preadv64(int fd, const struct iovec* ios, int count, off64_t offset) { | 
|  | 81 | return __preadv64(fd, ios, count, offset, offset >> 32); | 
|  | 82 | } | 
|  | 83 | ssize_t pwritev(int fd, const struct iovec* ios, int count, off_t offset) { | 
| Jerry Zhang | f55dbc0 | 2018-03-06 15:27:07 -0800 | [diff] [blame] | 84 | return pwritev64(fd, ios, count, offset); | 
| Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 85 | } | 
|  | 86 | ssize_t pwritev64(int fd, const struct iovec* ios, int count, off64_t offset) { | 
|  | 87 | return __pwritev64(fd, ios, count, offset, offset >> 32); | 
|  | 88 | } | 
|  | 89 |  | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 90 | // There is no fallocate for 32-bit off_t, so we need to widen and call fallocate64. | 
|  | 91 | int fallocate(int fd, int mode, off_t offset, off_t length) { | 
|  | 92 | return fallocate64(fd, mode, static_cast<off64_t>(offset), static_cast<off64_t>(length)); | 
|  | 93 | } | 
|  | 94 |  | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 95 | // There is no getrlimit64 system call, so we need to use prlimit64. | 
|  | 96 | int getrlimit64(int resource, rlimit64* limits64) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 97 | return prlimit64(0, resource, nullptr, limits64); | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
|  | 100 | // There is no setrlimit64 system call, so we need to use prlimit64. | 
|  | 101 | int setrlimit64(int resource, const rlimit64* limits64) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 102 | return prlimit64(0, resource, limits64, nullptr); | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 103 | } | 
| Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | // There is no prlimit system call, so we need to use prlimit64. | 
|  | 106 | int prlimit(pid_t pid, int resource, const rlimit* n32, rlimit* o32) { | 
|  | 107 | rlimit64 n64; | 
|  | 108 | if (n32 != nullptr) { | 
|  | 109 | n64.rlim_cur = (n32->rlim_cur == RLIM_INFINITY) ? RLIM64_INFINITY : n32->rlim_cur; | 
|  | 110 | n64.rlim_max = (n32->rlim_max == RLIM_INFINITY) ? RLIM64_INFINITY : n32->rlim_max; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | rlimit64 o64; | 
|  | 114 | int result = prlimit64(pid, resource, | 
|  | 115 | (n32 != nullptr) ? &n64 : nullptr, | 
|  | 116 | (o32 != nullptr) ? &o64 : nullptr); | 
|  | 117 | if (result != -1 && o32 != nullptr) { | 
|  | 118 | o32->rlim_cur = (o64.rlim_cur == RLIM64_INFINITY) ? RLIM_INFINITY : o64.rlim_cur; | 
|  | 119 | o32->rlim_max = (o64.rlim_max == RLIM64_INFINITY) ? RLIM_INFINITY : o64.rlim_max; | 
|  | 120 | } | 
|  | 121 | return result; | 
|  | 122 | } |