fcntl.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* $OpenBSD: fcntl.h,v 1.21 2015/05/17 01:22:01 deraadt Exp $ */
  2. /* $NetBSD: fcntl.h,v 1.8 1995/03/26 20:24:12 jtc Exp $ */
  3. /*-
  4. * Copyright (c) 1983, 1990, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. * (c) UNIX System Laboratories, Inc.
  7. * All or some portions of this file are derived from material licensed
  8. * to the University of California by American Telephone and Telegraph
  9. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  10. * the permission of UNIX System Laboratories, Inc.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)fcntl.h 8.3 (Berkeley) 1/21/94
  37. */
  38. #ifndef _SYS_FCNTL_H_
  39. #define _SYS_FCNTL_H_
  40. /*
  41. * This file includes the definitions for open and fcntl
  42. * described by POSIX for <fcntl.h>; it also includes
  43. * related kernel definitions.
  44. */
  45. #include <sys/cdefs.h>
  46. #ifndef _KERNEL
  47. #include <sys/types.h>
  48. #endif
  49. /*
  50. * File status flags: these are used by open(2), fcntl(2).
  51. * They are also used (indirectly) in the kernel file structure f_flags,
  52. * which is a superset of the open/fcntl flags. Open flags and f_flags
  53. * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags).
  54. * Open/fcntl flags begin with O_; kernel-internal flags begin with F.
  55. */
  56. /* open-only flags */
  57. #define O_RDONLY 0x0000 /* open for reading only */
  58. #define O_WRONLY 0x0001 /* open for writing only */
  59. #define O_RDWR 0x0002 /* open for reading and writing */
  60. #define O_ACCMODE 0x0003 /* mask for above modes */
  61. /*
  62. * Kernel encoding of open mode; separate read and write bits that are
  63. * independently testable: 1 greater than the above.
  64. *
  65. * XXX
  66. * FREAD and FWRITE are excluded from the #ifdef _KERNEL so that TIOCFLUSH,
  67. * which was documented to use FREAD/FWRITE, continues to work.
  68. */
  69. #if __BSD_VISIBLE
  70. #define FREAD 0x0001
  71. #define FWRITE 0x0002
  72. #endif
  73. #define O_NONBLOCK 0x0004 /* no delay */
  74. #define O_APPEND 0x0008 /* set append mode */
  75. #if __BSD_VISIBLE
  76. #define O_SHLOCK 0x0010 /* open with shared file lock */
  77. #define O_EXLOCK 0x0020 /* open with exclusive file lock */
  78. #define O_ASYNC 0x0040 /* signal pgrp when data ready */
  79. #define O_FSYNC 0x0080 /* backwards compatibility */
  80. #define O_NOFOLLOW 0x0100 /* if path is a symlink, don't follow */
  81. #endif
  82. #if __POSIX_VISIBLE >= 199309 || __XPG_VISIBLE >= 420
  83. #define O_SYNC 0x0080 /* synchronous writes */
  84. #endif
  85. #define O_CREAT 0x0200 /* create if nonexistent */
  86. #define O_TRUNC 0x0400 /* truncate to zero length */
  87. #define O_EXCL 0x0800 /* error if already exists */
  88. /*
  89. * POSIX 1003.1 specifies a higher granularity for synchronous operations
  90. * than we support. Since synchronicity is all or nothing in OpenBSD
  91. * we just define these to be the same as O_SYNC.
  92. */
  93. #define O_DSYNC O_SYNC /* synchronous data writes */
  94. #define O_RSYNC O_SYNC /* synchronous reads */
  95. /* defined by POSIX 1003.1; BSD default, this bit is not required */
  96. #define O_NOCTTY 0x8000 /* don't assign controlling terminal */
  97. /* defined by POSIX Issue 7 */
  98. #define O_CLOEXEC 0x10000 /* atomically set FD_CLOEXEC */
  99. #define O_DIRECTORY 0x20000 /* fail if not a directory */
  100. #ifdef _KERNEL
  101. /*
  102. * convert from open() flags to/from fflags; convert O_RD/WR to FREAD/FWRITE.
  103. * For out-of-range values for the flags, be slightly careful (but lossy).
  104. */
  105. #define FFLAGS(oflags) (((oflags) & ~O_ACCMODE) | (((oflags) + 1) & O_ACCMODE))
  106. #define OFLAGS(fflags) (((fflags) & ~O_ACCMODE) | (((fflags) - 1) & O_ACCMODE))
  107. /* bits to save after open */
  108. #define FMASK (FREAD|FWRITE|FAPPEND|FASYNC|FFSYNC|FNONBLOCK)
  109. /* bits settable by fcntl(F_SETFL, ...) */
  110. #define FCNTLFLAGS (FAPPEND|FASYNC|FFSYNC|FNONBLOCK)
  111. #endif
  112. /*
  113. * The O_* flags used to have only F* names, which were used in the kernel
  114. * and by fcntl. We retain the F* names for the kernel f_flags field
  115. * and for backward compatibility for fcntl.
  116. */
  117. #if __BSD_VISIBLE
  118. #define FAPPEND O_APPEND /* kernel/compat */
  119. #define FASYNC O_ASYNC /* kernel/compat */
  120. #define FFSYNC O_SYNC /* kernel */
  121. #define FNONBLOCK O_NONBLOCK /* kernel */
  122. #define FNDELAY O_NONBLOCK /* compat */
  123. #define O_NDELAY O_NONBLOCK /* compat */
  124. #endif
  125. /*
  126. * Constants used for fcntl(2)
  127. */
  128. /* command values */
  129. #define F_DUPFD 0 /* duplicate file descriptor */
  130. #define F_GETFD 1 /* get file descriptor flags */
  131. #define F_SETFD 2 /* set file descriptor flags */
  132. #define F_GETFL 3 /* get file status flags */
  133. #define F_SETFL 4 /* set file status flags */
  134. #if __POSIX_VISIBLE >= 200112 || __XPG_VISIBLE >= 500
  135. #define F_GETOWN 5 /* get SIGIO/SIGURG proc/pgrp */
  136. #define F_SETOWN 6 /* set SIGIO/SIGURG proc/pgrp */
  137. #endif
  138. #define F_GETLK 7 /* get record locking information */
  139. #define F_SETLK 8 /* set record locking information */
  140. #define F_SETLKW 9 /* F_SETLK; wait if blocked */
  141. #if __POSIX_VISIBLE >= 200809
  142. #define F_DUPFD_CLOEXEC 10 /* duplicate with FD_CLOEXEC set */
  143. #endif
  144. #if __BSD_VISIBLE
  145. #define F_ISATTY 11 /* used by isatty(3) */
  146. #endif
  147. /* file descriptor flags (F_GETFD, F_SETFD) */
  148. #define FD_CLOEXEC 1 /* close-on-exec flag */
  149. /* record locking flags (F_GETLK, F_SETLK, F_SETLKW) */
  150. #define F_RDLCK 1 /* shared or read lock */
  151. #define F_UNLCK 2 /* unlock */
  152. #define F_WRLCK 3 /* exclusive or write lock */
  153. #ifdef _KERNEL
  154. #define F_WAIT 0x010 /* Wait until lock is granted */
  155. #define F_FLOCK 0x020 /* Use flock(2) semantics for lock */
  156. #define F_POSIX 0x040 /* Use POSIX semantics for lock */
  157. #endif
  158. /*
  159. * Advisory file segment locking data type -
  160. * information passed to system by user
  161. */
  162. struct flock {
  163. off_t l_start; /* starting offset */
  164. off_t l_len; /* len = 0 means until end of file */
  165. pid_t l_pid; /* lock owner */
  166. short l_type; /* lock type: read/write, etc. */
  167. short l_whence; /* type of l_start */
  168. };
  169. #if __BSD_VISIBLE
  170. /* lock operations for flock(2) */
  171. #define LOCK_SH 0x01 /* shared file lock */
  172. #define LOCK_EX 0x02 /* exclusive file lock */
  173. #define LOCK_NB 0x04 /* don't block when locking */
  174. #define LOCK_UN 0x08 /* unlock file */
  175. #endif
  176. #if __POSIX_VISIBLE >= 200809
  177. #define AT_FDCWD -100
  178. #define AT_EACCESS 0x01
  179. #define AT_SYMLINK_NOFOLLOW 0x02
  180. #define AT_SYMLINK_FOLLOW 0x04
  181. #define AT_REMOVEDIR 0x08
  182. #endif
  183. #ifndef _KERNEL
  184. __BEGIN_DECLS
  185. int open(const char *, int, ...);
  186. int creat(const char *, mode_t);
  187. int fcntl(int, int, ...);
  188. #if __BSD_VISIBLE
  189. int flock(int, int);
  190. #endif
  191. #if __POSIX_VISIBLE >= 200809
  192. int openat(int, const char *, int, ...);
  193. #endif
  194. __END_DECLS
  195. #endif
  196. #endif /* !_SYS_FCNTL_H_ */