fcntl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /********************************************************************************
  2. * include/fcntl.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ********************************************************************************/
  20. #ifndef __INCLUDE_FCNTL_H
  21. #define __INCLUDE_FCNTL_H
  22. /********************************************************************************
  23. * Included Files
  24. ********************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <stdint.h>
  28. /********************************************************************************
  29. * Pre-processor Definitions
  30. ********************************************************************************/
  31. /* open flag settings for open() (and related APIs) */
  32. #define O_RDONLY (1 << 0) /* Open for read access (only) */
  33. #define O_RDOK O_RDONLY /* Read access is permitted (non-standard) */
  34. #define O_WRONLY (1 << 1) /* Open for write access (only) */
  35. #define O_WROK O_WRONLY /* Write access is permitted (non-standard) */
  36. #define O_RDWR (O_RDOK|O_WROK) /* Open for both read & write access */
  37. #define O_CREAT (1 << 2) /* Create file/sem/mq object */
  38. #define O_EXCL (1 << 3) /* Name must not exist when opened */
  39. #define O_APPEND (1 << 4) /* Keep contents, append to end */
  40. #define O_TRUNC (1 << 5) /* Delete contents */
  41. #define O_NONBLOCK (1 << 6) /* Don't wait for data */
  42. #define O_NDELAY O_NONBLOCK /* Synonym for O_NONBLOCK */
  43. #define O_SYNC (1 << 7) /* Synchronize output on write */
  44. #define O_DSYNC O_SYNC /* Equivalent to OSYNC in NuttX */
  45. #define O_BINARY (1 << 8) /* Open the file in binary (untranslated) mode. */
  46. #define O_DIRECT (1 << 9) /* Avoid caching, write directly to hardware */
  47. #define O_CLOEXEC (1 << 10) /* Close on execute */
  48. /* Unsupported, but required open flags */
  49. #define O_RSYNC 0 /* Synchronize input on read */
  50. #define O_ACCMODE O_RDWR /* Mask for access mode */
  51. #define O_NOCTTY 0 /* Required by POSIX */
  52. #define O_TEXT 0 /* Open the file in text (translated) mode. */
  53. /* This is the highest bit number used in the open flags bitset. Bits above
  54. * this bit number may be used within NuttX for other, internal purposes.
  55. */
  56. #define _O_MAXBIT 8
  57. /* Synonyms historically used as F_SETFL flags (BSD). */
  58. #define FNDELAY O_NONBLOCK /* Don't wait for data */
  59. #define FNONBLOCK O_NONBLOCK /* Don't wait for data */
  60. #define FAPPEND O_APPEND /* Keep contents, append to end */
  61. #define FSYNC O_SYNC /* Synchronize output on write */
  62. #define FASYNC 0 /* No counterpart in NuttX */
  63. /* FFCNTL is all the bits that may be set via fcntl. */
  64. #define FFCNTL (FNONBLOCK | FNDELAY | FAPPEND | FSYNC | FASYNC)
  65. /* fcntl() commands */
  66. #define F_DUPFD 0 /* Duplicate a file descriptor */
  67. #define F_GETFD 1 /* Read the file descriptor flags */
  68. #define F_GETFL 2 /* Read the file status flags */
  69. #define F_GETLEASE 3 /* Indicates what type of lease is held on fd (linux) */
  70. #define F_GETLK 4 /* Check if we could place a lock */
  71. #define F_GETOWN 5 /* Get the pid receiving SIGIO and SIGURG signals for fd */
  72. #define F_GETSIG 6 /* Get the signal sent */
  73. #define F_NOTIFY 7 /* Provide notification when directory referred to by fd changes (linux)*/
  74. #define F_SETFD 8 /* Set the file descriptor flags to value */
  75. #define F_SETFL 9 /* Set the file status flags to the value */
  76. #define F_SETLEASE 10 /* Set or remove file lease (linux) */
  77. #define F_SETLK 11 /* Acquire or release a lock on range of bytes */
  78. #define F_SETLKW 12 /* Like F_SETLK, but wait for lock to become available */
  79. #define F_SETOWN 13 /* Set pid that will receive SIGIO and SIGURG signals for fd */
  80. #define F_SETSIG 14 /* Set the signal to be sent */
  81. #define F_GETPATH 15 /* Get the path of the file descriptor(BSD/macOS) */
  82. /* For posix fcntl() and lockf() */
  83. #define F_RDLCK 0 /* Take out a read lease */
  84. #define F_WRLCK 1 /* Take out a write lease */
  85. #define F_UNLCK 2 /* Remove a lease */
  86. /* close-on-exec flag for F_GETRL and F_SETFL */
  87. #define FD_CLOEXEC 1
  88. /* These are the notifications that can be received from F_NOTIFY (linux) */
  89. #define DN_ACCESS 0 /* A file was accessed */
  90. #define DN_MODIFY 1 /* A file was modified */
  91. #define DN_CREATE 2 /* A file was created */
  92. #define DN_DELETE 3 /* A file was unlinked */
  93. #define DN_RENAME 4 /* A file was renamed */
  94. #define DN_ATTRIB 5 /* Attributes of a file were changed */
  95. /* int creat(const char *path, mode_t mode);
  96. *
  97. * is equivalent to open with O_WRONLY|O_CREAT|O_TRUNC.
  98. */
  99. #define creat(path, mode) open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)
  100. /********************************************************************************
  101. * Public Type Definitions
  102. ********************************************************************************/
  103. /* struct flock is the third argument for F_GETLK, F_SETLK and F_SETLKW */
  104. struct flock
  105. {
  106. int16_t l_type; /* Type of lock: F_RDLCK, F_WRLCK, F_UNLCK */
  107. int16_t l_whence; /* How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_END */
  108. off_t l_start; /* Starting offset for lock */
  109. off_t l_len; /* Number of bytes to lock */
  110. pid_t l_pid; /* PID of process blocking our lock (F_GETLK only) */
  111. };
  112. /********************************************************************************
  113. * Public Function Prototypes
  114. ********************************************************************************/
  115. #undef EXTERN
  116. #if defined(__cplusplus)
  117. #define EXTERN extern "C"
  118. extern "C"
  119. {
  120. #else
  121. #define EXTERN extern
  122. #endif
  123. /********************************************************************************
  124. * Public Data
  125. ********************************************************************************/
  126. /* POSIX-like File System Interfaces */
  127. int open(FAR const char *path, int oflag, ...);
  128. int fcntl(int fd, int cmd, ...);
  129. int posix_fallocate(int fd, off_t offset, off_t len);
  130. #undef EXTERN
  131. #if defined(__cplusplus)
  132. }
  133. #endif
  134. #endif /* __INCLUDE_FCNTL_H */