1 /* Checking macros for fcntl functions.
2 Copyright (C) 2007-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _FCNTL_H
20 # error "Never include <bits/fcntl2.h> directly; use <fcntl.h> instead."
21 #endif
22
23 /* Check that calls to open and openat with O_CREAT or O_TMPFILE set have an
24 appropriate third/fourth parameter. */
25 #ifndef __USE_FILE_OFFSET64
26 extern int __open_2 (const char *__path, int __oflag) __nonnull ((1));
27 extern int __REDIRECT (__open_alias, (const char *__path, int __oflag, ...),
28 open) __nonnull ((1));
29 #else
30 extern int __REDIRECT (__open_2, (const char *__path, int __oflag),
31 __open64_2) __nonnull ((1));
32 extern int __REDIRECT (__open_alias, (const char *__path, int __oflag, ...),
33 open64) __nonnull ((1));
34 #endif
35 __errordecl (__open_too_many_args,
36 "open can be called either with 2 or 3 arguments, not more");
37 __errordecl (__open_missing_mode,
38 "open with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
39
40 __fortify_function int
open(const char * __path,int __oflag,...)41 open (const char *__path, int __oflag, ...)
42 {
43 if (__va_arg_pack_len () > 1)
44 __open_too_many_args ();
45
46 if (__builtin_constant_p (__oflag))
47 {
48 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
49 {
50 __open_missing_mode ();
51 return __open_2 (__path, __oflag);
52 }
53 return __open_alias (__path, __oflag, __va_arg_pack ());
54 }
55
56 if (__va_arg_pack_len () < 1)
57 return __open_2 (__path, __oflag);
58
59 return __open_alias (__path, __oflag, __va_arg_pack ());
60 }
61
62
63 #ifdef __USE_LARGEFILE64
64 extern int __open64_2 (const char *__path, int __oflag) __nonnull ((1));
65 extern int __REDIRECT (__open64_alias, (const char *__path, int __oflag,
66 ...), open64) __nonnull ((1));
67 __errordecl (__open64_too_many_args,
68 "open64 can be called either with 2 or 3 arguments, not more");
69 __errordecl (__open64_missing_mode,
70 "open64 with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
71
72 __fortify_function int
open64(const char * __path,int __oflag,...)73 open64 (const char *__path, int __oflag, ...)
74 {
75 if (__va_arg_pack_len () > 1)
76 __open64_too_many_args ();
77
78 if (__builtin_constant_p (__oflag))
79 {
80 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
81 {
82 __open64_missing_mode ();
83 return __open64_2 (__path, __oflag);
84 }
85 return __open64_alias (__path, __oflag, __va_arg_pack ());
86 }
87
88 if (__va_arg_pack_len () < 1)
89 return __open64_2 (__path, __oflag);
90
91 return __open64_alias (__path, __oflag, __va_arg_pack ());
92 }
93 #endif
94
95
96 #ifdef __USE_ATFILE
97 # ifndef __USE_FILE_OFFSET64
98 extern int __openat_2 (int __fd, const char *__path, int __oflag)
99 __nonnull ((2));
100 extern int __REDIRECT (__openat_alias, (int __fd, const char *__path,
101 int __oflag, ...), openat)
102 __nonnull ((2));
103 # else
104 extern int __REDIRECT (__openat_2, (int __fd, const char *__path,
105 int __oflag), __openat64_2)
106 __nonnull ((2));
107 extern int __REDIRECT (__openat_alias, (int __fd, const char *__path,
108 int __oflag, ...), openat64)
109 __nonnull ((2));
110 # endif
111 __errordecl (__openat_too_many_args,
112 "openat can be called either with 3 or 4 arguments, not more");
113 __errordecl (__openat_missing_mode,
114 "openat with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
115
116 __fortify_function int
openat(int __fd,const char * __path,int __oflag,...)117 openat (int __fd, const char *__path, int __oflag, ...)
118 {
119 if (__va_arg_pack_len () > 1)
120 __openat_too_many_args ();
121
122 if (__builtin_constant_p (__oflag))
123 {
124 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
125 {
126 __openat_missing_mode ();
127 return __openat_2 (__fd, __path, __oflag);
128 }
129 return __openat_alias (__fd, __path, __oflag, __va_arg_pack ());
130 }
131
132 if (__va_arg_pack_len () < 1)
133 return __openat_2 (__fd, __path, __oflag);
134
135 return __openat_alias (__fd, __path, __oflag, __va_arg_pack ());
136 }
137
138
139 # ifdef __USE_LARGEFILE64
140 extern int __openat64_2 (int __fd, const char *__path, int __oflag)
141 __nonnull ((2));
142 extern int __REDIRECT (__openat64_alias, (int __fd, const char *__path,
143 int __oflag, ...), openat64)
144 __nonnull ((2));
145 __errordecl (__openat64_too_many_args,
146 "openat64 can be called either with 3 or 4 arguments, not more");
147 __errordecl (__openat64_missing_mode,
148 "openat64 with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
149
150 __fortify_function int
openat64(int __fd,const char * __path,int __oflag,...)151 openat64 (int __fd, const char *__path, int __oflag, ...)
152 {
153 if (__va_arg_pack_len () > 1)
154 __openat64_too_many_args ();
155
156 if (__builtin_constant_p (__oflag))
157 {
158 if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
159 {
160 __openat64_missing_mode ();
161 return __openat64_2 (__fd, __path, __oflag);
162 }
163 return __openat64_alias (__fd, __path, __oflag, __va_arg_pack ());
164 }
165
166 if (__va_arg_pack_len () < 1)
167 return __openat64_2 (__fd, __path, __oflag);
168
169 return __openat64_alias (__fd, __path, __oflag, __va_arg_pack ());
170 }
171 # endif
172 #endif
173