blob: c7922895c9bad1cdf04460ce026215f4552af317 [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);
100int execl(const char* __path, const char* __arg0, ...);
101int execlp(const char* __file, const char* __arg0, ...);
102int execle(const char* __path, const char* __arg0, ...);
David 'Digit' Turnerb083bb52011-05-26 02:46:41 +0200103
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700104int nice(int __incr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700106int setuid(uid_t __uid);
107uid_t getuid(void);
108int seteuid(uid_t __uid);
109uid_t geteuid(void);
110int setgid(gid_t __gid);
111gid_t getgid(void);
112int setegid(gid_t __gid);
113gid_t getegid(void);
114int getgroups(int __size, gid_t* __list);
115int setgroups(size_t __size, const gid_t* __list);
116int setreuid(uid_t __ruid, uid_t __euid);
117int setregid(gid_t __rgid, gid_t __egid);
118int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
119int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
120int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
121int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
122char* getlogin(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700124long fpathconf(int __fd, int __name);
125long pathconf(const char* __path, int __name);
Elliott Hughesa186b2e2014-09-22 14:49:07 -0700126
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700127int access(const char* __path, int __mode);
128int faccessat(int __dirfd, const char* __path, int __mode, int __flags) __INTRODUCED_IN(16);
129int link(const char* __oldpath, const char* __newpath);
130int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
131 const char* __newpath, int __flags) __INTRODUCED_IN(21);
132int unlink(const char* __path);
133int unlinkat(int __dirfd, const char* __path, int __flags);
134int chdir(const char* __path);
135int fchdir(int __fd);
136int rmdir(const char* __path);
137int pipe(int* __pipefd);
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700138#if defined(__USE_GNU)
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700139int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
David 'Digit' Turner275cd482010-09-27 17:33:08 +0200140#endif
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700141int chroot(const char* __path);
142int symlink(const char* __oldpath, const char* __newpath);
143int symlinkat(const char* __oldpath, int __newdirfd, const char* __newpath) __INTRODUCED_IN(21);
144ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
145ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
146 size_t __bufsiz) __INTRODUCED_IN(21);
147int chown(const char* __path, uid_t __owner, gid_t __group);
148int fchown(int __fd, uid_t __owner, gid_t __group);
149int fchownat(int __dirfd, const char* __path, uid_t __owner, gid_t __group, int __flags);
150int lchown(const char* __path, uid_t __owner, gid_t __group);
151char* getcwd(char* __buf, size_t __size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700153int sync(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700155int close(int __fd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700157ssize_t read(int __fd, void* __buf, size_t __count);
158ssize_t write(int __fd, const void* __buf, size_t __count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700160int dup(int __oldfd);
161int dup2(int __oldfd, int __newfd);
162int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
163int fsync(int __fd);
164int fdatasync(int __fd) __INTRODUCED_IN(9);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800165
166#if defined(__USE_FILE_OFFSET64)
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700167off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800168#else
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700169off_t lseek(int __fd, off_t __offset, int __whence);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800170#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700171
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700172off64_t lseek64(int __fd, off64_t __offset, int __whence);
Dan Albertaf4713e2015-09-04 12:59:53 -0700173
174#if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ >= 21
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700175int truncate(const char* __path, off_t __length) __RENAME(truncate64) __INTRODUCED_IN(21);
176ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset) __RENAME(pread64)
Josh Gao14adff12016-04-29 12:00:55 -0700177 __INTRODUCED_IN(12);
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700178ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset)
Josh Gao14adff12016-04-29 12:00:55 -0700179 __RENAME(pwrite64) __INTRODUCED_IN(12);
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700180int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64) __INTRODUCED_IN(12);
Dan Albertaf4713e2015-09-04 12:59:53 -0700181#else
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700182int truncate(const char* __path, off_t __length);
183ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
184ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset);
185int ftruncate(int __fd, off_t __length);
Dan Albertaf4713e2015-09-04 12:59:53 -0700186#endif
187
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700188int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
189ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(12);
190ssize_t pwrite64(int __fd, const void* __buf, size_t __count, off64_t __offset)
Josh Gao14adff12016-04-29 12:00:55 -0700191 __INTRODUCED_IN(12);
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700192int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(12);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700194int pause(void);
195unsigned int alarm(unsigned int __seconds);
196unsigned int sleep(unsigned int __seconds);
197int usleep(useconds_t __usec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
Dan Albertaf4713e2015-09-04 12:59:53 -0700199int gethostname(char* __name, size_t __len);
Josh Gao14adff12016-04-29 12:00:55 -0700200int sethostname(const char* __name, size_t __len) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700202int brk(void* __addr);
203void* sbrk(ptrdiff_t __increment);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700205int isatty(int __fd);
206char* ttyname(int __fd);
207int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700209int acct(const char* __filepath);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210
Dan Albertaf4713e2015-09-04 12:59:53 -0700211#if __ANDROID_API__ >= 21
Josh Gaob6a4a4c2016-07-26 16:34:40 -0700212int getpagesize(void) __INTRODUCED_IN(21);
Dan Albertaf4713e2015-09-04 12:59:53 -0700213#else
214__inline__ int getpagesize(void) {
215 return sysconf(_SC_PAGESIZE);
216}
217#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Dan Albertaf4713e2015-09-04 12:59:53 -0700219long syscall(long __number, ...);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700221int daemon(int __nochdir, int __noclose);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800223#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700224int cacheflush(long __addr, long __nbytes, long __cache);
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800225 /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
226#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700228pid_t tcgetpgrp(int __fd);
229int tcsetpgrp(int __fd, pid_t __pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230
Elliott Hughescf399f72009-10-05 13:19:05 -0700231/* Used to retry syscalls that can return EINTR. */
232#define TEMP_FAILURE_RETRY(exp) ({ \
Dan Alberta7821b72014-05-21 20:33:28 -0700233 __typeof__(exp) _rc; \
Elliott Hughescf399f72009-10-05 13:19:05 -0700234 do { \
235 _rc = (exp); \
236 } while (_rc == -1 && errno == EINTR); \
237 _rc; })
238
Dan Albertaf4713e2015-09-04 12:59:53 -0700239/* TODO(unified-headers): Factor out all the FORTIFY features. */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700240char* __getcwd_chk(char*, size_t, size_t) __INTRODUCED_IN(24);
Daniel Micay9101b002015-05-20 15:31:26 -0400241__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700242char* __getcwd_real(char*, size_t) __RENAME(getcwd);
Daniel Micay9101b002015-05-20 15:31:26 -0400243
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700244ssize_t __pread_chk(int, void*, size_t, off_t, size_t) __INTRODUCED_IN(23);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400245__errordecl(__pread_dest_size_error, "pread called with size bigger than destination");
246__errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700247ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400248
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700249ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t) __INTRODUCED_IN(23);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400250__errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination");
251__errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700252ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64) __INTRODUCED_IN(12);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400253
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700254ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400255__errordecl(__pwrite_dest_size_error, "pwrite called with size bigger than destination");
256__errordecl(__pwrite_count_toobig_error, "pwrite called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700257ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
Daniel Micayafdd1542015-07-20 21:37:29 -0400258
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700259ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400260__errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination");
261__errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700262ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64) __INTRODUCED_IN(12);
Daniel Micayafdd1542015-07-20 21:37:29 -0400263
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700264ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700265__errordecl(__read_dest_size_error, "read called with size bigger than destination");
266__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700267ssize_t __read_real(int, void*, size_t) __RENAME(read);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700268
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700269ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400270__errordecl(__write_dest_size_error, "write called with size bigger than destination");
271__errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700272ssize_t __write_real(int, const void*, size_t) __RENAME(write);
Daniel Micayafdd1542015-07-20 21:37:29 -0400273
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700274ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay42281882015-04-17 11:26:36 -0400275__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
276__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700277ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
Daniel Micay42281882015-04-17 11:26:36 -0400278
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700279ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay42281882015-04-17 11:26:36 -0400280__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
281__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700282ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat) __INTRODUCED_IN(21);
Daniel Micay42281882015-04-17 11:26:36 -0400283
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700284int getdomainname(char*, size_t) __INTRODUCED_IN_FUTURE;
285int setdomainname(const char*, size_t) __INTRODUCED_IN_FUTURE;
Greg Hackmanne2faf072016-03-03 08:37:53 -0800286
Dan Albert658727e2014-10-07 11:10:36 -0700287#if defined(__BIONIC_FORTIFY)
288
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700289#if __ANDROID_API__ >= 24
Daniel Micay9101b002015-05-20 15:31:26 -0400290__BIONIC_FORTIFY_INLINE
291char* getcwd(char* buf, size_t size) {
292 size_t bos = __bos(buf);
293
294#if defined(__clang__)
295 /*
296 * Work around LLVM's incorrect __builtin_object_size implementation here
297 * to avoid needing the workaround in the __getcwd_chk ABI forever.
298 *
299 * https://llvm.org/bugs/show_bug.cgi?id=23277
300 */
301 if (buf == NULL) {
302 bos = __BIONIC_FORTIFY_UNKNOWN_SIZE;
303 }
304#else
305 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
306 return __getcwd_real(buf, size);
307 }
308
309 if (__builtin_constant_p(size) && (size > bos)) {
310 __getcwd_dest_size_error();
311 }
312
313 if (__builtin_constant_p(size) && (size <= bos)) {
314 return __getcwd_real(buf, size);
315 }
316#endif
317
318 return __getcwd_chk(buf, size, bos);
319}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700320#endif /* __ANDROID_API__ >= 24 */
Daniel Micay9101b002015-05-20 15:31:26 -0400321
Daniel Micaye7e1c872015-04-16 09:07:45 -0400322#if defined(__USE_FILE_OFFSET64)
323#define __PREAD_PREFIX(x) __pread64_ ## x
324#else
325#define __PREAD_PREFIX(x) __pread_ ## x
326#endif
327
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700328#if __ANDROID_API__ >= 23
Daniel Micaye7e1c872015-04-16 09:07:45 -0400329__BIONIC_FORTIFY_INLINE
330ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
331 size_t bos = __bos0(buf);
332
333#if !defined(__clang__)
334 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
335 __PREAD_PREFIX(count_toobig_error)();
336 }
337
338 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
339 return __PREAD_PREFIX(real)(fd, buf, count, offset);
340 }
341
342 if (__builtin_constant_p(count) && (count > bos)) {
343 __PREAD_PREFIX(dest_size_error)();
344 }
345
346 if (__builtin_constant_p(count) && (count <= bos)) {
347 return __PREAD_PREFIX(real)(fd, buf, count, offset);
348 }
349#endif
350
351 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
352}
353
354__BIONIC_FORTIFY_INLINE
355ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) {
356 size_t bos = __bos0(buf);
357
358#if !defined(__clang__)
359 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
360 __pread64_count_toobig_error();
361 }
362
363 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
364 return __pread64_real(fd, buf, count, offset);
365 }
366
367 if (__builtin_constant_p(count) && (count > bos)) {
368 __pread64_dest_size_error();
369 }
370
371 if (__builtin_constant_p(count) && (count <= bos)) {
372 return __pread64_real(fd, buf, count, offset);
373 }
374#endif
375
376 return __pread64_chk(fd, buf, count, offset, bos);
377}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700378#endif /* __ANDROID_API__ >= 23 */
Daniel Micaye7e1c872015-04-16 09:07:45 -0400379
Daniel Micayafdd1542015-07-20 21:37:29 -0400380#if defined(__USE_FILE_OFFSET64)
381#define __PWRITE_PREFIX(x) __pwrite64_ ## x
382#else
383#define __PWRITE_PREFIX(x) __pwrite_ ## x
384#endif
385
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700386#if __ANDROID_API__ >= 24
Daniel Micayafdd1542015-07-20 21:37:29 -0400387__BIONIC_FORTIFY_INLINE
388ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) {
389 size_t bos = __bos0(buf);
390
391#if !defined(__clang__)
392 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
393 __PWRITE_PREFIX(count_toobig_error)();
394 }
395
396 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
397 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
398 }
399
400 if (__builtin_constant_p(count) && (count > bos)) {
401 __PWRITE_PREFIX(dest_size_error)();
402 }
403
404 if (__builtin_constant_p(count) && (count <= bos)) {
405 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
406 }
407#endif
408
409 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
410}
411
412__BIONIC_FORTIFY_INLINE
413ssize_t pwrite64(int fd, const void* buf, size_t count, off64_t offset) {
414 size_t bos = __bos0(buf);
415
416#if !defined(__clang__)
417 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
418 __pwrite64_count_toobig_error();
419 }
420
421 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
422 return __pwrite64_real(fd, buf, count, offset);
423 }
424
425 if (__builtin_constant_p(count) && (count > bos)) {
426 __pwrite64_dest_size_error();
427 }
428
429 if (__builtin_constant_p(count) && (count <= bos)) {
430 return __pwrite64_real(fd, buf, count, offset);
431 }
432#endif
433
434 return __pwrite64_chk(fd, buf, count, offset, bos);
435}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700436#endif /* __ANDROID_API__ >= 24 */
Daniel Micayafdd1542015-07-20 21:37:29 -0400437
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700438#if __ANDROID_API__ >= 21
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700439__BIONIC_FORTIFY_INLINE
440ssize_t read(int fd, void* buf, size_t count) {
441 size_t bos = __bos0(buf);
442
443#if !defined(__clang__)
444 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
445 __read_count_toobig_error();
446 }
447
448 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
449 return __read_real(fd, buf, count);
450 }
451
452 if (__builtin_constant_p(count) && (count > bos)) {
453 __read_dest_size_error();
454 }
455
456 if (__builtin_constant_p(count) && (count <= bos)) {
457 return __read_real(fd, buf, count);
458 }
459#endif
460
461 return __read_chk(fd, buf, count, bos);
462}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700463#endif /* __ANDROID_API__ >= 21 */
Daniel Micaye7e1c872015-04-16 09:07:45 -0400464
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700465#if __ANDROID_API__ >= 24
Daniel Micay42281882015-04-17 11:26:36 -0400466__BIONIC_FORTIFY_INLINE
Daniel Micayafdd1542015-07-20 21:37:29 -0400467ssize_t write(int fd, const void* buf, size_t count) {
468 size_t bos = __bos0(buf);
469
470#if !defined(__clang__)
471#if 0 /* work around a false positive due to a missed optimization */
472 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
473 __write_count_toobig_error();
474 }
475#endif
476
477 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
478 return __write_real(fd, buf, count);
479 }
480
481 if (__builtin_constant_p(count) && (count > bos)) {
482 __write_dest_size_error();
483 }
484
485 if (__builtin_constant_p(count) && (count <= bos)) {
486 return __write_real(fd, buf, count);
487 }
488#endif
489
490 return __write_chk(fd, buf, count, bos);
491}
Dan Albertdfa6bbb2016-08-02 15:08:32 -0700492#endif /* __ANDROID_API__ >= 24 */
Daniel Micayafdd1542015-07-20 21:37:29 -0400493
494__BIONIC_FORTIFY_INLINE
Daniel Micay42281882015-04-17 11:26:36 -0400495ssize_t readlink(const char* path, char* buf, size_t size) {
496 size_t bos = __bos(buf);
497
498#if !defined(__clang__)
499 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
500 __readlink_size_toobig_error();
501 }
502
503 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
504 return __readlink_real(path, buf, size);
505 }
506
507 if (__builtin_constant_p(size) && (size > bos)) {
508 __readlink_dest_size_error();
509 }
510
511 if (__builtin_constant_p(size) && (size <= bos)) {
512 return __readlink_real(path, buf, size);
513 }
514#endif
515
516 return __readlink_chk(path, buf, size, bos);
517}
518
519__BIONIC_FORTIFY_INLINE
520ssize_t readlinkat(int dirfd, const char* path, char* buf, size_t size) {
521 size_t bos = __bos(buf);
522
523#if !defined(__clang__)
524 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
525 __readlinkat_size_toobig_error();
526 }
527
528 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
529 return __readlinkat_real(dirfd, path, buf, size);
530 }
531
532 if (__builtin_constant_p(size) && (size > bos)) {
533 __readlinkat_dest_size_error();
534 }
535
536 if (__builtin_constant_p(size) && (size <= bos)) {
537 return __readlinkat_real(dirfd, path, buf, size);
538 }
539#endif
540
541 return __readlinkat_chk(dirfd, path, buf, size, bos);
542}
543
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700544#endif /* defined(__BIONIC_FORTIFY) */
545
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800546__END_DECLS
547
548#endif /* _UNISTD_H_ */