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 #ifndef _UNISTD_H_
29 #define _UNISTD_H_
30
31 #include <stddef.h>
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/select.h>
35 #include <sys/sysconf.h>
36 #include <pathconf.h>
37
38 __BEGIN_DECLS
39
40 /* Standard file descriptor numbers. */
41 #define STDIN_FILENO 0
42 #define STDOUT_FILENO 1
43 #define STDERR_FILENO 2
44
45 /* Values for whence in fseek and lseek */
46 #define SEEK_SET 0
47 #define SEEK_CUR 1
48 #define SEEK_END 2
49
50 extern char** environ;
51
52 extern __noreturn void _exit(int);
53
54 extern pid_t fork(void);
55 extern pid_t vfork(void);
56 extern pid_t getpid(void);
57 extern pid_t gettid(void) __pure2;
58 extern pid_t getpgid(pid_t);
59 extern int setpgid(pid_t, pid_t);
60 extern pid_t getppid(void);
61 extern pid_t getpgrp(void);
62 extern int setpgrp(void);
63 extern pid_t getsid(pid_t);
64 extern pid_t setsid(void);
65
66 extern int execv(const char *, char * const *);
67 extern int execvp(const char *, char * const *);
68 extern int execvpe(const char *, char * const *, char * const *);
69 extern int execve(const char *, char * const *, char * const *);
70 extern int execl(const char *, const char *, ...);
71 extern int execlp(const char *, const char *, ...);
72 extern int execle(const char *, const char *, ...);
73
74 extern int nice(int);
75
76 extern int setuid(uid_t);
77 extern uid_t getuid(void);
78 extern int seteuid(uid_t);
79 extern uid_t geteuid(void);
80 extern int setgid(gid_t);
81 extern gid_t getgid(void);
82 extern int setegid(gid_t);
83 extern gid_t getegid(void);
84 extern int getgroups(int, gid_t *);
85 extern int setgroups(size_t, const gid_t *);
86 extern int setreuid(uid_t, uid_t);
87 extern int setregid(gid_t, gid_t);
88 extern int setresuid(uid_t, uid_t, uid_t);
89 extern int setresgid(gid_t, gid_t, gid_t);
90 extern int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
91 extern int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
92 extern char* getlogin(void);
93
94 /* Macros for access() */
95 #define R_OK 4 /* Read */
96 #define W_OK 2 /* Write */
97 #define X_OK 1 /* Execute */
98 #define F_OK 0 /* Existence */
99
100 extern int access(const char*, int);
101 extern int faccessat(int, const char*, int, int);
102 extern int link(const char*, const char*);
103 extern int linkat(int, const char*, int, const char*, int);
104 extern int unlink(const char*);
105 extern int unlinkat(int, const char*, int);
106 extern int chdir(const char *);
107 extern int fchdir(int);
108 extern int rmdir(const char *);
109 extern int pipe(int *);
110 #ifdef _GNU_SOURCE
111 extern int pipe2(int *, int);
112 #endif
113 extern int chroot(const char *);
114 extern int symlink(const char*, const char*);
115 extern int symlinkat(const char*, int, const char*);
116 extern ssize_t readlink(const char*, char*, size_t);
117 extern ssize_t readlinkat(int, const char*, char*, size_t);
118 extern int chown(const char *, uid_t, gid_t);
119 extern int fchown(int, uid_t, gid_t);
120 extern int fchownat(int, const char*, uid_t, gid_t, int);
121 extern int lchown(const char *, uid_t, gid_t);
122 extern int truncate(const char *, off_t);
123 extern int truncate64(const char *, off64_t);
124 extern char *getcwd(char *, size_t);
125
126 extern int sync(void);
127
128 extern int close(int);
129 extern off_t lseek(int, off_t, int);
130 extern off64_t lseek64(int, off64_t, int);
131
132 extern ssize_t read(int, void *, size_t);
133 extern ssize_t write(int, const void *, size_t);
134 extern ssize_t pread(int, void *, size_t, off_t);
135 extern ssize_t pread64(int, void *, size_t, off64_t);
136 extern ssize_t pwrite(int, const void *, size_t, off_t);
137 extern ssize_t pwrite64(int, const void *, size_t, off64_t);
138
139 extern int dup(int);
140 extern int dup2(int, int);
141 #ifdef _GNU_SOURCE
142 extern int dup3(int, int, int);
143 #endif
144 extern int fcntl(int, int, ...);
145 extern int ioctl(int, int, ...);
146 extern int flock(int, int);
147 extern int fsync(int);
148 extern int fdatasync(int);
149 extern int ftruncate(int, off_t);
150 extern int ftruncate64(int, off64_t);
151
152 extern int pause(void);
153 extern unsigned int alarm(unsigned int);
154 extern unsigned int sleep(unsigned int);
155 extern int usleep(useconds_t);
156
157 extern int gethostname(char *, size_t);
158
159 extern void *__brk(void *);
160 extern int brk(void *);
161 extern void *sbrk(ptrdiff_t);
162
163 extern int getopt(int, char * const *, const char *);
164 extern char *optarg;
165 extern int optind, opterr, optopt;
166
167 extern int isatty(int);
168 extern char* ttyname(int) __warnattr("ttyname is not thread-safe; use ttyname_r instead");
169 extern int ttyname_r(int, char*, size_t);
170
171 extern int acct(const char* filepath);
172
173 int getpagesize(void);
174
175 long sysconf(int);
176
177 extern int daemon(int, int);
178
179 #if defined(__arm__) || (defined(__mips__) && !defined(__LP64__))
180 extern int cacheflush(long, long, long);
181 /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
182 #endif
183
184 extern pid_t tcgetpgrp(int fd);
185 extern int tcsetpgrp(int fd, pid_t _pid);
186
187 /* Used to retry syscalls that can return EINTR. */
188 #define TEMP_FAILURE_RETRY(exp) ({ \
189 __typeof__(exp) _rc; \
190 do { \
191 _rc = (exp); \
192 } while (_rc == -1 && errno == EINTR); \
193 _rc; })
194
195 #if defined(__BIONIC_FORTIFY)
196 extern ssize_t __read_chk(int, void*, size_t, size_t);
197 __errordecl(__read_dest_size_error, "read called with size bigger than destination");
198 __errordecl(__read_count_toobig_error, "read called with count > SSIZE_MAX");
199 extern ssize_t __read_real(int, void*, size_t)
200 __asm__(__USER_LABEL_PREFIX__ "read");
201
202 __BIONIC_FORTIFY_INLINE
read(int fd,void * buf,size_t count)203 ssize_t read(int fd, void* buf, size_t count) {
204 size_t bos = __bos0(buf);
205
206 #if !defined(__clang__)
207 if (__builtin_constant_p(count) && (count > SSIZE_MAX)) {
208 __read_count_toobig_error();
209 }
210
211 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
212 return __read_real(fd, buf, count);
213 }
214
215 if (__builtin_constant_p(count) && (count > bos)) {
216 __read_dest_size_error();
217 }
218
219 if (__builtin_constant_p(count) && (count <= bos)) {
220 return __read_real(fd, buf, count);
221 }
222 #endif
223
224 return __read_chk(fd, buf, count, bos);
225 }
226 #endif /* defined(__BIONIC_FORTIFY) */
227
228 __END_DECLS
229
230 #endif /* _UNISTD_H_ */
231