fcntl-linux.h 16 KB

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