fcntl2.h 8.5 KB

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