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 #pragma once
30 
31 #include <stddef.h>
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/select.h>
35 
36 #include <bits/fcntl.h>
37 #include <bits/getopt.h>
38 #include <bits/ioctl.h>
39 #include <bits/lockf.h>
40 #include <bits/posix_limits.h>
41 #include <bits/seek_constants.h>
42 #include <bits/sysconf.h>
43 
44 __BEGIN_DECLS
45 
46 #define STDIN_FILENO	0
47 #define STDOUT_FILENO	1
48 #define STDERR_FILENO	2
49 
50 #define F_OK 0
51 #define X_OK 1
52 #define W_OK 2
53 #define R_OK 4
54 
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 
76 extern char** environ;
77 
78 __noreturn void _exit(int __status);
79 
80 pid_t  fork(void);
81 pid_t  vfork(void) __returns_twice;
82 pid_t  getpid(void);
83 pid_t  gettid(void) __attribute_const__;
84 pid_t  getpgid(pid_t __pid);
85 int    setpgid(pid_t __pid, pid_t __pgid);
86 pid_t  getppid(void);
87 pid_t  getpgrp(void);
88 int    setpgrp(void);
89 pid_t  getsid(pid_t __pid) __INTRODUCED_IN(17);
90 pid_t  setsid(void);
91 
92 int execv(const char* __path, char* const* __argv);
93 int execvp(const char* __file, char* const* __argv);
94 int execvpe(const char* __file, char* const* __argv, char* const* __envp) __INTRODUCED_IN(21);
95 int execve(const char* __file, char* const* __argv, char* const* __envp);
96 int execl(const char* __path, const char* __arg0, ...) __attribute__((__sentinel__));
97 int execlp(const char* __file, const char* __arg0, ...) __attribute__((__sentinel__));
98 int execle(const char* __path, const char* __arg0, ... /*,  char* const* __envp */)
99     __attribute__((__sentinel__(1)));
100 int fexecve(int __fd, char* const* __argv, char* const* __envp) __INTRODUCED_IN(28);
101 
102 int nice(int __incr);
103 
104 /**
105  * [setegid(2)](http://man7.org/linux/man-pages/man2/setegid.2.html) sets
106  * the effective group ID.
107  *
108  * On Android, this function only affects the calling thread, not all threads
109  * in the process.
110  *
111  * Returns 0 on success, and returns -1 and sets `errno` on failure.
112  */
113 int setegid(gid_t __gid);
114 
115 /**
116  * [seteuid(2)](http://man7.org/linux/man-pages/man2/seteuid.2.html) sets
117  * the effective user ID.
118  *
119  * On Android, this function only affects the calling thread, not all threads
120  * in the process.
121  *
122  * Returns 0 on success, and returns -1 and sets `errno` on failure.
123  */
124 int seteuid(uid_t __uid);
125 
126 /**
127  * [setgid(2)](http://man7.org/linux/man-pages/man2/setgid.2.html) sets
128  * the group ID.
129  *
130  * On Android, this function only affects the calling thread, not all threads
131  * in the process.
132  *
133  * Returns 0 on success, and returns -1 and sets `errno` on failure.
134  */
135 int setgid(gid_t __gid);
136 
137 /**
138  * [setregid(2)](http://man7.org/linux/man-pages/man2/setregid.2.html) sets
139  * the real and effective group IDs (use -1 to leave an ID unchanged).
140  *
141  * On Android, this function only affects the calling thread, not all threads
142  * in the process.
143  *
144  * Returns 0 on success, and returns -1 and sets `errno` on failure.
145  */
146 int setregid(gid_t __rgid, gid_t __egid);
147 
148 /**
149  * [setresgid(2)](http://man7.org/linux/man-pages/man2/setresgid.2.html) sets
150  * the real, effective, and saved group IDs (use -1 to leave an ID unchanged).
151  *
152  * On Android, this function only affects the calling thread, not all threads
153  * in the process.
154  *
155  * Returns 0 on success, and returns -1 and sets `errno` on failure.
156  */
157 int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid);
158 
159 /**
160  * [setresuid(2)](http://man7.org/linux/man-pages/man2/setresuid.2.html) sets
161  * the real, effective, and saved user IDs (use -1 to leave an ID unchanged).
162  *
163  * On Android, this function only affects the calling thread, not all threads
164  * in the process.
165  *
166  * Returns 0 on success, and returns -1 and sets `errno` on failure.
167  */
168 int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid);
169 
170 /**
171  * [setreuid(2)](http://man7.org/linux/man-pages/man2/setreuid.2.html) sets
172  * the real and effective group IDs (use -1 to leave an ID unchanged).
173  *
174  * On Android, this function only affects the calling thread, not all threads
175  * in the process.
176  *
177  * Returns 0 on success, and returns -1 and sets `errno` on failure.
178  */
179 int setreuid(uid_t __ruid, uid_t __euid);
180 
181 /**
182  * [setuid(2)](http://man7.org/linux/man-pages/man2/setuid.2.html) sets
183  * the user ID.
184  *
185  * On Android, this function only affects the calling thread, not all threads
186  * in the process.
187  *
188  * Returns 0 on success, and returns -1 and sets `errno` on failure.
189  */
190 int setuid(uid_t __uid);
191 
192 uid_t getuid(void);
193 uid_t geteuid(void);
194 gid_t getgid(void);
195 gid_t getegid(void);
196 int getgroups(int __size, gid_t* __list);
197 int setgroups(size_t __size, const gid_t* __list);
198 int getresuid(uid_t* __ruid, uid_t* __euid, uid_t* __suid);
199 int getresgid(gid_t* __rgid, gid_t* __egid, gid_t* __sgid);
200 char* getlogin(void);
201 int getlogin_r(char* __buffer, size_t __buffer_size) __INTRODUCED_IN(28);
202 
203 long fpathconf(int __fd, int __name);
204 long pathconf(const char* __path, int __name);
205 
206 int access(const char* __path, int __mode);
207 int faccessat(int __dirfd, const char* __path, int __mode, int __flags);
208 int link(const char* __old_path, const char* __new_path);
209 int linkat(int __old_dir_fd, const char* __old_path, int __new_dir_fd, const char* __new_path, int __flags) __INTRODUCED_IN(21);
210 int unlink(const char* __path);
211 int unlinkat(int __dirfd, const char* __path, int __flags);
212 int chdir(const char* __path);
213 int fchdir(int __fd);
214 int rmdir(const char* __path);
215 int pipe(int __fds[2]);
216 #if defined(__USE_GNU)
217 int pipe2(int __fds[2], int __flags);
218 #endif
219 int chroot(const char* __path);
220 int symlink(const char* __old_path, const char* __new_path);
221 int symlinkat(const char* __old_path, int __new_dir_fd, const char* __new_path) __INTRODUCED_IN(21);
222 ssize_t readlink(const char* __path, char* __buf, size_t __buf_size);
223 ssize_t readlinkat(int __dir_fd, const char* __path, char* __buf, size_t __buf_size)
224     __INTRODUCED_IN(21);
225 int chown(const char* __path, uid_t __owner, gid_t __group);
226 int fchown(int __fd, uid_t __owner, gid_t __group);
227 int fchownat(int __dir_fd, const char* __path, uid_t __owner, gid_t __group, int __flags);
228 int lchown(const char* __path, uid_t __owner, gid_t __group);
229 char* getcwd(char* __buf, size_t __size);
230 
231 void sync(void);
232 #if defined(__USE_GNU)
233 int syncfs(int __fd) __INTRODUCED_IN(28);
234 #endif
235 
236 int close(int __fd);
237 
238 ssize_t read(int __fd, void* __buf, size_t __count);
239 ssize_t write(int __fd, const void* __buf, size_t __count);
240 
241 int dup(int __old_fd);
242 int dup2(int __old_fd, int __new_fd);
243 int dup3(int __old_fd, int __new_fd, int __flags) __INTRODUCED_IN(21);
244 int fsync(int __fd);
245 int fdatasync(int __fd);
246 
247 /* See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md */
248 #if defined(__USE_FILE_OFFSET64)
249 int truncate(const char* __path, off_t __length) __RENAME(truncate64) __INTRODUCED_IN(21);
250 off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
251 ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset) __RENAME(pread64);
252 ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset) __RENAME(pwrite64);
253 int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64);
254 #else
255 int truncate(const char* __path, off_t __length);
256 off_t lseek(int __fd, off_t __offset, int __whence);
257 ssize_t pread(int __fd, void* __buf, size_t __count, off_t __offset);
258 ssize_t pwrite(int __fd, const void* __buf, size_t __count, off_t __offset);
259 int ftruncate(int __fd, off_t __length);
260 #endif
261 
262 int truncate64(const char* __path, off64_t __length) __INTRODUCED_IN(21);
263 off64_t lseek64(int __fd, off64_t __offset, int __whence);
264 ssize_t pread64(int __fd, void* __buf, size_t __count, off64_t __offset);
265 ssize_t pwrite64(int __fd, const void* __buf, size_t __count, off64_t __offset);
266 int ftruncate64(int __fd, off64_t __length);
267 
268 int pause(void);
269 unsigned int alarm(unsigned int __seconds);
270 unsigned int sleep(unsigned int __seconds);
271 int usleep(useconds_t __microseconds);
272 
273 int gethostname(char* __buf, size_t __buf_size);
274 int sethostname(const char* __name, size_t __n) __INTRODUCED_IN(23);
275 
276 int brk(void* __addr);
277 void* sbrk(ptrdiff_t __increment);
278 
279 int isatty(int __fd);
280 char* ttyname(int __fd);
281 int ttyname_r(int __fd, char* __buf, size_t __buf_size);
282 
283 int acct(const char* __path);
284 
285 #if __ANDROID_API__ >= 21
286 int getpagesize(void) __INTRODUCED_IN(21);
287 #else
getpagesize(void)288 static __inline__ int getpagesize(void) {
289   return sysconf(_SC_PAGESIZE);
290 }
291 #endif
292 
293 long syscall(long __number, ...);
294 
295 int daemon(int __no_chdir, int __no_close);
296 
297 #if defined(__arm__)
298 int cacheflush(long __addr, long __nbytes, long __cache);
299     /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
300 #endif
301 
302 pid_t tcgetpgrp(int __fd);
303 int tcsetpgrp(int __fd, pid_t __pid);
304 
305 /* Used to retry syscalls that can return EINTR. */
306 #define TEMP_FAILURE_RETRY(exp) ({         \
307     __typeof__(exp) _rc;                   \
308     do {                                   \
309         _rc = (exp);                       \
310     } while (_rc == -1 && errno == EINTR); \
311     _rc; })
312 
313 int getdomainname(char* __buf, size_t __buf_size) __INTRODUCED_IN(26);
314 int setdomainname(const char* __name, size_t __n) __INTRODUCED_IN(26);
315 
316 #if __ANDROID_API__ >= 28
317 void swab(const void* __src, void* __dst, ssize_t __byte_count) __INTRODUCED_IN(28);
318 #endif
319 
320 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
321 #define _UNISTD_H_
322 #include <bits/fortify/unistd.h>
323 #undef _UNISTD_H_
324 #endif
325 
326 __END_DECLS
327 
328 #include <android/legacy_unistd_inlines.h>
329