1
0

fcntl.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. fcntl.h
  8. Abstract:
  9. This header contains definitions for file control operations.
  10. Author:
  11. Evan Green 14-Mar-2013
  12. --*/
  13. #ifndef _FCNTL_H
  14. #define _FCNTL_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. //
  21. // ---------------------------------------------------------------- Definitions
  22. //
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. //
  27. // Define file control commands.
  28. //
  29. //
  30. // Duplicate a file descriptor.
  31. //
  32. #define F_DUPFD 1
  33. //
  34. // Get file descriptor flags.
  35. //
  36. #define F_GETFD 2
  37. //
  38. // Set file descriptor flags.
  39. //
  40. #define F_SETFD 3
  41. //
  42. // Get status flags and file access modes.
  43. //
  44. #define F_GETFL 4
  45. //
  46. // Set status flags.
  47. //
  48. #define F_SETFL 5
  49. //
  50. // Get record locking information.
  51. //
  52. #define F_GETLK 6
  53. //
  54. // Set record locking information.
  55. //
  56. #define F_SETLK 7
  57. //
  58. // Set record locking information, wait if blocked.
  59. //
  60. #define F_SETLKW 8
  61. //
  62. // Get process or process group ID to receive SIGURG signals.
  63. //
  64. #define F_GETOWN 9
  65. //
  66. // Set process or process group ID to receive SIGURG signals.
  67. //
  68. #define F_SETOWN 10
  69. //
  70. // Close all file descriptors greater than or equal to the given value.
  71. //
  72. #define F_CLOSEM 11
  73. //
  74. // There's no need for 64-bit versions, since off_t is always 64 bits.
  75. //
  76. #define F_GETLK64 F_GETLK
  77. #define F_SETLK64 F_SETLK
  78. #define F_SETLKW64 F_SETLKW
  79. //
  80. // There is no struct flock64, so just define it to be the same as the regular
  81. // structure.
  82. //
  83. #define flock64 flock
  84. //
  85. // Define file creation flags for the open call.
  86. //
  87. //
  88. // Open the file for reading only.
  89. //
  90. #define O_RDONLY 0x00000001
  91. //
  92. // Open the flag for writing only.
  93. //
  94. #define O_WRONLY 0x00000002
  95. //
  96. // Open the flag for reading and writing.
  97. //
  98. #define O_RDWR (O_RDONLY | O_WRONLY)
  99. //
  100. // Define the access mode mask.
  101. //
  102. #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
  103. //
  104. // Set this flag to have all writes append to the end of the file.
  105. //
  106. #define O_APPEND 0x00000008
  107. //
  108. // Set this flag to open the file with execute permissions.
  109. //
  110. #define O_EXEC 0x00000010
  111. //
  112. // Set this flag to open a directory for search only (meaning no reads, but
  113. // it can be used with the at* functions).
  114. //
  115. #define O_SEARCH O_EXEC
  116. //
  117. // Set this flag to open a directory. If the given path does not resolve to
  118. // a directory, then an open attempt fails.
  119. //
  120. #define O_DIRECTORY 0x00000020
  121. //
  122. // Set this flag to fail if the path names a symbolic link. Symbolic links in
  123. // earlier components of the path will still be followed.
  124. //
  125. #define O_NOFOLLOW 0x00000040
  126. //
  127. // Set this flag to cause all I/O to cause writes to be sent down to the
  128. // underlying hardware immediately. When the write function returns, the data
  129. // will be in the hands of the hardware.
  130. //
  131. #define O_SYNC 0x00000080
  132. #define O_DSYNC 0x00000080
  133. #define O_RSYNC 0x00000080
  134. //
  135. // Set this flag to create the file if it doesn't exist.
  136. //
  137. #define O_CREAT 0x00000100
  138. //
  139. // Set this flag if the file should be truncated to a zero size when opened.
  140. //
  141. #define O_TRUNC 0x00000200
  142. //
  143. // Set this flag to create the file exclusively (fail if the file exists).
  144. //
  145. #define O_EXCL 0x00000400
  146. //
  147. // Set this flag if when opening a terminal device, the terminal should not
  148. // become the processes controlling terminal.
  149. //
  150. #define O_NOCTTY 0x00000800
  151. //
  152. // Set this flag to use non-blocking mode, meaning I/O operations return
  153. // immediately if no I/O can be performed at the time of the call.
  154. //
  155. #define O_NONBLOCK 0x00001000
  156. #define O_NDELAY O_NONBLOCK
  157. //
  158. // Set this flag to avoid updating the access time of the file when it is read.
  159. //
  160. #define O_NOATIME 0x00002000
  161. //
  162. // Set this flag to have the handle be automatically closed when an exec
  163. // function is called.
  164. //
  165. #define O_CLOEXEC 0x00004000
  166. //
  167. // Set this flag to open the handle only for path traversal, and with no
  168. // read or write access.
  169. //
  170. #define O_PATH 0x00008000
  171. //
  172. // Set this flag to open the file with asynchronous mode. Note that
  173. // fcntl(F_SETOWN) still needs to be called to fully enable asynchronous mode.
  174. //
  175. #define O_ASYNC 0x00010000
  176. #define FASYNC O_ASYNC
  177. //
  178. // Set this flag to enable opening files whose offsets cannot be described in
  179. // off_t types but can be described in off64_t. Since off_t is always 64-bits,
  180. // this flag is ignored and the definition is provided only for compatibility
  181. // with older operating systems.
  182. //
  183. #define O_LARGEFILE 0x0000
  184. //
  185. // Define file descriptor flags.
  186. //
  187. //
  188. // This flag is set if the file descriptor is closed when a new image is
  189. // executed.
  190. //
  191. #define FD_CLOEXEC 0x0001
  192. //
  193. // Define file lock types.
  194. //
  195. //
  196. // Read locks block write locks, but do not block other read locks.
  197. //
  198. #define F_RDLCK 1
  199. //
  200. // Write locks block any other lock on that portion of the file.
  201. //
  202. #define F_WRLCK 2
  203. //
  204. // The unlock value is used to release a record lock on a region.
  205. //
  206. #define F_UNLCK 3
  207. //
  208. // Supply this value to the at* functions to use the current working directory
  209. // for relative paths (the same behavior as the non-at equivalents).
  210. //
  211. #define AT_FDCWD (-1)
  212. //
  213. // Set this flag to get information for a symbolic link itself, and not the
  214. // destination of the symbolic link.
  215. //
  216. #define AT_SYMLINK_NOFOLLOW 0x00000001
  217. //
  218. // Set this flag to follow a symbolic link.
  219. //
  220. #define AT_SYMLINK_FOLLOW 0x00000002
  221. //
  222. // Set this flag in the faccessat function to use the effective user and group
  223. // IDs for permission checking rather than the real user and group IDs.
  224. //
  225. #define AT_EACCESS 0x00000004
  226. //
  227. // Set this flag in the unlinkat function to attempt to remove a directory.
  228. //
  229. #define AT_REMOVEDIR 0x00000008
  230. //
  231. // ------------------------------------------------------ Data Type Definitions
  232. //
  233. /*++
  234. Structure Description:
  235. This structure stores information about an advisory file record lock.
  236. Members:
  237. l_start - Stores the starting offset within the file of the record lock.
  238. l_len - Stores the length of the record lock, in bytes. A value of zero
  239. indicates the record extends to the end of the file.
  240. l_pid - Stores the identifier of the process that owns the lock. This is
  241. filled in by the get lock operation, and ignored when creating a lock.
  242. l_type - Stores the type of lock. Valid values are F_RDLCK, F_WRLCK, and
  243. F_UNLCK.
  244. l_whence - Stores the SEEK_* parameter that defines the origin of the
  245. offset. This is always SEEK_SET when lock information is returned.
  246. --*/
  247. struct flock {
  248. off_t l_start;
  249. off_t l_len;
  250. pid_t l_pid;
  251. short l_type;
  252. short l_whence;
  253. };
  254. //
  255. // -------------------------------------------------------------------- Globals
  256. //
  257. //
  258. // -------------------------------------------------------- Function Prototypes
  259. //
  260. LIBC_API
  261. int
  262. open (
  263. const char *Path,
  264. int OpenFlags,
  265. ...
  266. );
  267. /*++
  268. Routine Description:
  269. This routine opens a file and connects it to a file descriptor.
  270. Arguments:
  271. Path - Supplies a pointer to a null terminated string containing the path
  272. of the file to open.
  273. OpenFlags - Supplies a set of flags ORed together. See O_* definitions.
  274. ... - Supplies an optional integer representing the permission mask to set
  275. if the file is to be created by this open call.
  276. Return Value:
  277. Returns a file descriptor on success.
  278. -1 on failure. The errno variable will be set to indicate the error.
  279. --*/
  280. LIBC_API
  281. int
  282. openat (
  283. int Directory,
  284. const char *Path,
  285. int OpenFlags,
  286. ...
  287. );
  288. /*++
  289. Routine Description:
  290. This routine opens a file and connects it to a file descriptor.
  291. Arguments:
  292. Directory - Supplies an optional file descriptor. If the given path
  293. is a relative path, the directory referenced by this descriptor will
  294. be used as a starting point for path resolution. Supply AT_FDCWD to
  295. use the working directory for relative paths.
  296. Path - Supplies a pointer to a null terminated string containing the path
  297. of the file to open.
  298. OpenFlags - Supplies a set of flags ORed together. See O_* definitions.
  299. ... - Supplies an optional integer representing the permission mask to set
  300. if the file is to be created by this open call.
  301. Return Value:
  302. Returns a file descriptor on success.
  303. -1 on failure. The errno variable will be set to indicate the error.
  304. --*/
  305. LIBC_API
  306. int
  307. fcntl (
  308. int FileDescriptor,
  309. int Command,
  310. ...
  311. );
  312. /*++
  313. Routine Description:
  314. This routine performs a file control operation on an open file handle.
  315. Arguments:
  316. FileDescriptor - Supplies the file descriptor to operate on.
  317. Command - Supplies the file control command. See F_* definitions.
  318. ... - Supplies any additional command-specific arguments.
  319. Return Value:
  320. Returns some value other than -1 to indicate success. For some commands
  321. (like F_DUPFD) this is a file descriptor. For others (like F_GETFD and
  322. F_GETFL) this is a bitfield of status flags.
  323. -1 on error, and errno will be set to indicate the error.
  324. --*/
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. #endif