blob: c81028e79144ddb3b17a8be8d31de2bdff50f3e1 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughese9893992013-10-17 11:45:22 -070028
Elliott Hughesb361e682019-11-01 16:47:46 -070029#pragma once
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030#define _FCNTL_H
31
Elliott Hughesb361e682019-11-01 16:47:46 -070032/**
33 * @file fcntl.h
34 * @brief File control operations.
35 */
36
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <sys/cdefs.h>
38#include <sys/types.h>
Elliott Hughes01e505a2014-01-07 17:47:20 -080039#include <linux/fadvise.h>
Nick Desaulniers09d900d2016-07-19 15:20:24 -070040#include <linux/falloc.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#include <linux/fcntl.h>
Elliott Hughes64d83822014-12-29 11:14:38 -080042#include <linux/stat.h>
Elliott Hughes3f525d42014-06-24 16:32:01 -070043#include <linux/uio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044
Josh Gaoee8d1692016-04-07 14:16:30 -070045#include <bits/fcntl.h>
Elliott Hughesfd936ae2016-08-12 10:16:34 -070046#include <bits/seek_constants.h>
Josh Gaoee8d1692016-04-07 14:16:30 -070047
Elliott Hughes5704c422016-01-25 18:06:24 -080048#if defined(__USE_GNU) || defined(__USE_BSD)
49#include <bits/lockf.h>
50#endif
51
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052__BEGIN_DECLS
53
Elliott Hughes09e77f32020-01-29 19:20:45 -080054#if defined(__LP64__)
55
Christopher Ferrisba8d4f42014-09-03 19:56:49 -070056/* LP64 kernels don't have F_*64 defines because their flock is 64-bit. */
Elliott Hughes09e77f32020-01-29 19:20:45 -080057
Elliott Hughesb361e682019-11-01 16:47:46 -070058/** Flag for flock(). */
Serban Constantinescu48501af2014-03-14 13:16:25 +000059#define F_GETLK64 F_GETLK
Elliott Hughesb361e682019-11-01 16:47:46 -070060/** Flag for flock(). */
Serban Constantinescu48501af2014-03-14 13:16:25 +000061#define F_SETLK64 F_SETLK
Elliott Hughesb361e682019-11-01 16:47:46 -070062/** Flag for flock(). */
Serban Constantinescu48501af2014-03-14 13:16:25 +000063#define F_SETLKW64 F_SETLKW
Elliott Hughes09e77f32020-01-29 19:20:45 -080064
65#elif defined(__USE_FILE_OFFSET64)
66
67/* For _FILE_OFFSET_BITS=64, redirect the constants to the off64_t variants. */
68
69#undef F_GETLK
70#undef F_SETLK
71#undef F_SETLKW
72
73/** Flag for flock(). */
74#define F_GETLK F_GETLK64
75/** Flag for flock(). */
76#define F_SETLK F_SETLK64
77/** Flag for flock(). */
78#define F_SETLKW F_SETLKW64
79
Serban Constantinescu48501af2014-03-14 13:16:25 +000080#endif
81
Elliott Hughesb361e682019-11-01 16:47:46 -070082/** Flag for open(). */
Elliott Hughes3f525d42014-06-24 16:32:01 -070083#define O_ASYNC FASYNC
Elliott Hughesb361e682019-11-01 16:47:46 -070084/** Flag for open(). */
Elliott Hughes31165ed2014-09-23 17:34:29 -070085#define O_RSYNC O_SYNC
Elliott Hughes3f525d42014-06-24 16:32:01 -070086
Elliott Hughesb361e682019-11-01 16:47:46 -070087/** Flag for splice(). */
Elliott Hughes3f525d42014-06-24 16:32:01 -070088#define SPLICE_F_MOVE 1
Elliott Hughesb361e682019-11-01 16:47:46 -070089/** Flag for splice(). */
Elliott Hughes3f525d42014-06-24 16:32:01 -070090#define SPLICE_F_NONBLOCK 2
Elliott Hughesb361e682019-11-01 16:47:46 -070091/** Flag for splice(). */
Elliott Hughes3f525d42014-06-24 16:32:01 -070092#define SPLICE_F_MORE 4
Elliott Hughesb361e682019-11-01 16:47:46 -070093/** Flag for splice(). */
Elliott Hughes3f525d42014-06-24 16:32:01 -070094#define SPLICE_F_GIFT 8
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Elliott Hughes95c6cd72019-12-20 13:26:14 -080096#if __ANDROID_API__ >= 26
Elliott Hughesb361e682019-11-01 16:47:46 -070097/** Flag for sync_file_range(). */
Elliott Hughes34347272014-02-26 11:10:32 -080098#define SYNC_FILE_RANGE_WAIT_BEFORE 1
Elliott Hughesb361e682019-11-01 16:47:46 -070099/** Flag for sync_file_range(). */
Elliott Hughes34347272014-02-26 11:10:32 -0800100#define SYNC_FILE_RANGE_WRITE 2
Elliott Hughesb361e682019-11-01 16:47:46 -0700101/** Flag for sync_file_range(). */
Elliott Hughes34347272014-02-26 11:10:32 -0800102#define SYNC_FILE_RANGE_WAIT_AFTER 4
Elliott Hughes9d250722017-07-05 15:59:58 -0700103#endif
Elliott Hughes34347272014-02-26 11:10:32 -0800104
Elliott Hughesb361e682019-11-01 16:47:46 -0700105/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000106 * [creat(2)](https://man7.org/linux/man-pages/man2/creat.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700107 * creates a file.
108 *
109 * Returns a new file descriptor on success and returns -1 and sets `errno` on
110 * failure.
111 */
zijunzhao4aa1ea02023-01-30 19:48:44 +0000112int creat(const char* _Nonnull __path, mode_t __mode);
Elliott Hughesb361e682019-11-01 16:47:46 -0700113/** See creat(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700114int creat64(const char* _Nonnull __path, mode_t __mode);
Elliott Hughesb361e682019-11-01 16:47:46 -0700115
116/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000117 * [openat(2)](https://man7.org/linux/man-pages/man2/openat.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700118 * opens (and possibly creates) a file.
119 *
120 * Returns a new file descriptor on success and returns -1 and sets `errno` on
121 * failure.
122 */
zijunzhao4aa1ea02023-01-30 19:48:44 +0000123int openat(int __dir_fd, const char* _Nonnull __path, int __flags, ...);
Elliott Hughesb361e682019-11-01 16:47:46 -0700124/** See openat(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700125int openat64(int __dir_fd, const char* _Nonnull __path, int __flags, ...);
Elliott Hughesb361e682019-11-01 16:47:46 -0700126
127/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000128 * [open(2)](https://man7.org/linux/man-pages/man2/open.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700129 * opens (and possibly creates) a file.
130 *
131 * Returns a new file descriptor on success and returns -1 and sets `errno` on
132 * failure.
133 */
zijunzhao4aa1ea02023-01-30 19:48:44 +0000134int open(const char* _Nonnull __path, int __flags, ...);
Elliott Hughesb361e682019-11-01 16:47:46 -0700135/** See open(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700136int open64(const char* _Nonnull __path, int __flags, ...);
Elliott Hughesb361e682019-11-01 16:47:46 -0700137
138/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000139 * [splice(2)](https://man7.org/linux/man-pages/man2/splice.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700140 * splices data to/from a pipe.
141 *
142 * Valid flags are `SPLICE_F_MOVE`, `SPLICE_F_NONBLOCK`, `SPLICE_F_MORE`, and
143 * `SPLICE_F_GIFT`.
144 *
145 * Returns the number of bytes spliced on success and returns -1 and sets
146 * `errno` on failure.
Elliott Hughesb361e682019-11-01 16:47:46 -0700147 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700148ssize_t splice(int __in_fd, off64_t* __BIONIC_COMPLICATED_NULLNESS __in_offset, int __out_fd, off64_t* __BIONIC_COMPLICATED_NULLNESS __out_offset, size_t __length, unsigned int __flags);
Elliott Hughesb361e682019-11-01 16:47:46 -0700149
150/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000151 * [tee(2)](https://man7.org/linux/man-pages/man2/tee.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700152 * duplicates data from one pipe to another.
153 *
154 * Valid flags are `SPLICE_F_MOVE`, `SPLICE_F_NONBLOCK`, `SPLICE_F_MORE`, and
155 * `SPLICE_F_GIFT`.
156 *
157 * Returns the number of bytes duplicated on success and returns -1 and sets
158 * `errno` on failure.
Elliott Hughesb361e682019-11-01 16:47:46 -0700159 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700160ssize_t tee(int __in_fd, int __out_fd, size_t __length, unsigned int __flags);
Elliott Hughesb361e682019-11-01 16:47:46 -0700161
162/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000163 * [vmsplice(2)](https://man7.org/linux/man-pages/man2/vmsplice.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700164 * splices data to/from a pipe.
165 *
166 * Valid flags are `SPLICE_F_MOVE`, `SPLICE_F_NONBLOCK`, `SPLICE_F_MORE`, and
167 * `SPLICE_F_GIFT`.
168 *
169 * Returns the number of bytes spliced on success and returns -1 and sets
170 * `errno` on failure.
Elliott Hughesb361e682019-11-01 16:47:46 -0700171 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700172ssize_t vmsplice(int __fd, const struct iovec* _Nonnull __iov, size_t __count, unsigned int __flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173
Elliott Hughesb361e682019-11-01 16:47:46 -0700174/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000175 * [fallocate(2)](https://man7.org/linux/man-pages/man2/fallocate.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700176 * is a Linux-specific extension of posix_fallocate().
177 *
178 * Valid flags are `FALLOC_FL_KEEP_SIZE`, `FALLOC_FL_PUNCH_HOLE`,
179 * `FALLOC_FL_NO_HIDE_STALE`, `FALLOC_FL_COLLAPSE_RANGE`,
180 * `FALLOC_FL_ZERO_RANGE`, `FALLOC_FL_INSERT_RANGE`, and
181 * `FALLOC_FL_UNSHARE_RANGE`.
182 *
183 * Returns 0 on success and returns -1 and sets `errno` on failure.
Elliott Hughesb361e682019-11-01 16:47:46 -0700184 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700185int fallocate(int __fd, int __mode, off_t __offset, off_t __length) __RENAME_IF_FILE_OFFSET64(fallocate64);
Elliott Hughesb361e682019-11-01 16:47:46 -0700186/** See fallocate(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700187int fallocate64(int __fd, int __mode, off64_t __offset, off64_t __length);
Elliott Hughesb361e682019-11-01 16:47:46 -0700188
189/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000190 * [posix_fadvise(2)](https://man7.org/linux/man-pages/man2/posix_fadvise.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700191 * declares an expected access pattern for file data.
192 *
193 * Valid flags are `POSIX_FADV_NORMAL`, `POSIX_FADV_RANDOM`,
194 * `POSIX_FADV_SEQUENTIAL`, `POSIX_FADV_WILLNEED`, `POSIX_FADV_DONTNEED`,
195 * and `POSIX_FADV_NOREUSE`.
196 *
197 * Returns 0 on success and returns an error number on failure.
Elliott Hughesb361e682019-11-01 16:47:46 -0700198 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700199int posix_fadvise(int __fd, off_t __offset, off_t __length, int __advice) __RENAME_IF_FILE_OFFSET64(posix_fadvise64);
Elliott Hughesb361e682019-11-01 16:47:46 -0700200/** See posix_fadvise(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700201int posix_fadvise64(int __fd, off64_t __offset, off64_t __length, int __advice);
Elliott Hughesb361e682019-11-01 16:47:46 -0700202
203/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000204 * [posix_fallocate(2)](https://man7.org/linux/man-pages/man2/posix_fallocate.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700205 * allocates file space.
206 *
207 * Returns 0 on success and returns an error number on failure.
Elliott Hughesb361e682019-11-01 16:47:46 -0700208 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700209int posix_fallocate(int __fd, off_t __offset, off_t __length) __RENAME_IF_FILE_OFFSET64(posix_fallocate64);
Elliott Hughesb361e682019-11-01 16:47:46 -0700210/** See posix_fallocate(). */
Elliott Hughes655e4302023-06-16 12:39:33 -0700211int posix_fallocate64(int __fd, off64_t __offset, off64_t __length);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800212
Elliott Hughes55147ad2016-04-05 11:06:02 -0700213#if defined(__USE_GNU)
Elliott Hughesb361e682019-11-01 16:47:46 -0700214
215/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000216 * [readahead(2)](https://man7.org/linux/man-pages/man2/readahead.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700217 * initiates readahead for the given file.
218 *
219 * Returns 0 on success and returns -1 and sets `errno` on failure.
220 */
221ssize_t readahead(int __fd, off64_t __offset, size_t __length);
222
223/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000224 * [sync_file_range(2)](https://man7.org/linux/man-pages/man2/sync_file_range.2.html)
Elliott Hughesb361e682019-11-01 16:47:46 -0700225 * syncs part of a file with disk.
226 *
227 * Valid flags are `SYNC_FILE_RANGE_WAIT_BEFORE`, `SYNC_FILE_RANGE_WRITE`, and
228 * `SYNC_FILE_RANGE_WAIT_AFTER`.
229 *
230 * Returns 0 on success and returns -1 and sets `errno` on failure.
231 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000232int sync_file_range(int __fd, off64_t __offset, off64_t __length, unsigned int __flags) __INTRODUCED_IN(26);
Elliott Hughesb361e682019-11-01 16:47:46 -0700233
Elliott Hughes55147ad2016-04-05 11:06:02 -0700234#endif
235
George Burgess IVb97049c2017-07-24 15:05:05 -0700236#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
237#include <bits/fortify/fcntl.h>
238#endif
Nick Kralevich8118f622012-06-26 15:08:06 -0700239
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240__END_DECLS