blob: 86949de8e42ee1c1191b8717491559fd1f7c8c33 [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 Hughesf4c948a2014-08-19 11:16:41 -070028
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#ifndef _UNISTD_H_
30#define _UNISTD_H_
31
32#include <stddef.h>
33#include <sys/cdefs.h>
34#include <sys/types.h>
35#include <sys/select.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
Josh Gaoee8d1692016-04-07 14:16:30 -070037#include <bits/fcntl.h>
Josh Gao7449e592016-04-07 16:37:49 -070038#include <bits/getopt.h>
Josh Gao98e574c2016-04-07 14:19:03 -070039#include <bits/ioctl.h>
Elliott Hughes5704c422016-01-25 18:06:24 -080040#include <bits/lockf.h>
41#include <bits/posix_limits.h>
Josh Gao8c8ef592016-04-07 16:33:30 -070042#include <bits/sysconf.h>
Elliott Hughes1ed337d2015-02-02 14:02:09 -080043
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044__BEGIN_DECLS
45
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046#define STDIN_FILENO 0
47#define STDOUT_FILENO 1
48#define STDERR_FILENO 2
49
Elliott Hughes1ed337d2015-02-02 14:02:09 -080050#define F_OK 0
51#define X_OK 1
52#define W_OK 2
53#define R_OK 4
54
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055#define SEEK_SET 0
56#define SEEK_CUR 1
57#define SEEK_END 2
58
Elliott Hughesa186b2e2014-09-22 14:49:07 -070059#define _PC_FILESIZEBITS 0
60#define _PC_LINK_MAX 1
61#define _PC_MAX_CANON 2
62#define _PC_MAX_INPUT 3
63#define _PC_NAME_MAX 4
64#define _PC_PATH_MAX 5
65#define _PC_PIPE_BUF 6
66#define _PC_2_SYMLINKS 7
67#define _PC_ALLOC_SIZE_MIN 8
68#define _PC_REC_INCR_XFER_SIZE 9
69#define _PC_REC_MAX_XFER_SIZE 10
70#define _PC_REC_MIN_XFER_SIZE 11
71#define _PC_REC_XFER_ALIGN 12
72#define _PC_SYMLINK_MAX 13
73#define _PC_CHOWN_RESTRICTED 14
74#define _PC_NO_TRUNC 15
75#define _PC_VDISABLE 16
76#define _PC_ASYNC_IO 17
77#define _PC_PRIO_IO 18
78#define _PC_SYNC_IO 19
79
Elliott Hughes58d9e282014-04-22 17:41:00 -070080extern char** environ;
81
Elliott Hughes3b2096a2016-07-22 18:57:12 -070082__noreturn void _exit(int __status);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Elliott Hughes3b2096a2016-07-22 18:57:12 -070084pid_t fork(void);
85pid_t vfork(void);
86pid_t getpid(void);
87pid_t gettid(void) __pure2;
88pid_t getpgid(pid_t __pid);
89int setpgid(pid_t __pid, pid_t __pgid);
90pid_t getppid(void);
91pid_t getpgrp(void);
92int setpgrp(void);
93pid_t getsid(pid_t __pid) __INTRODUCED_IN(17);
94pid_t setsid(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Elliott Hughes3b2096a2016-07-22 18:57:12 -070096int execv(const char* __path, char* const* __argv);
97int execvp(const char* __file, char* const* __argv);
98int execvpe(const char* __file, char* const* __argv, char* const* __envp) __INTRODUCED_IN(21);
99int execve(const char* __file, char* const* __argv, char* const* __envp);
Josh Gaod80a52e2016-08-10 15:18:29 -0700100int execl(const char* __path, const char* __arg0, ...) __attribute__((__sentinel__));
101int execlp(const char* __file, const char* __arg0, ...) __attribute__((__sentinel__));
102int execle(const char* __path, const char* __arg0, ... /*, char* const* __envp */)
103 __attribute__((__sentinel__(1)));
David 'Digit' Turnerb083bb52011-05-26 02:46:41 +0200104
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700105int nice(int __incr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700107int setuid(uid_t __uid);
108uid_t getuid(void);
109int seteuid(uid_t __uid);
110uid_t geteuid(void);
111int setgid(gid_t __gid);
112gid_t getgid(void);
113int setegid(gid_t __gid);
114gid_t getegid(void);
115int getgroups(int __size, gid_t* __list);
116int setgroups(size_t __size, const gid_t* __list);
117int setreuid(uid_t __ruid, uid_t __euid);
118int setregid(gid_t __rgid, gid_t __egid);
119int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
120int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
121int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
122int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
123char* getlogin(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700125long fpathconf(int __fd, int __name);
126long pathconf(const char* __path, int __name);
Elliott Hughesa186b2e2014-09-22 14:49:07 -0700127
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700128int access(const char* __path, int __mode);
129int faccessat(int __dirfd, const char* __path, int __mode, int __flags) __INTRODUCED_IN(16);
130int link(const char* __oldpath, const char* __newpath);
131int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
132 const char* __newpath, int __flags) __INTRODUCED_IN(21);
133int unlink(const char* __path);
134int unlinkat(int __dirfd, const char* __path, int __flags);
135int chdir(const char* __path);
136int fchdir(int __fd);
137int rmdir(const char* __path);
138int pipe(int* __pipefd);
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700139#if defined(__USE_GNU)
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700140int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
David 'Digit' Turner275cd482010-09-27 17:33:08 +0200141#endif
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700142int chroot(const char* __path);
143int symlink(const char* __oldpath, const char* __newpath);
144int symlinkat(const char* __oldpath, int __newdirfd, const char* __newpath) __INTRODUCED_IN(21);
145ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
146ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
147 size_t __bufsiz) __INTRODUCED_IN(21);
148int chown(const char* __path, uid_t __owner, gid_t __group);
149int fchown(int __fd, uid_t __owner, gid_t __group);
150int fchownat(int __dirfd, const char* __path, uid_t __owner, gid_t __group, int __flags);
151int lchown(const char* __path, uid_t __owner, gid_t __group);
152char* getcwd(char* __buf, size_t __size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700154int sync(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700156int close(int __fd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700158ssize_t read(int __fd, void* __buf, size_t __count);
159ssize_t write(int __fd, const void* __buf, size_t __count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700161int dup(int __oldfd);
162int dup2(int __oldfd, int __newfd);
163int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
164int fsync(int __fd);
165int fdatasync(int __fd) __INTRODUCED_IN(9);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800166
167#if defined(__USE_FILE_OFFSET64)
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700168off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800169#else
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700170off_t lseek(int __fd, off_t __offset, int __whence);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800171#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700172
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700173off64_t lseek64(int __fd, off64_t __offset, int __whence);
Dan Albertaf4713e2015-09-04 12:59:53 -0700174
175#if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ >= 21
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700176int truncate(const char* __path, off_t __length) __RENAME(truncate64) __INTRODUCED_IN(21);
177ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset) __RENAME(pread64)
Josh Gao14adff12016-04-29 12:00:55 -0700178 __INTRODUCED_IN(12);
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700179ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset)
Josh Gao14adff12016-04-29 12:00:55 -0700180 __RENAME(pwrite64) __INTRODUCED_IN(12);
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700181int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64) __INTRODUCED_IN(12);
Dan Albertaf4713e2015-09-04 12:59:53 -0700182#else
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700183int truncate(const char* __path, off_t __length);
184ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
185ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset);
186int ftruncate(int __fd, off_t __length);
Dan Albertaf4713e2015-09-04 12:59:53 -0700187#endif
188
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700189int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
190ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(12);
191ssize_t pwrite64(int __fd, const void* __buf, size_t __count, off64_t __offset)
Josh Gao14adff12016-04-29 12:00:55 -0700192 __INTRODUCED_IN(12);
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700193int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(12);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700195int pause(void);
196unsigned int alarm(unsigned int __seconds);
197unsigned int sleep(unsigned int __seconds);
198int usleep(useconds_t __usec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199
Dan Albertaf4713e2015-09-04 12:59:53 -0700200int gethostname(char* __name, size_t __len);
Josh Gao14adff12016-04-29 12:00:55 -0700201int sethostname(const char* __name, size_t __len) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700203int brk(void* __addr);
204void* sbrk(ptrdiff_t __increment);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700206int isatty(int __fd);
207char* ttyname(int __fd);
208int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700210int acct(const char* __filepath);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211
Dan Albertaf4713e2015-09-04 12:59:53 -0700212#if __ANDROID_API__ >= 21
Josh Gaob6a4a4c2016-07-26 16:34:40 -0700213int getpagesize(void) __INTRODUCED_IN(21);
Dan Albertaf4713e2015-09-04 12:59:53 -0700214#else
215__inline__ int getpagesize(void) {
216 return sysconf(_SC_PAGESIZE);
217}
218#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219
Dan Albertaf4713e2015-09-04 12:59:53 -0700220long syscall(long __number, ...);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700222int daemon(int __nochdir, int __noclose);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800224#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700225int cacheflush(long __addr, long __nbytes, long __cache);
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800226 /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
227#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700229pid_t tcgetpgrp(int __fd);
230int tcsetpgrp(int __fd, pid_t __pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231
Elliott Hughescf399f72009-10-05 13:19:05 -0700232/* Used to retry syscalls that can return EINTR. */
233#define TEMP_FAILURE_RETRY(exp) ({ \
Dan Alberta7821b72014-05-21 20:33:28 -0700234 __typeof__(exp) _rc; \
Elliott Hughescf399f72009-10-05 13:19:05 -0700235 do { \
236 _rc = (exp); \
237 } while (_rc == -1 && errno == EINTR); \
238 _rc; })
239
Dan Albertaf4713e2015-09-04 12:59:53 -0700240/* TODO(unified-headers): Factor out all the FORTIFY features. */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700241char* __getcwd_chk(char*, size_t, size_t) __INTRODUCED_IN(24);
Daniel Micay9101b002015-05-20 15:31:26 -0400242__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700243char* __getcwd_real(char*, size_t) __RENAME(getcwd);
Daniel Micay9101b002015-05-20 15:31:26 -0400244
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700245ssize_t __pread_chk(int, void*, size_t, off_t, size_t) __INTRODUCED_IN(23);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400246__errordecl(__pread_dest_size_error, "pread called with size bigger than destination");
247__errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700248ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400249
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700250ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t) __INTRODUCED_IN(23);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400251__errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination");
252__errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700253ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64) __INTRODUCED_IN(12);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400254
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700255ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400256__errordecl(__pwrite_dest_size_error, "pwrite called with size bigger than destination");
257__errordecl(__pwrite_count_toobig_error, "pwrite called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700258ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
Daniel Micayafdd1542015-07-20 21:37:29 -0400259
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700260ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400261__errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination");
262__errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700263ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64) __INTRODUCED_IN(12);
Daniel Micayafdd1542015-07-20 21:37:29 -0400264
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700265ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700266__errordecl(__read_dest_size_error, "read called with size bigger than destination");
267__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700268ssize_t __read_real(int, void*, size_t) __RENAME(read);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700269
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700270ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400271__errordecl(__write_dest_size_error, "write called with size bigger than destination");
272__errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700273ssize_t __write_real(int, const void*, size_t) __RENAME(write);
Daniel Micayafdd1542015-07-20 21:37:29 -0400274
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700275ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay42281882015-04-17 11:26:36 -0400276__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
277__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700278ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
Daniel Micay42281882015-04-17 11:26:36 -0400279
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700280ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay42281882015-04-17 11:26:36 -0400281__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
282__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700283ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat) __INTRODUCED_IN(21);
Daniel Micay42281882015-04-17 11:26:36 -0400284
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700285int getdomainname(char*, size_t) __INTRODUCED_IN_FUTURE;
286int setdomainname(const char*, size_t) __INTRODUCED_IN_FUTURE;
Greg Hackmanne2faf072016-03-03 08:37:53 -0800287
Dan Albert658727e2014-10-07 11:10:36 -0700288#if defined(__BIONIC_FORTIFY)
289
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700290#if __ANDROID_API__ >= 24
Daniel Micay9101b002015-05-20 15:31:26 -0400291__BIONIC_FORTIFY_INLINE
292char* getcwd(char* buf, size_t size) {
293 size_t bos = __bos(buf);
294
295#if defined(__clang__)
296 /*
297 * Work around LLVM's incorrect __builtin_object_size implementation here
298 * to avoid needing the workaround in the __getcwd_chk ABI forever.
299 *
300 * https://llvm.org/bugs/show_bug.cgi?id=23277
301 */
302 if (buf == NULL) {
303 bos = __BIONIC_FORTIFY_UNKNOWN_SIZE;
304 }
305#else
306 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
307 return __getcwd_real(buf, size);
308 }
309
310 if (__builtin_constant_p(size) && (size > bos)) {
311 __getcwd_dest_size_error();
312 }
313
314 if (__builtin_constant_p(size) && (size <= bos)) {
315 return __getcwd_real(buf, size);
316 }
317#endif
318
319 return __getcwd_chk(buf, size, bos);
320}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700321#endif /* __ANDROID_API__ >= 24 */
Daniel Micay9101b002015-05-20 15:31:26 -0400322
Daniel Micaye7e1c872015-04-16 09:07:45 -0400323#if defined(__USE_FILE_OFFSET64)
324#define __PREAD_PREFIX(x) __pread64_ ## x
325#else
326#define __PREAD_PREFIX(x) __pread_ ## x
327#endif
328
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700329#if __ANDROID_API__ >= 23
Daniel Micaye7e1c872015-04-16 09:07:45 -0400330__BIONIC_FORTIFY_INLINE
331ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
332 size_t bos = __bos0(buf);
333
334#if !defined(__clang__)
335 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
336 __PREAD_PREFIX(count_toobig_error)();
337 }
338
339 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
340 return __PREAD_PREFIX(real)(fd, buf, count, offset);
341 }
342
343 if (__builtin_constant_p(count) && (count > bos)) {
344 __PREAD_PREFIX(dest_size_error)();
345 }
346
347 if (__builtin_constant_p(count) && (count <= bos)) {
348 return __PREAD_PREFIX(real)(fd, buf, count, offset);
349 }
350#endif
351
352 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
353}
354
355__BIONIC_FORTIFY_INLINE
356ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) {
357 size_t bos = __bos0(buf);
358
359#if !defined(__clang__)
360 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
361 __pread64_count_toobig_error();
362 }
363
364 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
365 return __pread64_real(fd, buf, count, offset);
366 }
367
368 if (__builtin_constant_p(count) && (count > bos)) {
369 __pread64_dest_size_error();
370 }
371
372 if (__builtin_constant_p(count) && (count <= bos)) {
373 return __pread64_real(fd, buf, count, offset);
374 }
375#endif
376
377 return __pread64_chk(fd, buf, count, offset, bos);
378}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700379#endif /* __ANDROID_API__ >= 23 */
Daniel Micaye7e1c872015-04-16 09:07:45 -0400380
Daniel Micayafdd1542015-07-20 21:37:29 -0400381#if defined(__USE_FILE_OFFSET64)
382#define __PWRITE_PREFIX(x) __pwrite64_ ## x
383#else
384#define __PWRITE_PREFIX(x) __pwrite_ ## x
385#endif
386
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700387#if __ANDROID_API__ >= 24
Daniel Micayafdd1542015-07-20 21:37:29 -0400388__BIONIC_FORTIFY_INLINE
389ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) {
390 size_t bos = __bos0(buf);
391
392#if !defined(__clang__)
393 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
394 __PWRITE_PREFIX(count_toobig_error)();
395 }
396
397 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
398 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
399 }
400
401 if (__builtin_constant_p(count) && (count > bos)) {
402 __PWRITE_PREFIX(dest_size_error)();
403 }
404
405 if (__builtin_constant_p(count) && (count <= bos)) {
406 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
407 }
408#endif
409
410 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
411}
412
413__BIONIC_FORTIFY_INLINE
414ssize_t pwrite64(int fd, const void* buf, size_t count, off64_t offset) {
415 size_t bos = __bos0(buf);
416
417#if !defined(__clang__)
418 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
419 __pwrite64_count_toobig_error();
420 }
421
422 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
423 return __pwrite64_real(fd, buf, count, offset);
424 }
425
426 if (__builtin_constant_p(count) && (count > bos)) {
427 __pwrite64_dest_size_error();
428 }
429
430 if (__builtin_constant_p(count) && (count <= bos)) {
431 return __pwrite64_real(fd, buf, count, offset);
432 }
433#endif
434
435 return __pwrite64_chk(fd, buf, count, offset, bos);
436}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700437#endif /* __ANDROID_API__ >= 24 */
Daniel Micayafdd1542015-07-20 21:37:29 -0400438
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700439#if __ANDROID_API__ >= 21
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700440__BIONIC_FORTIFY_INLINE
441ssize_t read(int fd, void* buf, size_t count) {
442 size_t bos = __bos0(buf);
443
444#if !defined(__clang__)
445 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
446 __read_count_toobig_error();
447 }
448
449 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
450 return __read_real(fd, buf, count);
451 }
452
453 if (__builtin_constant_p(count) && (count > bos)) {
454 __read_dest_size_error();
455 }
456
457 if (__builtin_constant_p(count) && (count <= bos)) {
458 return __read_real(fd, buf, count);
459 }
460#endif
461
462 return __read_chk(fd, buf, count, bos);
463}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700464#endif /* __ANDROID_API__ >= 21 */
Daniel Micaye7e1c872015-04-16 09:07:45 -0400465
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700466#if __ANDROID_API__ >= 24
Daniel Micay42281882015-04-17 11:26:36 -0400467__BIONIC_FORTIFY_INLINE
Daniel Micayafdd1542015-07-20 21:37:29 -0400468ssize_t write(int fd, const void* buf, size_t count) {
469 size_t bos = __bos0(buf);
470
471#if !defined(__clang__)
472#if 0 /* work around a false positive due to a missed optimization */
473 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
474 __write_count_toobig_error();
475 }
476#endif
477
478 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
479 return __write_real(fd, buf, count);
480 }
481
482 if (__builtin_constant_p(count) && (count > bos)) {
483 __write_dest_size_error();
484 }
485
486 if (__builtin_constant_p(count) && (count <= bos)) {
487 return __write_real(fd, buf, count);
488 }
489#endif
490
491 return __write_chk(fd, buf, count, bos);
492}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700493#endif /* __ANDROID_API__ >= 24 */
Daniel Micayafdd1542015-07-20 21:37:29 -0400494
495__BIONIC_FORTIFY_INLINE
Daniel Micay42281882015-04-17 11:26:36 -0400496ssize_t readlink(const char* path, char* buf, size_t size) {
497 size_t bos = __bos(buf);
498
499#if !defined(__clang__)
500 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
501 __readlink_size_toobig_error();
502 }
503
504 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
505 return __readlink_real(path, buf, size);
506 }
507
508 if (__builtin_constant_p(size) && (size > bos)) {
509 __readlink_dest_size_error();
510 }
511
512 if (__builtin_constant_p(size) && (size <= bos)) {
513 return __readlink_real(path, buf, size);
514 }
515#endif
516
517 return __readlink_chk(path, buf, size, bos);
518}
519
520__BIONIC_FORTIFY_INLINE
521ssize_t readlinkat(int dirfd, const char* path, char* buf, size_t size) {
522 size_t bos = __bos(buf);
523
524#if !defined(__clang__)
525 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
526 __readlinkat_size_toobig_error();
527 }
528
529 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
530 return __readlinkat_real(dirfd, path, buf, size);
531 }
532
533 if (__builtin_constant_p(size) && (size > bos)) {
534 __readlinkat_dest_size_error();
535 }
536
537 if (__builtin_constant_p(size) && (size <= bos)) {
538 return __readlinkat_real(dirfd, path, buf, size);
539 }
540#endif
541
542 return __readlinkat_chk(dirfd, path, buf, size, bos);
543}
544
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700545#endif /* defined(__BIONIC_FORTIFY) */
546
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800547__END_DECLS
548
549#endif /* _UNISTD_H_ */