fcntl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Copyright (C) 1991-2021 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /*
  15. * POSIX Standard: 6.5 File Control Operations <fcntl.h>
  16. */
  17. #ifndef _FCNTL_H
  18. #define _FCNTL_H 1
  19. #include <features.h>
  20. /* This must be early so <bits/fcntl.h> can define types winningly. */
  21. __BEGIN_DECLS
  22. /* Get __mode_t, __dev_t and __off_t .*/
  23. #include <bits/types.h>
  24. /* Get the definitions of O_*, F_*, FD_*: all the
  25. numbers and flag bits for `open', `fcntl', et al. */
  26. #include <bits/fcntl.h>
  27. /* Detect if open needs mode as a third argument (or for openat as a fourth
  28. argument). */
  29. #ifdef __O_TMPFILE
  30. # define __OPEN_NEEDS_MODE(oflag) \
  31. (((oflag) & O_CREAT) != 0 || ((oflag) & __O_TMPFILE) == __O_TMPFILE)
  32. #else
  33. # define __OPEN_NEEDS_MODE(oflag) (((oflag) & O_CREAT) != 0)
  34. #endif
  35. /* POSIX.1-2001 specifies that these types are defined by <fcntl.h>.
  36. Earlier POSIX standards permitted any type ending in `_t' to be defined
  37. by any POSIX header, so we don't conditionalize the definitions here. */
  38. #ifndef __mode_t_defined
  39. typedef __mode_t mode_t;
  40. # define __mode_t_defined
  41. #endif
  42. #ifndef __off_t_defined
  43. # ifndef __USE_FILE_OFFSET64
  44. typedef __off_t off_t;
  45. # else
  46. typedef __off64_t off_t;
  47. # endif
  48. # define __off_t_defined
  49. #endif
  50. #if defined __USE_LARGEFILE64 && !defined __off64_t_defined
  51. typedef __off64_t off64_t;
  52. # define __off64_t_defined
  53. #endif
  54. #ifndef __pid_t_defined
  55. typedef __pid_t pid_t;
  56. # define __pid_t_defined
  57. #endif
  58. /* For XPG all symbols from <sys/stat.h> should also be available. */
  59. #ifdef __USE_XOPEN2K8
  60. # include <bits/types/struct_timespec.h>
  61. #endif
  62. #if defined __USE_XOPEN || defined __USE_XOPEN2K8
  63. # include <bits/stat.h>
  64. # define S_IFMT __S_IFMT
  65. # define S_IFDIR __S_IFDIR
  66. # define S_IFCHR __S_IFCHR
  67. # define S_IFBLK __S_IFBLK
  68. # define S_IFREG __S_IFREG
  69. # ifdef __S_IFIFO
  70. # define S_IFIFO __S_IFIFO
  71. # endif
  72. # ifdef __S_IFLNK
  73. # define S_IFLNK __S_IFLNK
  74. # endif
  75. # if (defined __USE_UNIX98 || defined __USE_XOPEN2K8) && defined __S_IFSOCK
  76. # define S_IFSOCK __S_IFSOCK
  77. # endif
  78. /* Protection bits. */
  79. # define S_ISUID __S_ISUID /* Set user ID on execution. */
  80. # define S_ISGID __S_ISGID /* Set group ID on execution. */
  81. # if defined __USE_MISC || defined __USE_XOPEN
  82. /* Save swapped text after use (sticky bit). This is pretty well obsolete. */
  83. # define S_ISVTX __S_ISVTX
  84. # endif
  85. # define S_IRUSR __S_IREAD /* Read by owner. */
  86. # define S_IWUSR __S_IWRITE /* Write by owner. */
  87. # define S_IXUSR __S_IEXEC /* Execute by owner. */
  88. /* Read, write, and execute by owner. */
  89. # define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC)
  90. # define S_IRGRP (S_IRUSR >> 3) /* Read by group. */
  91. # define S_IWGRP (S_IWUSR >> 3) /* Write by group. */
  92. # define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */
  93. /* Read, write, and execute by group. */
  94. # define S_IRWXG (S_IRWXU >> 3)
  95. # define S_IROTH (S_IRGRP >> 3) /* Read by others. */
  96. # define S_IWOTH (S_IWGRP >> 3) /* Write by others. */
  97. # define S_IXOTH (S_IXGRP >> 3) /* Execute by others. */
  98. /* Read, write, and execute by others. */
  99. # define S_IRWXO (S_IRWXG >> 3)
  100. #endif
  101. #ifdef __USE_MISC
  102. # ifndef R_OK /* Verbatim from <unistd.h>. Ugh. */
  103. /* Values for the second argument to access.
  104. These may be OR'd together. */
  105. # define R_OK 4 /* Test for read permission. */
  106. # define W_OK 2 /* Test for write permission. */
  107. # define X_OK 1 /* Test for execute permission. */
  108. # define F_OK 0 /* Test for existence. */
  109. # endif
  110. #endif /* Use misc. */
  111. /* XPG wants the following symbols. <stdio.h> has the same definitions. */
  112. #if defined __USE_XOPEN || defined __USE_XOPEN2K8
  113. # define SEEK_SET 0 /* Seek from beginning of file. */
  114. # define SEEK_CUR 1 /* Seek from current position. */
  115. # define SEEK_END 2 /* Seek from end of file. */
  116. #endif /* XPG */
  117. /* The constants AT_REMOVEDIR and AT_EACCESS have the same value. AT_EACCESS
  118. is meaningful only to faccessat, while AT_REMOVEDIR is meaningful only to
  119. unlinkat. The two functions do completely different things and therefore,
  120. the flags can be allowed to overlap. For example, passing AT_REMOVEDIR to
  121. faccessat would be undefined behavior and thus treating it equivalent to
  122. AT_EACCESS is valid undefined behavior. */
  123. #ifdef __USE_ATFILE
  124. # define AT_FDCWD -100 /* Special value used to indicate
  125. the *at functions should use the
  126. current working directory. */
  127. # define AT_SYMLINK_NOFOLLOW 0x100 /* Do not follow symbolic links. */
  128. # define AT_REMOVEDIR 0x200 /* Remove directory instead of
  129. unlinking file. */
  130. # define AT_SYMLINK_FOLLOW 0x400 /* Follow symbolic links. */
  131. # ifdef __USE_GNU
  132. # define AT_NO_AUTOMOUNT 0x800 /* Suppress terminal automount
  133. traversal. */
  134. # define AT_EMPTY_PATH 0x1000 /* Allow empty relative pathname. */
  135. # define AT_STATX_SYNC_TYPE 0x6000
  136. # define AT_STATX_SYNC_AS_STAT 0x0000
  137. # define AT_STATX_FORCE_SYNC 0x2000
  138. # define AT_STATX_DONT_SYNC 0x4000
  139. # define AT_RECURSIVE 0x8000 /* Apply to the entire subtree. */
  140. # endif
  141. # define AT_EACCESS 0x200 /* Test access permitted for
  142. effective IDs, not real IDs. */
  143. #endif
  144. /* Do the file control operation described by CMD on FD.
  145. The remaining arguments are interpreted depending on CMD.
  146. This function is a cancellation point and therefore not marked with
  147. __THROW. */
  148. #ifndef __USE_FILE_OFFSET64
  149. extern int fcntl (int __fd, int __cmd, ...);
  150. #else
  151. # ifdef __REDIRECT
  152. extern int __REDIRECT (fcntl, (int __fd, int __cmd, ...), fcntl64);
  153. # else
  154. # define fcntl fcntl64
  155. # endif
  156. #endif
  157. #ifdef __USE_LARGEFILE64
  158. extern int fcntl64 (int __fd, int __cmd, ...);
  159. #endif
  160. /* Open FILE and return a new file descriptor for it, or -1 on error.
  161. OFLAG determines the type of access used. If O_CREAT or O_TMPFILE is set
  162. in OFLAG, the third argument is taken as a `mode_t', the mode of the
  163. created file.
  164. This function is a cancellation point and therefore not marked with
  165. __THROW. */
  166. #ifndef __USE_FILE_OFFSET64
  167. extern int open (const char *__file, int __oflag, ...) __nonnull ((1));
  168. #else
  169. # ifdef __REDIRECT
  170. extern int __REDIRECT (open, (const char *__file, int __oflag, ...), open64)
  171. __nonnull ((1));
  172. # else
  173. # define open open64
  174. # endif
  175. #endif
  176. #ifdef __USE_LARGEFILE64
  177. extern int open64 (const char *__file, int __oflag, ...) __nonnull ((1));
  178. #endif
  179. #ifdef __USE_ATFILE
  180. /* Similar to `open' but a relative path name is interpreted relative to
  181. the directory for which FD is a descriptor.
  182. NOTE: some other `openat' implementation support additional functionality
  183. through this interface, especially using the O_XATTR flag. This is not
  184. yet supported here.
  185. This function is a cancellation point and therefore not marked with
  186. __THROW. */
  187. # ifndef __USE_FILE_OFFSET64
  188. extern int openat (int __fd, const char *__file, int __oflag, ...)
  189. __nonnull ((2));
  190. # else
  191. # ifdef __REDIRECT
  192. extern int __REDIRECT (openat, (int __fd, const char *__file, int __oflag,
  193. ...), openat64) __nonnull ((2));
  194. # else
  195. # define openat openat64
  196. # endif
  197. # endif
  198. # ifdef __USE_LARGEFILE64
  199. extern int openat64 (int __fd, const char *__file, int __oflag, ...)
  200. __nonnull ((2));
  201. # endif
  202. #endif
  203. /* Create and open FILE, with mode MODE. This takes an `int' MODE
  204. argument because that is what `mode_t' will be widened to.
  205. This function is a cancellation point and therefore not marked with
  206. __THROW. */
  207. #ifndef __USE_FILE_OFFSET64
  208. extern int creat (const char *__file, mode_t __mode) __nonnull ((1));
  209. #else
  210. # ifdef __REDIRECT
  211. extern int __REDIRECT (creat, (const char *__file, mode_t __mode),
  212. creat64) __nonnull ((1));
  213. # else
  214. # define creat creat64
  215. # endif
  216. #endif
  217. #ifdef __USE_LARGEFILE64
  218. extern int creat64 (const char *__file, mode_t __mode) __nonnull ((1));
  219. #endif
  220. #if !defined F_LOCK && (defined __USE_MISC || (defined __USE_XOPEN_EXTENDED \
  221. && !defined __USE_POSIX))
  222. /* NOTE: These declarations also appear in <unistd.h>; be sure to keep both
  223. files consistent. Some systems have them there and some here, and some
  224. software depends on the macros being defined without including both. */
  225. /* `lockf' is a simpler interface to the locking facilities of `fcntl'.
  226. LEN is always relative to the current file position.
  227. The CMD argument is one of the following. */
  228. # define F_ULOCK 0 /* Unlock a previously locked region. */
  229. # define F_LOCK 1 /* Lock a region for exclusive use. */
  230. # define F_TLOCK 2 /* Test and lock a region for exclusive use. */
  231. # define F_TEST 3 /* Test a region for other processes locks. */
  232. # ifndef __USE_FILE_OFFSET64
  233. extern int lockf (int __fd, int __cmd, off_t __len);
  234. # else
  235. # ifdef __REDIRECT
  236. extern int __REDIRECT (lockf, (int __fd, int __cmd, __off64_t __len), lockf64);
  237. # else
  238. # define lockf lockf64
  239. # endif
  240. # endif
  241. # ifdef __USE_LARGEFILE64
  242. extern int lockf64 (int __fd, int __cmd, off64_t __len);
  243. # endif
  244. #endif
  245. #ifdef __USE_XOPEN2K
  246. /* Advice the system about the expected behaviour of the application with
  247. respect to the file associated with FD. */
  248. # ifndef __USE_FILE_OFFSET64
  249. extern int posix_fadvise (int __fd, off_t __offset, off_t __len,
  250. int __advise) __THROW;
  251. # else
  252. # ifdef __REDIRECT_NTH
  253. extern int __REDIRECT_NTH (posix_fadvise, (int __fd, __off64_t __offset,
  254. __off64_t __len, int __advise),
  255. posix_fadvise64);
  256. # else
  257. # define posix_fadvise posix_fadvise64
  258. # endif
  259. # endif
  260. # ifdef __USE_LARGEFILE64
  261. extern int posix_fadvise64 (int __fd, off64_t __offset, off64_t __len,
  262. int __advise) __THROW;
  263. # endif
  264. /* Reserve storage for the data of the file associated with FD.
  265. This function is a possible cancellation point and therefore not
  266. marked with __THROW. */
  267. # ifndef __USE_FILE_OFFSET64
  268. extern int posix_fallocate (int __fd, off_t __offset, off_t __len);
  269. # else
  270. # ifdef __REDIRECT
  271. extern int __REDIRECT (posix_fallocate, (int __fd, __off64_t __offset,
  272. __off64_t __len),
  273. posix_fallocate64);
  274. # else
  275. # define posix_fallocate posix_fallocate64
  276. # endif
  277. # endif
  278. # ifdef __USE_LARGEFILE64
  279. extern int posix_fallocate64 (int __fd, off64_t __offset, off64_t __len);
  280. # endif
  281. #endif
  282. /* Define some inlines helping to catch common problems. */
  283. #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function \
  284. && defined __va_arg_pack_len
  285. # include <bits/fcntl2.h>
  286. #endif
  287. __END_DECLS
  288. #endif /* fcntl.h */