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  */
28 
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>
37 
38 #include <bits/lockf.h>
39 #include <bits/posix_limits.h>
40 
41 __BEGIN_DECLS
42 
43 #define STDIN_FILENO	0
44 #define STDOUT_FILENO	1
45 #define STDERR_FILENO	2
46 
47 #define F_OK 0
48 #define X_OK 1
49 #define W_OK 2
50 #define R_OK 4
51 
52 #define SEEK_SET 0
53 #define SEEK_CUR 1
54 #define SEEK_END 2
55 
56 #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 
77 extern char** environ;
78 
79 extern __noreturn void _exit(int __status);
80 
81 extern pid_t  fork(void);
82 extern pid_t  vfork(void);
83 extern pid_t  getpid(void);
84 extern pid_t  gettid(void) __pure2;
85 extern pid_t  getpgid(pid_t __pid);
86 extern int    setpgid(pid_t __pid, pid_t __pgid);
87 extern pid_t  getppid(void);
88 extern pid_t  getpgrp(void);
89 extern int    setpgrp(void);
90 extern pid_t  getsid(pid_t __pid) __INTRODUCED_IN(21);
91 extern pid_t  setsid(void);
92 
93 extern int execv(const char* __path, char* const* __argv);
94 extern int execvp(const char* __file, char* const* __argv);
95 extern int execvpe(const char* __file, char* const* __argv, char* const* __envp)
96   __INTRODUCED_IN(21);
97 extern int execve(const char* __file, char* const* __argv, char* const* __envp);
98 extern int execl(const char* __path, const char* __arg0, ...);
99 extern int execlp(const char* __file, const char* __arg0, ...);
100 extern int execle(const char* __path, const char* __arg0, ...);
101 
102 extern int nice(int __incr);
103 
104 extern int setuid(uid_t __uid);
105 extern uid_t getuid(void);
106 extern int seteuid(uid_t __uid);
107 extern uid_t geteuid(void);
108 extern int setgid(gid_t __gid);
109 extern gid_t getgid(void);
110 extern int setegid(gid_t __gid);
111 extern gid_t getegid(void);
112 extern int getgroups(int __size, gid_t* __list);
113 extern int setgroups(size_t __size, const gid_t* __list);
114 extern int setreuid(uid_t __ruid, uid_t __euid);
115 extern int setregid(gid_t __rgid, gid_t __egid);
116 extern int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
117 extern int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
118 extern int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
119 extern int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
120 extern char* getlogin(void);
121 
122 extern long fpathconf(int __fd, int __name);
123 extern long pathconf(const char* __path, int __name);
124 
125 extern int access(const char* __path, int __mode);
126 extern int faccessat(int __dirfd, const char* __path, int __mode, int __flags)
127   __INTRODUCED_IN(21);
128 extern int link(const char* __oldpath, const char* __newpath);
129 extern int linkat(int __olddirfd, const char* __oldpath, int __newdirfd,
130                   const char* __newpath, int __flags) __INTRODUCED_IN(21);
131 extern int unlink(const char* __path);
132 extern int unlinkat(int __dirfd, const char* __path, int __flags);
133 extern int chdir(const char* __path);
134 extern int fchdir(int __fd);
135 extern int rmdir(const char* __path);
136 extern int pipe(int* __pipefd);
137 #if defined(__USE_GNU)
138 extern int pipe2(int* __pipefd, int __flags) __INTRODUCED_IN(9);
139 #endif
140 extern int chroot(const char* __path);
141 extern int symlink(const char* __oldpath, const char* __newpath);
142 extern int symlinkat(const char* __oldpath, int __newdirfd,
143                      const char* __newpath) __INTRODUCED_IN(21);
144 extern ssize_t readlink(const char* __path, char* __buf, size_t __bufsiz);
145 extern ssize_t readlinkat(int __dirfd, const char* __path, char* __buf,
146                           size_t __bufsiz) __INTRODUCED_IN(21);
147 extern int chown(const char* __path, uid_t __owner, gid_t __group);
148 extern int fchown(int __fd, uid_t __owner, gid_t __group);
149 extern int fchownat(int __dirfd, const char* __path, uid_t __owner,
150                     gid_t __group, int __flags);
151 extern int lchown(const char* __path, uid_t __owner, gid_t __group);
152 extern char* getcwd(char* __buf, size_t __size);
153 
154 extern int sync(void);
155 
156 extern int close(int __fd);
157 
158 extern ssize_t read(int __fd, void* __buf, size_t __count);
159 extern ssize_t write(int __fd, const void* __buf, size_t __count);
160 
161 extern int dup(int __oldfd);
162 extern int dup2(int __oldfd, int __newfd);
163 extern int dup3(int __oldfd, int __newfd, int __flags) __INTRODUCED_IN(21);
164 extern int fcntl(int __fd, int __cmd, ...);
165 extern int ioctl(int __fd, int __request, ...);
166 extern int fsync(int __fd);
167 extern int fdatasync(int __fd) __INTRODUCED_IN(9);
168 
169 #if defined(__USE_FILE_OFFSET64)
170 extern off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
171 #else
172 extern off_t lseek(int __fd, off_t __offset, int __whence);
173 #endif
174 
175 extern off64_t lseek64(int __fd, off64_t __offset, int __whence);
176 
177 #if defined(__USE_FILE_OFFSET64) && __ANDROID_API__ >= 21
178 extern int truncate(const char* __path, off_t __length) __RENAME(truncate64);
179 extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset)
180   __RENAME(pread64);
181 extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
182                       off_t __offset) __RENAME(pwrite64);
183 extern int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64);
184 #else
185 extern int truncate(const char* __path, off_t __length);
186 extern ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
187 extern ssize_t pwrite(int __fd, const void* __buf, size_t __count,
188                       off_t __offset);
189 extern int ftruncate(int __fd, off_t __length);
190 #endif
191 
192 extern int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
193 extern ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset) __INTRODUCED_IN(21);
194 extern ssize_t pwrite64(int __fd, const void* __buf, size_t __count,
195                         off64_t __offset) __INTRODUCED_IN(21);
196 extern int ftruncate64(int __fd, off64_t __length) __INTRODUCED_IN(21);
197 
198 extern int pause(void);
199 extern unsigned int alarm(unsigned int __seconds);
200 extern unsigned int sleep(unsigned int __seconds);
201 extern int usleep(useconds_t __usec);
202 
203 int gethostname(char* __name, size_t __len);
204 int sethostname(const char* __name, size_t __len);
205 
206 extern void* __brk(void* __addr);
207 extern int brk(void* __addr);
208 extern void* sbrk(ptrdiff_t __increment);
209 
210 extern int getopt(int __argc, char* const* __argv, const char* __argstring);
211 extern char* optarg;
212 extern int optind, opterr, optopt;
213 
214 extern int isatty(int __fd);
215 extern char* ttyname(int __fd);
216 extern int ttyname_r(int __fd, char* __buf, size_t __buflen) __INTRODUCED_IN(8);
217 
218 extern int acct(const char* __filepath);
219 
220 long sysconf(int __name);
221 
222 #if __ANDROID_API__ >= 21
223 int getpagesize(void);
224 #else
getpagesize(void)225 __inline__ int getpagesize(void) {
226   return sysconf(_SC_PAGESIZE);
227 }
228 #endif
229 
230 long syscall(long __number, ...);
231 
232 extern int daemon(int __nochdir, int __noclose);
233 
234 #if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
235 extern int cacheflush(long __addr, long __nbytes, long __cache);
236     /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
237 #endif
238 
239 extern pid_t tcgetpgrp(int __fd);
240 extern int tcsetpgrp(int __fd, pid_t __pid);
241 
242 /* Used to retry syscalls that can return EINTR. */
243 #define TEMP_FAILURE_RETRY(exp) ({         \
244     __typeof__(exp) _rc;                   \
245     do {                                   \
246         _rc = (exp);                       \
247     } while (_rc == -1 && errno == EINTR); \
248     _rc; })
249 
250 /* TODO(unified-headers): Factor out all the FORTIFY features. */
251 extern char* __getcwd_chk(char*, size_t, size_t);
252 __errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
253 extern char* __getcwd_real(char*, size_t) __RENAME(getcwd);
254 
255 extern 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");
258 extern ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
259 
260 extern 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");
263 extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64);
264 
265 extern 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");
268 extern ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
269 
270 extern 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");
273 extern ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64);
274 
275 extern 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");
278 extern ssize_t __read_real(int, void*, size_t) __RENAME(read);
279 
280 extern 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");
283 extern ssize_t __write_real(int, const void*, size_t) __RENAME(write);
284 
285 extern 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");
288 extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
289 
290 extern 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");
293 extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat);
294 
295 #if defined(__BIONIC_FORTIFY)
296 
297 __BIONIC_FORTIFY_INLINE
getcwd(char * buf,size_t size)298 char* getcwd(char* buf, size_t size) {
299     size_t bos = __bos(buf);
300 
301 #if defined(__clang__)
302     /*
303      * Work around LLVM's incorrect __builtin_object_size implementation here
304      * to avoid needing the workaround in the __getcwd_chk ABI forever.
305      *
306      * https://llvm.org/bugs/show_bug.cgi?id=23277
307      */
308     if (buf == NULL) {
309         bos = __BIONIC_FORTIFY_UNKNOWN_SIZE;
310     }
311 #else
312     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
313         return __getcwd_real(buf, size);
314     }
315 
316     if (__builtin_constant_p(size) && (size > bos)) {
317         __getcwd_dest_size_error();
318     }
319 
320     if (__builtin_constant_p(size) && (size <= bos)) {
321         return __getcwd_real(buf, size);
322     }
323 #endif
324 
325     return __getcwd_chk(buf, size, bos);
326 }
327 
328 #if defined(__USE_FILE_OFFSET64)
329 #define __PREAD_PREFIX(x) __pread64_ ## x
330 #else
331 #define __PREAD_PREFIX(x) __pread_ ## x
332 #endif
333 
334 __BIONIC_FORTIFY_INLINE
pread(int fd,void * buf,size_t count,off_t offset)335 ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
336     size_t bos = __bos0(buf);
337 
338 #if !defined(__clang__)
339     if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
340         __PREAD_PREFIX(count_toobig_error)();
341     }
342 
343     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
344         return __PREAD_PREFIX(real)(fd, buf, count, offset);
345     }
346 
347     if (__builtin_constant_p(count) && (count > bos)) {
348         __PREAD_PREFIX(dest_size_error)();
349     }
350 
351     if (__builtin_constant_p(count) && (count <= bos)) {
352         return __PREAD_PREFIX(real)(fd, buf, count, offset);
353     }
354 #endif
355 
356     return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
357 }
358 
359 __BIONIC_FORTIFY_INLINE
pread64(int fd,void * buf,size_t count,off64_t offset)360 ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) {
361     size_t bos = __bos0(buf);
362 
363 #if !defined(__clang__)
364     if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
365         __pread64_count_toobig_error();
366     }
367 
368     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
369         return __pread64_real(fd, buf, count, offset);
370     }
371 
372     if (__builtin_constant_p(count) && (count > bos)) {
373         __pread64_dest_size_error();
374     }
375 
376     if (__builtin_constant_p(count) && (count <= bos)) {
377         return __pread64_real(fd, buf, count, offset);
378     }
379 #endif
380 
381     return __pread64_chk(fd, buf, count, offset, bos);
382 }
383 
384 #if defined(__USE_FILE_OFFSET64)
385 #define __PWRITE_PREFIX(x) __pwrite64_ ## x
386 #else
387 #define __PWRITE_PREFIX(x) __pwrite_ ## x
388 #endif
389 
390 __BIONIC_FORTIFY_INLINE
pwrite(int fd,const void * buf,size_t count,off_t offset)391 ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) {
392     size_t bos = __bos0(buf);
393 
394 #if !defined(__clang__)
395     if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
396         __PWRITE_PREFIX(count_toobig_error)();
397     }
398 
399     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
400         return __PWRITE_PREFIX(real)(fd, buf, count, offset);
401     }
402 
403     if (__builtin_constant_p(count) && (count > bos)) {
404         __PWRITE_PREFIX(dest_size_error)();
405     }
406 
407     if (__builtin_constant_p(count) && (count <= bos)) {
408         return __PWRITE_PREFIX(real)(fd, buf, count, offset);
409     }
410 #endif
411 
412     return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
413 }
414 
415 __BIONIC_FORTIFY_INLINE
pwrite64(int fd,const void * buf,size_t count,off64_t offset)416 ssize_t pwrite64(int fd, const void* buf, size_t count, off64_t offset) {
417     size_t bos = __bos0(buf);
418 
419 #if !defined(__clang__)
420     if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
421         __pwrite64_count_toobig_error();
422     }
423 
424     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
425         return __pwrite64_real(fd, buf, count, offset);
426     }
427 
428     if (__builtin_constant_p(count) && (count > bos)) {
429         __pwrite64_dest_size_error();
430     }
431 
432     if (__builtin_constant_p(count) && (count <= bos)) {
433         return __pwrite64_real(fd, buf, count, offset);
434     }
435 #endif
436 
437     return __pwrite64_chk(fd, buf, count, offset, bos);
438 }
439 
440 __BIONIC_FORTIFY_INLINE
read(int fd,void * buf,size_t count)441 ssize_t read(int fd, void* buf, size_t count) {
442     size_t bos = __bos0(buf);
443 
444 #if !defined(__clang__)
445     if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
446         __read_count_toobig_error();
447     }
448 
449     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
450         return __read_real(fd, buf, count);
451     }
452 
453     if (__builtin_constant_p(count) && (count > bos)) {
454         __read_dest_size_error();
455     }
456 
457     if (__builtin_constant_p(count) && (count <= bos)) {
458         return __read_real(fd, buf, count);
459     }
460 #endif
461 
462     return __read_chk(fd, buf, count, bos);
463 }
464 
465 __BIONIC_FORTIFY_INLINE
write(int fd,const void * buf,size_t count)466 ssize_t write(int fd, const void* buf, size_t count) {
467     size_t bos = __bos0(buf);
468 
469 #if !defined(__clang__)
470 #if 0 /* work around a false positive due to a missed optimization */
471     if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
472         __write_count_toobig_error();
473     }
474 #endif
475 
476     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
477         return __write_real(fd, buf, count);
478     }
479 
480     if (__builtin_constant_p(count) && (count > bos)) {
481         __write_dest_size_error();
482     }
483 
484     if (__builtin_constant_p(count) && (count <= bos)) {
485         return __write_real(fd, buf, count);
486     }
487 #endif
488 
489     return __write_chk(fd, buf, count, bos);
490 }
491 
492 __BIONIC_FORTIFY_INLINE
readlink(const char * path,char * buf,size_t size)493 ssize_t readlink(const char* path, char* buf, size_t size) {
494     size_t bos = __bos(buf);
495 
496 #if !defined(__clang__)
497     if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
498         __readlink_size_toobig_error();
499     }
500 
501     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
502         return __readlink_real(path, buf, size);
503     }
504 
505     if (__builtin_constant_p(size) && (size > bos)) {
506         __readlink_dest_size_error();
507     }
508 
509     if (__builtin_constant_p(size) && (size <= bos)) {
510         return __readlink_real(path, buf, size);
511     }
512 #endif
513 
514     return __readlink_chk(path, buf, size, bos);
515 }
516 
517 __BIONIC_FORTIFY_INLINE
readlinkat(int dirfd,const char * path,char * buf,size_t size)518 ssize_t readlinkat(int dirfd, const char* path, char* buf, size_t size) {
519     size_t bos = __bos(buf);
520 
521 #if !defined(__clang__)
522     if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
523         __readlinkat_size_toobig_error();
524     }
525 
526     if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
527         return __readlinkat_real(dirfd, path, buf, size);
528     }
529 
530     if (__builtin_constant_p(size) && (size > bos)) {
531         __readlinkat_dest_size_error();
532     }
533 
534     if (__builtin_constant_p(size) && (size <= bos)) {
535         return __readlinkat_real(dirfd, path, buf, size);
536     }
537 #endif
538 
539     return __readlinkat_chk(dirfd, path, buf, size, bos);
540 }
541 
542 #endif /* defined(__BIONIC_FORTIFY) */
543 
544 __END_DECLS
545 
546 #endif /* _UNISTD_H_ */
547