fcntl2.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Checking macros for fcntl functions.
  17. Copyright (C) 2007-2026 Free Software Foundation, Inc.
  18. This file is part of the GNU C Library.
  19. The GNU C Library is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2.1 of the License, or (at your option) any later version.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <https://www.gnu.org/licenses/>. */
  30. #ifndef _FCNTL_H
  31. # error "Never include <bits/fcntl2.h> directly; use <fcntl.h> instead."
  32. #endif
  33. /* Check that calls to open and openat with O_CREAT or O_TMPFILE set have an
  34. appropriate third/fourth parameter. */
  35. #ifndef __USE_FILE_OFFSET64
  36. extern int __open_2 (const char *__path, int __oflag) __nonnull ((1));
  37. extern int __REDIRECT (__open_alias, (const char *__path, int __oflag, ...),
  38. open) __nonnull ((1));
  39. #else
  40. extern int __REDIRECT (__open_2, (const char *__path, int __oflag),
  41. __open64_2) __nonnull ((1));
  42. extern int __REDIRECT (__open_alias, (const char *__path, int __oflag, ...),
  43. open64) __nonnull ((1));
  44. #endif
  45. #ifdef __va_arg_pack_len
  46. __errordecl (__open_too_many_args,
  47. "open can be called either with 2 or 3 arguments, not more");
  48. __errordecl (__open_missing_mode,
  49. "open with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
  50. __fortify_function int
  51. open (const char *__path, int __oflag, ...)
  52. {
  53. if (__va_arg_pack_len () > 1)
  54. __open_too_many_args ();
  55. if (__builtin_constant_p (__oflag))
  56. {
  57. if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
  58. {
  59. __open_missing_mode ();
  60. return __open_2 (__path, __oflag);
  61. }
  62. return __open_alias (__path, __oflag, __va_arg_pack ());
  63. }
  64. if (__va_arg_pack_len () < 1)
  65. return __open_2 (__path, __oflag);
  66. return __open_alias (__path, __oflag, __va_arg_pack ());
  67. }
  68. #elif __fortify_use_clang
  69. __fortify_function __attribute_overloadable__ int
  70. open (__fortify_clang_overload_arg (const char *, ,__path), int __oflag)
  71. __fortify_clang_error (__OPEN_NEEDS_MODE (__oflag),
  72. "open with O_CREAT or O_TMPFILE in second argument needs 3 arguments")
  73. {
  74. return __open_2 (__path, __oflag);
  75. }
  76. __fortify_function __attribute_overloadable__ int
  77. open (__fortify_clang_overload_arg (const char *, ,__path), int __oflag,
  78. mode_t __mode)
  79. {
  80. return __open_alias (__path, __oflag, __mode);
  81. }
  82. #endif
  83. #ifdef __USE_LARGEFILE64
  84. extern int __open64_2 (const char *__path, int __oflag) __nonnull ((1));
  85. extern int __REDIRECT (__open64_alias, (const char *__path, int __oflag,
  86. ...), open64) __nonnull ((1));
  87. # ifdef __va_arg_pack_len
  88. __errordecl (__open64_too_many_args,
  89. "open64 can be called either with 2 or 3 arguments, not more");
  90. __errordecl (__open64_missing_mode,
  91. "open64 with O_CREAT or O_TMPFILE in second argument needs 3 arguments");
  92. __fortify_function int
  93. open64 (const char *__path, int __oflag, ...)
  94. {
  95. if (__va_arg_pack_len () > 1)
  96. __open64_too_many_args ();
  97. if (__builtin_constant_p (__oflag))
  98. {
  99. if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
  100. {
  101. __open64_missing_mode ();
  102. return __open64_2 (__path, __oflag);
  103. }
  104. return __open64_alias (__path, __oflag, __va_arg_pack ());
  105. }
  106. if (__va_arg_pack_len () < 1)
  107. return __open64_2 (__path, __oflag);
  108. return __open64_alias (__path, __oflag, __va_arg_pack ());
  109. }
  110. # elif __fortify_use_clang
  111. __fortify_function_error_function __attribute_overloadable__ int
  112. open64 (const char *__path, int __oflag, mode_t __mode, ...)
  113. __fortify_clang_unavailable ("open64 can be called either with 2 or 3 arguments, not more");
  114. __fortify_function __attribute_overloadable__ int
  115. open64 (__fortify_clang_overload_arg (const char *, ,__path), int __oflag)
  116. __fortify_clang_prefer_this_overload
  117. __fortify_clang_error (__OPEN_NEEDS_MODE (__oflag),
  118. "open64 with O_CREAT or O_TMPFILE in second argument needs 3 arguments")
  119. {
  120. return __open64_2 (__path, __oflag);
  121. }
  122. __fortify_function __attribute_overloadable__ int
  123. open64 (__fortify_clang_overload_arg (const char *, ,__path), int __oflag,
  124. mode_t __mode)
  125. {
  126. return __open64_alias (__path, __oflag, __mode);
  127. }
  128. # endif
  129. #endif
  130. #ifdef __USE_ATFILE
  131. # ifndef __USE_FILE_OFFSET64
  132. extern int __openat_2 (int __fd, const char *__path, int __oflag)
  133. __nonnull ((2));
  134. extern int __REDIRECT (__openat_alias, (int __fd, const char *__path,
  135. int __oflag, ...), openat)
  136. __nonnull ((2));
  137. # else
  138. extern int __REDIRECT (__openat_2, (int __fd, const char *__path,
  139. int __oflag), __openat64_2)
  140. __nonnull ((2));
  141. extern int __REDIRECT (__openat_alias, (int __fd, const char *__path,
  142. int __oflag, ...), openat64)
  143. __nonnull ((2));
  144. # endif
  145. # ifdef __va_arg_pack_len
  146. __errordecl (__openat_too_many_args,
  147. "openat can be called either with 3 or 4 arguments, not more");
  148. __errordecl (__openat_missing_mode,
  149. "openat with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
  150. __fortify_function int
  151. openat (int __fd, const char *__path, int __oflag, ...)
  152. {
  153. if (__va_arg_pack_len () > 1)
  154. __openat_too_many_args ();
  155. if (__builtin_constant_p (__oflag))
  156. {
  157. if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
  158. {
  159. __openat_missing_mode ();
  160. return __openat_2 (__fd, __path, __oflag);
  161. }
  162. return __openat_alias (__fd, __path, __oflag, __va_arg_pack ());
  163. }
  164. if (__va_arg_pack_len () < 1)
  165. return __openat_2 (__fd, __path, __oflag);
  166. return __openat_alias (__fd, __path, __oflag, __va_arg_pack ());
  167. }
  168. # elif __fortify_use_clang
  169. __fortify_function_error_function __attribute_overloadable__ int
  170. openat (int __fd, const char *__path, int __oflag, mode_t __mode, ...)
  171. __fortify_clang_unavailable ("openat can be called either with 3 or 4 arguments, not more");
  172. __fortify_function __attribute_overloadable__ int
  173. openat (int __fd, __fortify_clang_overload_arg (const char *, ,__path),
  174. int __oflag)
  175. __fortify_clang_prefer_this_overload
  176. __fortify_clang_error (__OPEN_NEEDS_MODE (__oflag),
  177. "openat with O_CREAT or O_TMPFILE in third argument needs 4 arguments")
  178. {
  179. return __openat_2 (__fd, __path, __oflag);
  180. }
  181. __fortify_function __attribute_overloadable__ int
  182. openat (int __fd, __fortify_clang_overload_arg (const char *, ,__path),
  183. int __oflag, mode_t __mode)
  184. {
  185. return __openat_alias (__fd, __path, __oflag, __mode);
  186. }
  187. # endif
  188. # ifdef __USE_LARGEFILE64
  189. extern int __openat64_2 (int __fd, const char *__path, int __oflag)
  190. __nonnull ((2));
  191. extern int __REDIRECT (__openat64_alias, (int __fd, const char *__path,
  192. int __oflag, ...), openat64)
  193. __nonnull ((2));
  194. __errordecl (__openat64_too_many_args,
  195. "openat64 can be called either with 3 or 4 arguments, not more");
  196. __errordecl (__openat64_missing_mode,
  197. "openat64 with O_CREAT or O_TMPFILE in third argument needs 4 arguments");
  198. # ifdef __va_arg_pack_len
  199. __fortify_function int
  200. openat64 (int __fd, const char *__path, int __oflag, ...)
  201. {
  202. if (__va_arg_pack_len () > 1)
  203. __openat64_too_many_args ();
  204. if (__builtin_constant_p (__oflag))
  205. {
  206. if (__OPEN_NEEDS_MODE (__oflag) && __va_arg_pack_len () < 1)
  207. {
  208. __openat64_missing_mode ();
  209. return __openat64_2 (__fd, __path, __oflag);
  210. }
  211. return __openat64_alias (__fd, __path, __oflag, __va_arg_pack ());
  212. }
  213. if (__va_arg_pack_len () < 1)
  214. return __openat64_2 (__fd, __path, __oflag);
  215. return __openat64_alias (__fd, __path, __oflag, __va_arg_pack ());
  216. }
  217. # elif __fortify_use_clang
  218. __fortify_function_error_function __attribute_overloadable__ int
  219. openat64 (int __fd, const char *__path, int __oflag, mode_t __mode, ...)
  220. __fortify_clang_unavailable ("openat64 can be called either with 3 or 4 arguments, not more");
  221. __fortify_function __attribute_overloadable__ int
  222. openat64 (int __fd, __fortify_clang_overload_arg (const char *, ,__path),
  223. int __oflag)
  224. __fortify_clang_prefer_this_overload
  225. __fortify_clang_error (__OPEN_NEEDS_MODE (__oflag),
  226. "openat64 with O_CREAT or O_TMPFILE in third argument needs 4 arguments")
  227. {
  228. return __openat64_2 (__fd, __path, __oflag);
  229. }
  230. __fortify_function __attribute_overloadable__ int
  231. openat64 (int __fd, __fortify_clang_overload_arg (const char *, ,__path),
  232. int __oflag, mode_t __mode)
  233. {
  234. return __openat64_alias (__fd, __path, __oflag, __mode);
  235. }
  236. # endif
  237. # endif
  238. #endif