blob: 20d258d7d7ff0424850367c2913f11078e26a0ca [file] [log] [blame]
Elliott Hughes06209252013-11-06 16:20:54 -08001/*
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 Hughesa9325132015-04-16 17:56:12 -070029#undef _FORTIFY_SOURCE
30
Elliott Hughes06209252013-11-06 16:20:54 -080031#include <errno.h>
Elliott Hughesf64b8ea2014-02-03 16:20:46 -080032#include <fcntl.h>
Elliott Hughes06209252013-11-06 16:20:54 -080033#include <stdarg.h>
Elliott Hughes0f461e32014-01-09 10:17:03 -080034#include <sys/resource.h>
Elliott Hughes06209252013-11-06 16:20:54 -080035#include <sys/types.h>
Elliott Hughes6f4594d2015-08-26 13:27:43 -070036#include <sys/uio.h>
Elliott Hughes06209252013-11-06 16:20:54 -080037#include <sys/vfs.h>
Elliott Hughes06209252013-11-06 16:20:54 -080038#include <unistd.h>
39
40#if __LP64__
41#error This code is only needed on 32-bit systems!
42#endif
43
44// System calls we need.
45extern "C" int __fcntl64(int, int, void*);
Elliott Hughes06209252013-11-06 16:20:54 -080046extern "C" int __llseek(int, unsigned long, unsigned long, off64_t*, int);
Elliott Hughes6f4594d2015-08-26 13:27:43 -070047extern "C" int __preadv64(int, const struct iovec*, int, long, long);
48extern "C" int __pwritev64(int, const struct iovec*, int, long, long);
Elliott Hughes06209252013-11-06 16:20:54 -080049
50// For fcntl we use the fcntl64 system call to signal that we're using struct flock64.
51int fcntl(int fd, int cmd, ...) {
52 va_list ap;
53
54 va_start(ap, cmd);
55 void* arg = va_arg(ap, void*);
56 va_end(ap);
57
58 return __fcntl64(fd, cmd, arg);
59}
60
Elliott Hughes06209252013-11-06 16:20:54 -080061// For lseek64 we need to use the llseek system call which splits the off64_t in two and
62// returns the off64_t result via a pointer because 32-bit kernels can't return 64-bit results.
63off64_t lseek64(int fd, off64_t off, int whence) {
64 off64_t result;
65 unsigned long off_hi = static_cast<unsigned long>(off >> 32);
66 unsigned long off_lo = static_cast<unsigned long>(off);
67 if (__llseek(fd, off_hi, off_lo, &result, whence) < 0) {
68 return -1;
69 }
70 return result;
71}
72
73// There is no pread for 32-bit off_t, so we need to widen and call pread64.
74ssize_t pread(int fd, void* buf, size_t byte_count, off_t offset) {
75 return pread64(fd, buf, byte_count, static_cast<off64_t>(offset));
76}
77
78// There is no pwrite for 32-bit off_t, so we need to widen and call pwrite64.
79ssize_t pwrite(int fd, const void* buf, size_t byte_count, off_t offset) {
80 return pwrite64(fd, buf, byte_count, static_cast<off64_t>(offset));
81}
Elliott Hughes0f461e32014-01-09 10:17:03 -080082
Elliott Hughes6f4594d2015-08-26 13:27:43 -070083// On LP32, there is no off_t preadv/pwritev, and even the 64-bit preadv/pwritev
84// don't use off64_t (see SYSCALLS.TXT for more). Here, this means that we need
85// to implement all four functions because the two system calls don't match any
86// of the userspace functions. Unlike llseek, the pair is split lo-hi, not hi-lo.
87ssize_t preadv(int fd, const struct iovec* ios, int count, off_t offset) {
88 return __preadv64(fd, ios, count, offset, 0);
89}
90ssize_t preadv64(int fd, const struct iovec* ios, int count, off64_t offset) {
91 return __preadv64(fd, ios, count, offset, offset >> 32);
92}
93ssize_t pwritev(int fd, const struct iovec* ios, int count, off_t offset) {
94 return __pwritev64(fd, ios, count, offset, 0);
95}
96ssize_t pwritev64(int fd, const struct iovec* ios, int count, off64_t offset) {
97 return __pwritev64(fd, ios, count, offset, offset >> 32);
98}
99
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800100// There is no fallocate for 32-bit off_t, so we need to widen and call fallocate64.
101int fallocate(int fd, int mode, off_t offset, off_t length) {
102 return fallocate64(fd, mode, static_cast<off64_t>(offset), static_cast<off64_t>(length));
103}
104
Elliott Hughes0f461e32014-01-09 10:17:03 -0800105// There is no getrlimit64 system call, so we need to use prlimit64.
106int getrlimit64(int resource, rlimit64* limits64) {
107 return prlimit64(0, resource, NULL, limits64);
108}
109
110// There is no setrlimit64 system call, so we need to use prlimit64.
111int setrlimit64(int resource, const rlimit64* limits64) {
112 return prlimit64(0, resource, limits64, NULL);
113}