George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #error "Never include this file directly; instead, include <fcntl.h>" |
| 31 | #endif |
| 32 | |
| 33 | int __open_2(const char*, int) __INTRODUCED_IN(17); |
| 34 | int __openat_2(int, const char*, int) __INTRODUCED_IN(17); |
| 35 | /* |
| 36 | * These are the easiest way to call the real open even in clang FORTIFY. |
| 37 | */ |
| 38 | int __open_real(const char*, int, ...) __RENAME(open); |
| 39 | int __openat_real(int, const char*, int, ...) __RENAME(openat); |
| 40 | |
| 41 | #if defined(__BIONIC_FORTIFY) |
| 42 | #define __open_too_many_args_error "too many arguments" |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 43 | #define __open_too_few_args_error "called with O_CREAT or O_TMPFILE, but missing mode" |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 44 | #define __open_useless_modes_warning "has superfluous mode bits; missing O_CREAT?" |
| 45 | /* O_TMPFILE shares bits with O_DIRECTORY. */ |
| 46 | #define __open_modes_useful(flags) (((flags) & O_CREAT) || ((flags) & O_TMPFILE) == O_TMPFILE) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 47 | |
| 48 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
| 49 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
| 50 | int open(const char* pathname, int flags, mode_t modes, ...) __overloadable |
| 51 | __errorattr(__open_too_many_args_error); |
| 52 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 53 | /* |
| 54 | * pass_object_size serves two purposes here, neither of which involve __bos: it |
| 55 | * disqualifies this function from having its address taken (so &open works), |
| 56 | * and it makes overload resolution prefer open(const char *, int) over |
| 57 | * open(const char *, int, ...). |
| 58 | */ |
| 59 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 0df4c37 | 2017-07-31 21:39:33 -0700 | [diff] [blame] | 60 | int open(const char* const __pass_object_size pathname, int flags) |
| 61 | __overloadable |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 62 | __clang_error_if(__open_modes_useful(flags), "'open' " __open_too_few_args_error) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 63 | return __open_2(pathname, flags); |
| 64 | } |
| 65 | |
| 66 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 67 | int open(const char* const __pass_object_size pathname, int flags, mode_t modes) |
| 68 | __overloadable |
| 69 | __clang_warning_if(!__open_modes_useful(flags) && modes, |
| 70 | "'open' " __open_useless_modes_warning) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 71 | return __open_real(pathname, flags, modes); |
| 72 | } |
| 73 | |
| 74 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 75 | int openat(int dirfd, const char* pathname, int flags, mode_t modes, ...) |
| 76 | __overloadable |
| 77 | __errorattr(__open_too_many_args_error); |
| 78 | |
| 79 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 80 | int openat(int dirfd, const char* const __pass_object_size pathname, int flags) |
George Burgess IV | 0df4c37 | 2017-07-31 21:39:33 -0700 | [diff] [blame] | 81 | __overloadable |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 82 | __clang_error_if(__open_modes_useful(flags), "'openat' " __open_too_few_args_error) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 83 | return __openat_2(dirfd, pathname, flags); |
| 84 | } |
| 85 | |
| 86 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 87 | int openat(int dirfd, const char* const __pass_object_size pathname, int flags, mode_t modes) |
| 88 | __overloadable |
| 89 | __clang_warning_if(!__open_modes_useful(flags) && modes, |
| 90 | "'openat' " __open_useless_modes_warning) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 91 | return __openat_real(dirfd, pathname, flags, modes); |
| 92 | } |
| 93 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
| 94 | |
George Burgess IV | 2356c93 | 2019-06-06 17:18:13 -0700 | [diff] [blame^] | 95 | #if __ANDROID_API__ >= __ANDROID_API_R__ |
| 96 | int __open64_2(const char*, int) __INTRODUCED_IN(30); |
| 97 | int __openat64_2(int, const char*, int) __INTRODUCED_IN(30); |
| 98 | int __open64_real(const char* __path, int __flags, ...) __RENAME(open64); |
| 99 | int __openat64_real(int, const char*, int, ...) __RENAME(openat64); |
| 100 | |
| 101 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
| 102 | int open64(const char* pathname, int flags, mode_t modes, ...) __overloadable |
| 103 | __errorattr(__open_too_many_args_error); |
| 104 | |
| 105 | __BIONIC_FORTIFY_INLINE |
| 106 | int open64(const char* const __pass_object_size pathname, int flags) |
| 107 | __overloadable |
| 108 | __clang_error_if(__open_modes_useful(flags), "'open64' " __open_too_few_args_error) { |
| 109 | return __open64_2(pathname, flags); |
| 110 | } |
| 111 | |
| 112 | __BIONIC_FORTIFY_INLINE |
| 113 | int open64(const char* const __pass_object_size pathname, int flags, mode_t modes) |
| 114 | __overloadable |
| 115 | __clang_warning_if(!__open_modes_useful(flags) && modes, |
| 116 | "'open64' " __open_useless_modes_warning) { |
| 117 | return __open64_real(pathname, flags, modes); |
| 118 | } |
| 119 | |
| 120 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
| 121 | int openat64(int dirfd, const char* pathname, int flags, mode_t modes, ...) |
| 122 | __overloadable |
| 123 | __errorattr(__open_too_many_args_error); |
| 124 | |
| 125 | __BIONIC_FORTIFY_INLINE |
| 126 | int openat64(int dirfd, const char* const __pass_object_size pathname, int flags) |
| 127 | __overloadable |
| 128 | __clang_error_if(__open_modes_useful(flags), "'openat64' " __open_too_few_args_error) { |
| 129 | return __openat64_2(dirfd, pathname, flags); |
| 130 | } |
| 131 | |
| 132 | __BIONIC_FORTIFY_INLINE |
| 133 | int openat64(int dirfd, const char* const __pass_object_size pathname, int flags, mode_t modes) |
| 134 | __overloadable |
| 135 | __clang_warning_if(!__open_modes_useful(flags) && modes, |
| 136 | "'openat64' " __open_useless_modes_warning) { |
| 137 | return __openat64_real(dirfd, pathname, flags, modes); |
| 138 | } |
| 139 | #endif /* __ANDROID_API__ >= __ANDROID_API_R__ */ |
| 140 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 141 | #undef __open_too_many_args_error |
| 142 | #undef __open_too_few_args_error |
George Burgess IV | 4e37d53 | 2017-08-03 17:11:35 -0700 | [diff] [blame] | 143 | #undef __open_useless_modes_warning |
| 144 | #undef __open_modes_useful |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 145 | #endif /* defined(__BIONIC_FORTIFY) */ |