The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | f4c948a | 2014-08-19 11:16:41 -0700 | [diff] [blame] | 28 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 29 | #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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | |
Elliott Hughes | 1ed337d | 2015-02-02 14:02:09 -0800 | [diff] [blame] | 38 | #include <machine/posix_limits.h> |
| 39 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | __BEGIN_DECLS |
| 41 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 42 | #define STDIN_FILENO 0 |
| 43 | #define STDOUT_FILENO 1 |
| 44 | #define STDERR_FILENO 2 |
| 45 | |
Elliott Hughes | 1ed337d | 2015-02-02 14:02:09 -0800 | [diff] [blame] | 46 | #define F_OK 0 |
| 47 | #define X_OK 1 |
| 48 | #define W_OK 2 |
| 49 | #define R_OK 4 |
| 50 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | #define SEEK_SET 0 |
| 52 | #define SEEK_CUR 1 |
| 53 | #define SEEK_END 2 |
| 54 | |
Elliott Hughes | a186b2e | 2014-09-22 14:49:07 -0700 | [diff] [blame] | 55 | #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 Hughes | 58d9e28 | 2014-04-22 17:41:00 -0700 | [diff] [blame] | 76 | extern char** environ; |
| 77 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 78 | extern __noreturn void _exit(int); |
| 79 | |
| 80 | extern pid_t fork(void); |
| 81 | extern pid_t vfork(void); |
| 82 | extern pid_t getpid(void); |
Elliott Hughes | b27a840 | 2014-06-10 20:47:49 -0700 | [diff] [blame] | 83 | extern pid_t gettid(void) __pure2; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | extern pid_t getpgid(pid_t); |
| 85 | extern int setpgid(pid_t, pid_t); |
| 86 | extern pid_t getppid(void); |
| 87 | extern pid_t getpgrp(void); |
| 88 | extern int setpgrp(void); |
Irina Tirdea | 1ad10a5 | 2012-08-29 11:48:35 +0300 | [diff] [blame] | 89 | extern pid_t getsid(pid_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 90 | extern pid_t setsid(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 91 | |
| 92 | extern int execv(const char *, char * const *); |
| 93 | extern int execvp(const char *, char * const *); |
Elliott Hughes | 0133944 | 2014-02-20 18:04:58 -0800 | [diff] [blame] | 94 | extern int execvpe(const char *, char * const *, char * const *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 95 | extern int execve(const char *, char * const *, char * const *); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 96 | extern int execl(const char *, const char *, ...); |
| 97 | extern int execlp(const char *, const char *, ...); |
| 98 | extern int execle(const char *, const char *, ...); |
David 'Digit' Turner | b083bb5 | 2011-05-26 02:46:41 +0200 | [diff] [blame] | 99 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | extern int nice(int); |
| 101 | |
| 102 | extern int setuid(uid_t); |
| 103 | extern uid_t getuid(void); |
| 104 | extern int seteuid(uid_t); |
| 105 | extern uid_t geteuid(void); |
| 106 | extern int setgid(gid_t); |
| 107 | extern gid_t getgid(void); |
| 108 | extern int setegid(gid_t); |
| 109 | extern gid_t getegid(void); |
| 110 | extern int getgroups(int, gid_t *); |
| 111 | extern int setgroups(size_t, const gid_t *); |
| 112 | extern int setreuid(uid_t, uid_t); |
| 113 | extern int setregid(gid_t, gid_t); |
| 114 | extern int setresuid(uid_t, uid_t, uid_t); |
| 115 | extern int setresgid(gid_t, gid_t, gid_t); |
| 116 | extern int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid); |
| 117 | extern int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 118 | extern char* getlogin(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | |
Elliott Hughes | a186b2e | 2014-09-22 14:49:07 -0700 | [diff] [blame] | 120 | extern long fpathconf(int, int); |
| 121 | extern long pathconf(const char*, int); |
| 122 | |
Elliott Hughes | f8fcfbc | 2013-10-22 13:28:46 -0700 | [diff] [blame] | 123 | extern int access(const char*, int); |
| 124 | extern int faccessat(int, const char*, int, int); |
| 125 | extern int link(const char*, const char*); |
| 126 | extern int linkat(int, const char*, int, const char*, int); |
| 127 | extern int unlink(const char*); |
| 128 | extern int unlinkat(int, const char*, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 129 | extern int chdir(const char *); |
| 130 | extern int fchdir(int); |
| 131 | extern int rmdir(const char *); |
| 132 | extern int pipe(int *); |
Elliott Hughes | 5f5cc45 | 2014-08-18 16:04:03 -0700 | [diff] [blame] | 133 | #if defined(__USE_GNU) |
David 'Digit' Turner | 275cd48 | 2010-09-27 17:33:08 +0200 | [diff] [blame] | 134 | extern int pipe2(int *, int); |
| 135 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 136 | extern int chroot(const char *); |
Elliott Hughes | f8fcfbc | 2013-10-22 13:28:46 -0700 | [diff] [blame] | 137 | extern int symlink(const char*, const char*); |
| 138 | extern int symlinkat(const char*, int, const char*); |
| 139 | extern ssize_t readlink(const char*, char*, size_t); |
| 140 | extern ssize_t readlinkat(int, const char*, char*, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 141 | extern int chown(const char *, uid_t, gid_t); |
| 142 | extern int fchown(int, uid_t, gid_t); |
Elliott Hughes | f8fcfbc | 2013-10-22 13:28:46 -0700 | [diff] [blame] | 143 | extern int fchownat(int, const char*, uid_t, gid_t, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 144 | extern int lchown(const char *, uid_t, gid_t); |
| 145 | extern char *getcwd(char *, size_t); |
| 146 | |
| 147 | extern int sync(void); |
| 148 | |
| 149 | extern int close(int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 150 | |
| 151 | extern ssize_t read(int, void *, size_t); |
| 152 | extern ssize_t write(int, const void *, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 153 | |
| 154 | extern int dup(int); |
| 155 | extern int dup2(int, int); |
Elliott Hughes | cac7b9d | 2013-10-23 09:48:29 -0700 | [diff] [blame] | 156 | extern int dup3(int, int, int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 157 | extern int fcntl(int, int, ...); |
| 158 | extern int ioctl(int, int, ...); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 159 | extern int fsync(int); |
| 160 | extern int fdatasync(int); |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 161 | |
| 162 | #if defined(__USE_FILE_OFFSET64) |
| 163 | extern int truncate(const char *, off_t) __RENAME(truncate64); |
| 164 | extern off_t lseek(int, off_t, int) __RENAME(lseek64); |
| 165 | extern ssize_t pread(int, void *, size_t, off_t) __RENAME(pread64); |
| 166 | extern ssize_t pwrite(int, const void *, size_t, off_t) __RENAME(pwrite64); |
| 167 | extern int ftruncate(int, off_t) __RENAME(ftruncate64); |
| 168 | #else |
| 169 | extern int truncate(const char *, off_t); |
| 170 | extern off_t lseek(int, off_t, int); |
| 171 | extern ssize_t pread(int, void *, size_t, off_t); |
| 172 | extern ssize_t pwrite(int, const void *, size_t, off_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 173 | extern int ftruncate(int, off_t); |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 174 | #endif |
| 175 | extern int truncate64(const char *, off64_t); |
| 176 | extern off64_t lseek64(int, off64_t, int); |
| 177 | extern ssize_t pread64(int, void *, size_t, off64_t); |
| 178 | extern ssize_t pwrite64(int, const void *, size_t, off64_t); |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 179 | extern int ftruncate64(int, off64_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 180 | |
| 181 | extern int pause(void); |
| 182 | extern unsigned int alarm(unsigned int); |
| 183 | extern unsigned int sleep(unsigned int); |
Elliott Hughes | ab61eb3 | 2013-11-20 16:09:06 -0800 | [diff] [blame] | 184 | extern int usleep(useconds_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 185 | |
Elliott Hughes | b86a4c7 | 2014-11-07 16:07:13 -0800 | [diff] [blame] | 186 | int gethostname(char*, size_t); |
| 187 | int sethostname(const char*, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 188 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 189 | extern void *__brk(void *); |
| 190 | extern int brk(void *); |
| 191 | extern void *sbrk(ptrdiff_t); |
| 192 | |
| 193 | extern int getopt(int, char * const *, const char *); |
| 194 | extern char *optarg; |
| 195 | extern int optind, opterr, optopt; |
| 196 | |
| 197 | extern int isatty(int); |
Elliott Hughes | a381fe8 | 2014-12-09 20:30:23 -0800 | [diff] [blame] | 198 | extern char* ttyname(int); |
Colin Cross | fc10b24 | 2010-01-13 17:48:34 -0800 | [diff] [blame] | 199 | extern int ttyname_r(int, char*, size_t); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 200 | |
| 201 | extern int acct(const char* filepath); |
| 202 | |
Bernhard Rosenkraenzer | 9ae59c0 | 2013-09-18 23:29:08 +0200 | [diff] [blame] | 203 | int getpagesize(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 204 | |
Elliott Hughes | 60d84af | 2014-11-14 15:14:44 -0800 | [diff] [blame] | 205 | long sysconf(int); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | |
Elliott Hughes | 4c5891d | 2015-02-19 22:49:44 -0800 | [diff] [blame] | 207 | long syscall(long number, ...); |
| 208 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 209 | extern int daemon(int, int); |
| 210 | |
Elliott Hughes | a6ecba4 | 2014-02-10 18:37:02 -0800 | [diff] [blame] | 211 | #if defined(__arm__) || (defined(__mips__) && !defined(__LP64__)) |
| 212 | extern int cacheflush(long, long, long); |
| 213 | /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */ |
| 214 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 215 | |
| 216 | extern pid_t tcgetpgrp(int fd); |
| 217 | extern int tcsetpgrp(int fd, pid_t _pid); |
| 218 | |
Elliott Hughes | cf399f7 | 2009-10-05 13:19:05 -0700 | [diff] [blame] | 219 | /* Used to retry syscalls that can return EINTR. */ |
| 220 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
Dan Albert | a7821b7 | 2014-05-21 20:33:28 -0700 | [diff] [blame] | 221 | __typeof__(exp) _rc; \ |
Elliott Hughes | cf399f7 | 2009-10-05 13:19:05 -0700 | [diff] [blame] | 222 | do { \ |
| 223 | _rc = (exp); \ |
| 224 | } while (_rc == -1 && errno == EINTR); \ |
| 225 | _rc; }) |
| 226 | |
Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 227 | extern char* __getcwd_chk(char*, size_t, size_t); |
| 228 | __errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination"); |
| 229 | extern char* __getcwd_real(char*, size_t) __RENAME(getcwd); |
| 230 | |
Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 231 | extern ssize_t __pread_chk(int, void*, size_t, off_t, size_t); |
| 232 | __errordecl(__pread_dest_size_error, "pread called with size bigger than destination"); |
| 233 | __errordecl(__pread_count_toobig_error, "pread called with count > SSIZE_MAX"); |
| 234 | extern ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread); |
| 235 | |
| 236 | extern ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t); |
| 237 | __errordecl(__pread64_dest_size_error, "pread64 called with size bigger than destination"); |
| 238 | __errordecl(__pread64_count_toobig_error, "pread64 called with count > SSIZE_MAX"); |
| 239 | extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64); |
| 240 | |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 241 | extern ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t); |
| 242 | __errordecl(__pwrite_dest_size_error, "pwrite called with size bigger than destination"); |
| 243 | __errordecl(__pwrite_count_toobig_error, "pwrite called with count > SSIZE_MAX"); |
| 244 | extern ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite); |
| 245 | |
| 246 | extern ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t); |
| 247 | __errordecl(__pwrite64_dest_size_error, "pwrite64 called with size bigger than destination"); |
| 248 | __errordecl(__pwrite64_count_toobig_error, "pwrite64 called with count > SSIZE_MAX"); |
| 249 | extern ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64); |
| 250 | |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 251 | extern ssize_t __read_chk(int, void*, size_t, size_t); |
| 252 | __errordecl(__read_dest_size_error, "read called with size bigger than destination"); |
| 253 | __errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX"); |
Elliott Hughes | 2cfb4e8 | 2014-08-18 14:45:42 -0700 | [diff] [blame] | 254 | extern ssize_t __read_real(int, void*, size_t) __RENAME(read); |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 255 | |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 256 | extern ssize_t __write_chk(int, const void*, size_t, size_t); |
| 257 | __errordecl(__write_dest_size_error, "write called with size bigger than destination"); |
| 258 | __errordecl(__write_count_toobig_error, "write called with count > SSIZE_MAX"); |
| 259 | extern ssize_t __write_real(int, const void*, size_t) __RENAME(write); |
| 260 | |
Daniel Micay | 4228188 | 2015-04-17 11:26:36 -0400 | [diff] [blame] | 261 | extern ssize_t __readlink_chk(const char*, char*, size_t, size_t); |
| 262 | __errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination"); |
| 263 | __errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX"); |
| 264 | extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink); |
| 265 | |
| 266 | extern ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t); |
| 267 | __errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination"); |
| 268 | __errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX"); |
| 269 | extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat); |
| 270 | |
Dan Albert | 658727e | 2014-10-07 11:10:36 -0700 | [diff] [blame] | 271 | #if defined(__BIONIC_FORTIFY) |
| 272 | |
Daniel Micay | 9101b00 | 2015-05-20 15:31:26 -0400 | [diff] [blame] | 273 | __BIONIC_FORTIFY_INLINE |
| 274 | char* getcwd(char* buf, size_t size) { |
| 275 | size_t bos = __bos(buf); |
| 276 | |
| 277 | #if defined(__clang__) |
| 278 | /* |
| 279 | * Work around LLVM's incorrect __builtin_object_size implementation here |
| 280 | * to avoid needing the workaround in the __getcwd_chk ABI forever. |
| 281 | * |
| 282 | * https://llvm.org/bugs/show_bug.cgi?id=23277 |
| 283 | */ |
| 284 | if (buf == NULL) { |
| 285 | bos = __BIONIC_FORTIFY_UNKNOWN_SIZE; |
| 286 | } |
| 287 | #else |
| 288 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 289 | return __getcwd_real(buf, size); |
| 290 | } |
| 291 | |
| 292 | if (__builtin_constant_p(size) && (size > bos)) { |
| 293 | __getcwd_dest_size_error(); |
| 294 | } |
| 295 | |
| 296 | if (__builtin_constant_p(size) && (size <= bos)) { |
| 297 | return __getcwd_real(buf, size); |
| 298 | } |
| 299 | #endif |
| 300 | |
| 301 | return __getcwd_chk(buf, size, bos); |
| 302 | } |
| 303 | |
Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 304 | #if defined(__USE_FILE_OFFSET64) |
| 305 | #define __PREAD_PREFIX(x) __pread64_ ## x |
| 306 | #else |
| 307 | #define __PREAD_PREFIX(x) __pread_ ## x |
| 308 | #endif |
| 309 | |
| 310 | __BIONIC_FORTIFY_INLINE |
| 311 | ssize_t pread(int fd, void* buf, size_t count, off_t offset) { |
| 312 | size_t bos = __bos0(buf); |
| 313 | |
| 314 | #if !defined(__clang__) |
| 315 | if (__builtin_constant_p(count) && (count > SSIZE_MAX)) { |
| 316 | __PREAD_PREFIX(count_toobig_error)(); |
| 317 | } |
| 318 | |
| 319 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 320 | return __PREAD_PREFIX(real)(fd, buf, count, offset); |
| 321 | } |
| 322 | |
| 323 | if (__builtin_constant_p(count) && (count > bos)) { |
| 324 | __PREAD_PREFIX(dest_size_error)(); |
| 325 | } |
| 326 | |
| 327 | if (__builtin_constant_p(count) && (count <= bos)) { |
| 328 | return __PREAD_PREFIX(real)(fd, buf, count, offset); |
| 329 | } |
| 330 | #endif |
| 331 | |
| 332 | return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos); |
| 333 | } |
| 334 | |
| 335 | __BIONIC_FORTIFY_INLINE |
| 336 | ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) { |
| 337 | size_t bos = __bos0(buf); |
| 338 | |
| 339 | #if !defined(__clang__) |
| 340 | if (__builtin_constant_p(count) && (count > SSIZE_MAX)) { |
| 341 | __pread64_count_toobig_error(); |
| 342 | } |
| 343 | |
| 344 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 345 | return __pread64_real(fd, buf, count, offset); |
| 346 | } |
| 347 | |
| 348 | if (__builtin_constant_p(count) && (count > bos)) { |
| 349 | __pread64_dest_size_error(); |
| 350 | } |
| 351 | |
| 352 | if (__builtin_constant_p(count) && (count <= bos)) { |
| 353 | return __pread64_real(fd, buf, count, offset); |
| 354 | } |
| 355 | #endif |
| 356 | |
| 357 | return __pread64_chk(fd, buf, count, offset, bos); |
| 358 | } |
| 359 | |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 360 | #if defined(__USE_FILE_OFFSET64) |
| 361 | #define __PWRITE_PREFIX(x) __pwrite64_ ## x |
| 362 | #else |
| 363 | #define __PWRITE_PREFIX(x) __pwrite_ ## x |
| 364 | #endif |
| 365 | |
| 366 | __BIONIC_FORTIFY_INLINE |
| 367 | ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) { |
| 368 | size_t bos = __bos0(buf); |
| 369 | |
| 370 | #if !defined(__clang__) |
| 371 | if (__builtin_constant_p(count) && (count > SSIZE_MAX)) { |
| 372 | __PWRITE_PREFIX(count_toobig_error)(); |
| 373 | } |
| 374 | |
| 375 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 376 | return __PWRITE_PREFIX(real)(fd, buf, count, offset); |
| 377 | } |
| 378 | |
| 379 | if (__builtin_constant_p(count) && (count > bos)) { |
| 380 | __PWRITE_PREFIX(dest_size_error)(); |
| 381 | } |
| 382 | |
| 383 | if (__builtin_constant_p(count) && (count <= bos)) { |
| 384 | return __PWRITE_PREFIX(real)(fd, buf, count, offset); |
| 385 | } |
| 386 | #endif |
| 387 | |
| 388 | return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos); |
| 389 | } |
| 390 | |
| 391 | __BIONIC_FORTIFY_INLINE |
| 392 | ssize_t pwrite64(int fd, const void* buf, size_t count, off64_t offset) { |
| 393 | size_t bos = __bos0(buf); |
| 394 | |
| 395 | #if !defined(__clang__) |
| 396 | if (__builtin_constant_p(count) && (count > SSIZE_MAX)) { |
| 397 | __pwrite64_count_toobig_error(); |
| 398 | } |
| 399 | |
| 400 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 401 | return __pwrite64_real(fd, buf, count, offset); |
| 402 | } |
| 403 | |
| 404 | if (__builtin_constant_p(count) && (count > bos)) { |
| 405 | __pwrite64_dest_size_error(); |
| 406 | } |
| 407 | |
| 408 | if (__builtin_constant_p(count) && (count <= bos)) { |
| 409 | return __pwrite64_real(fd, buf, count, offset); |
| 410 | } |
| 411 | #endif |
| 412 | |
| 413 | return __pwrite64_chk(fd, buf, count, offset, bos); |
| 414 | } |
| 415 | |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 416 | __BIONIC_FORTIFY_INLINE |
| 417 | ssize_t read(int fd, void* buf, size_t count) { |
| 418 | size_t bos = __bos0(buf); |
| 419 | |
| 420 | #if !defined(__clang__) |
| 421 | if (__builtin_constant_p(count) && (count > SSIZE_MAX)) { |
| 422 | __read_count_toobig_error(); |
| 423 | } |
| 424 | |
| 425 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 426 | return __read_real(fd, buf, count); |
| 427 | } |
| 428 | |
| 429 | if (__builtin_constant_p(count) && (count > bos)) { |
| 430 | __read_dest_size_error(); |
| 431 | } |
| 432 | |
| 433 | if (__builtin_constant_p(count) && (count <= bos)) { |
| 434 | return __read_real(fd, buf, count); |
| 435 | } |
| 436 | #endif |
| 437 | |
| 438 | return __read_chk(fd, buf, count, bos); |
| 439 | } |
Daniel Micay | e7e1c87 | 2015-04-16 09:07:45 -0400 | [diff] [blame] | 440 | |
Daniel Micay | 4228188 | 2015-04-17 11:26:36 -0400 | [diff] [blame] | 441 | __BIONIC_FORTIFY_INLINE |
Daniel Micay | afdd154 | 2015-07-20 21:37:29 -0400 | [diff] [blame] | 442 | ssize_t write(int fd, const void* buf, size_t count) { |
| 443 | size_t bos = __bos0(buf); |
| 444 | |
| 445 | #if !defined(__clang__) |
| 446 | #if 0 /* work around a false positive due to a missed optimization */ |
| 447 | if (__builtin_constant_p(count) && (count > SSIZE_MAX)) { |
| 448 | __write_count_toobig_error(); |
| 449 | } |
| 450 | #endif |
| 451 | |
| 452 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 453 | return __write_real(fd, buf, count); |
| 454 | } |
| 455 | |
| 456 | if (__builtin_constant_p(count) && (count > bos)) { |
| 457 | __write_dest_size_error(); |
| 458 | } |
| 459 | |
| 460 | if (__builtin_constant_p(count) && (count <= bos)) { |
| 461 | return __write_real(fd, buf, count); |
| 462 | } |
| 463 | #endif |
| 464 | |
| 465 | return __write_chk(fd, buf, count, bos); |
| 466 | } |
| 467 | |
| 468 | __BIONIC_FORTIFY_INLINE |
Daniel Micay | 4228188 | 2015-04-17 11:26:36 -0400 | [diff] [blame] | 469 | ssize_t readlink(const char* path, char* buf, size_t size) { |
| 470 | size_t bos = __bos(buf); |
| 471 | |
| 472 | #if !defined(__clang__) |
| 473 | if (__builtin_constant_p(size) && (size > SSIZE_MAX)) { |
| 474 | __readlink_size_toobig_error(); |
| 475 | } |
| 476 | |
| 477 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 478 | return __readlink_real(path, buf, size); |
| 479 | } |
| 480 | |
| 481 | if (__builtin_constant_p(size) && (size > bos)) { |
| 482 | __readlink_dest_size_error(); |
| 483 | } |
| 484 | |
| 485 | if (__builtin_constant_p(size) && (size <= bos)) { |
| 486 | return __readlink_real(path, buf, size); |
| 487 | } |
| 488 | #endif |
| 489 | |
| 490 | return __readlink_chk(path, buf, size, bos); |
| 491 | } |
| 492 | |
| 493 | __BIONIC_FORTIFY_INLINE |
| 494 | ssize_t readlinkat(int dirfd, const char* path, char* buf, size_t size) { |
| 495 | size_t bos = __bos(buf); |
| 496 | |
| 497 | #if !defined(__clang__) |
| 498 | if (__builtin_constant_p(size) && (size > SSIZE_MAX)) { |
| 499 | __readlinkat_size_toobig_error(); |
| 500 | } |
| 501 | |
| 502 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 503 | return __readlinkat_real(dirfd, path, buf, size); |
| 504 | } |
| 505 | |
| 506 | if (__builtin_constant_p(size) && (size > bos)) { |
| 507 | __readlinkat_dest_size_error(); |
| 508 | } |
| 509 | |
| 510 | if (__builtin_constant_p(size) && (size <= bos)) { |
| 511 | return __readlinkat_real(dirfd, path, buf, size); |
| 512 | } |
| 513 | #endif |
| 514 | |
| 515 | return __readlinkat_chk(dirfd, path, buf, size, bos); |
| 516 | } |
| 517 | |
Nick Kralevich | b036b5c | 2013-10-09 20:16:34 -0700 | [diff] [blame] | 518 | #endif /* defined(__BIONIC_FORTIFY) */ |
| 519 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 520 | __END_DECLS |
| 521 | |
| 522 | #endif /* _UNISTD_H_ */ |