blob: 2d62c1ffb39064e04a480c6f4bc84cef9ff0f7f9 [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>
36#include <sys/sysconf.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037
Elliott Hughes5704c422016-01-25 18:06:24 -080038#include <bits/lockf.h>
39#include <bits/posix_limits.h>
Elliott Hughes1ed337d2015-02-02 14:02:09 -080040
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041__BEGIN_DECLS
42
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043#define STDIN_FILENO 0
44#define STDOUT_FILENO 1
45#define STDERR_FILENO 2
46
Elliott Hughes1ed337d2015-02-02 14:02:09 -080047#define F_OK 0
48#define X_OK 1
49#define W_OK 2
50#define R_OK 4
51
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052#define SEEK_SET 0
53#define SEEK_CUR 1
54#define SEEK_END 2
55
Elliott Hughesa186b2e2014-09-22 14:49:07 -070056#define _PC_FILESIZEBITS 0
57#define _PC_LINK_MAX 1
58#define _PC_MAX_CANON 2
59#define _PC_MAX_INPUT 3
60#define _PC_NAME_MAX 4
61#define _PC_PATH_MAX 5
62#define _PC_PIPE_BUF 6
63#define _PC_2_SYMLINKS 7
64#define _PC_ALLOC_SIZE_MIN 8
65#define _PC_REC_INCR_XFER_SIZE 9
66#define _PC_REC_MAX_XFER_SIZE 10
67#define _PC_REC_MIN_XFER_SIZE 11
68#define _PC_REC_XFER_ALIGN 12
69#define _PC_SYMLINK_MAX 13
70#define _PC_CHOWN_RESTRICTED 14
71#define _PC_NO_TRUNC 15
72#define _PC_VDISABLE 16
73#define _PC_ASYNC_IO 17
74#define _PC_PRIO_IO 18
75#define _PC_SYNC_IO 19
76
Elliott Hughes58d9e282014-04-22 17:41:00 -070077extern char** environ;
78
Dan Albertaf4713e2015-09-04 12:59:53 -070079extern __noreturn void _exit(int __status);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
81extern pid_t fork(void);
82extern pid_t vfork(void);
83extern pid_t getpid(void);
Elliott Hughesb27a8402014-06-10 20:47:49 -070084extern pid_t gettid(void) __pure2;
Dan Albertaf4713e2015-09-04 12:59:53 -070085extern pid_t getpgid(pid_t __pid);
86extern int setpgid(pid_t __pid, pid_t __pgid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087extern pid_t getppid(void);
88extern pid_t getpgrp(void);
89extern int setpgrp(void);
Dan Albertaf4713e2015-09-04 12:59:53 -070090extern pid_t getsid(pid_t __pid) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091extern pid_t setsid(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Dan Albertaf4713e2015-09-04 12:59:53 -070093extern int execv(const char* __path, char* const* __argv);
94extern int execvp(const char* __file, char* const* __argv);
95extern int execvpe(const char* __file, char* const* __argv, char* const* __envp)
96 __INTRODUCED_IN(21);
97extern int execve(const char* __file, char* const* __argv, char* const* __envp);
98extern int execl(const char* __path, const char* __arg0, ...);
99extern int execlp(const char* __file, const char* __arg0, ...);
100extern int execle(const char* __path, const char* __arg0, ...);
David 'Digit' Turnerb083bb52011-05-26 02:46:41 +0200101
Dan Albertaf4713e2015-09-04 12:59:53 -0700102extern int nice(int __incr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
Dan Albertaf4713e2015-09-04 12:59:53 -0700104extern int setuid(uid_t __uid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105extern uid_t getuid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700106extern int seteuid(uid_t __uid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107extern uid_t geteuid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700108extern int setgid(gid_t __gid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109extern gid_t getgid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700110extern int setegid(gid_t __gid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111extern gid_t getegid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700112extern int getgroups(int __size, gid_t* __list);
113extern int setgroups(size_t __size, const gid_t* __list);
114extern int setreuid(uid_t __ruid, uid_t __euid);
115extern int setregid(gid_t __rgid, gid_t __egid);
116extern int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
117extern int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
118extern int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
119extern int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120extern char* getlogin(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
Dan Albertaf4713e2015-09-04 12:59:53 -0700122extern long fpathconf(int __fd, int __name);
123extern long pathconf(const char* __path, int __name);
Elliott Hughesa186b2e2014-09-22 14:49:07 -0700124
Dan Albertaf4713e2015-09-04 12:59:53 -0700125extern int access(const char* __path, int __mode);
126extern int faccessat(int __dirfd, const char* __path, int __mode, int __flags)
127 __INTRODUCED_IN(21);
128extern int link(const char* __oldpath, const char* __newpath);
129extern int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
130 const char* __newpath, int __flags) __INTRODUCED_IN(21);
131extern int unlink(const char* __path);
132extern int unlinkat(int __dirfd, const char* __path, int __flags);
133extern int chdir(const char* __path);
134extern int fchdir(int __fd);
135extern int rmdir(const char* __path);
136extern int pipe(int* __pipefd);
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700137#if defined(__USE_GNU)
Dan Albertaf4713e2015-09-04 12:59:53 -0700138extern int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
David 'Digit' Turner275cd482010-09-27 17:33:08 +0200139#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700140extern int chroot(const char* __path);
141extern int symlink(const char* __oldpath, const char* __newpath);
142extern int symlinkat(const char* __oldpath, int __newdirfd,
143 const char* __newpath) __INTRODUCED_IN(21);
144extern ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
145extern ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
146 size_t __bufsiz) __INTRODUCED_IN(21);
147extern int chown(const char* __path, uid_t __owner, gid_t __group);
148extern int fchown(int __fd, uid_t __owner, gid_t __group);
149extern int fchownat(int __dirfd, const char* __path, uid_t __owner,
150 gid_t __group, int __flags);
151extern int lchown(const char* __path, uid_t __owner, gid_t __group);
152extern char* getcwd(char* __buf, size_t __size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153
154extern int sync(void);
155
Dan Albertaf4713e2015-09-04 12:59:53 -0700156extern int close(int __fd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
Dan Albertaf4713e2015-09-04 12:59:53 -0700158extern ssize_t read(int __fd, void* __buf, size_t __count);
159extern ssize_t write(int __fd, const void* __buf, size_t __count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
Dan Albertaf4713e2015-09-04 12:59:53 -0700161extern int dup(int __oldfd);
162extern int dup2(int __oldfd, int __newfd);
163extern int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
164extern int fcntl(int __fd, int __cmd, ...);
165extern int ioctl(int __fd, int __request, ...);
166extern int fsync(int __fd);
167extern int fdatasync(int __fd) __INTRODUCED_IN(9);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800168
169#if defined(__USE_FILE_OFFSET64)
Dan Albertaf4713e2015-09-04 12:59:53 -0700170extern off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800171#else
Dan Albertaf4713e2015-09-04 12:59:53 -0700172extern off_t lseek(int __fd, off_t __offset, int __whence);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800173#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700174
175extern off64_t lseek64(int __fd, off64_t __offset, int __whence);
176
177#if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ >= 21
178extern int truncate(const char* __path, off_t __length) __RENAME(truncate64);
179extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset)
180 __RENAME(pread64);
181extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
182 off_t __offset) __RENAME(pwrite64);
183extern int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64);
184#else
185extern int truncate(const char* __path, off_t __length);
186extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
187extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
188 off_t __offset);
189extern int ftruncate(int __fd, off_t __length);
190#endif
191
192extern int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
193extern ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(21);
194extern ssize_t pwrite64(int __fd, const void* __buf, size_t __count,
195 off64_t __offset) __INTRODUCED_IN(21);
196extern int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
198extern int pause(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700199extern unsigned int alarm(unsigned int __seconds);
200extern unsigned int sleep(unsigned int __seconds);
201extern int usleep(useconds_t __usec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202
Dan Albertaf4713e2015-09-04 12:59:53 -0700203int gethostname(char* __name, size_t __len);
204int sethostname(const char* __name, size_t __len);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205
Dan Albertaf4713e2015-09-04 12:59:53 -0700206extern void* __brk(void* __addr);
207extern int brk(void* __addr);
208extern void* sbrk(ptrdiff_t __increment);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209
Dan Albertaf4713e2015-09-04 12:59:53 -0700210extern int getopt(int __argc, char* const* __argv, const char* __argstring);
211extern char* optarg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212extern int optind, opterr, optopt;
213
Dan Albertaf4713e2015-09-04 12:59:53 -0700214extern int isatty(int __fd);
215extern char* ttyname(int __fd);
216extern int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
Dan Albertaf4713e2015-09-04 12:59:53 -0700218extern int acct(const char* __filepath);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219
Dan Albertaf4713e2015-09-04 12:59:53 -0700220long sysconf(int __name);
221
222#if __ANDROID_API__ >= 21
Bernhard Rosenkraenzer9ae59c02013-09-18 23:29:08 +0200223int getpagesize(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700224#else
225__inline__ int getpagesize(void) {
226 return sysconf(_SC_PAGESIZE);
227}
228#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
Dan Albertaf4713e2015-09-04 12:59:53 -0700230long syscall(long __number, ...);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231
Dan Albertaf4713e2015-09-04 12:59:53 -0700232extern int daemon(int __nochdir, int __noclose);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800234#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Dan Albertaf4713e2015-09-04 12:59:53 -0700235extern int cacheflush(long __addr, long __nbytes, long __cache);
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800236 /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
237#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238
Dan Albertaf4713e2015-09-04 12:59:53 -0700239extern pid_t tcgetpgrp(int __fd);
240extern int tcsetpgrp(int __fd, pid_t __pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241
Elliott Hughescf399f72009-10-05 13:19:05 -0700242/* Used to retry syscalls that can return EINTR. */
243#define TEMP_FAILURE_RETRY(exp) ({ \
Dan Alberta7821b72014-05-21 20:33:28 -0700244 __typeof__(exp) _rc; \
Elliott Hughescf399f72009-10-05 13:19:05 -0700245 do { \
246 _rc = (exp); \
247 } while (_rc == -1 && errno == EINTR); \
248 _rc; })
249
Dan Albertaf4713e2015-09-04 12:59:53 -0700250/* TODO(unified-headers): Factor out all the FORTIFY features. */
Daniel Micay9101b002015-05-20 15:31:26 -0400251extern char* __getcwd_chk(char*, size_t, size_t);
252__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
253extern char* __getcwd_real(char*, size_t) __RENAME(getcwd);
254
Daniel Micaye7e1c872015-04-16 09:07:45 -0400255extern ssize_t __pread_chk(int, void*, size_t, off_t, size_t);
256__errordecl(__pread_dest_size_error, "pread called with size bigger than destination");
257__errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX");
258extern ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
259
260extern ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t);
261__errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination");
262__errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX");
263extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64);
264
Daniel Micayafdd1542015-07-20 21:37:29 -0400265extern ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t);
266__errordecl(__pwrite_dest_size_error, "pwrite called with size bigger than destination");
267__errordecl(__pwrite_count_toobig_error, "pwrite called with count > SSIZE_MAX");
268extern ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
269
270extern ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t);
271__errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination");
272__errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX");
273extern ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64);
274
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700275extern ssize_t __read_chk(int, void*, size_t, size_t);
276__errordecl(__read_dest_size_error, "read called with size bigger than destination");
277__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700278extern ssize_t __read_real(int, void*, size_t) __RENAME(read);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700279
Daniel Micayafdd1542015-07-20 21:37:29 -0400280extern ssize_t __write_chk(int, const void*, size_t, size_t);
281__errordecl(__write_dest_size_error, "write called with size bigger than destination");
282__errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX");
283extern ssize_t __write_real(int, const void*, size_t) __RENAME(write);
284
Daniel Micay42281882015-04-17 11:26:36 -0400285extern ssize_t __readlink_chk(const char*, char*, size_t, size_t);
286__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
287__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
288extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
289
290extern ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t);
291__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
292__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
293extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat);
294
Greg Hackmanne2faf072016-03-03 08:37:53 -0800295extern int getdomainname(char*, size_t);
296extern int setdomainname(const char*, size_t);
297
Dan Albert658727e2014-10-07 11:10:36 -0700298#if defined(__BIONIC_FORTIFY)
299
Daniel Micay9101b002015-05-20 15:31:26 -0400300__BIONIC_FORTIFY_INLINE
301char* getcwd(char* buf, size_t size) {
302 size_t bos = __bos(buf);
303
304#if defined(__clang__)
305 /*
306 * Work around LLVM's incorrect __builtin_object_size implementation here
307 * to avoid needing the workaround in the __getcwd_chk ABI forever.
308 *
309 * https://llvm.org/bugs/show_bug.cgi?id=23277
310 */
311 if (buf == NULL) {
312 bos = __BIONIC_FORTIFY_UNKNOWN_SIZE;
313 }
314#else
315 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
316 return __getcwd_real(buf, size);
317 }
318
319 if (__builtin_constant_p(size) && (size > bos)) {
320 __getcwd_dest_size_error();
321 }
322
323 if (__builtin_constant_p(size) && (size <= bos)) {
324 return __getcwd_real(buf, size);
325 }
326#endif
327
328 return __getcwd_chk(buf, size, bos);
329}
330
Daniel Micaye7e1c872015-04-16 09:07:45 -0400331#if defined(__USE_FILE_OFFSET64)
332#define __PREAD_PREFIX(x) __pread64_ ## x
333#else
334#define __PREAD_PREFIX(x) __pread_ ## x
335#endif
336
337__BIONIC_FORTIFY_INLINE
338ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
339 size_t bos = __bos0(buf);
340
341#if !defined(__clang__)
342 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
343 __PREAD_PREFIX(count_toobig_error)();
344 }
345
346 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
347 return __PREAD_PREFIX(real)(fd, buf, count, offset);
348 }
349
350 if (__builtin_constant_p(count) && (count > bos)) {
351 __PREAD_PREFIX(dest_size_error)();
352 }
353
354 if (__builtin_constant_p(count) && (count <= bos)) {
355 return __PREAD_PREFIX(real)(fd, buf, count, offset);
356 }
357#endif
358
359 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
360}
361
362__BIONIC_FORTIFY_INLINE
363ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) {
364 size_t bos = __bos0(buf);
365
366#if !defined(__clang__)
367 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
368 __pread64_count_toobig_error();
369 }
370
371 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
372 return __pread64_real(fd, buf, count, offset);
373 }
374
375 if (__builtin_constant_p(count) && (count > bos)) {
376 __pread64_dest_size_error();
377 }
378
379 if (__builtin_constant_p(count) && (count <= bos)) {
380 return __pread64_real(fd, buf, count, offset);
381 }
382#endif
383
384 return __pread64_chk(fd, buf, count, offset, bos);
385}
386
Daniel Micayafdd1542015-07-20 21:37:29 -0400387#if defined(__USE_FILE_OFFSET64)
388#define __PWRITE_PREFIX(x) __pwrite64_ ## x
389#else
390#define __PWRITE_PREFIX(x) __pwrite_ ## x
391#endif
392
393__BIONIC_FORTIFY_INLINE
394ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) {
395 size_t bos = __bos0(buf);
396
397#if !defined(__clang__)
398 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
399 __PWRITE_PREFIX(count_toobig_error)();
400 }
401
402 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
403 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
404 }
405
406 if (__builtin_constant_p(count) && (count > bos)) {
407 __PWRITE_PREFIX(dest_size_error)();
408 }
409
410 if (__builtin_constant_p(count) && (count <= bos)) {
411 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
412 }
413#endif
414
415 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
416}
417
418__BIONIC_FORTIFY_INLINE
419ssize_t pwrite64(int fd, const void* buf, size_t count, off64_t offset) {
420 size_t bos = __bos0(buf);
421
422#if !defined(__clang__)
423 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
424 __pwrite64_count_toobig_error();
425 }
426
427 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
428 return __pwrite64_real(fd, buf, count, offset);
429 }
430
431 if (__builtin_constant_p(count) && (count > bos)) {
432 __pwrite64_dest_size_error();
433 }
434
435 if (__builtin_constant_p(count) && (count <= bos)) {
436 return __pwrite64_real(fd, buf, count, offset);
437 }
438#endif
439
440 return __pwrite64_chk(fd, buf, count, offset, bos);
441}
442
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700443__BIONIC_FORTIFY_INLINE
444ssize_t read(int fd, void* buf, size_t count) {
445 size_t bos = __bos0(buf);
446
447#if !defined(__clang__)
448 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
449 __read_count_toobig_error();
450 }
451
452 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
453 return __read_real(fd, buf, count);
454 }
455
456 if (__builtin_constant_p(count) && (count > bos)) {
457 __read_dest_size_error();
458 }
459
460 if (__builtin_constant_p(count) && (count <= bos)) {
461 return __read_real(fd, buf, count);
462 }
463#endif
464
465 return __read_chk(fd, buf, count, bos);
466}
Daniel Micaye7e1c872015-04-16 09:07:45 -0400467
Daniel Micay42281882015-04-17 11:26:36 -0400468__BIONIC_FORTIFY_INLINE
Daniel Micayafdd1542015-07-20 21:37:29 -0400469ssize_t write(int fd, const void* buf, size_t count) {
470 size_t bos = __bos0(buf);
471
472#if !defined(__clang__)
473#if 0 /* work around a false positive due to a missed optimization */
474 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
475 __write_count_toobig_error();
476 }
477#endif
478
479 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
480 return __write_real(fd, buf, count);
481 }
482
483 if (__builtin_constant_p(count) && (count > bos)) {
484 __write_dest_size_error();
485 }
486
487 if (__builtin_constant_p(count) && (count <= bos)) {
488 return __write_real(fd, buf, count);
489 }
490#endif
491
492 return __write_chk(fd, buf, count, bos);
493}
494
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_ */