blob: 937a8732b085b45c8c205c975239dceed7b9e567 [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
Dan Albertaf4713e2015-09-04 12:59:53 -070082extern __noreturn void _exit(int __status);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
84extern pid_t fork(void);
85extern pid_t vfork(void);
86extern pid_t getpid(void);
Elliott Hughesb27a8402014-06-10 20:47:49 -070087extern pid_t gettid(void) __pure2;
Dan Albertaf4713e2015-09-04 12:59:53 -070088extern pid_t getpgid(pid_t __pid);
89extern int setpgid(pid_t __pid, pid_t __pgid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090extern pid_t getppid(void);
91extern pid_t getpgrp(void);
92extern int setpgrp(void);
Dan Albertaf4713e2015-09-04 12:59:53 -070093extern pid_t getsid(pid_t __pid) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094extern pid_t setsid(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Dan Albertaf4713e2015-09-04 12:59:53 -070096extern int execv(const char* __path, char* const* __argv);
97extern int execvp(const char* __file, char* const* __argv);
98extern int execvpe(const char* __file, char* const* __argv, char* const* __envp)
99 __INTRODUCED_IN(21);
100extern int execve(const char* __file, char* const* __argv, char* const* __envp);
101extern int execl(const char* __path, const char* __arg0, ...);
102extern int execlp(const char* __file, const char* __arg0, ...);
103extern int execle(const char* __path, const char* __arg0, ...);
David 'Digit' Turnerb083bb52011-05-26 02:46:41 +0200104
Dan Albertaf4713e2015-09-04 12:59:53 -0700105extern int nice(int __incr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Dan Albertaf4713e2015-09-04 12:59:53 -0700107extern int setuid(uid_t __uid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108extern uid_t getuid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700109extern int seteuid(uid_t __uid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110extern uid_t geteuid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700111extern int setgid(gid_t __gid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112extern gid_t getgid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700113extern int setegid(gid_t __gid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114extern gid_t getegid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700115extern int getgroups(int __size, gid_t* __list);
116extern int setgroups(size_t __size, const gid_t* __list);
117extern int setreuid(uid_t __ruid, uid_t __euid);
118extern int setregid(gid_t __rgid, gid_t __egid);
119extern int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
120extern int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
121extern int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
122extern int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123extern char* getlogin(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Dan Albertaf4713e2015-09-04 12:59:53 -0700125extern long fpathconf(int __fd, int __name);
126extern long pathconf(const char* __path, int __name);
Elliott Hughesa186b2e2014-09-22 14:49:07 -0700127
Dan Albertaf4713e2015-09-04 12:59:53 -0700128extern int access(const char* __path, int __mode);
129extern int faccessat(int __dirfd, const char* __path, int __mode, int __flags)
130 __INTRODUCED_IN(21);
131extern int link(const char* __oldpath, const char* __newpath);
132extern int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
133 const char* __newpath, int __flags) __INTRODUCED_IN(21);
134extern int unlink(const char* __path);
135extern int unlinkat(int __dirfd, const char* __path, int __flags);
136extern int chdir(const char* __path);
137extern int fchdir(int __fd);
138extern int rmdir(const char* __path);
139extern int pipe(int* __pipefd);
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700140#if defined(__USE_GNU)
Dan Albertaf4713e2015-09-04 12:59:53 -0700141extern int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
David 'Digit' Turner275cd482010-09-27 17:33:08 +0200142#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700143extern int chroot(const char* __path);
144extern int symlink(const char* __oldpath, const char* __newpath);
145extern int symlinkat(const char* __oldpath, int __newdirfd,
146 const char* __newpath) __INTRODUCED_IN(21);
147extern ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
148extern ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
149 size_t __bufsiz) __INTRODUCED_IN(21);
150extern int chown(const char* __path, uid_t __owner, gid_t __group);
151extern int fchown(int __fd, uid_t __owner, gid_t __group);
152extern int fchownat(int __dirfd, const char* __path, uid_t __owner,
153 gid_t __group, int __flags);
154extern int lchown(const char* __path, uid_t __owner, gid_t __group);
155extern char* getcwd(char* __buf, size_t __size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156
157extern int sync(void);
158
Dan Albertaf4713e2015-09-04 12:59:53 -0700159extern int close(int __fd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
Dan Albertaf4713e2015-09-04 12:59:53 -0700161extern ssize_t read(int __fd, void* __buf, size_t __count);
162extern ssize_t write(int __fd, const void* __buf, size_t __count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
Dan Albertaf4713e2015-09-04 12:59:53 -0700164extern int dup(int __oldfd);
165extern int dup2(int __oldfd, int __newfd);
166extern int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
Dan Albertaf4713e2015-09-04 12:59:53 -0700167extern int fsync(int __fd);
168extern int fdatasync(int __fd) __INTRODUCED_IN(9);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800169
170#if defined(__USE_FILE_OFFSET64)
Dan Albertaf4713e2015-09-04 12:59:53 -0700171extern off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800172#else
Dan Albertaf4713e2015-09-04 12:59:53 -0700173extern off_t lseek(int __fd, off_t __offset, int __whence);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800174#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700175
176extern off64_t lseek64(int __fd, off64_t __offset, int __whence);
177
178#if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ >= 21
Josh Gao14adff12016-04-29 12:00:55 -0700179extern int truncate(const char* __path, off_t __length) __RENAME(truncate64) __INTRODUCED_IN(21);
180extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset) __RENAME(pread64)
181 __INTRODUCED_IN(12);
182extern ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset)
183 __RENAME(pwrite64) __INTRODUCED_IN(12);
184extern int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64) __INTRODUCED_IN(12);
Dan Albertaf4713e2015-09-04 12:59:53 -0700185#else
186extern int truncate(const char* __path, off_t __length);
187extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
188extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
189 off_t __offset);
190extern int ftruncate(int __fd, off_t __length);
191#endif
192
193extern int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
Josh Gao14adff12016-04-29 12:00:55 -0700194extern ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(12);
195extern ssize_t pwrite64(int __fd, const void* __buf, size_t __count, off64_t __offset)
196 __INTRODUCED_IN(12);
197extern int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(12);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
199extern int pause(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700200extern unsigned int alarm(unsigned int __seconds);
201extern unsigned int sleep(unsigned int __seconds);
202extern int usleep(useconds_t __usec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203
Dan Albertaf4713e2015-09-04 12:59:53 -0700204int gethostname(char* __name, size_t __len);
Josh Gao14adff12016-04-29 12:00:55 -0700205int sethostname(const char* __name, size_t __len) __INTRODUCED_IN(23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206
Dan Albertaf4713e2015-09-04 12:59:53 -0700207extern void* __brk(void* __addr);
208extern int brk(void* __addr);
209extern void* sbrk(ptrdiff_t __increment);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210
Dan Albertaf4713e2015-09-04 12:59:53 -0700211extern int isatty(int __fd);
212extern char* ttyname(int __fd);
213extern int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214
Dan Albertaf4713e2015-09-04 12:59:53 -0700215extern int acct(const char* __filepath);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216
Dan Albertaf4713e2015-09-04 12:59:53 -0700217#if __ANDROID_API__ >= 21
Bernhard Rosenkraenzer9ae59c02013-09-18 23:29:08 +0200218int getpagesize(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700219#else
220__inline__ int getpagesize(void) {
221 return sysconf(_SC_PAGESIZE);
222}
223#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224
Dan Albertaf4713e2015-09-04 12:59:53 -0700225long syscall(long __number, ...);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226
Dan Albertaf4713e2015-09-04 12:59:53 -0700227extern int daemon(int __nochdir, int __noclose);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800229#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Dan Albertaf4713e2015-09-04 12:59:53 -0700230extern int cacheflush(long __addr, long __nbytes, long __cache);
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800231 /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
232#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
Dan Albertaf4713e2015-09-04 12:59:53 -0700234extern pid_t tcgetpgrp(int __fd);
235extern int tcsetpgrp(int __fd, pid_t __pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236
Elliott Hughescf399f72009-10-05 13:19:05 -0700237/* Used to retry syscalls that can return EINTR. */
238#define TEMP_FAILURE_RETRY(exp) ({ \
Dan Alberta7821b72014-05-21 20:33:28 -0700239 __typeof__(exp) _rc; \
Elliott Hughescf399f72009-10-05 13:19:05 -0700240 do { \
241 _rc = (exp); \
242 } while (_rc == -1 && errno == EINTR); \
243 _rc; })
244
Dan Albertaf4713e2015-09-04 12:59:53 -0700245/* TODO(unified-headers): Factor out all the FORTIFY features. */
Josh Gao14adff12016-04-29 12:00:55 -0700246extern char* __getcwd_chk(char*, size_t, size_t) __INTRODUCED_IN(24);
Daniel Micay9101b002015-05-20 15:31:26 -0400247__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
248extern char* __getcwd_real(char*, size_t) __RENAME(getcwd);
249
Josh Gao14adff12016-04-29 12:00:55 -0700250extern ssize_t __pread_chk(int, void*, size_t, off_t, size_t) __INTRODUCED_IN(23);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400251__errordecl(__pread_dest_size_error, "pread called with size bigger than destination");
252__errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX");
253extern ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
254
Josh Gao14adff12016-04-29 12:00:55 -0700255extern ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t) __INTRODUCED_IN(23);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400256__errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination");
257__errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX");
Josh Gao14adff12016-04-29 12:00:55 -0700258extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64) __INTRODUCED_IN(12);
Daniel Micaye7e1c872015-04-16 09:07:45 -0400259
Josh Gao14adff12016-04-29 12:00:55 -0700260extern ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400261__errordecl(__pwrite_dest_size_error, "pwrite called with size bigger than destination");
262__errordecl(__pwrite_count_toobig_error, "pwrite called with count > SSIZE_MAX");
263extern ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
264
Josh Gao14adff12016-04-29 12:00:55 -0700265extern ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400266__errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination");
267__errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX");
Josh Gao14adff12016-04-29 12:00:55 -0700268extern ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64)
269 __INTRODUCED_IN(12);
Daniel Micayafdd1542015-07-20 21:37:29 -0400270
Josh Gao14adff12016-04-29 12:00:55 -0700271extern ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700272__errordecl(__read_dest_size_error, "read called with size bigger than destination");
273__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700274extern ssize_t __read_real(int, void*, size_t) __RENAME(read);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700275
Josh Gao14adff12016-04-29 12:00:55 -0700276extern ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24);
Daniel Micayafdd1542015-07-20 21:37:29 -0400277__errordecl(__write_dest_size_error, "write called with size bigger than destination");
278__errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX");
279extern ssize_t __write_real(int, const void*, size_t) __RENAME(write);
280
Josh Gao14adff12016-04-29 12:00:55 -0700281extern ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay42281882015-04-17 11:26:36 -0400282__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
283__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
284extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
285
Josh Gao14adff12016-04-29 12:00:55 -0700286extern ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
Daniel Micay42281882015-04-17 11:26:36 -0400287__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
288__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
289extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat);
290
Josh Gao14adff12016-04-29 12:00:55 -0700291extern int getdomainname(char*, size_t) __INTRODUCED_IN(25);
292extern int setdomainname(const char*, size_t) __INTRODUCED_IN(25);
Greg Hackmanne2faf072016-03-03 08:37:53 -0800293
Dan Albert658727e2014-10-07 11:10:36 -0700294#if defined(__BIONIC_FORTIFY)
295
Daniel Micay9101b002015-05-20 15:31:26 -0400296__BIONIC_FORTIFY_INLINE
297char* getcwd(char* buf, size_t size) {
298 size_t bos = __bos(buf);
299
300#if defined(__clang__)
301 /*
302 * Work around LLVM's incorrect __builtin_object_size implementation here
303 * to avoid needing the workaround in the __getcwd_chk ABI forever.
304 *
305 * https://llvm.org/bugs/show_bug.cgi?id=23277
306 */
307 if (buf == NULL) {
308 bos = __BIONIC_FORTIFY_UNKNOWN_SIZE;
309 }
310#else
311 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
312 return __getcwd_real(buf, size);
313 }
314
315 if (__builtin_constant_p(size) && (size > bos)) {
316 __getcwd_dest_size_error();
317 }
318
319 if (__builtin_constant_p(size) && (size <= bos)) {
320 return __getcwd_real(buf, size);
321 }
322#endif
323
324 return __getcwd_chk(buf, size, bos);
325}
326
Daniel Micaye7e1c872015-04-16 09:07:45 -0400327#if defined(__USE_FILE_OFFSET64)
328#define __PREAD_PREFIX(x) __pread64_ ## x
329#else
330#define __PREAD_PREFIX(x) __pread_ ## x
331#endif
332
333__BIONIC_FORTIFY_INLINE
334ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
335 size_t bos = __bos0(buf);
336
337#if !defined(__clang__)
338 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
339 __PREAD_PREFIX(count_toobig_error)();
340 }
341
342 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
343 return __PREAD_PREFIX(real)(fd, buf, count, offset);
344 }
345
346 if (__builtin_constant_p(count) && (count > bos)) {
347 __PREAD_PREFIX(dest_size_error)();
348 }
349
350 if (__builtin_constant_p(count) && (count <= bos)) {
351 return __PREAD_PREFIX(real)(fd, buf, count, offset);
352 }
353#endif
354
355 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
356}
357
358__BIONIC_FORTIFY_INLINE
359ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) {
360 size_t bos = __bos0(buf);
361
362#if !defined(__clang__)
363 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
364 __pread64_count_toobig_error();
365 }
366
367 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
368 return __pread64_real(fd, buf, count, offset);
369 }
370
371 if (__builtin_constant_p(count) && (count > bos)) {
372 __pread64_dest_size_error();
373 }
374
375 if (__builtin_constant_p(count) && (count <= bos)) {
376 return __pread64_real(fd, buf, count, offset);
377 }
378#endif
379
380 return __pread64_chk(fd, buf, count, offset, bos);
381}
382
Daniel Micayafdd1542015-07-20 21:37:29 -0400383#if defined(__USE_FILE_OFFSET64)
384#define __PWRITE_PREFIX(x) __pwrite64_ ## x
385#else
386#define __PWRITE_PREFIX(x) __pwrite_ ## x
387#endif
388
389__BIONIC_FORTIFY_INLINE
390ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) {
391 size_t bos = __bos0(buf);
392
393#if !defined(__clang__)
394 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
395 __PWRITE_PREFIX(count_toobig_error)();
396 }
397
398 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
399 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
400 }
401
402 if (__builtin_constant_p(count) && (count > bos)) {
403 __PWRITE_PREFIX(dest_size_error)();
404 }
405
406 if (__builtin_constant_p(count) && (count <= bos)) {
407 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
408 }
409#endif
410
411 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
412}
413
414__BIONIC_FORTIFY_INLINE
415ssize_t pwrite64(int fd, const void* buf, size_t count, off64_t offset) {
416 size_t bos = __bos0(buf);
417
418#if !defined(__clang__)
419 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
420 __pwrite64_count_toobig_error();
421 }
422
423 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
424 return __pwrite64_real(fd, buf, count, offset);
425 }
426
427 if (__builtin_constant_p(count) && (count > bos)) {
428 __pwrite64_dest_size_error();
429 }
430
431 if (__builtin_constant_p(count) && (count <= bos)) {
432 return __pwrite64_real(fd, buf, count, offset);
433 }
434#endif
435
436 return __pwrite64_chk(fd, buf, count, offset, bos);
437}
438
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}
Daniel Micaye7e1c872015-04-16 09:07:45 -0400463
Daniel Micay42281882015-04-17 11:26:36 -0400464__BIONIC_FORTIFY_INLINE
Daniel Micayafdd1542015-07-20 21:37:29 -0400465ssize_t write(int fd, const void* buf, size_t count) {
466 size_t bos = __bos0(buf);
467
468#if !defined(__clang__)
469#if 0 /* work around a false positive due to a missed optimization */
470 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
471 __write_count_toobig_error();
472 }
473#endif
474
475 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
476 return __write_real(fd, buf, count);
477 }
478
479 if (__builtin_constant_p(count) && (count > bos)) {
480 __write_dest_size_error();
481 }
482
483 if (__builtin_constant_p(count) && (count <= bos)) {
484 return __write_real(fd, buf, count);
485 }
486#endif
487
488 return __write_chk(fd, buf, count, bos);
489}
490
491__BIONIC_FORTIFY_INLINE
Daniel Micay42281882015-04-17 11:26:36 -0400492ssize_t readlink(const char* path, char* buf, size_t size) {
493 size_t bos = __bos(buf);
494
495#if !defined(__clang__)
496 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
497 __readlink_size_toobig_error();
498 }
499
500 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
501 return __readlink_real(path, buf, size);
502 }
503
504 if (__builtin_constant_p(size) && (size > bos)) {
505 __readlink_dest_size_error();
506 }
507
508 if (__builtin_constant_p(size) && (size <= bos)) {
509 return __readlink_real(path, buf, size);
510 }
511#endif
512
513 return __readlink_chk(path, buf, size, bos);
514}
515
516__BIONIC_FORTIFY_INLINE
517ssize_t readlinkat(int dirfd, const char* path, char* buf, size_t size) {
518 size_t bos = __bos(buf);
519
520#if !defined(__clang__)
521 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
522 __readlinkat_size_toobig_error();
523 }
524
525 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
526 return __readlinkat_real(dirfd, path, buf, size);
527 }
528
529 if (__builtin_constant_p(size) && (size > bos)) {
530 __readlinkat_dest_size_error();
531 }
532
533 if (__builtin_constant_p(size) && (size <= bos)) {
534 return __readlinkat_real(dirfd, path, buf, size);
535 }
536#endif
537
538 return __readlinkat_chk(dirfd, path, buf, size, bos);
539}
540
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700541#endif /* defined(__BIONIC_FORTIFY) */
542
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800543__END_DECLS
544
545#endif /* _UNISTD_H_ */