fcntl-linux.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* O_*, F_*, FD_* bit values for Linux.
  2. Copyright (C) 2001-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 use <bits/fcntl-linux.h> directly; include <fcntl.h> instead."
  17. #endif
  18. /* This file contains shared definitions between Linux architectures
  19. and is included by <bits/fcntl.h> to declare them. The various
  20. #ifndef cases allow the architecture specific file to define those
  21. values with different values.
  22. A minimal <bits/fcntl.h> contains just:
  23. struct flock {...}
  24. #ifdef __USE_LARGEFILE64
  25. struct flock64 {...}
  26. #endif
  27. #include <bits/fcntl-linux.h>
  28. */
  29. #ifdef __USE_GNU
  30. # include <bits/types/struct_iovec.h>
  31. #endif
  32. /* open/fcntl. */
  33. #define O_ACCMODE 0003
  34. #define O_RDONLY 00
  35. #define O_WRONLY 01
  36. #define O_RDWR 02
  37. #ifndef O_CREAT
  38. # define O_CREAT 0100 /* Not fcntl. */
  39. #endif
  40. #ifndef O_EXCL
  41. # define O_EXCL 0200 /* Not fcntl. */
  42. #endif
  43. #ifndef O_NOCTTY
  44. # define O_NOCTTY 0400 /* Not fcntl. */
  45. #endif
  46. #ifndef O_TRUNC
  47. # define O_TRUNC 01000 /* Not fcntl. */
  48. #endif
  49. #ifndef O_APPEND
  50. # define O_APPEND 02000
  51. #endif
  52. #ifndef O_NONBLOCK
  53. # define O_NONBLOCK 04000
  54. #endif
  55. #ifndef O_NDELAY
  56. # define O_NDELAY O_NONBLOCK
  57. #endif
  58. #ifndef O_SYNC
  59. # define O_SYNC 04010000
  60. #endif
  61. #define O_FSYNC O_SYNC
  62. #ifndef O_ASYNC
  63. # define O_ASYNC 020000
  64. #endif
  65. #ifndef __O_LARGEFILE
  66. # define __O_LARGEFILE 0100000
  67. #endif
  68. #ifndef __O_DIRECTORY
  69. # define __O_DIRECTORY 0200000
  70. #endif
  71. #ifndef __O_NOFOLLOW
  72. # define __O_NOFOLLOW 0400000
  73. #endif
  74. #ifndef __O_CLOEXEC
  75. # define __O_CLOEXEC 02000000
  76. #endif
  77. #ifndef __O_DIRECT
  78. # define __O_DIRECT 040000
  79. #endif
  80. #ifndef __O_NOATIME
  81. # define __O_NOATIME 01000000
  82. #endif
  83. #ifndef __O_PATH
  84. # define __O_PATH 010000000
  85. #endif
  86. #ifndef __O_DSYNC
  87. # define __O_DSYNC 010000
  88. #endif
  89. #ifndef __O_TMPFILE
  90. # define __O_TMPFILE (020000000 | __O_DIRECTORY)
  91. #endif
  92. #ifndef F_GETLK
  93. # if !defined __USE_FILE_OFFSET64 && __TIMESIZE != 64
  94. # define F_GETLK 5 /* Get record locking info. */
  95. # define F_SETLK 6 /* Set record locking info (non-blocking). */
  96. # define F_SETLKW 7 /* Set record locking info (blocking). */
  97. # else
  98. # define F_GETLK F_GETLK64 /* Get record locking info. */
  99. # define F_SETLK F_SETLK64 /* Set record locking info (non-blocking).*/
  100. # define F_SETLKW F_SETLKW64 /* Set record locking info (blocking). */
  101. # endif
  102. #endif
  103. #ifndef F_GETLK64
  104. # define F_GETLK64 12 /* Get record locking info. */
  105. # define F_SETLK64 13 /* Set record locking info (non-blocking). */
  106. # define F_SETLKW64 14 /* Set record locking info (blocking). */
  107. #endif
  108. /* open file description locks.
  109. Usually record locks held by a process are released on *any* close and are
  110. not inherited across a fork.
  111. These cmd values will set locks that conflict with process-associated record
  112. locks, but are "owned" by the opened file description, not the process.
  113. This means that they are inherited across fork or clone with CLONE_FILES
  114. like BSD (flock) locks, and they are only released automatically when the
  115. last reference to the the file description against which they were acquired
  116. is put. */
  117. #ifdef __USE_GNU
  118. # define F_OFD_GETLK 36
  119. # define F_OFD_SETLK 37
  120. # define F_OFD_SETLKW 38
  121. #endif
  122. #ifdef __USE_LARGEFILE64
  123. # define O_LARGEFILE __O_LARGEFILE
  124. #endif
  125. #ifdef __USE_XOPEN2K8
  126. # define O_DIRECTORY __O_DIRECTORY /* Must be a directory. */
  127. # define O_NOFOLLOW __O_NOFOLLOW /* Do not follow links. */
  128. # define O_CLOEXEC __O_CLOEXEC /* Set close_on_exec. */
  129. #endif
  130. #ifdef __USE_GNU
  131. # define O_DIRECT __O_DIRECT /* Direct disk access. */
  132. # define O_NOATIME __O_NOATIME /* Do not set atime. */
  133. # define O_PATH __O_PATH /* Resolve pathname but do not open file. */
  134. # define O_TMPFILE __O_TMPFILE /* Atomically create nameless file. */
  135. #endif
  136. /* For now, Linux has no separate synchronicity options for read
  137. operations. We define O_RSYNC therefore as the same as O_SYNC
  138. since this is a superset. */
  139. #if defined __USE_POSIX199309 || defined __USE_UNIX98
  140. # define O_DSYNC __O_DSYNC /* Synchronize data. */
  141. # if defined __O_RSYNC
  142. # define O_RSYNC __O_RSYNC /* Synchronize read operations. */
  143. # else
  144. # define O_RSYNC O_SYNC /* Synchronize read operations. */
  145. # endif
  146. #endif
  147. /* Values for the second argument to `fcntl'. */
  148. #define F_DUPFD 0 /* Duplicate file descriptor. */
  149. #define F_GETFD 1 /* Get file descriptor flags. */
  150. #define F_SETFD 2 /* Set file descriptor flags. */
  151. #define F_GETFL 3 /* Get file status flags. */
  152. #define F_SETFL 4 /* Set file status flags. */
  153. #ifndef __F_SETOWN
  154. # define __F_SETOWN 8
  155. # define __F_GETOWN 9
  156. #endif
  157. #if defined __USE_UNIX98 || defined __USE_XOPEN2K8
  158. # define F_SETOWN __F_SETOWN /* Get owner (process receiving SIGIO). */
  159. # define F_GETOWN __F_GETOWN /* Set owner (process receiving SIGIO). */
  160. #endif
  161. #ifndef __F_SETSIG
  162. # define __F_SETSIG 10 /* Set number of signal to be sent. */
  163. # define __F_GETSIG 11 /* Get number of signal to be sent. */
  164. #endif
  165. #ifndef __F_SETOWN_EX
  166. # define __F_SETOWN_EX 15 /* Get owner (thread receiving SIGIO). */
  167. # define __F_GETOWN_EX 16 /* Set owner (thread receiving SIGIO). */
  168. #endif
  169. #ifdef __USE_GNU
  170. # define F_SETSIG __F_SETSIG /* Set number of signal to be sent. */
  171. # define F_GETSIG __F_GETSIG /* Get number of signal to be sent. */
  172. # define F_SETOWN_EX __F_SETOWN_EX /* Get owner (thread receiving SIGIO). */
  173. # define F_GETOWN_EX __F_GETOWN_EX /* Set owner (thread receiving SIGIO). */
  174. #endif
  175. #ifdef __USE_GNU
  176. # define F_SETLEASE 1024 /* Set a lease. */
  177. # define F_GETLEASE 1025 /* Enquire what lease is active. */
  178. # define F_NOTIFY 1026 /* Request notifications on a directory. */
  179. # define F_DUPFD_QUERY 1027 /* Compare two file descriptors for sameness. */
  180. # define F_CREATED_QUERY 1028 /* Was the file just created? */
  181. # define F_SETPIPE_SZ 1031 /* Set pipe page size array. */
  182. # define F_GETPIPE_SZ 1032 /* Set pipe page size array. */
  183. # define F_ADD_SEALS 1033 /* Add seals to file. */
  184. # define F_GET_SEALS 1034 /* Get seals for file. */
  185. /* Set / get write life time hints. */
  186. # define F_GET_RW_HINT 1035
  187. # define F_SET_RW_HINT 1036
  188. # define F_GET_FILE_RW_HINT 1037
  189. # define F_SET_FILE_RW_HINT 1038
  190. #endif
  191. #ifdef __USE_XOPEN2K8
  192. # define F_DUPFD_CLOEXEC 1030 /* Duplicate file descriptor with
  193. close-on-exit set. */
  194. #endif
  195. /* For F_[GET|SET]FD. */
  196. #define FD_CLOEXEC 1 /* Actually anything with low bit set goes */
  197. #ifdef __USE_GNU
  198. # define FD_PIDFS_ROOT -10002 /* Root of the pidfs filesystem */
  199. #endif
  200. #ifndef F_RDLCK
  201. /* For posix fcntl() and `l_type' field of a `struct flock' for lockf(). */
  202. # define F_RDLCK 0 /* Read lock. */
  203. # define F_WRLCK 1 /* Write lock. */
  204. # define F_UNLCK 2 /* Remove lock. */
  205. #endif
  206. /* For old implementation of BSD flock. */
  207. #ifndef F_EXLCK
  208. # define F_EXLCK 4 /* or 3 */
  209. # define F_SHLCK 8 /* or 4 */
  210. #endif
  211. #ifdef __USE_MISC
  212. /* Operations for BSD flock, also used by the kernel implementation. */
  213. # define LOCK_SH 1 /* Shared lock. */
  214. # define LOCK_EX 2 /* Exclusive lock. */
  215. # define LOCK_NB 4 /* Or'd with one of the above to prevent
  216. blocking. */
  217. # define LOCK_UN 8 /* Remove lock. */
  218. #endif
  219. #ifdef __USE_GNU
  220. # define LOCK_MAND 32 /* This is a mandatory flock: */
  221. # define LOCK_READ 64 /* ... which allows concurrent read operations. */
  222. # define LOCK_WRITE 128 /* ... which allows concurrent write operations. */
  223. # define LOCK_RW 192 /* ... Which allows concurrent read & write operations. */
  224. #endif
  225. #ifdef __USE_GNU
  226. /* Types of directory notifications that may be requested with F_NOTIFY. */
  227. # define DN_ACCESS 0x00000001 /* File accessed. */
  228. # define DN_MODIFY 0x00000002 /* File modified. */
  229. # define DN_CREATE 0x00000004 /* File created. */
  230. # define DN_DELETE 0x00000008 /* File removed. */
  231. # define DN_RENAME 0x00000010 /* File renamed. */
  232. # define DN_ATTRIB 0x00000020 /* File changed attributes. */
  233. # define DN_MULTISHOT 0x80000000 /* Don't remove notifier. */
  234. #endif
  235. #ifdef __USE_GNU
  236. /* Owner types. */
  237. enum __pid_type
  238. {
  239. F_OWNER_TID = 0, /* Kernel thread. */
  240. F_OWNER_PID, /* Process. */
  241. F_OWNER_PGRP, /* Process group. */
  242. F_OWNER_GID = F_OWNER_PGRP /* Alternative, obsolete name. */
  243. };
  244. /* Structure to use with F_GETOWN_EX and F_SETOWN_EX. */
  245. struct f_owner_ex
  246. {
  247. enum __pid_type type; /* Owner type of ID. */
  248. __pid_t pid; /* ID of owner. */
  249. };
  250. #endif
  251. #ifdef __USE_GNU
  252. /* Types of seals. */
  253. # define F_SEAL_SEAL 0x0001 /* Prevent further seals from being set. */
  254. # define F_SEAL_SHRINK 0x0002 /* Prevent file from shrinking. */
  255. # define F_SEAL_GROW 0x0004 /* Prevent file from growing. */
  256. # define F_SEAL_WRITE 0x0008 /* Prevent writes. */
  257. # define F_SEAL_FUTURE_WRITE 0x0010 /* Prevent future writes while
  258. mapped. */
  259. # define F_SEAL_EXEC 0x0020 /* Prevent chmod modifying exec bits. */
  260. #endif
  261. #ifdef __USE_GNU
  262. /* Hint values for F_{GET,SET}_RW_HINT. */
  263. # define RWH_WRITE_LIFE_NOT_SET 0
  264. # define RWF_WRITE_LIFE_NOT_SET RWH_WRITE_LIFE_NOT_SET
  265. # define RWH_WRITE_LIFE_NONE 1
  266. # define RWH_WRITE_LIFE_SHORT 2
  267. # define RWH_WRITE_LIFE_MEDIUM 3
  268. # define RWH_WRITE_LIFE_LONG 4
  269. # define RWH_WRITE_LIFE_EXTREME 5
  270. #endif
  271. /* Define some more compatibility macros to be backward compatible with
  272. BSD systems which did not managed to hide these kernel macros. */
  273. #ifdef __USE_MISC
  274. # define FAPPEND O_APPEND
  275. # define FFSYNC O_FSYNC
  276. # define FASYNC O_ASYNC
  277. # define FNONBLOCK O_NONBLOCK
  278. # define FNDELAY O_NDELAY
  279. #endif /* Use misc. */
  280. #ifndef __POSIX_FADV_DONTNEED
  281. # define __POSIX_FADV_DONTNEED 4
  282. # define __POSIX_FADV_NOREUSE 5
  283. #endif
  284. /* Advise to `posix_fadvise'. */
  285. #ifdef __USE_XOPEN2K
  286. # define POSIX_FADV_NORMAL 0 /* No further special treatment. */
  287. # define POSIX_FADV_RANDOM 1 /* Expect random page references. */
  288. # define POSIX_FADV_SEQUENTIAL 2 /* Expect sequential page references. */
  289. # define POSIX_FADV_WILLNEED 3 /* Will need these pages. */
  290. # define POSIX_FADV_DONTNEED __POSIX_FADV_DONTNEED /* Don't need these pages. */
  291. # define POSIX_FADV_NOREUSE __POSIX_FADV_NOREUSE /* Data will be accessed once. */
  292. #endif
  293. #ifdef __USE_GNU
  294. /* Flags for SYNC_FILE_RANGE. */
  295. # define SYNC_FILE_RANGE_WAIT_BEFORE 1 /* Wait upon writeout of all pages
  296. in the range before performing the
  297. write. */
  298. # define SYNC_FILE_RANGE_WRITE 2 /* Initiate writeout of all those
  299. dirty pages in the range which are
  300. not presently under writeback. */
  301. # define SYNC_FILE_RANGE_WAIT_AFTER 4 /* Wait upon writeout of all pages in
  302. the range after performing the
  303. write. */
  304. /* SYNC_FILE_RANGE_WRITE_AND_WAIT ensures all pages in the range are
  305. written to disk before returning. */
  306. # define SYNC_FILE_RANGE_WRITE_AND_WAIT (SYNC_FILE_RANGE_WRITE \
  307. | SYNC_FILE_RANGE_WAIT_BEFORE \
  308. | SYNC_FILE_RANGE_WAIT_AFTER)
  309. /* Flags for SPLICE and VMSPLICE. */
  310. # define SPLICE_F_MOVE 1 /* Move pages instead of copying. */
  311. # define SPLICE_F_NONBLOCK 2 /* Don't block on the pipe splicing
  312. (but we may still block on the fd
  313. we splice from/to). */
  314. # define SPLICE_F_MORE 4 /* Expect more data. */
  315. # define SPLICE_F_GIFT 8 /* Pages passed in are a gift. */
  316. /* Flags for fallocate. */
  317. # include <linux/falloc.h>
  318. /* File handle structure. */
  319. struct file_handle
  320. {
  321. unsigned int handle_bytes;
  322. int handle_type;
  323. /* File identifier. */
  324. unsigned char f_handle[0];
  325. };
  326. /* Maximum handle size (for now). */
  327. # define MAX_HANDLE_SZ 128
  328. #endif
  329. #ifdef __USE_GNU
  330. /* Flags for name_to_handle_at. See comment in fcntl.h about the use
  331. of the same AT_* flag bits for different purposes in different
  332. functions. */
  333. # define AT_HANDLE_FID AT_REMOVEDIR /* File handle is needed
  334. to compare object
  335. identity and may not
  336. be usable to
  337. open_by_handle_at. */
  338. # define AT_HANDLE_MNT_ID_UNIQUE 1 /* Return the 64-bit unique mount
  339. ID. */
  340. # define AT_HANDLE_CONNECTABLE 2 /* Request a connectable file handle */
  341. /* Flags for execveat2(2). */
  342. # define AT_EXECVE_CHECK 0x10000 /* Only perform a check if execution
  343. would be allowed */
  344. #endif
  345. __BEGIN_DECLS
  346. #ifdef __USE_GNU
  347. /* Provide kernel hint to read ahead. */
  348. extern __ssize_t readahead (int __fd, __off64_t __offset, size_t __count)
  349. __THROW;
  350. /* Selective file content synch'ing.
  351. This function is a possible cancellation point and therefore not
  352. marked with __THROW. */
  353. extern int sync_file_range (int __fd, __off64_t __offset, __off64_t __count,
  354. unsigned int __flags);
  355. /* Splice address range into a pipe.
  356. This function is a possible cancellation point and therefore not
  357. marked with __THROW. */
  358. extern __ssize_t vmsplice (int __fdout, const struct iovec *__iov,
  359. size_t __count, unsigned int __flags);
  360. /* Splice two files together.
  361. This function is a possible cancellation point and therefore not
  362. marked with __THROW. */
  363. extern __ssize_t splice (int __fdin, __off64_t *__offin, int __fdout,
  364. __off64_t *__offout, size_t __len,
  365. unsigned int __flags);
  366. /* In-kernel implementation of tee for pipe buffers.
  367. This function is a possible cancellation point and therefore not
  368. marked with __THROW. */
  369. extern __ssize_t tee (int __fdin, int __fdout, size_t __len,
  370. unsigned int __flags);
  371. /* Reserve storage for the data of the file associated with FD.
  372. This function is a possible cancellation point and therefore not
  373. marked with __THROW. */
  374. # ifndef __USE_FILE_OFFSET64
  375. extern int fallocate (int __fd, int __mode, __off_t __offset, __off_t __len);
  376. # else
  377. # ifdef __REDIRECT
  378. extern int __REDIRECT (fallocate, (int __fd, int __mode, __off64_t __offset,
  379. __off64_t __len),
  380. fallocate64);
  381. # else
  382. # define fallocate fallocate64
  383. # endif
  384. # endif
  385. # ifdef __USE_LARGEFILE64
  386. extern int fallocate64 (int __fd, int __mode, __off64_t __offset,
  387. __off64_t __len);
  388. # endif
  389. /* Map file name to file handle. */
  390. extern int name_to_handle_at (int __dfd, const char *__name,
  391. struct file_handle *__handle, int *__mnt_id,
  392. int __flags) __THROW;
  393. /* Open file using the file handle.
  394. This function is a possible cancellation point and therefore not
  395. marked with __THROW. */
  396. extern int open_by_handle_at (int __mountdirfd, struct file_handle *__handle,
  397. int __flags);
  398. #ifdef __has_include
  399. # if __has_include ("linux/openat2.h")
  400. # include "linux/openat2.h"
  401. # define __glibc_has_open_how 1
  402. # endif
  403. #endif
  404. #include <bits/openat2.h>
  405. /* Similar to `openat' but the arguments are packed on HOW with the size
  406. USIZE. If flags and mode from HOW are non-zero, then openat2 operates
  407. like openat.
  408. Unlike openat, unknown or invalid flags result in an error (EINVAL),
  409. rather than being ignored. The mode must be zero unless one of O_CREAT
  410. or O_TMPFILE are set.
  411. The kernel does not support legacy non-LFS interface. */
  412. extern int openat2 (int __dfd, const char * __filename,
  413. const struct open_how * __how,
  414. __SIZE_TYPE__ __usize)
  415. __nonnull ((2, 3));
  416. #endif /* use GNU */
  417. #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
  418. # include <bits/fcntl-linux-fortify.h>
  419. #endif
  420. __END_DECLS