blob: 88bcc7826c5b889dcfda0d6c44b4f0fb1f6a769d [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#ifndef _FCNTL_H
30#define _FCNTL_H
31
32#include <sys/cdefs.h>
33#include <sys/types.h>
Elliott Hughes01e505a2014-01-07 17:47:20 -080034#include <linux/fadvise.h>
Nick Desaulniers09d900d2016-07-19 15:20:24 -070035#include <linux/falloc.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <linux/fcntl.h>
Elliott Hughes64d83822014-12-29 11:14:38 -080037#include <linux/stat.h>
Elliott Hughes3f525d42014-06-24 16:32:01 -070038#include <linux/uio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
Josh Gaoee8d1692016-04-07 14:16:30 -070040#include <bits/fcntl.h>
Elliott Hughesfd936ae2016-08-12 10:16:34 -070041#include <bits/seek_constants.h>
Josh Gaoee8d1692016-04-07 14:16:30 -070042
Elliott Hughes5704c422016-01-25 18:06:24 -080043#if defined(__USE_GNU) || defined(__USE_BSD)
44#include <bits/lockf.h>
45#endif
46
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047__BEGIN_DECLS
48
Serban Constantinescu48501af2014-03-14 13:16:25 +000049#ifdef __LP64__
Christopher Ferrisba8d4f42014-09-03 19:56:49 -070050/* LP64 kernels don't have F_*64 defines because their flock is 64-bit. */
Serban Constantinescu48501af2014-03-14 13:16:25 +000051#define F_GETLK64 F_GETLK
52#define F_SETLK64 F_SETLK
53#define F_SETLKW64 F_SETLKW
54#endif
55
Elliott Hughes3f525d42014-06-24 16:32:01 -070056#define O_ASYNC FASYNC
Elliott Hughes31165ed2014-09-23 17:34:29 -070057#define O_RSYNC O_SYNC
Elliott Hughes3f525d42014-06-24 16:32:01 -070058
59#define SPLICE_F_MOVE 1
60#define SPLICE_F_NONBLOCK 2
61#define SPLICE_F_MORE 4
62#define SPLICE_F_GIFT 8
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
Elliott Hughes34347272014-02-26 11:10:32 -080064#define SYNC_FILE_RANGE_WAIT_BEFORE 1
65#define SYNC_FILE_RANGE_WRITE 2
66#define SYNC_FILE_RANGE_WAIT_AFTER 4
67
Elliott Hughes3b2096a2016-07-22 18:57:12 -070068int creat(const char*, mode_t);
69int creat64(const char*, mode_t) __INTRODUCED_IN(21);
George Burgess IV7cc779f2017-02-09 00:00:31 -080070int openat(int, const char*, int, ...) __overloadable
71 __RENAME_CLANG(openat);
Elliott Hughes3b2096a2016-07-22 18:57:12 -070072int openat64(int, const char*, int, ...) __INTRODUCED_IN(21);
George Burgess IV7cc779f2017-02-09 00:00:31 -080073int open(const char*, int, ...) __overloadable __RENAME_CLANG(open);
Elliott Hughes3b2096a2016-07-22 18:57:12 -070074int open64(const char*, int, ...) __INTRODUCED_IN(21);
75ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int) __INTRODUCED_IN(21);
76ssize_t tee(int, int, size_t, unsigned int) __INTRODUCED_IN(21);
77ssize_t vmsplice(int, const struct iovec*, size_t, unsigned int) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
Elliott Hughes68dc20d2015-02-06 22:28:49 -080079#if defined(__USE_FILE_OFFSET64)
Elliott Hughes3b2096a2016-07-22 18:57:12 -070080int fallocate(int, int, off_t, off_t) __RENAME(fallocate64) __INTRODUCED_IN(21);
81int posix_fadvise(int, off_t, off_t, int) __RENAME(posix_fadvise64) __INTRODUCED_IN(21);
Elliott Hughes9bf24972016-10-25 14:13:32 -070082int posix_fallocate(int, off_t, off_t) __RENAME(posix_fallocate64) __INTRODUCED_IN(21);
Elliott Hughes68dc20d2015-02-06 22:28:49 -080083#else
Elliott Hughes3b2096a2016-07-22 18:57:12 -070084int fallocate(int, int, off_t, off_t) __INTRODUCED_IN(21);
85int posix_fadvise(int, off_t, off_t, int) __INTRODUCED_IN(21);
86int posix_fallocate(int, off_t, off_t) __INTRODUCED_IN(21);
Elliott Hughes68dc20d2015-02-06 22:28:49 -080087#endif
Elliott Hughes3b2096a2016-07-22 18:57:12 -070088int fallocate64(int, int, off64_t, off64_t) __INTRODUCED_IN(21);
89int posix_fadvise64(int, off64_t, off64_t, int) __INTRODUCED_IN(21);
90int posix_fallocate64(int, off64_t, off64_t) __INTRODUCED_IN(21);
Elliott Hughes68dc20d2015-02-06 22:28:49 -080091
Elliott Hughes55147ad2016-04-05 11:06:02 -070092#if defined(__USE_GNU)
Josh Gao46b44162016-05-27 11:14:16 -070093ssize_t readahead(int, off64_t, size_t) __INTRODUCED_IN(16);
Josh Gao34c599a2016-04-29 13:45:25 -070094int sync_file_range(int, off64_t, off64_t, unsigned int) __INTRODUCED_IN_FUTURE;
Elliott Hughes55147ad2016-04-05 11:06:02 -070095#endif
96
Elliott Hughes3b2096a2016-07-22 18:57:12 -070097int __open_2(const char*, int) __INTRODUCED_IN(17);
Elliott Hughes3b2096a2016-07-22 18:57:12 -070098int __openat_2(int, const char*, int) __INTRODUCED_IN(17);
George Burgess IV7cc779f2017-02-09 00:00:31 -080099/*
100 * These are the easiest way to call the real open even in clang FORTIFY.
101 */
102int __open_real(const char*, int, ...) __RENAME(open);
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700103int __openat_real(int, const char*, int, ...) __RENAME(openat);
George Burgess IV7cc779f2017-02-09 00:00:31 -0800104
Elliott Hughescd0609f2013-12-19 12:21:07 -0800105
Dan Albert658727e2014-10-07 11:10:36 -0700106#if defined(__BIONIC_FORTIFY)
George Burgess IV7cc779f2017-02-09 00:00:31 -0800107#define __open_too_many_args_error "too many arguments"
108#define __open_too_few_args_error "called with O_CREAT, but missing mode"
109#if defined(__clang__)
Dan Albert658727e2014-10-07 11:10:36 -0700110
George Burgess IV7cc779f2017-02-09 00:00:31 -0800111__BIONIC_ERROR_FUNCTION_VISIBILITY
112int open(const char* pathname, int flags, mode_t modes, ...) __overloadable
113 __errorattr(__open_too_many_args_error);
114
115__BIONIC_ERROR_FUNCTION_VISIBILITY
116int open(const char* pathname, int flags) __overloadable
117 __enable_if(flags & O_CREAT, __open_too_few_args_error)
118 __errorattr(__open_too_few_args_error);
119
120/*
121 * pass_object_size serves two purposes here, neither of which involve __bos: it
122 * disqualifies this function from having its address taken (so &open works),
123 * and it makes overload resolution prefer open(const char *, int) over
124 * open(const char *, int, ...).
125 */
126__BIONIC_FORTIFY_INLINE
127int open(const char* const __pass_object_size pathname,
128 int flags) __overloadable {
129 return __open_2(pathname, flags);
130}
131
132__BIONIC_FORTIFY_INLINE
133int open(const char* const __pass_object_size pathname, int flags, mode_t modes)
134 __overloadable {
135 return __open_real(pathname, flags, modes);
136}
137
138__BIONIC_ERROR_FUNCTION_VISIBILITY
139int openat(int dirfd, const char* pathname, int flags) __overloadable
140 __enable_if(flags & O_CREAT, __open_too_few_args_error)
141 __errorattr(__open_too_few_args_error);
142
143__BIONIC_ERROR_FUNCTION_VISIBILITY
144int openat(int dirfd, const char* pathname, int flags, mode_t modes, ...)
145 __overloadable
146 __errorattr(__open_too_many_args_error);
147
148__BIONIC_FORTIFY_INLINE
149int openat(int dirfd, const char* const __pass_object_size pathname,
150 int flags) __overloadable {
151 return __openat_2(dirfd, pathname, flags);
152}
153
154__BIONIC_FORTIFY_INLINE
155int openat(int dirfd, const char* const __pass_object_size pathname, int flags,
156 mode_t modes) __overloadable {
157 return __openat_real(dirfd, pathname, flags, modes);
158}
159
160#else /* defined(__clang__) */
161__errordecl(__creat_missing_mode, __open_too_few_args_error);
162__errordecl(__creat_too_many_args, __open_too_many_args_error);
Nick Kralevich8118f622012-06-26 15:08:06 -0700163
164__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800165int open(const char* pathname, int flags, ...) {
Nick Kralevich8118f622012-06-26 15:08:06 -0700166 if (__builtin_constant_p(flags)) {
167 if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700168 __creat_missing_mode(); /* Compile time error. */
Nick Kralevich8118f622012-06-26 15:08:06 -0700169 }
170 }
171
172 if (__builtin_va_arg_pack_len() > 1) {
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700173 __creat_too_many_args(); /* Compile time error. */
Nick Kralevich8118f622012-06-26 15:08:06 -0700174 }
175
Nick Kralevicha3e230d2012-07-02 12:24:42 -0700176 if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
Nick Kralevich8118f622012-06-26 15:08:06 -0700177 return __open_2(pathname, flags);
178 }
179
180 return __open_real(pathname, flags, __builtin_va_arg_pack());
181}
182
Nick Kralevicha3e230d2012-07-02 12:24:42 -0700183__BIONIC_FORTIFY_INLINE
Elliott Hughescd0609f2013-12-19 12:21:07 -0800184int openat(int dirfd, const char* pathname, int flags, ...) {
Nick Kralevicha3e230d2012-07-02 12:24:42 -0700185 if (__builtin_constant_p(flags)) {
186 if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700187 __creat_missing_mode(); /* Compile time error. */
Nick Kralevicha3e230d2012-07-02 12:24:42 -0700188 }
189 }
190
191 if (__builtin_va_arg_pack_len() > 1) {
Elliott Hughes3ba55f82016-06-08 18:11:23 -0700192 __creat_too_many_args(); /* Compile time error. */
Nick Kralevicha3e230d2012-07-02 12:24:42 -0700193 }
194
195 if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
196 return __openat_2(dirfd, pathname, flags);
197 }
198
199 return __openat_real(dirfd, pathname, flags, __builtin_va_arg_pack());
200}
201
George Burgess IV7cc779f2017-02-09 00:00:31 -0800202#endif /* defined(__clang__) */
Elliott Hughescd0609f2013-12-19 12:21:07 -0800203
George Burgess IV7cc779f2017-02-09 00:00:31 -0800204#undef __open_too_many_args_error
205#undef __open_too_few_args_error
Elliott Hughescd0609f2013-12-19 12:21:07 -0800206#endif /* defined(__BIONIC_FORTIFY) */
Nick Kralevich8118f622012-06-26 15:08:06 -0700207
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208__END_DECLS
209
210#endif /* _FCNTL_H */