blob: 23b47a3720020f94b7b3814c2b159e598b2bf783 [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 Hughes1ed337d2015-02-02 14:02:09 -080038#include <machine/posix_limits.h>
39
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040__BEGIN_DECLS
41
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042#define STDIN_FILENO 0
43#define STDOUT_FILENO 1
44#define STDERR_FILENO 2
45
Elliott Hughes1ed337d2015-02-02 14:02:09 -080046#define F_OK 0
47#define X_OK 1
48#define W_OK 2
49#define R_OK 4
50
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051#define SEEK_SET 0
52#define SEEK_CUR 1
53#define SEEK_END 2
54
Elliott Hughesa186b2e2014-09-22 14:49:07 -070055#define _PC_FILESIZEBITS 0
56#define _PC_LINK_MAX 1
57#define _PC_MAX_CANON 2
58#define _PC_MAX_INPUT 3
59#define _PC_NAME_MAX 4
60#define _PC_PATH_MAX 5
61#define _PC_PIPE_BUF 6
62#define _PC_2_SYMLINKS 7
63#define _PC_ALLOC_SIZE_MIN 8
64#define _PC_REC_INCR_XFER_SIZE 9
65#define _PC_REC_MAX_XFER_SIZE 10
66#define _PC_REC_MIN_XFER_SIZE 11
67#define _PC_REC_XFER_ALIGN 12
68#define _PC_SYMLINK_MAX 13
69#define _PC_CHOWN_RESTRICTED 14
70#define _PC_NO_TRUNC 15
71#define _PC_VDISABLE 16
72#define _PC_ASYNC_IO 17
73#define _PC_PRIO_IO 18
74#define _PC_SYNC_IO 19
75
Elliott Hughes58d9e282014-04-22 17:41:00 -070076extern char** environ;
77
Dan Albertaf4713e2015-09-04 12:59:53 -070078extern __noreturn void _exit(int __status);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
80extern pid_t fork(void);
81extern pid_t vfork(void);
82extern pid_t getpid(void);
Elliott Hughesb27a8402014-06-10 20:47:49 -070083extern pid_t gettid(void) __pure2;
Dan Albertaf4713e2015-09-04 12:59:53 -070084extern pid_t getpgid(pid_t __pid);
85extern int setpgid(pid_t __pid, pid_t __pgid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086extern pid_t getppid(void);
87extern pid_t getpgrp(void);
88extern int setpgrp(void);
Dan Albertaf4713e2015-09-04 12:59:53 -070089extern pid_t getsid(pid_t __pid) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090extern pid_t setsid(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Dan Albertaf4713e2015-09-04 12:59:53 -070092extern int execv(const char* __path, char* const* __argv);
93extern int execvp(const char* __file, char* const* __argv);
94extern int execvpe(const char* __file, char* const* __argv, char* const* __envp)
95 __INTRODUCED_IN(21);
96extern int execve(const char* __file, char* const* __argv, char* const* __envp);
97extern int execl(const char* __path, const char* __arg0, ...);
98extern int execlp(const char* __file, const char* __arg0, ...);
99extern int execle(const char* __path, const char* __arg0, ...);
David 'Digit' Turnerb083bb52011-05-26 02:46:41 +0200100
Dan Albertaf4713e2015-09-04 12:59:53 -0700101extern int nice(int __incr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102
Dan Albertaf4713e2015-09-04 12:59:53 -0700103extern int setuid(uid_t __uid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104extern uid_t getuid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700105extern int seteuid(uid_t __uid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106extern uid_t geteuid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700107extern int setgid(gid_t __gid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108extern gid_t getgid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700109extern int setegid(gid_t __gid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110extern gid_t getegid(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700111extern int getgroups(int __size, gid_t* __list);
112extern int setgroups(size_t __size, const gid_t* __list);
113extern int setreuid(uid_t __ruid, uid_t __euid);
114extern int setregid(gid_t __rgid, gid_t __egid);
115extern int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
116extern int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
117extern int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
118extern int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119extern char* getlogin(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
Dan Albertaf4713e2015-09-04 12:59:53 -0700121extern long fpathconf(int __fd, int __name);
122extern long pathconf(const char* __path, int __name);
Elliott Hughesa186b2e2014-09-22 14:49:07 -0700123
Dan Albertaf4713e2015-09-04 12:59:53 -0700124extern int access(const char* __path, int __mode);
125extern int faccessat(int __dirfd, const char* __path, int __mode, int __flags)
126 __INTRODUCED_IN(21);
127extern int link(const char* __oldpath, const char* __newpath);
128extern int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
129 const char* __newpath, int __flags) __INTRODUCED_IN(21);
130extern int unlink(const char* __path);
131extern int unlinkat(int __dirfd, const char* __path, int __flags);
132extern int chdir(const char* __path);
133extern int fchdir(int __fd);
134extern int rmdir(const char* __path);
135extern int pipe(int* __pipefd);
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700136#if defined(__USE_GNU)
Dan Albertaf4713e2015-09-04 12:59:53 -0700137extern int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
David 'Digit' Turner275cd482010-09-27 17:33:08 +0200138#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700139extern int chroot(const char* __path);
140extern int symlink(const char* __oldpath, const char* __newpath);
141extern int symlinkat(const char* __oldpath, int __newdirfd,
142 const char* __newpath) __INTRODUCED_IN(21);
143extern ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
144extern ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
145 size_t __bufsiz) __INTRODUCED_IN(21);
146extern int chown(const char* __path, uid_t __owner, gid_t __group);
147extern int fchown(int __fd, uid_t __owner, gid_t __group);
148extern int fchownat(int __dirfd, const char* __path, uid_t __owner,
149 gid_t __group, int __flags);
150extern int lchown(const char* __path, uid_t __owner, gid_t __group);
151extern char* getcwd(char* __buf, size_t __size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152
153extern int sync(void);
154
Dan Albertaf4713e2015-09-04 12:59:53 -0700155extern int close(int __fd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156
Dan Albertaf4713e2015-09-04 12:59:53 -0700157extern ssize_t read(int __fd, void* __buf, size_t __count);
158extern ssize_t write(int __fd, const void* __buf, size_t __count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Dan Albertaf4713e2015-09-04 12:59:53 -0700160extern int dup(int __oldfd);
161extern int dup2(int __oldfd, int __newfd);
162extern int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
163extern int fcntl(int __fd, int __cmd, ...);
164extern int ioctl(int __fd, int __request, ...);
165extern int fsync(int __fd);
166extern int fdatasync(int __fd) __INTRODUCED_IN(9);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800167
168#if defined(__USE_FILE_OFFSET64)
Dan Albertaf4713e2015-09-04 12:59:53 -0700169extern off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800170#else
Dan Albertaf4713e2015-09-04 12:59:53 -0700171extern off_t lseek(int __fd, off_t __offset, int __whence);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800172#endif
Dan Albertaf4713e2015-09-04 12:59:53 -0700173
174extern off64_t lseek64(int __fd, off64_t __offset, int __whence);
175
176#if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ >= 21
177extern int truncate(const char* __path, off_t __length) __RENAME(truncate64);
178extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset)
179 __RENAME(pread64);
180extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
181 off_t __offset) __RENAME(pwrite64);
182extern int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64);
183#else
184extern int truncate(const char* __path, off_t __length);
185extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
186extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
187 off_t __offset);
188extern int ftruncate(int __fd, off_t __length);
189#endif
190
191extern int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
192extern ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(21);
193extern ssize_t pwrite64(int __fd, const void* __buf, size_t __count,
194 off64_t __offset) __INTRODUCED_IN(21);
195extern int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(21);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
197extern int pause(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700198extern unsigned int alarm(unsigned int __seconds);
199extern unsigned int sleep(unsigned int __seconds);
200extern int usleep(useconds_t __usec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
Dan Albertaf4713e2015-09-04 12:59:53 -0700202int gethostname(char* __name, size_t __len);
203int sethostname(const char* __name, size_t __len);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204
Dan Albertaf4713e2015-09-04 12:59:53 -0700205extern void* __brk(void* __addr);
206extern int brk(void* __addr);
207extern void* sbrk(ptrdiff_t __increment);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
Dan Albertaf4713e2015-09-04 12:59:53 -0700209extern int getopt(int __argc, char* const* __argv, const char* __argstring);
210extern char* optarg;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211extern int optind, opterr, optopt;
212
Dan Albertaf4713e2015-09-04 12:59:53 -0700213extern int isatty(int __fd);
214extern char* ttyname(int __fd);
215extern int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216
Dan Albertaf4713e2015-09-04 12:59:53 -0700217extern int acct(const char* __filepath);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218
Dan Albertaf4713e2015-09-04 12:59:53 -0700219long sysconf(int __name);
220
221#if __ANDROID_API__ >= 21
Bernhard Rosenkraenzer9ae59c02013-09-18 23:29:08 +0200222int getpagesize(void);
Dan Albertaf4713e2015-09-04 12:59:53 -0700223#else
224__inline__ int getpagesize(void) {
225 return sysconf(_SC_PAGESIZE);
226}
227#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
Dan Albertaf4713e2015-09-04 12:59:53 -0700229long syscall(long __number, ...);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230
Dan Albertaf4713e2015-09-04 12:59:53 -0700231extern int daemon(int __nochdir, int __noclose);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800233#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
Dan Albertaf4713e2015-09-04 12:59:53 -0700234extern int cacheflush(long __addr, long __nbytes, long __cache);
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800235 /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
236#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237
Dan Albertaf4713e2015-09-04 12:59:53 -0700238extern pid_t tcgetpgrp(int __fd);
239extern int tcsetpgrp(int __fd, pid_t __pid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
Elliott Hughescf399f72009-10-05 13:19:05 -0700241/* Used to retry syscalls that can return EINTR. */
242#define TEMP_FAILURE_RETRY(exp) ({ \
Dan Alberta7821b72014-05-21 20:33:28 -0700243 __typeof__(exp) _rc; \
Elliott Hughescf399f72009-10-05 13:19:05 -0700244 do { \
245 _rc = (exp); \
246 } while (_rc == -1 && errno == EINTR); \
247 _rc; })
248
Dan Albertaf4713e2015-09-04 12:59:53 -0700249/* TODO(unified-headers): Factor out all the FORTIFY features. */
Daniel Micay9101b002015-05-20 15:31:26 -0400250extern char* __getcwd_chk(char*, size_t, size_t);
251__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
252extern char* __getcwd_real(char*, size_t) __RENAME(getcwd);
253
Daniel Micaye7e1c872015-04-16 09:07:45 -0400254extern ssize_t __pread_chk(int, void*, size_t, off_t, size_t);
255__errordecl(__pread_dest_size_error, "pread called with size bigger than destination");
256__errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX");
257extern ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
258
259extern ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t);
260__errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination");
261__errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX");
262extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64);
263
Daniel Micayafdd1542015-07-20 21:37:29 -0400264extern ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t);
265__errordecl(__pwrite_dest_size_error, "pwrite called with size bigger than destination");
266__errordecl(__pwrite_count_toobig_error, "pwrite called with count > SSIZE_MAX");
267extern ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
268
269extern ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t);
270__errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination");
271__errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX");
272extern ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64);
273
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700274extern ssize_t __read_chk(int, void*, size_t, size_t);
275__errordecl(__read_dest_size_error, "read called with size bigger than destination");
276__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700277extern ssize_t __read_real(int, void*, size_t) __RENAME(read);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700278
Daniel Micayafdd1542015-07-20 21:37:29 -0400279extern ssize_t __write_chk(int, const void*, size_t, size_t);
280__errordecl(__write_dest_size_error, "write called with size bigger than destination");
281__errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX");
282extern ssize_t __write_real(int, const void*, size_t) __RENAME(write);
283
Daniel Micay42281882015-04-17 11:26:36 -0400284extern ssize_t __readlink_chk(const char*, char*, size_t, size_t);
285__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
286__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
287extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
288
289extern ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t);
290__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
291__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
292extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat);
293
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_ */