• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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* _Nullable * _Nullable 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* _Nonnull __path, char* _Nullable const* _Nullable __argv);
93 int execvp(const char* _Nonnull __file, char* _Nullable const* _Nullable __argv);
94 int execvpe(const char* _Nonnull __file, char* _Nullable const* _Nullable __argv, char* _Nullable const* _Nullable __envp) __INTRODUCED_IN(21);
95 int execve(const char* _Nonnull __file, char* _Nullable const* _Nullable __argv, char* _Nullable const* _Nullable __envp);
96 int execl(const char* _Nonnull __path, const char* _Nullable __arg0, ...) __attribute__((__sentinel__));
97 int execlp(const char* _Nonnull __file, const char* _Nullable __arg0, ...) __attribute__((__sentinel__));
98 int execle(const char* _Nonnull __path, const char* _Nullable __arg0, ... /*,  char* const* __envp */)
99     __attribute__((__sentinel__(1)));
100 int fexecve(int __fd, char* _Nullable const* _Nullable __argv, char* _Nullable const* _Nullable __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* _Nullable __list);
197 int setgroups(size_t __size, const gid_t* _Nullable __list);
198 int getresuid(uid_t* _Nonnull __ruid, uid_t* _Nonnull __euid, uid_t* _Nonnull __suid);
199 int getresgid(gid_t* _Nonnull __rgid, gid_t* _Nonnull __egid, gid_t* _Nonnull __sgid);
200 char* _Nullable getlogin(void);
201 int getlogin_r(char* _Nonnull __buffer, size_t __buffer_size) __INTRODUCED_IN(28);
202 
203 long fpathconf(int __fd, int __name);
204 long pathconf(const char* _Nonnull __path, int __name);
205 
206 int access(const char* _Nonnull __path, int __mode);
207 int faccessat(int __dirfd, const char* _Nonnull __path, int __mode, int __flags);
208 int link(const char* _Nonnull __old_path, const char* _Nonnull __new_path);
209 int linkat(int __old_dir_fd, const char* _Nonnull __old_path, int __new_dir_fd, const char* _Nonnull __new_path, int __flags) __INTRODUCED_IN(21);
210 int unlink(const char* _Nonnull __path);
211 int unlinkat(int __dirfd, const char* _Nonnull __path, int __flags);
212 int chdir(const char* _Nonnull __path);
213 int fchdir(int __fd);
214 int rmdir(const char* _Nonnull __path);
215 int pipe(int __fds[_Nonnull 2]);
216 #if defined(__USE_GNU)
217 int pipe2(int __fds[_Nonnull 2], int __flags);
218 #endif
219 int chroot(const char* _Nonnull __path);
220 int symlink(const char* _Nonnull __old_path, const char* _Nonnull __new_path);
221 int symlinkat(const char* _Nonnull __old_path, int __new_dir_fd, const char* _Nonnull __new_path) __INTRODUCED_IN(21);
222 ssize_t readlink(const char* _Nonnull __path, char* _Nonnull __buf, size_t __buf_size);
223 ssize_t readlinkat(int __dir_fd, const char* _Nonnull __path, char* _Nonnull __buf, size_t __buf_size)
224     __INTRODUCED_IN(21);
225 int chown(const char* _Nonnull __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* _Nonnull __path, uid_t __owner, gid_t __group, int __flags);
228 int lchown(const char* _Nonnull __path, uid_t __owner, gid_t __group);
229 char* _Nullable getcwd(char* _Nullable __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 /**
239  * [read(2)](https://man7.org/linux/man-pages/man2/read.2.html) reads
240  * up to `__count` bytes from file descriptor `__fd` into `__buf`.
241  *
242  * Note: `__buf` is not normally nullable, but may be null in the
243  * special case of a zero-length read(), which while not generally
244  * useful may be meaningful to some device drivers.
245  *
246  * Returns the number of bytes read on success, and returns -1 and sets `errno` on failure.
247  */
248 ssize_t read(int __fd, void* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __count);
249 
250 /**
251  * [write(2)](https://man7.org/linux/man-pages/man2/write.2.html) writes
252  * up to `__count` bytes to file descriptor `__fd` from `__buf`.
253  *
254  * Note: `__buf` is not normally nullable, but may be null in the
255  * special case of a zero-length write(), which while not generally
256  * useful may be meaningful to some device drivers.
257  *
258  * Returns the number of bytes written on success, and returns -1 and sets `errno` on failure.
259  */
260 ssize_t write(int __fd, const void* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __count);
261 
262 int dup(int __old_fd);
263 int dup2(int __old_fd, int __new_fd);
264 int dup3(int __old_fd, int __new_fd, int __flags) __INTRODUCED_IN(21);
265 int fsync(int __fd);
266 int fdatasync(int __fd);
267 
268 /* See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md */
269 #if defined(__USE_FILE_OFFSET64)
270 int truncate(const char* _Nonnull __path, off_t __length) __RENAME(truncate64) __INTRODUCED_IN(21);
271 off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64);
272 ssize_t pread(int __fd, void* _Nonnull __buf, size_t __count, off_t __offset) __RENAME(pread64);
273 ssize_t pwrite(int __fd, const void* _Nonnull __buf, size_t __count, off_t __offset) __RENAME(pwrite64);
274 int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64);
275 #else
276 int truncate(const char* _Nonnull __path, off_t __length);
277 off_t lseek(int __fd, off_t __offset, int __whence);
278 ssize_t pread(int __fd, void* _Nonnull __buf, size_t __count, off_t __offset);
279 ssize_t pwrite(int __fd, const void* _Nonnull __buf, size_t __count, off_t __offset);
280 int ftruncate(int __fd, off_t __length);
281 #endif
282 
283 int truncate64(const char* _Nonnull __path, off64_t __length) __INTRODUCED_IN(21);
284 off64_t lseek64(int __fd, off64_t __offset, int __whence);
285 ssize_t pread64(int __fd, void* _Nonnull __buf, size_t __count, off64_t __offset);
286 ssize_t pwrite64(int __fd, const void* _Nonnull __buf, size_t __count, off64_t __offset);
287 int ftruncate64(int __fd, off64_t __length);
288 
289 int pause(void);
290 unsigned int alarm(unsigned int __seconds);
291 unsigned int sleep(unsigned int __seconds);
292 int usleep(useconds_t __microseconds);
293 
294 int gethostname(char* _Nonnull _buf, size_t __buf_size);
295 int sethostname(const char* _Nonnull __name, size_t __n) __INTRODUCED_IN(23);
296 
297 int brk(void* _Nonnull __addr);
298 void* _Nullable sbrk(ptrdiff_t __increment);
299 
300 int isatty(int __fd);
301 char* _Nullable ttyname(int __fd);
302 int ttyname_r(int __fd, char* _Nonnull __buf, size_t __buf_size);
303 
304 int acct(const char* _Nullable __path);
305 
306 int getpagesize(void) __INTRODUCED_IN(21);
307 
308 long syscall(long __number, ...);
309 
310 int daemon(int __no_chdir, int __no_close);
311 
312 #if defined(__arm__)
313 int cacheflush(long __addr, long __nbytes, long __cache);
314     /* __attribute__((deprecated("use __builtin___clear_cache instead"))); */
315 #endif
316 
317 pid_t tcgetpgrp(int __fd);
318 int tcsetpgrp(int __fd, pid_t __pid);
319 
320 /* Used to retry syscalls that can return EINTR. */
321 #define TEMP_FAILURE_RETRY(exp) ({         \
322     __typeof__(exp) _rc;                   \
323     do {                                   \
324         _rc = (exp);                       \
325     } while (_rc == -1 && errno == EINTR); \
326     _rc; })
327 
328 int getdomainname(char* _Nonnull __buf, size_t __buf_size) __INTRODUCED_IN(26);
329 int setdomainname(const char* _Nonnull __name, size_t __n) __INTRODUCED_IN(26);
330 
331 /**
332  * [copy_file_range(2)](https://man7.org/linux/man-pages/man2/copy_file_range.2.html) copies
333  * a range of data from one file descriptor to another.
334  *
335  * Available since API level 34.
336  *
337  * Returns the number of bytes copied on success, and returns -1 and sets
338  * `errno` on failure.
339  */
340 ssize_t copy_file_range(int __fd_in, off64_t* _Nullable __off_in, int __fd_out, off64_t* _Nullable __off_out, size_t __length, unsigned int __flags) __INTRODUCED_IN(34);
341 
342 #if __ANDROID_API__ >= 28
343 void swab(const void* _Nonnull __src, void* _Nonnull __dst, ssize_t __byte_count) __INTRODUCED_IN(28);
344 #endif
345 
346 /**
347  * [close_range(2)](https://man7.org/linux/man-pages/man2/close_range.2.html)
348  * performs an action (which depends on value of flags) on an inclusive range
349  * of file descriptors.
350  *
351  * Available since API level 34.
352  *
353  * Note: there is no emulation on too old kernels, hence this will fail with
354  * -1/ENOSYS on pre-5.9 kernels, -1/EINVAL for unsupported flags.  In particular
355  * CLOSE_RANGE_CLOEXEC requires 5.11, though support was backported to Android
356  * Common Kernel 5.10-T.
357  *
358  * Returns 0 on success, and returns -1 and sets `errno` on failure.
359  */
360 int close_range(unsigned int __min_fd, unsigned int __max_fd, int __flags) __INTRODUCED_IN(34);
361 
362 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
363 #define _UNISTD_H_
364 #include <bits/fortify/unistd.h>
365 #undef _UNISTD_H_
366 #endif
367 
368 __END_DECLS
369 
370 #include <android/legacy_unistd_inlines.h>
371