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 #ifndef _FCNTL_H 30 #define _FCNTL_H 31 32 #include <sys/cdefs.h> 33 #include <sys/types.h> 34 #include <linux/fadvise.h> 35 #include <linux/falloc.h> 36 #include <linux/fcntl.h> 37 #include <linux/stat.h> 38 #include <linux/uio.h> 39 40 #include <bits/fcntl.h> 41 #include <bits/seek_constants.h> 42 43 #if defined(__USE_GNU) || defined(__USE_BSD) 44 #include <bits/lockf.h> 45 #endif 46 47 __BEGIN_DECLS 48 49 #ifdef __LP64__ 50 /* LP64 kernels don't have F_*64 defines because their flock is 64-bit. */ 51 #define F_GETLK64 F_GETLK 52 #define F_SETLK64 F_SETLK 53 #define F_SETLKW64 F_SETLKW 54 #endif 55 56 #define O_ASYNC FASYNC 57 #define O_RSYNC O_SYNC 58 59 #if __ANDROID_API__ >= __ANDROID_API_L__ 60 #define SPLICE_F_MOVE 1 61 #define SPLICE_F_NONBLOCK 2 62 #define SPLICE_F_MORE 4 63 #define SPLICE_F_GIFT 8 64 #endif 65 66 #if __ANDROID_API__ >= __ANDROID_API_O__ 67 #define SYNC_FILE_RANGE_WAIT_BEFORE 1 68 #define SYNC_FILE_RANGE_WRITE 2 69 #define SYNC_FILE_RANGE_WAIT_AFTER 4 70 #endif 71 72 int creat(const char*, mode_t); 73 int creat64(const char*, mode_t) __INTRODUCED_IN(21); 74 int openat(int, const char*, int, ...) __overloadable 75 __RENAME_CLANG(openat); 76 int openat64(int, const char*, int, ...) __INTRODUCED_IN(21); 77 int open(const char*, int, ...) __overloadable __RENAME_CLANG(open); 78 int open64(const char*, int, ...) __INTRODUCED_IN(21); 79 ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int) __INTRODUCED_IN(21); 80 ssize_t tee(int, int, size_t, unsigned int) __INTRODUCED_IN(21); 81 ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int) __INTRODUCED_IN(21); 82 83 #if defined(__USE_FILE_OFFSET64) 84 int fallocate(int, int, off_t, off_t) __RENAME(fallocate64) __INTRODUCED_IN(21); 85 int posix_fadvise(int, off_t, off_t, int) __RENAME(posix_fadvise64) __INTRODUCED_IN(21); 86 int posix_fallocate(int, off_t, off_t) __RENAME(posix_fallocate64) __INTRODUCED_IN(21); 87 #else 88 int fallocate(int, int, off_t, off_t) __INTRODUCED_IN(21); 89 int posix_fadvise(int, off_t, off_t, int) __INTRODUCED_IN(21); 90 int posix_fallocate(int, off_t, off_t) __INTRODUCED_IN(21); 91 #endif 92 int fallocate64(int, int, off64_t, off64_t) __INTRODUCED_IN(21); 93 int posix_fadvise64(int, off64_t, off64_t, int) __INTRODUCED_IN(21); 94 int posix_fallocate64(int, off64_t, off64_t) __INTRODUCED_IN(21); 95 96 #if defined(__USE_GNU) 97 ssize_t readahead(int, off64_t, size_t) __INTRODUCED_IN(16); 98 int sync_file_range(int, off64_t, off64_t, unsigned int) __INTRODUCED_IN(26); 99 #endif 100 101 int __open_2(const char*, int) __INTRODUCED_IN(17); 102 int __openat_2(int, const char*, int) __INTRODUCED_IN(17); 103 /* 104 * These are the easiest way to call the real open even in clang FORTIFY. 105 */ 106 int __open_real(const char*, int, ...) __RENAME(open); 107 int __openat_real(int, const char*, int, ...) __RENAME(openat); 108 109 110 #if defined(__BIONIC_FORTIFY) 111 #define __open_too_many_args_error "too many arguments" 112 #define __open_too_few_args_error "called with O_CREAT, but missing mode" 113 #if defined(__clang__) 114 115 #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ 116 __BIONIC_ERROR_FUNCTION_VISIBILITY 117 int open(const char* pathname, int flags, mode_t modes, ...) __overloadable 118 __errorattr(__open_too_many_args_error); 119 120 __BIONIC_ERROR_FUNCTION_VISIBILITY 121 int open(const char* pathname, int flags) __overloadable 122 __enable_if(flags & O_CREAT, __open_too_few_args_error) 123 __errorattr(__open_too_few_args_error); 124 125 /* 126 * pass_object_size serves two purposes here, neither of which involve __bos: it 127 * disqualifies this function from having its address taken (so &open works), 128 * and it makes overload resolution prefer open(const char *, int) over 129 * open(const char *, int, ...). 130 */ 131 __BIONIC_FORTIFY_INLINE 132 int open(const char* const __pass_object_size pathname, 133 int flags) __overloadable { 134 return __open_2(pathname, flags); 135 } 136 137 __BIONIC_FORTIFY_INLINE 138 int open(const char* const __pass_object_size pathname, int flags, mode_t modes) 139 __overloadable { 140 return __open_real(pathname, flags, modes); 141 } 142 143 __BIONIC_ERROR_FUNCTION_VISIBILITY 144 int openat(int dirfd, const char* pathname, int flags) __overloadable 145 __enable_if(flags & O_CREAT, __open_too_few_args_error) 146 __errorattr(__open_too_few_args_error); 147 148 __BIONIC_ERROR_FUNCTION_VISIBILITY 149 int openat(int dirfd, const char* pathname, int flags, mode_t modes, ...) 150 __overloadable 151 __errorattr(__open_too_many_args_error); 152 153 __BIONIC_FORTIFY_INLINE 154 int openat(int dirfd, const char* const __pass_object_size pathname, 155 int flags) __overloadable { 156 return __openat_2(dirfd, pathname, flags); 157 } 158 159 __BIONIC_FORTIFY_INLINE 160 int openat(int dirfd, const char* const __pass_object_size pathname, int flags, 161 mode_t modes) __overloadable { 162 return __openat_real(dirfd, pathname, flags, modes); 163 } 164 #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ 165 166 #else /* defined(__clang__) */ 167 __errordecl(__creat_missing_mode, __open_too_few_args_error); 168 __errordecl(__creat_too_many_args, __open_too_many_args_error); 169 170 #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ 171 __BIONIC_FORTIFY_INLINE 172 int open(const char* pathname, int flags, ...) { 173 if (__builtin_constant_p(flags)) { 174 if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) { 175 __creat_missing_mode(); /* Compile time error. */ 176 } 177 } 178 179 if (__builtin_va_arg_pack_len() > 1) { 180 __creat_too_many_args(); /* Compile time error. */ 181 } 182 183 if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) { 184 return __open_2(pathname, flags); 185 } 186 187 return __open_real(pathname, flags, __builtin_va_arg_pack()); 188 } 189 190 __BIONIC_FORTIFY_INLINE 191 int openat(int dirfd, const char* pathname, int flags, ...) { 192 if (__builtin_constant_p(flags)) { 193 if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) { 194 __creat_missing_mode(); /* Compile time error. */ 195 } 196 } 197 198 if (__builtin_va_arg_pack_len() > 1) { 199 __creat_too_many_args(); /* Compile time error. */ 200 } 201 202 if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) { 203 return __openat_2(dirfd, pathname, flags); 204 } 205 206 return __openat_real(dirfd, pathname, flags, __builtin_va_arg_pack()); 207 } 208 #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ 209 210 #endif /* defined(__clang__) */ 211 212 #undef __open_too_many_args_error 213 #undef __open_too_few_args_error 214 #endif /* defined(__BIONIC_FORTIFY) */ 215 216 __END_DECLS 217 218 #endif /* _FCNTL_H */ 219