Home | History | Annotate | Download | only in bionic
      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 
     29 #include <errno.h>
     30 #include <fcntl.h>
     31 #include <stdarg.h>
     32 #include <sys/resource.h>
     33 #include <sys/types.h>
     34 #include <sys/vfs.h>
     35 #include <unistd.h>
     36 
     37 #if __LP64__
     38 #error This code is only needed on 32-bit systems!
     39 #endif
     40 
     41 // System calls we need.
     42 extern "C" int __fcntl64(int, int, void*);
     43 extern "C" int __fstatfs64(int, size_t, struct statfs*);
     44 extern "C" int __llseek(int, unsigned long, unsigned long, off64_t*, int);
     45 extern "C" int __statfs64(const char*, size_t, struct statfs*);
     46 
     47 // For fcntl we use the fcntl64 system call to signal that we're using struct flock64.
     48 int fcntl(int fd, int cmd, ...) {
     49   va_list ap;
     50 
     51   va_start(ap, cmd);
     52   void* arg = va_arg(ap, void*);
     53   va_end(ap);
     54 
     55   return __fcntl64(fd, cmd, arg);
     56 }
     57 
     58 // For fstatfs we need to add the extra argument giving the kernel the size of the buffer.
     59 int fstatfs(int fd, struct statfs* stat) {
     60   return __fstatfs64(fd, sizeof(*stat), stat);
     61 }
     62 __strong_alias(fstatfs64, fstatfs);
     63 
     64 // For statfs we need to add the extra argument giving the kernel the size of the buffer.
     65 int statfs(const char* path, struct statfs* stat) {
     66   return __statfs64(path, sizeof(*stat), stat);
     67 }
     68 __strong_alias(statfs64, statfs);
     69 
     70 // For lseek64 we need to use the llseek system call which splits the off64_t in two and
     71 // returns the off64_t result via a pointer because 32-bit kernels can't return 64-bit results.
     72 off64_t lseek64(int fd, off64_t off, int whence) {
     73   off64_t result;
     74   unsigned long off_hi = static_cast<unsigned long>(off >> 32);
     75   unsigned long off_lo = static_cast<unsigned long>(off);
     76   if (__llseek(fd, off_hi, off_lo, &result, whence) < 0) {
     77     return -1;
     78   }
     79   return result;
     80 }
     81 
     82 // There is no pread for 32-bit off_t, so we need to widen and call pread64.
     83 ssize_t pread(int fd, void* buf, size_t byte_count, off_t offset) {
     84   return pread64(fd, buf, byte_count, static_cast<off64_t>(offset));
     85 }
     86 
     87 // There is no pwrite for 32-bit off_t, so we need to widen and call pwrite64.
     88 ssize_t pwrite(int fd, const void* buf, size_t byte_count, off_t offset) {
     89   return pwrite64(fd, buf, byte_count, static_cast<off64_t>(offset));
     90 }
     91 
     92 // There is no fallocate for 32-bit off_t, so we need to widen and call fallocate64.
     93 int fallocate(int fd, int mode, off_t offset, off_t length) {
     94   return fallocate64(fd, mode, static_cast<off64_t>(offset), static_cast<off64_t>(length));
     95 }
     96 
     97 // There is no getrlimit64 system call, so we need to use prlimit64.
     98 int getrlimit64(int resource, rlimit64* limits64) {
     99   return prlimit64(0, resource, NULL, limits64);
    100 }
    101 
    102 // There is no setrlimit64 system call, so we need to use prlimit64.
    103 int setrlimit64(int resource, const rlimit64* limits64) {
    104   return prlimit64(0, resource, limits64, NULL);
    105 }
    106