blob: 3e0a590800b88a81229eb35a2c3c0539df406846 [file] [log] [blame]
George Burgess IVb97049c2017-07-24 15:05:05 -07001/*
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
33int __open_2(const char*, int) __INTRODUCED_IN(17);
34int __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 */
38int __open_real(const char*, int, ...) __RENAME(open);
39int __openat_real(int, const char*, int, ...) __RENAME(openat);
40
41#if defined(__BIONIC_FORTIFY)
42#define __open_too_many_args_error "too many arguments"
43#define __open_too_few_args_error "called with O_CREAT, but missing mode"
44#if defined(__clang__)
45
46#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
47__BIONIC_ERROR_FUNCTION_VISIBILITY
48int open(const char* pathname, int flags, mode_t modes, ...) __overloadable
49 __errorattr(__open_too_many_args_error);
50
George Burgess IVb97049c2017-07-24 15:05:05 -070051/*
52 * pass_object_size serves two purposes here, neither of which involve __bos: it
53 * disqualifies this function from having its address taken (so &open works),
54 * and it makes overload resolution prefer open(const char *, int) over
55 * open(const char *, int, ...).
56 */
57__BIONIC_FORTIFY_INLINE
George Burgess IV0df4c372017-07-31 21:39:33 -070058int open(const char* const __pass_object_size pathname, int flags)
59 __overloadable
60 __clang_error_if(flags & O_CREAT, "'open' " __open_too_few_args_error) {
George Burgess IVb97049c2017-07-24 15:05:05 -070061 return __open_2(pathname, flags);
62}
63
64__BIONIC_FORTIFY_INLINE
George Burgess IV0df4c372017-07-31 21:39:33 -070065int open(const char* const __pass_object_size pathname, int flags, mode_t modes) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070066 return __open_real(pathname, flags, modes);
67}
68
69__BIONIC_ERROR_FUNCTION_VISIBILITY
George Burgess IVb97049c2017-07-24 15:05:05 -070070int openat(int dirfd, const char* pathname, int flags, mode_t modes, ...)
71 __overloadable
72 __errorattr(__open_too_many_args_error);
73
74__BIONIC_FORTIFY_INLINE
75int openat(int dirfd, const char* const __pass_object_size pathname,
George Burgess IV0df4c372017-07-31 21:39:33 -070076 int flags)
77 __overloadable
78 __clang_error_if(flags & O_CREAT, "'openat' " __open_too_few_args_error) {
George Burgess IVb97049c2017-07-24 15:05:05 -070079 return __openat_2(dirfd, pathname, flags);
80}
81
82__BIONIC_FORTIFY_INLINE
83int openat(int dirfd, const char* const __pass_object_size pathname, int flags,
84 mode_t modes) __overloadable {
85 return __openat_real(dirfd, pathname, flags, modes);
86}
87#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
88
89#else /* defined(__clang__) */
90__errordecl(__creat_missing_mode, __open_too_few_args_error);
91__errordecl(__creat_too_many_args, __open_too_many_args_error);
92
93#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
94__BIONIC_FORTIFY_INLINE
95int open(const char* pathname, int flags, ...) {
96 if (__builtin_constant_p(flags)) {
97 if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
98 __creat_missing_mode(); /* Compile time error. */
99 }
100 }
101
102 if (__builtin_va_arg_pack_len() > 1) {
103 __creat_too_many_args(); /* Compile time error. */
104 }
105
106 if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
107 return __open_2(pathname, flags);
108 }
109
110 return __open_real(pathname, flags, __builtin_va_arg_pack());
111}
112
113__BIONIC_FORTIFY_INLINE
114int openat(int dirfd, const char* pathname, int flags, ...) {
115 if (__builtin_constant_p(flags)) {
116 if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
117 __creat_missing_mode(); /* Compile time error. */
118 }
119 }
120
121 if (__builtin_va_arg_pack_len() > 1) {
122 __creat_too_many_args(); /* Compile time error. */
123 }
124
125 if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
126 return __openat_2(dirfd, pathname, flags);
127 }
128
129 return __openat_real(dirfd, pathname, flags, __builtin_va_arg_pack());
130}
131#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
132
133#endif /* defined(__clang__) */
134
135#undef __open_too_many_args_error
136#undef __open_too_few_args_error
137#endif /* defined(__BIONIC_FORTIFY) */