1 /** 2 * @defgroup fcntl Fcntl 3 * @ingroup libc 4 */ 5 6 #ifndef _FCNTL_H 7 #define _FCNTL_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 #include <features.h> 14 15 #define __NEED_off_t 16 #define __NEED_pid_t 17 #define __NEED_mode_t 18 19 #ifdef _GNU_SOURCE 20 #define __NEED_size_t 21 #define __NEED_ssize_t 22 #define __NEED_struct_iovec 23 #endif 24 #ifdef __LITEOS__ 25 #define __NEED_int64_t 26 #endif 27 28 #include <bits/alltypes.h> 29 30 #include <bits/fcntl.h> 31 32 struct flock { 33 short l_type; 34 short l_whence; 35 off_t l_start; 36 off_t l_len; 37 pid_t l_pid; 38 }; 39 40 41 /** 42 * @ingroup fcntl 43 * 44 * @par Description: 45 * Create a new file or rewrite an existing one. 46 * @param path [IN] Path of the file which need to be created. 47 * @param mode [IN] Describes the permissions of the file, should be an integer type. 48 * (For example: 0666, 0777) 49 * @attention 50 * <ul> 51 * <li>None.</li> 52 * </ul> 53 * 54 * @retval #0 Upon successful completion. 55 * @retval #-1 Failed and set errno to indicate the error. 56 * 57 * @par Errors 58 * <ul> 59 * <li>Refer to open.</li> 60 * </ul> 61 * 62 * @par Dependency: 63 * <ul><li>fcntl.h</li></ul> 64 * @see None 65 */ 66 int creat(const char *, mode_t); 67 /* func realization is used lwip_ioctl, func declaration conflict hear*/ 68 #ifndef __LITEOS__ 69 int fcntl(int, int, ...); 70 #endif 71 /** 72 * @ingroup fcntl 73 * 74 * @par Description: 75 * The open() function shall establish the connection between a file and a file descriptor. 76 * It shall create an open file description that refers to a file and a file descriptor that refers to that open file 77 * description. The file descriptor is used by other I/O functions to refer to that file. The file status flags 78 * The file offset used to mark the current position within the file shall be set to the beginning of the file. 79 * and file access modes of the open file description shall be set according to the value of oflags. 80 * @param path [IN] Points to a pathname naming the file. 81 * @param oflags [IN] Values for oflags are constructed by a bitwise-inclusive OR of flags from the following list, 82 * defined in <libc/kernel/asm-generic/fcntl.h>. Applications should specify one of the median values 83 * (file access patterns) in the value of oflags in the attention description. 84 * @param "..." [IN] If the file is opened for creation, description file mode_t mode. 85 * @attention 86 * <ul> 87 * <li><b>O_RDONLY</b>: Open for reading only.</li> 88 * <li><b>O_RDWR</b>: Open for reading and writing. The result is undefined if this flag is applied to a FIFO.</li> 89 * <li><b>O_WRONLY</b>: Open for writing only.</li> 90 * <li><b>O_CREAT</b>: If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, 91 * if O_DIRECTORY is not set the file shall be created as a regular file; the access permission bits of the file mode 92 * shall be set to the value of the argument following the oflag argument taken as type mode_t.</li> 93 * <li><b>O_EXCL</b>: If O_CREAT and O_EXCL are set, open() shall fail if the file exists. 94 * The check for the existence of the file and the creation of the file if it does not exist shall be atomic with 95 * respect to other threads executing open() naming the same filename in the same directory with O_EXCL and O_CREAT set. 96 * If O_EXCL and O_CREAT are set, and path names a symbolic link, open() shall fail and set errno to [EEXIST], 97 * regardless of the contents of the symbolic link. If O_EXCL is set and O_CREAT is not set, the result is undefined 98 * .</li> 99 * <li><b>O_NOCTTY</b>: Not support.</li> 100 * <li><b>O_APPEND</b>: If set, the file offset shall be set to the end of the file prior to each write.</li> 101 * <li><b>O_NONBLOCK</b>: When opening a FIFO with O_RDONLY or O_WRONLY set: 102 * If O_NONBLOCK is set, an open() for reading-only shall return without delay. 103 * An open() for writing-only shall return an error if no process currently has the file open for reading. 104 * If O_NONBLOCK is clear, an open() for reading-only shall block the calling thread until a thread opens the file 105 * for writing. An open() for writing-only shall block the calling thread until a thread opens the file for reading.</li> 106 * <li><b>O_TRUNC</b>: If the file exists and is a regular file, and the file is successfully opened O_RDWR or O_WRONLY, 107 * its length shall be truncated to 0, and the mode and owner shall be unchanged. It shall have no effect on FIFO special 108 * files or terminal device files. Its effect on other file types is implementation-defined. 109 * The result of using O_TRUNC without either O_RDWR or O_WRONLY is undefined.</li> 110 * <li><b>O_DIRECTORY</b>: No support, if set this mode returns error code [EACCES].</li> 111 * <li><b>O_NOFOLLOW</b>: Not support.</li> 112 * <li><b>O_DIRECT</b>: Not support.</li> 113 * </ul> 114 * 115 * @retval #0 Upon successful completion. 116 * @retval #-1 Failed and set errno to indicate the error. 117 * 118 * @par Errors 119 * <ul> 120 * <li><b>EACCES</b>: Permission bits of the file mode do not permit the requested access, 121 * or search permission is denied on a component of the path prefix, or set oflags with O_DIRECTORY shall return it.</li> 122 * <li><b>EINVAL</b>: The path name format is invalid.</li> 123 * <li><b>ENAMETOOLONG</b>: The length of a component of a pathname is longer than {NAME_MAX}.</li> 124 * <li><b>ENOENT</b>: O_CREAT is not set and the named file does not exist. Or a directory component in pathname does not 125 * exist or is a dangling symbolic link. This error code is also returned in the fat file system: no valid FAT volume, 126 * not find the file, not find the path</li> 127 * <li><b>ENOMEM</b>: Out of memory.</li> 128 * <li><b>ENXIO</b>: Inode is invalid or not a "normal" character driver or not a mountpoint.</li> 129 * <li><b>EMFILE</b>: All file descriptors available to the process are currently open.</li> 130 * <li><b>EPERM</b>: Get the file structure corresponding to the file descriptor failed. 131 * In fat file system: This error code is also returned when operation not permitted or access denied due to prohibited 132 * access or directory full.</li> 133 * <li><b>EEXIST</b>: O_CREAT and O_EXCL are set, and the named file exists. 134 * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer ro the physical drive cannot work or Assertion 135 * failed.</li> 136 * <li><b>EROFS</b>: The named file resides on a read-only file system and either O_WRONLY, O_RDWR, O_CREAT 137 * (if the file does not exist), or O_TRUNC is set in the oflags argument.</li> 138 * <li><b>ENOSPC</b>: The directory or file system that would contain the new file cannot be expanded, 139 * the file does not exist, and O_CREAT is specified.</li> 140 * <li><b>ENFILE</b>: The maximum allowable number of files is currently open in the system.</li> 141 * <li><b>EISDIR</b>: The named file is a directory and oflag includes O_WRONLY or O_RDWR, or includes O_CREAT without 142 * O_DIRECTORY.</li> 143 * <li><b>ENOTDIR</b>: A component of the path prefix names an existing file that is neither a directory nor a symbolic 144 * link to a directory; or O_CREAT and O_EXCL are not specified, the path argument contains at least one non-<slash> 145 * character and ends with one or more trailing <slash> characters, and the last pathname component names an existing 146 * file that is neither a directory nor a symbolic link to a directory; or the path 147 * argument resolves to a non-directory file.</li> 148 * <li><b>EBUSY</b>: The operation is rejected according to the file sharing policy.</li> 149 * <li><b>ENODEV</b>: No such device.</li> 150 * <li><b>ENOTEMPTY</b>: Directory not empty.</li> 151 * <li><b>ELOOP</b>: Too many symbolic links encountered.</li> 152 * <li><b>EFAULT</b>: Bad address.</li> 153 * </ul> 154 * 155 * @par Dependency: 156 * <ul><li>fcntl.h</li></ul> 157 * @see None 158 */ 159 int open(const char *, int, ...); 160 int openat(int, const char *, int, ...); 161 int posix_fadvise(int, off_t, off_t, int); 162 int posix_fallocate(int, off_t, off_t); 163 164 #define O_SEARCH O_PATH 165 #define O_EXEC O_PATH 166 #define O_TTY_INIT 0 167 168 #define O_ACCMODE (03|O_SEARCH) 169 #define O_RDONLY 00 170 #define O_WRONLY 01 171 #define O_RDWR 02 172 173 #define F_OFD_GETLK 36 174 #define F_OFD_SETLK 37 175 #define F_OFD_SETLKW 38 176 177 #define F_DUPFD_CLOEXEC 1030 178 179 #define F_RDLCK 0 180 #define F_WRLCK 1 181 #define F_UNLCK 2 182 183 #define FD_CLOEXEC 1 184 185 #define AT_FDCWD (-100) 186 #define AT_SYMLINK_NOFOLLOW 0x100 187 #define AT_REMOVEDIR 0x200 188 #define AT_SYMLINK_FOLLOW 0x400 189 #define AT_EACCESS 0x200 190 191 #define POSIX_FADV_NORMAL 0 192 #define POSIX_FADV_RANDOM 1 193 #define POSIX_FADV_SEQUENTIAL 2 194 #define POSIX_FADV_WILLNEED 3 195 #ifndef POSIX_FADV_DONTNEED 196 #define POSIX_FADV_DONTNEED 4 197 #define POSIX_FADV_NOREUSE 5 198 #endif 199 200 #undef SEEK_SET 201 #undef SEEK_CUR 202 #undef SEEK_END 203 #define SEEK_SET 0 204 #define SEEK_CUR 1 205 #define SEEK_END 2 206 207 #ifndef S_IRUSR 208 #define S_ISUID 04000 209 #define S_ISGID 02000 210 #define S_ISVTX 01000 211 #define S_IRUSR 0400 212 #define S_IWUSR 0200 213 #define S_IXUSR 0100 214 #define S_IRWXU 0700 215 #define S_IRGRP 0040 216 #define S_IWGRP 0020 217 #define S_IXGRP 0010 218 #define S_IRWXG 0070 219 #define S_IROTH 0004 220 #define S_IWOTH 0002 221 #define S_IXOTH 0001 222 #define S_IRWXO 0007 223 #endif 224 225 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 226 #define AT_NO_AUTOMOUNT 0x800 227 #define AT_EMPTY_PATH 0x1000 228 #define AT_STATX_SYNC_TYPE 0x6000 229 #define AT_STATX_SYNC_AS_STAT 0x0000 230 #define AT_STATX_FORCE_SYNC 0x2000 231 #define AT_STATX_DONT_SYNC 0x4000 232 #define AT_RECURSIVE 0x8000 233 234 #define FAPPEND O_APPEND 235 #define FFSYNC O_SYNC 236 #define FASYNC O_ASYNC 237 #define FNONBLOCK O_NONBLOCK 238 #define FNDELAY O_NDELAY 239 240 #define F_OK 0 241 #define R_OK 4 242 #define W_OK 2 243 #define X_OK 1 244 #define F_ULOCK 0 245 #define F_LOCK 1 246 #define F_TLOCK 2 247 #define F_TEST 3 248 249 #define F_SETLEASE 1024 250 #define F_GETLEASE 1025 251 #define F_NOTIFY 1026 252 #define F_CANCELLK 1029 253 #define F_SETPIPE_SZ 1031 254 #define F_GETPIPE_SZ 1032 255 #define F_ADD_SEALS 1033 256 #define F_GET_SEALS 1034 257 258 #define F_SEAL_SEAL 0x0001 259 #define F_SEAL_SHRINK 0x0002 260 #define F_SEAL_GROW 0x0004 261 #define F_SEAL_WRITE 0x0008 262 #define F_SEAL_FUTURE_WRITE 0x0010 263 264 #define F_GET_RW_HINT 1035 265 #define F_SET_RW_HINT 1036 266 #define F_GET_FILE_RW_HINT 1037 267 #define F_SET_FILE_RW_HINT 1038 268 269 #define RWF_WRITE_LIFE_NOT_SET 0 270 #define RWH_WRITE_LIFE_NONE 1 271 #define RWH_WRITE_LIFE_SHORT 2 272 #define RWH_WRITE_LIFE_MEDIUM 3 273 #define RWH_WRITE_LIFE_LONG 4 274 #define RWH_WRITE_LIFE_EXTREME 5 275 276 #define DN_ACCESS 0x00000001 277 #define DN_MODIFY 0x00000002 278 #define DN_CREATE 0x00000004 279 #define DN_DELETE 0x00000008 280 #define DN_RENAME 0x00000010 281 #define DN_ATTRIB 0x00000020 282 #define DN_MULTISHOT 0x80000000 283 284 int lockf(int, int, off_t); 285 #endif 286 287 #if defined(_GNU_SOURCE) 288 #define F_OWNER_TID 0 289 #define F_OWNER_PID 1 290 #define F_OWNER_PGRP 2 291 #define F_OWNER_GID 2 292 #ifndef __LITEOS__ 293 struct file_handle { 294 unsigned handle_bytes; 295 int handle_type; 296 unsigned char f_handle[]; 297 }; 298 #endif 299 struct f_owner_ex { 300 int type; 301 pid_t pid; 302 }; 303 #define FALLOC_FL_KEEP_SIZE 1 304 #define FALLOC_FL_PUNCH_HOLE 2 305 #define MAX_HANDLE_SZ 128 306 #define SYNC_FILE_RANGE_WAIT_BEFORE 1 307 #define SYNC_FILE_RANGE_WRITE 2 308 #define SYNC_FILE_RANGE_WAIT_AFTER 4 309 #define SPLICE_F_MOVE 1 310 #define SPLICE_F_NONBLOCK 2 311 #define SPLICE_F_MORE 4 312 #define SPLICE_F_GIFT 8 313 314 /** 315 * @ingroup fcntl 316 * 317 * @par Description: 318 * The fallocate() function shall ensure that any required storage for regular file data starting at offset and 319 * continuing for len bytes is allocated on the file system storage media. 320 * @param fd [IN] File descriptor. 321 * @param mode [IN] Operation mode. Only support FALLOC_FL_KEEP_SIZE. 322 * @param offset [IN] Offset of the file to allocated. 323 * @param len [IN] The size to allocate for the file. 324 * @attention 325 * <ul> 326 * <li>None.</li> 327 * </ul> 328 * 329 * @retval #0 Upon successful completion. 330 * @retval #-1 Failed and set errno to indicate the error. 331 * 332 * @par Errors 333 * <ul> 334 * <li><b>EINVAL</b>: The len argument or the offset argument is less than zero or greater than INT_MAX, 335 * or operation mode is not equal to FALLOC_FL_KEEP_SIZE.</li> 336 * <li><b>EBADF</b>: The fd argument is not a valid file descriptor or or the underlying file system does not support 337 * this operation.</li> 338 * <li><b>EAGAIN</b>: The file list is NULL.</li> 339 * <li><b>EACCES</b>: Permission bits of the file mode do not permit the requested access, 340 * or search permission is denied on a component of the path prefix.</li> 341 * <li><b>ENOENT</b>: A directory component in pathname does not exist or is a dangling symbolic link.</li> 342 * <li><b>EEXIST</b>: The file/directory object is already exist.</li> 343 * <li><b>EIO</b>: An I/O error occurred while reading from or writing to a file system.</li> 344 * <li><b>EROFS</b>: The physical drive is write protected.</li> 345 * <li><b>ENOSPC</b>: The directory or file system that would contain the new file cannot be expanded, 346 * the file does not exist, and O_CREAT is specified.</li> 347 * <li><b>ENFILE</b>: The maximum allowable number of files is currently open in the system.</li> 348 * <li><b>ENOTEMPTY</b>: Directory not empty.</li> 349 * <li><b>EISDIR</b>: The named file is a directory.</li> 350 * <li><b>ENOTDIR</b>: A component of the path prefix names an existing file that is neither a directory nor a symbolic 351 * link to a directory; or the path argument resolves to a non-directory file.</li> 352 * <li><b>EPERM</b>: Operation not permitted or access denied due to prohibited access or directory full.</li> 353 * <li><b>EBUSY</b>: The operation is rejected according to the file sharing policy.</li> 354 * </ul> 355 * 356 * @par Dependency: 357 * <ul><li>fcntl.h</li></ul> 358 * @see None 359 */ 360 int fallocate(int, int, off_t, off_t); 361 #ifndef __LITEOS__ 362 #define fallocate64 fallocate 363 int name_to_handle_at(int, const char *, struct file_handle *, int *, int); 364 int open_by_handle_at(int, struct file_handle *, int); 365 #endif 366 ssize_t readahead(int, off_t, size_t); 367 int sync_file_range(int, off_t, off_t, unsigned); 368 ssize_t vmsplice(int, const struct iovec *, size_t, unsigned); 369 ssize_t splice(int, off_t *, int, off_t *, size_t, unsigned); 370 ssize_t tee(int, int, size_t, unsigned); 371 #if defined(__LITEOS__) && !defined(__LP64__) 372 typedef long long _loff_t; 373 #define loff_t _loff_t 374 #else 375 #define loff_t off_t 376 #endif 377 #endif 378 379 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) 380 #ifdef __LITEOS__ 381 #define F_GETLK64 F_GETLK 382 #define F_SETLK64 F_SETLK 383 #define F_SETLKW64 F_SETLKW 384 #define flock64 flock 385 #define off64_t int64_t 386 int open64 (const char *, int, ...); 387 int openat64(int, const char *, int, ...); 388 int creat64(const char *, mode_t); 389 int lockf64(int, int, off64_t); 390 int posix_fadvise64(int, off64_t, off64_t, int); 391 int posix_fallocate64(int, off64_t, off64_t); 392 int fallocate64(int, int, off64_t, off64_t); 393 #else 394 #define F_GETLK64 F_GETLK 395 #define F_SETLK64 F_SETLK 396 #define F_SETLKW64 F_SETLKW 397 #define flock64 flock 398 #define open64 open 399 #define openat64 openat 400 #define creat64 creat 401 #define lockf64 lockf 402 #define posix_fadvise64 posix_fadvise 403 #define posix_fallocate64 posix_fallocate 404 #define off64_t off_t 405 #endif 406 #endif 407 408 #ifdef __cplusplus 409 } 410 #endif 411 412 #endif 413