xref: /third_party/musl/include/fortify/linux/fcntl.h (revision 570af302)
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef _FCNTL_H
17#error "Never include this file directly; instead, include <fcntl.h>"
18#endif
19
20#include "fortify.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26int __open_chk(const char*, int);
27int __openat_chk(int, const char*, int);
28#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
29int __open64_chk(const char*, int);
30int __openat64_chk(int, const char*, int);
31#endif
32/*
33 * Even in musl FORTIFY, the following is the easiest way to call a real open.
34 */
35int __open_real(const char*, int, ...) __DIAGNOSE_RENAME(open);
36int __openat_real(int, const char*, int, ...) __DIAGNOSE_RENAME(openat);
37#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
38int __open64_real(const char*, int, ...) __DIAGNOSE_RENAME(open64);
39int __openat64_real(int, const char*, int, ...) __DIAGNOSE_RENAME(openat64);
40#endif
41
42#ifdef __FORTIFY_COMPILATION
43__DIAGNOSE_FORTIFY_INLINE
44int open(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags)
45__DIAGNOSE_OVERLOAD
46__DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'open' " OPEN_TOO_FEW_ARGS_ERROR)
47{
48#ifdef __FORTIFY_RUNTIME
49    return __open_chk(path, flags);
50#else
51    return __open_real(path, flags);
52#endif
53}
54
55__DIAGNOSE_FORTIFY_INLINE
56int open(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, unsigned modes)
57__DIAGNOSE_OVERLOAD
58__DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'open' " OPEN_USELESS_MODES_WARNING)
59{
60    return __open_real(path, flags, modes);
61}
62
63__DIAGNOSE_FORTIFY_INLINE
64int openat(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags)
65__DIAGNOSE_OVERLOAD
66__DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'openat' " OPEN_TOO_FEW_ARGS_ERROR)
67{
68#ifdef __FORTIFY_RUNTIME
69    return __openat_chk(dirfd, path, flags);
70#else
71    return __openat_real(dirfd, path, flags);
72#endif
73}
74
75__DIAGNOSE_FORTIFY_INLINE
76int openat(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, mode_t modes)
77__DIAGNOSE_OVERLOAD
78__DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'openat' " OPEN_USELESS_MODES_WARNING)
79{
80    return __openat_real(dirfd, path, flags, modes);
81}
82
83#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
84__DIAGNOSE_FORTIFY_INLINE
85int open64(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags)
86__DIAGNOSE_OVERLOAD
87__DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'open64' " OPEN_TOO_FEW_ARGS_ERROR)
88{
89#ifdef __FORTIFY_RUNTIME
90    return __open64_chk(path, flags);
91#else
92    return __open64_real(path, flags);
93#endif
94}
95
96__DIAGNOSE_FORTIFY_INLINE
97int open64(const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, mode_t modes)
98__DIAGNOSE_OVERLOAD
99__DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'open64' " OPEN_USELESS_MODES_WARNING)
100{
101    return __open64_real(path, flags, modes);
102}
103
104__DIAGNOSE_FORTIFY_INLINE
105int openat64(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags)
106__DIAGNOSE_OVERLOAD
107__DIAGNOSE_ERROR_IF(__DIAGNOSE_OPEN_MODES_USEFUL(flags), "'openat64' " OPEN_TOO_FEW_ARGS_ERROR)
108{
109#ifdef __FORTIFY_RUNTIME
110    return __openat64_chk(dirfd, path, flags);
111#else
112    return __openat64_real(dirfd, path, flags);
113#endif
114}
115
116__DIAGNOSE_FORTIFY_INLINE
117int openat64(int dirfd, const char* const __DIAGNOSE_PASS_OBJECT_SIZE path, int flags, mode_t modes)
118__DIAGNOSE_OVERLOAD
119__DIAGNOSE_WARNING_IF(!__DIAGNOSE_OPEN_MODES_USEFUL(flags) && modes, "'openat64' " OPEN_USELESS_MODES_WARNING)
120{
121    return __openat64_real(dirfd, path, flags, modes);
122}
123#endif
124
125#endif
126
127#ifdef __cplusplus
128}
129#endif