blob: ea6c8a1b0bcb643428496fbcd6744050f38c4d50 [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
179extern int truncate(const char* __path, off_t __length) __RENAME(truncate64);
180extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset)
181 __RENAME(pread64);
182extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
183 off_t __offset) __RENAME(pwrite64);
184extern int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64);
185#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);
194extern ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(21);
195extern ssize_t pwrite64(int __fd, const void* __buf, size_t __count,
196 off64_t __offset) __INTRODUCED_IN(21);
197extern int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(21);
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);
205int sethostname(const char* __name, size_t __len);
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. */
Daniel Micay9101b002015-05-20 15:31:26 -0400246extern char* __getcwd_chk(char*, size_t, size_t);
247__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
248extern char* __getcwd_real(char*, size_t) __RENAME(getcwd);
249
Daniel Micaye7e1c872015-04-16 09:07:45 -0400250extern ssize_t __pread_chk(int, void*, size_t, off_t, size_t);
251__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
255extern ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t);
256__errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination");
257__errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX");
258extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64);
259
Daniel Micayafdd1542015-07-20 21:37:29 -0400260extern ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t);
261__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
265extern ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t);
266__errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination");
267__errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX");
268extern ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64);
269
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700270extern ssize_t __read_chk(int, void*, size_t, size_t);
271__errordecl(__read_dest_size_error, "read called with size bigger than destination");
272__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700273extern ssize_t __read_real(int, void*, size_t) __RENAME(read);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700274
Daniel Micayafdd1542015-07-20 21:37:29 -0400275extern ssize_t __write_chk(int, const void*, size_t, size_t);
276__errordecl(__write_dest_size_error, "write called with size bigger than destination");
277__errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX");
278extern ssize_t __write_real(int, const void*, size_t) __RENAME(write);
279
Daniel Micay42281882015-04-17 11:26:36 -0400280extern ssize_t __readlink_chk(const char*, char*, size_t, size_t);
281__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
282__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
283extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
284
285extern ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t);
286__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
287__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
288extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat);
289
Greg Hackmanne2faf072016-03-03 08:37:53 -0800290extern int getdomainname(char*, size_t);
291extern int setdomainname(const char*, size_t);
292
Dan Albert658727e2014-10-07 11:10:36 -0700293#if defined(__BIONIC_FORTIFY)
294
Daniel Micay9101b002015-05-20 15:31:26 -0400295__BIONIC_FORTIFY_INLINE
296char* getcwd(char* buf, size_t size) {
297 size_t bos = __bos(buf);
298
299#if defined(__clang__)
300 /*
301 * Work around LLVM's incorrect __builtin_object_size implementation here
302 * to avoid needing the workaround in the __getcwd_chk ABI forever.
303 *
304 * https://llvm.org/bugs/show_bug.cgi?id=23277
305 */
306 if (buf == NULL) {
307 bos = __BIONIC_FORTIFY_UNKNOWN_SIZE;
308 }
309#else
310 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
311 return __getcwd_real(buf, size);
312 }
313
314 if (__builtin_constant_p(size) && (size > bos)) {
315 __getcwd_dest_size_error();
316 }
317
318 if (__builtin_constant_p(size) && (size <= bos)) {
319 return __getcwd_real(buf, size);
320 }
321#endif
322
323 return __getcwd_chk(buf, size, bos);
324}
325
Daniel Micaye7e1c872015-04-16 09:07:45 -0400326#if defined(__USE_FILE_OFFSET64)
327#define __PREAD_PREFIX(x) __pread64_ ## x
328#else
329#define __PREAD_PREFIX(x) __pread_ ## x
330#endif
331
332__BIONIC_FORTIFY_INLINE
333ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
334 size_t bos = __bos0(buf);
335
336#if !defined(__clang__)
337 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
338 __PREAD_PREFIX(count_toobig_error)();
339 }
340
341 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
342 return __PREAD_PREFIX(real)(fd, buf, count, offset);
343 }
344
345 if (__builtin_constant_p(count) && (count > bos)) {
346 __PREAD_PREFIX(dest_size_error)();
347 }
348
349 if (__builtin_constant_p(count) && (count <= bos)) {
350 return __PREAD_PREFIX(real)(fd, buf, count, offset);
351 }
352#endif
353
354 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
355}
356
357__BIONIC_FORTIFY_INLINE
358ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) {
359 size_t bos = __bos0(buf);
360
361#if !defined(__clang__)
362 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
363 __pread64_count_toobig_error();
364 }
365
366 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
367 return __pread64_real(fd, buf, count, offset);
368 }
369
370 if (__builtin_constant_p(count) && (count > bos)) {
371 __pread64_dest_size_error();
372 }
373
374 if (__builtin_constant_p(count) && (count <= bos)) {
375 return __pread64_real(fd, buf, count, offset);
376 }
377#endif
378
379 return __pread64_chk(fd, buf, count, offset, bos);
380}
381
Daniel Micayafdd1542015-07-20 21:37:29 -0400382#if defined(__USE_FILE_OFFSET64)
383#define __PWRITE_PREFIX(x) __pwrite64_ ## x
384#else
385#define __PWRITE_PREFIX(x) __pwrite_ ## x
386#endif
387
388__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}
437
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700438__BIONIC_FORTIFY_INLINE
439ssize_t read(int fd, void* buf, size_t count) {
440 size_t bos = __bos0(buf);
441
442#if !defined(__clang__)
443 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
444 __read_count_toobig_error();
445 }
446
447 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
448 return __read_real(fd, buf, count);
449 }
450
451 if (__builtin_constant_p(count) && (count > bos)) {
452 __read_dest_size_error();
453 }
454
455 if (__builtin_constant_p(count) && (count <= bos)) {
456 return __read_real(fd, buf, count);
457 }
458#endif
459
460 return __read_chk(fd, buf, count, bos);
461}
Daniel Micaye7e1c872015-04-16 09:07:45 -0400462
Daniel Micay42281882015-04-17 11:26:36 -0400463__BIONIC_FORTIFY_INLINE
Daniel Micayafdd1542015-07-20 21:37:29 -0400464ssize_t write(int fd, const void* buf, size_t count) {
465 size_t bos = __bos0(buf);
466
467#if !defined(__clang__)
468#if 0 /* work around a false positive due to a missed optimization */
469 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
470 __write_count_toobig_error();
471 }
472#endif
473
474 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
475 return __write_real(fd, buf, count);
476 }
477
478 if (__builtin_constant_p(count) && (count > bos)) {
479 __write_dest_size_error();
480 }
481
482 if (__builtin_constant_p(count) && (count <= bos)) {
483 return __write_real(fd, buf, count);
484 }
485#endif
486
487 return __write_chk(fd, buf, count, bos);
488}
489
490__BIONIC_FORTIFY_INLINE
Daniel Micay42281882015-04-17 11:26:36 -0400491ssize_t readlink(const char* path, char* buf, size_t size) {
492 size_t bos = __bos(buf);
493
494#if !defined(__clang__)
495 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
496 __readlink_size_toobig_error();
497 }
498
499 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
500 return __readlink_real(path, buf, size);
501 }
502
503 if (__builtin_constant_p(size) && (size > bos)) {
504 __readlink_dest_size_error();
505 }
506
507 if (__builtin_constant_p(size) && (size <= bos)) {
508 return __readlink_real(path, buf, size);
509 }
510#endif
511
512 return __readlink_chk(path, buf, size, bos);
513}
514
515__BIONIC_FORTIFY_INLINE
516ssize_t readlinkat(int dirfd, const char* path, char* buf, size_t size) {
517 size_t bos = __bos(buf);
518
519#if !defined(__clang__)
520 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
521 __readlinkat_size_toobig_error();
522 }
523
524 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
525 return __readlinkat_real(dirfd, path, buf, size);
526 }
527
528 if (__builtin_constant_p(size) && (size > bos)) {
529 __readlinkat_dest_size_error();
530 }
531
532 if (__builtin_constant_p(size) && (size <= bos)) {
533 return __readlinkat_real(dirfd, path, buf, size);
534 }
535#endif
536
537 return __readlinkat_chk(dirfd, path, buf, size, bos);
538}
539
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700540#endif /* defined(__BIONIC_FORTIFY) */
541
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800542__END_DECLS
543
544#endif /* _UNISTD_H_ */