blob: 05c62eb246e1566a09dfeaa6440698f3895af362 [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
Elliott Hughes655e4302023-06-16 12:39:33 -070033int __open_2(const char* _Nonnull, int);
34int __openat_2(int, const char* _Nonnull, int);
George Burgess IVb97049c2017-07-24 15:05:05 -070035/*
36 * These are the easiest way to call the real open even in clang FORTIFY.
37 */
zijunzhao725c96c2023-06-09 00:50:24 +000038int __open_real(const char* _Nonnull, int, ...) __RENAME(open);
39int __openat_real(int, const char* _Nonnull, int, ...) __RENAME(openat);
George Burgess IVb97049c2017-07-24 15:05:05 -070040
41#if defined(__BIONIC_FORTIFY)
42#define __open_too_many_args_error "too many arguments"
Elliott Hughesb115aef2017-08-04 09:34:19 -070043#define __open_too_few_args_error "called with O_CREAT or O_TMPFILE, but missing mode"
Elliott Hughes900a9892022-06-06 23:50:34 +000044#define __open_useless_modes_warning "has superfluous mode bits; missing O_CREAT or O_TMPFILE?"
George Burgess IV4e37d532017-08-03 17:11:35 -070045/* O_TMPFILE shares bits with O_DIRECTORY. */
46#define __open_modes_useful(flags) (((flags) & O_CREAT) || ((flags) & O_TMPFILE) == O_TMPFILE)
George Burgess IVb97049c2017-07-24 15:05:05 -070047
George Burgess IVb97049c2017-07-24 15:05:05 -070048__BIONIC_ERROR_FUNCTION_VISIBILITY
zijunzhao725c96c2023-06-09 00:50:24 +000049int open(const char* _Nonnull pathname, int flags, mode_t modes, ...) __overloadable
George Burgess IVb97049c2017-07-24 15:05:05 -070050 __errorattr(__open_too_many_args_error);
51
George Burgess IVb97049c2017-07-24 15:05:05 -070052/*
53 * pass_object_size serves two purposes here, neither of which involve __bos: it
54 * disqualifies this function from having its address taken (so &open works),
55 * and it makes overload resolution prefer open(const char *, int) over
56 * open(const char *, int, ...).
57 */
58__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +000059int open(const char* _Nonnull const __pass_object_size pathname, int flags)
George Burgess IV0df4c372017-07-31 21:39:33 -070060 __overloadable
Elliott Hughesb115aef2017-08-04 09:34:19 -070061 __clang_error_if(__open_modes_useful(flags), "'open' " __open_too_few_args_error) {
Elliott Hughesf4ace9d2023-02-23 17:38:37 +000062#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -070063 return __open_2(pathname, flags);
George Burgess IV9349b9e2019-09-18 17:16:01 -070064#else
65 return __open_real(pathname, flags);
George Burgess IV8a0cdb12019-10-21 13:27:57 -070066#endif
George Burgess IVb97049c2017-07-24 15:05:05 -070067}
68
69__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +000070int open(const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
George Burgess IV4e37d532017-08-03 17:11:35 -070071 __overloadable
72 __clang_warning_if(!__open_modes_useful(flags) && modes,
73 "'open' " __open_useless_modes_warning) {
George Burgess IVb97049c2017-07-24 15:05:05 -070074 return __open_real(pathname, flags, modes);
75}
76
77__BIONIC_ERROR_FUNCTION_VISIBILITY
zijunzhao725c96c2023-06-09 00:50:24 +000078int openat(int dirfd, const char* _Nonnull pathname, int flags, mode_t modes, ...)
George Burgess IVb97049c2017-07-24 15:05:05 -070079 __overloadable
80 __errorattr(__open_too_many_args_error);
81
82__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +000083int openat(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags)
George Burgess IV0df4c372017-07-31 21:39:33 -070084 __overloadable
Elliott Hughesb115aef2017-08-04 09:34:19 -070085 __clang_error_if(__open_modes_useful(flags), "'openat' " __open_too_few_args_error) {
Elliott Hughesf4ace9d2023-02-23 17:38:37 +000086#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -070087 return __openat_2(dirfd, pathname, flags);
George Burgess IV9349b9e2019-09-18 17:16:01 -070088#else
89 return __openat_real(dirfd, pathname, flags);
George Burgess IV8a0cdb12019-10-21 13:27:57 -070090#endif
George Burgess IVb97049c2017-07-24 15:05:05 -070091}
92
93__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +000094int openat(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
George Burgess IV4e37d532017-08-03 17:11:35 -070095 __overloadable
96 __clang_warning_if(!__open_modes_useful(flags) && modes,
97 "'openat' " __open_useless_modes_warning) {
George Burgess IVb97049c2017-07-24 15:05:05 -070098 return __openat_real(dirfd, pathname, flags, modes);
99}
George Burgess IVb97049c2017-07-24 15:05:05 -0700100
George Burgess IV9349b9e2019-09-18 17:16:01 -0700101/* Note that open == open64, so we reuse those bits in the open64 variants below. */
George Burgess IV2356c932019-06-06 17:18:13 -0700102
103__BIONIC_ERROR_FUNCTION_VISIBILITY
zijunzhao725c96c2023-06-09 00:50:24 +0000104int open64(const char* _Nonnull pathname, int flags, mode_t modes, ...) __overloadable
George Burgess IV2356c932019-06-06 17:18:13 -0700105 __errorattr(__open_too_many_args_error);
106
107__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +0000108int open64(const char* _Nonnull const __pass_object_size pathname, int flags)
George Burgess IV2356c932019-06-06 17:18:13 -0700109 __overloadable
110 __clang_error_if(__open_modes_useful(flags), "'open64' " __open_too_few_args_error) {
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700111 return open(pathname, flags);
George Burgess IV2356c932019-06-06 17:18:13 -0700112}
113
114__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +0000115int open64(const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
George Burgess IV2356c932019-06-06 17:18:13 -0700116 __overloadable
117 __clang_warning_if(!__open_modes_useful(flags) && modes,
118 "'open64' " __open_useless_modes_warning) {
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700119 return open(pathname, flags, modes);
George Burgess IV2356c932019-06-06 17:18:13 -0700120}
121
122__BIONIC_ERROR_FUNCTION_VISIBILITY
zijunzhao725c96c2023-06-09 00:50:24 +0000123int openat64(int dirfd, const char* _Nonnull pathname, int flags, mode_t modes, ...)
George Burgess IV2356c932019-06-06 17:18:13 -0700124 __overloadable
125 __errorattr(__open_too_many_args_error);
126
127__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +0000128int openat64(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags)
George Burgess IV2356c932019-06-06 17:18:13 -0700129 __overloadable
130 __clang_error_if(__open_modes_useful(flags), "'openat64' " __open_too_few_args_error) {
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700131 return openat(dirfd, pathname, flags);
George Burgess IV2356c932019-06-06 17:18:13 -0700132}
133
134__BIONIC_FORTIFY_INLINE
zijunzhao725c96c2023-06-09 00:50:24 +0000135int openat64(int dirfd, const char* _Nonnull const __pass_object_size pathname, int flags, mode_t modes)
George Burgess IV2356c932019-06-06 17:18:13 -0700136 __overloadable
137 __clang_warning_if(!__open_modes_useful(flags) && modes,
138 "'openat64' " __open_useless_modes_warning) {
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700139 return openat(dirfd, pathname, flags, modes);
George Burgess IV2356c932019-06-06 17:18:13 -0700140}
George Burgess IV2356c932019-06-06 17:18:13 -0700141
George Burgess IVb97049c2017-07-24 15:05:05 -0700142#undef __open_too_many_args_error
143#undef __open_too_few_args_error
George Burgess IV4e37d532017-08-03 17:11:35 -0700144#undef __open_useless_modes_warning
145#undef __open_modes_useful
George Burgess IVb97049c2017-07-24 15:05:05 -0700146#endif /* defined(__BIONIC_FORTIFY) */