blob: 904447ce3df8edadf854e640fe414a26d2911143 [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078extern __noreturn void _exit(int);
79
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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084extern pid_t getpgid(pid_t);
85extern int setpgid(pid_t, pid_t);
86extern pid_t getppid(void);
87extern pid_t getpgrp(void);
88extern int setpgrp(void);
Irina Tirdea1ad10a52012-08-29 11:48:35 +030089extern pid_t getsid(pid_t);
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
92extern int execv(const char *, char * const *);
93extern int execvp(const char *, char * const *);
Elliott Hughes01339442014-02-20 18:04:58 -080094extern int execvpe(const char *, char * const *, char * const *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095extern int execve(const char *, char * const *, char * const *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096extern int execl(const char *, const char *, ...);
97extern int execlp(const char *, const char *, ...);
98extern int execle(const char *, const char *, ...);
David 'Digit' Turnerb083bb52011-05-26 02:46:41 +020099
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800100extern int nice(int);
101
102extern int setuid(uid_t);
103extern uid_t getuid(void);
104extern int seteuid(uid_t);
105extern uid_t geteuid(void);
106extern int setgid(gid_t);
107extern gid_t getgid(void);
108extern int setegid(gid_t);
109extern gid_t getegid(void);
110extern int getgroups(int, gid_t *);
111extern int setgroups(size_t, const gid_t *);
112extern int setreuid(uid_t, uid_t);
113extern int setregid(gid_t, gid_t);
114extern int setresuid(uid_t, uid_t, uid_t);
115extern int setresgid(gid_t, gid_t, gid_t);
116extern int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
117extern int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118extern char* getlogin(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119
Elliott Hughesa186b2e2014-09-22 14:49:07 -0700120extern long fpathconf(int, int);
121extern long pathconf(const char*, int);
122
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -0700123extern int access(const char*, int);
124extern int faccessat(int, const char*, int, int);
125extern int link(const char*, const char*);
126extern int linkat(int, const char*, int, const char*, int);
127extern int unlink(const char*);
128extern int unlinkat(int, const char*, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129extern int chdir(const char *);
130extern int fchdir(int);
131extern int rmdir(const char *);
132extern int pipe(int *);
Elliott Hughes5f5cc452014-08-18 16:04:03 -0700133#if defined(__USE_GNU)
David 'Digit' Turner275cd482010-09-27 17:33:08 +0200134extern int pipe2(int *, int);
135#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136extern int chroot(const char *);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -0700137extern int symlink(const char*, const char*);
138extern int symlinkat(const char*, int, const char*);
139extern ssize_t readlink(const char*, char*, size_t);
140extern ssize_t readlinkat(int, const char*, char*, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141extern int chown(const char *, uid_t, gid_t);
142extern int fchown(int, uid_t, gid_t);
Elliott Hughesf8fcfbc2013-10-22 13:28:46 -0700143extern int fchownat(int, const char*, uid_t, gid_t, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144extern int lchown(const char *, uid_t, gid_t);
145extern char *getcwd(char *, size_t);
146
147extern int sync(void);
148
149extern int close(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150
151extern ssize_t read(int, void *, size_t);
152extern ssize_t write(int, const void *, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153
154extern int dup(int);
155extern int dup2(int, int);
Elliott Hughescac7b9d2013-10-23 09:48:29 -0700156extern int dup3(int, int, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157extern int fcntl(int, int, ...);
158extern int ioctl(int, int, ...);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159extern int fsync(int);
160extern int fdatasync(int);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800161
162#if defined(__USE_FILE_OFFSET64)
163extern int truncate(const char *, off_t) __RENAME(truncate64);
164extern off_t lseek(int, off_t, int) __RENAME(lseek64);
165extern ssize_t pread(int, void *, size_t, off_t) __RENAME(pread64);
166extern ssize_t pwrite(int, const void *, size_t, off_t) __RENAME(pwrite64);
167extern int ftruncate(int, off_t) __RENAME(ftruncate64);
168#else
169extern int truncate(const char *, off_t);
170extern off_t lseek(int, off_t, int);
171extern ssize_t pread(int, void *, size_t, off_t);
172extern ssize_t pwrite(int, const void *, size_t, off_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173extern int ftruncate(int, off_t);
Elliott Hughes68dc20d2015-02-06 22:28:49 -0800174#endif
175extern int truncate64(const char *, off64_t);
176extern off64_t lseek64(int, off64_t, int);
177extern ssize_t pread64(int, void *, size_t, off64_t);
178extern ssize_t pwrite64(int, const void *, size_t, off64_t);
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100179extern int ftruncate64(int, off64_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
181extern int pause(void);
182extern unsigned int alarm(unsigned int);
183extern unsigned int sleep(unsigned int);
Elliott Hughesab61eb32013-11-20 16:09:06 -0800184extern int usleep(useconds_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185
Elliott Hughesb86a4c72014-11-07 16:07:13 -0800186int gethostname(char*, size_t);
187int sethostname(const char*, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189extern void *__brk(void *);
190extern int brk(void *);
191extern void *sbrk(ptrdiff_t);
192
193extern int getopt(int, char * const *, const char *);
194extern char *optarg;
195extern int optind, opterr, optopt;
196
197extern int isatty(int);
Elliott Hughesa381fe82014-12-09 20:30:23 -0800198extern char* ttyname(int);
Colin Crossfc10b242010-01-13 17:48:34 -0800199extern int ttyname_r(int, char*, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
201extern int acct(const char* filepath);
202
Bernhard Rosenkraenzer9ae59c02013-09-18 23:29:08 +0200203int getpagesize(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204
Elliott Hughes60d84af2014-11-14 15:14:44 -0800205long sysconf(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206
Elliott Hughes4c5891d2015-02-19 22:49:44 -0800207long syscall(long number, ...);
208
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209extern int daemon(int, int);
210
Elliott Hughesa6ecba42014-02-10 18:37:02 -0800211#if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
212extern int cacheflush(long, long, long);
213 /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
214#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215
216extern pid_t tcgetpgrp(int fd);
217extern int tcsetpgrp(int fd, pid_t _pid);
218
Elliott Hughescf399f72009-10-05 13:19:05 -0700219/* Used to retry syscalls that can return EINTR. */
220#define TEMP_FAILURE_RETRY(exp) ({ \
Dan Alberta7821b72014-05-21 20:33:28 -0700221 __typeof__(exp) _rc; \
Elliott Hughescf399f72009-10-05 13:19:05 -0700222 do { \
223 _rc = (exp); \
224 } while (_rc == -1 && errno == EINTR); \
225 _rc; })
226
Daniel Micay9101b002015-05-20 15:31:26 -0400227extern char* __getcwd_chk(char*, size_t, size_t);
228__errordecl(__getcwd_dest_size_error, "getcwd called with size bigger than destination");
229extern char* __getcwd_real(char*, size_t) __RENAME(getcwd);
230
Daniel Micaye7e1c872015-04-16 09:07:45 -0400231extern 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");
234extern ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
235
236extern 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");
239extern ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64);
240
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700241extern ssize_t __read_chk(int, void*, size_t, size_t);
242__errordecl(__read_dest_size_error, "read called with size bigger than destination");
243__errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
Elliott Hughes2cfb4e82014-08-18 14:45:42 -0700244extern ssize_t __read_real(int, void*, size_t) __RENAME(read);
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700245
Daniel Micay42281882015-04-17 11:26:36 -0400246extern ssize_t __readlink_chk(const char*, char*, size_t, size_t);
247__errordecl(__readlink_dest_size_error, "readlink called with size bigger than destination");
248__errordecl(__readlink_size_toobig_error, "readlink called with size > SSIZE_MAX");
249extern ssize_t __readlink_real(const char*, char*, size_t) __RENAME(readlink);
250
251extern ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t);
252__errordecl(__readlinkat_dest_size_error, "readlinkat called with size bigger than destination");
253__errordecl(__readlinkat_size_toobig_error, "readlinkat called with size > SSIZE_MAX");
254extern ssize_t __readlinkat_real(int dirfd, const char*, char*, size_t) __RENAME(readlinkat);
255
Dan Albert658727e2014-10-07 11:10:36 -0700256#if defined(__BIONIC_FORTIFY)
257
Daniel Micay9101b002015-05-20 15:31:26 -0400258__BIONIC_FORTIFY_INLINE
259char* getcwd(char* buf, size_t size) {
260 size_t bos = __bos(buf);
261
262#if defined(__clang__)
263 /*
264 * Work around LLVM's incorrect __builtin_object_size implementation here
265 * to avoid needing the workaround in the __getcwd_chk ABI forever.
266 *
267 * https://llvm.org/bugs/show_bug.cgi?id=23277
268 */
269 if (buf == NULL) {
270 bos = __BIONIC_FORTIFY_UNKNOWN_SIZE;
271 }
272#else
273 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
274 return __getcwd_real(buf, size);
275 }
276
277 if (__builtin_constant_p(size) && (size > bos)) {
278 __getcwd_dest_size_error();
279 }
280
281 if (__builtin_constant_p(size) && (size <= bos)) {
282 return __getcwd_real(buf, size);
283 }
284#endif
285
286 return __getcwd_chk(buf, size, bos);
287}
288
Daniel Micaye7e1c872015-04-16 09:07:45 -0400289#if defined(__USE_FILE_OFFSET64)
290#define __PREAD_PREFIX(x) __pread64_ ## x
291#else
292#define __PREAD_PREFIX(x) __pread_ ## x
293#endif
294
295__BIONIC_FORTIFY_INLINE
296ssize_t pread(int fd, void* buf, size_t count, off_t offset) {
297 size_t bos = __bos0(buf);
298
299#if !defined(__clang__)
300 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
301 __PREAD_PREFIX(count_toobig_error)();
302 }
303
304 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
305 return __PREAD_PREFIX(real)(fd, buf, count, offset);
306 }
307
308 if (__builtin_constant_p(count) && (count > bos)) {
309 __PREAD_PREFIX(dest_size_error)();
310 }
311
312 if (__builtin_constant_p(count) && (count <= bos)) {
313 return __PREAD_PREFIX(real)(fd, buf, count, offset);
314 }
315#endif
316
317 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
318}
319
320__BIONIC_FORTIFY_INLINE
321ssize_t pread64(int fd, void* buf, size_t count, off64_t offset) {
322 size_t bos = __bos0(buf);
323
324#if !defined(__clang__)
325 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
326 __pread64_count_toobig_error();
327 }
328
329 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
330 return __pread64_real(fd, buf, count, offset);
331 }
332
333 if (__builtin_constant_p(count) && (count > bos)) {
334 __pread64_dest_size_error();
335 }
336
337 if (__builtin_constant_p(count) && (count <= bos)) {
338 return __pread64_real(fd, buf, count, offset);
339 }
340#endif
341
342 return __pread64_chk(fd, buf, count, offset, bos);
343}
344
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700345__BIONIC_FORTIFY_INLINE
346ssize_t read(int fd, void* buf, size_t count) {
347 size_t bos = __bos0(buf);
348
349#if !defined(__clang__)
350 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
351 __read_count_toobig_error();
352 }
353
354 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
355 return __read_real(fd, buf, count);
356 }
357
358 if (__builtin_constant_p(count) && (count > bos)) {
359 __read_dest_size_error();
360 }
361
362 if (__builtin_constant_p(count) && (count <= bos)) {
363 return __read_real(fd, buf, count);
364 }
365#endif
366
367 return __read_chk(fd, buf, count, bos);
368}
Daniel Micaye7e1c872015-04-16 09:07:45 -0400369
Daniel Micay42281882015-04-17 11:26:36 -0400370__BIONIC_FORTIFY_INLINE
371ssize_t readlink(const char* path, char* buf, size_t size) {
372 size_t bos = __bos(buf);
373
374#if !defined(__clang__)
375 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
376 __readlink_size_toobig_error();
377 }
378
379 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
380 return __readlink_real(path, buf, size);
381 }
382
383 if (__builtin_constant_p(size) && (size > bos)) {
384 __readlink_dest_size_error();
385 }
386
387 if (__builtin_constant_p(size) && (size <= bos)) {
388 return __readlink_real(path, buf, size);
389 }
390#endif
391
392 return __readlink_chk(path, buf, size, bos);
393}
394
395__BIONIC_FORTIFY_INLINE
396ssize_t readlinkat(int dirfd, const char* path, char* buf, size_t size) {
397 size_t bos = __bos(buf);
398
399#if !defined(__clang__)
400 if (__builtin_constant_p(size) && (size > SSIZE_MAX)) {
401 __readlinkat_size_toobig_error();
402 }
403
404 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
405 return __readlinkat_real(dirfd, path, buf, size);
406 }
407
408 if (__builtin_constant_p(size) && (size > bos)) {
409 __readlinkat_dest_size_error();
410 }
411
412 if (__builtin_constant_p(size) && (size <= bos)) {
413 return __readlinkat_real(dirfd, path, buf, size);
414 }
415#endif
416
417 return __readlinkat_chk(dirfd, path, buf, size, bos);
418}
419
Nick Kralevichb036b5c2013-10-09 20:16:34 -0700420#endif /* defined(__BIONIC_FORTIFY) */
421
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422__END_DECLS
423
424#endif /* _UNISTD_H_ */