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/getentropy.h> 38 #include <bits/getopt.h> 39 #include <bits/ioctl.h> 40 #include <bits/lockf.h> 41 #include <bits/posix_limits.h> 42 #include <bits/seek_constants.h> 43 #include <bits/sysconf.h> 44 45 __BEGIN_DECLS 46 47 #define STDIN_FILENO 0 48 #define STDOUT_FILENO 1 49 #define STDERR_FILENO 2 50 51 #define F_OK 0 52 #define X_OK 1 53 #define W_OK 2 54 #define R_OK 4 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* _Nullable * _Nullable environ; 78 79 __noreturn void _exit(int __status); 80 81 /** 82 * [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html) creates a new 83 * process. fork() runs any handlers set by pthread_atfork(). 84 * 85 * Returns 0 in the child, the pid of the child in the parent, 86 * and returns -1 and sets `errno` on failure. 87 */ 88 pid_t fork(void); 89 90 /** 91 * _Fork() creates a new process. _Fork() differs from fork() in that it does 92 * not run any handlers set by pthread_atfork(). In addition to any user-defined 93 * ones, bionic uses pthread_atfork() handlers to ensure consistency of its own 94 * state, so the child should only call 95 * [POSIX async-safe](https://man7.org/linux/man-pages/man7/signal-safety.7.html) 96 * functions. 97 * 98 * Returns 0 in the child, the pid of the child in the parent, 99 * and returns -1 and sets `errno` on failure. 100 * 101 * Available since API level 35. 102 */ 103 pid_t _Fork(void) __INTRODUCED_IN(35); 104 105 /** 106 * [vfork(2)](http://man7.org/linux/man-pages/man2/vfork.2.html) creates a new 107 * process. vfork() differs from fork() in that it does not run any handlers 108 * set by pthread_atfork(), and the parent is suspended until the child calls 109 * exec() or exits. 110 * 111 * Returns 0 in the child, the pid of the child in the parent, 112 * and returns -1 and sets `errno` on failure. 113 */ 114 pid_t vfork(void) __returns_twice; 115 116 /** 117 * [getpid(2)](http://man7.org/linux/man-pages/man2/getpid.2.html) returns 118 * the caller's process ID. 119 * 120 * Returns the caller's process ID. 121 */ 122 pid_t getpid(void); 123 124 /** 125 * [gettid(2)](http://man7.org/linux/man-pages/man2/gettid.2.html) returns 126 * the caller's thread ID. 127 * 128 * Returns the caller's thread ID. 129 */ 130 pid_t gettid(void); 131 132 pid_t getpgid(pid_t __pid); 133 int setpgid(pid_t __pid, pid_t __pgid); 134 pid_t getppid(void); 135 pid_t getpgrp(void); 136 int setpgrp(void); 137 pid_t getsid(pid_t __pid); 138 pid_t setsid(void); 139 140 int execv(const char* _Nonnull __path, char* _Nullable const* _Nullable __argv); 141 int execvp(const char* _Nonnull __file, char* _Nullable const* _Nullable __argv); 142 int execvpe(const char* _Nonnull __file, char* _Nullable const* _Nullable __argv, char* _Nullable const* _Nullable __envp); 143 int execve(const char* _Nonnull __file, char* _Nullable const* _Nullable __argv, char* _Nullable const* _Nullable __envp); 144 int execl(const char* _Nonnull __path, const char* _Nullable __arg0, ...) __attribute__((__sentinel__)); 145 int execlp(const char* _Nonnull __file, const char* _Nullable __arg0, ...) __attribute__((__sentinel__)); 146 int execle(const char* _Nonnull __path, const char* _Nullable __arg0, ... /*, char* const* __envp */) 147 __attribute__((__sentinel__(1))); 148 int fexecve(int __fd, char* _Nullable const* _Nullable __argv, char* _Nullable const* _Nullable __envp) __INTRODUCED_IN(28); 149 150 int nice(int __incr); 151 152 /** 153 * [setegid(2)](http://man7.org/linux/man-pages/man2/setegid.2.html) sets 154 * the effective group ID. 155 * 156 * On Android, this function only affects the calling thread, not all threads 157 * in the process. 158 * 159 * Returns 0 on success, and returns -1 and sets `errno` on failure. 160 */ 161 int setegid(gid_t __gid); 162 163 /** 164 * [seteuid(2)](http://man7.org/linux/man-pages/man2/seteuid.2.html) sets 165 * the effective user ID. 166 * 167 * On Android, this function only affects the calling thread, not all threads 168 * in the process. 169 * 170 * Returns 0 on success, and returns -1 and sets `errno` on failure. 171 */ 172 int seteuid(uid_t __uid); 173 174 /** 175 * [setgid(2)](http://man7.org/linux/man-pages/man2/setgid.2.html) sets 176 * the group ID. 177 * 178 * On Android, this function only affects the calling thread, not all threads 179 * in the process. 180 * 181 * Returns 0 on success, and returns -1 and sets `errno` on failure. 182 */ 183 int setgid(gid_t __gid); 184 185 /** 186 * [setregid(2)](http://man7.org/linux/man-pages/man2/setregid.2.html) sets 187 * the real and effective group IDs (use -1 to leave an ID unchanged). 188 * 189 * On Android, this function only affects the calling thread, not all threads 190 * in the process. 191 * 192 * Returns 0 on success, and returns -1 and sets `errno` on failure. 193 */ 194 int setregid(gid_t __rgid, gid_t __egid); 195 196 /** 197 * [setresgid(2)](http://man7.org/linux/man-pages/man2/setresgid.2.html) sets 198 * the real, effective, and saved group IDs (use -1 to leave an ID unchanged). 199 * 200 * On Android, this function only affects the calling thread, not all threads 201 * in the process. 202 * 203 * Returns 0 on success, and returns -1 and sets `errno` on failure. 204 */ 205 int setresgid(gid_t __rgid, gid_t __egid, gid_t __sgid); 206 207 /** 208 * [setresuid(2)](http://man7.org/linux/man-pages/man2/setresuid.2.html) sets 209 * the real, effective, and saved user IDs (use -1 to leave an ID unchanged). 210 * 211 * On Android, this function only affects the calling thread, not all threads 212 * in the process. 213 * 214 * Returns 0 on success, and returns -1 and sets `errno` on failure. 215 */ 216 int setresuid(uid_t __ruid, uid_t __euid, uid_t __suid); 217 218 /** 219 * [setreuid(2)](http://man7.org/linux/man-pages/man2/setreuid.2.html) sets 220 * the real and effective group IDs (use -1 to leave an ID unchanged). 221 * 222 * On Android, this function only affects the calling thread, not all threads 223 * in the process. 224 * 225 * Returns 0 on success, and returns -1 and sets `errno` on failure. 226 */ 227 int setreuid(uid_t __ruid, uid_t __euid); 228 229 /** 230 * [setuid(2)](http://man7.org/linux/man-pages/man2/setuid.2.html) sets 231 * the user ID. 232 * 233 * On Android, this function only affects the calling thread, not all threads 234 * in the process. 235 * 236 * Returns 0 on success, and returns -1 and sets `errno` on failure. 237 */ 238 int setuid(uid_t __uid); 239 240 uid_t getuid(void); 241 uid_t geteuid(void); 242 gid_t getgid(void); 243 gid_t getegid(void); 244 int getgroups(int __size, gid_t* _Nullable __list); 245 int setgroups(size_t __size, const gid_t* _Nullable __list); 246 int getresuid(uid_t* _Nonnull __ruid, uid_t* _Nonnull __euid, uid_t* _Nonnull __suid); 247 int getresgid(gid_t* _Nonnull __rgid, gid_t* _Nonnull __egid, gid_t* _Nonnull __sgid); 248 char* _Nullable getlogin(void); 249 int getlogin_r(char* _Nonnull __buffer, size_t __buffer_size) __INTRODUCED_IN(28); 250 251 long fpathconf(int __fd, int __name); 252 long pathconf(const char* _Nonnull __path, int __name); 253 254 int access(const char* _Nonnull __path, int __mode); 255 int faccessat(int __dirfd, const char* _Nonnull __path, int __mode, int __flags); 256 int link(const char* _Nonnull __old_path, const char* _Nonnull __new_path); 257 int linkat(int __old_dir_fd, const char* _Nonnull __old_path, int __new_dir_fd, const char* _Nonnull __new_path, int __flags); 258 int unlink(const char* _Nonnull __path); 259 int unlinkat(int __dirfd, const char* _Nonnull __path, int __flags); 260 int chdir(const char* _Nonnull __path); 261 int fchdir(int __fd); 262 int rmdir(const char* _Nonnull __path); 263 int pipe(int __fds[_Nonnull 2]); 264 #if defined(__USE_GNU) 265 int pipe2(int __fds[_Nonnull 2], int __flags); 266 #endif 267 int chroot(const char* _Nonnull __path); 268 int symlink(const char* _Nonnull __old_path, const char* _Nonnull __new_path); 269 int symlinkat(const char* _Nonnull __old_path, int __new_dir_fd, const char* _Nonnull __new_path); 270 ssize_t readlink(const char* _Nonnull __path, char* _Nonnull __buf, size_t __buf_size); 271 ssize_t readlinkat(int __dir_fd, const char* _Nonnull __path, char* _Nonnull __buf, size_t __buf_size); 272 int chown(const char* _Nonnull __path, uid_t __owner, gid_t __group); 273 int fchown(int __fd, uid_t __owner, gid_t __group); 274 int fchownat(int __dir_fd, const char* _Nonnull __path, uid_t __owner, gid_t __group, int __flags); 275 int lchown(const char* _Nonnull __path, uid_t __owner, gid_t __group); 276 char* _Nullable getcwd(char* _Nullable __buf, size_t __size); 277 278 void sync(void); 279 #if defined(__USE_GNU) 280 int syncfs(int __fd) __INTRODUCED_IN(28); 281 #endif 282 283 int close(int __fd); 284 285 /** 286 * [read(2)](https://man7.org/linux/man-pages/man2/read.2.html) reads 287 * up to `__count` bytes from file descriptor `__fd` into `__buf`. 288 * 289 * Note: `__buf` is not normally nullable, but may be null in the 290 * special case of a zero-length read(), which while not generally 291 * useful may be meaningful to some device drivers. 292 * 293 * Returns the number of bytes read on success, and returns -1 and sets `errno` on failure. 294 */ 295 ssize_t read(int __fd, void* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __count); 296 297 /** 298 * [write(2)](https://man7.org/linux/man-pages/man2/write.2.html) writes 299 * up to `__count` bytes to file descriptor `__fd` from `__buf`. 300 * 301 * Note: `__buf` is not normally nullable, but may be null in the 302 * special case of a zero-length write(), which while not generally 303 * useful may be meaningful to some device drivers. 304 * 305 * Returns the number of bytes written on success, and returns -1 and sets `errno` on failure. 306 */ 307 ssize_t write(int __fd, const void* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __count); 308 309 int dup(int __old_fd); 310 int dup2(int __old_fd, int __new_fd); 311 int dup3(int __old_fd, int __new_fd, int __flags); 312 int fsync(int __fd); 313 int fdatasync(int __fd); 314 315 /* See https://android.googlesource.com/platform/bionic/+/main/docs/32-bit-abi.md */ 316 #if defined(__USE_FILE_OFFSET64) 317 int truncate(const char* _Nonnull __path, off_t __length) __RENAME(truncate64); 318 off_t lseek(int __fd, off_t __offset, int __whence) __RENAME(lseek64); 319 ssize_t pread(int __fd, void* _Nonnull __buf, size_t __count, off_t __offset) __RENAME(pread64); 320 ssize_t pwrite(int __fd, const void* _Nonnull __buf, size_t __count, off_t __offset) __RENAME(pwrite64); 321 int ftruncate(int __fd, off_t __length) __RENAME(ftruncate64); 322 #else 323 int truncate(const char* _Nonnull __path, off_t __length); 324 off_t lseek(int __fd, off_t __offset, int __whence); 325 ssize_t pread(int __fd, void* _Nonnull __buf, size_t __count, off_t __offset); 326 ssize_t pwrite(int __fd, const void* _Nonnull __buf, size_t __count, off_t __offset); 327 int ftruncate(int __fd, off_t __length); 328 #endif 329 330 int truncate64(const char* _Nonnull __path, off64_t __length); 331 off64_t lseek64(int __fd, off64_t __offset, int __whence); 332 ssize_t pread64(int __fd, void* _Nonnull __buf, size_t __count, off64_t __offset); 333 ssize_t pwrite64(int __fd, const void* _Nonnull __buf, size_t __count, off64_t __offset); 334 int ftruncate64(int __fd, off64_t __length); 335 336 int pause(void); 337 unsigned int alarm(unsigned int __seconds); 338 unsigned int sleep(unsigned int __seconds); 339 int usleep(useconds_t __microseconds); 340 341 int gethostname(char* _Nonnull _buf, size_t __buf_size); 342 int sethostname(const char* _Nonnull __name, size_t __n) __INTRODUCED_IN(23); 343 344 int brk(void* _Nonnull __addr); 345 void* _Nullable sbrk(ptrdiff_t __increment); 346 347 int isatty(int __fd); 348 char* _Nullable ttyname(int __fd); 349 int ttyname_r(int __fd, char* _Nonnull __buf, size_t __buf_size); 350 351 int acct(const char* _Nullable __path); 352 353 /** 354 * [getpagesize(2)](https://man7.org/linux/man-pages/man2/getpagesize.2.html) 355 * returns the system's page size. This is slightly faster than going via 356 * sysconf(), and avoids the linear search in getauxval(). 357 * 358 * Returns the system's page size in bytes. 359 */ 360 int getpagesize(void) __attribute_const__; 361 362 long syscall(long __number, ...); 363 364 int daemon(int __no_chdir, int __no_close); 365 366 #if defined(__arm__) 367 /** 368 * New code should use __builtin___clear_cache() instead, which works on 369 * all architectures. 370 */ 371 int cacheflush(long __addr, long __nbytes, long __cache); 372 #endif 373 374 pid_t tcgetpgrp(int __fd); 375 int tcsetpgrp(int __fd, pid_t __pid); 376 377 /* Used to retry syscalls that can return EINTR. */ 378 #define TEMP_FAILURE_RETRY(exp) ({ \ 379 __typeof__(exp) _rc; \ 380 do { \ 381 _rc = (exp); \ 382 } while (_rc == -1 && errno == EINTR); \ 383 _rc; }) 384 385 int getdomainname(char* _Nonnull __buf, size_t __buf_size) __INTRODUCED_IN(26); 386 int setdomainname(const char* _Nonnull __name, size_t __n) __INTRODUCED_IN(26); 387 388 /** 389 * [copy_file_range(2)](https://man7.org/linux/man-pages/man2/copy_file_range.2.html) copies 390 * a range of data from one file descriptor to another. 391 * 392 * Available since API level 34. 393 * 394 * Returns the number of bytes copied on success, and returns -1 and sets 395 * `errno` on failure. 396 */ 397 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); 398 399 #if __ANDROID_API__ >= 28 400 void swab(const void* _Nonnull __src, void* _Nonnull __dst, ssize_t __byte_count) __INTRODUCED_IN(28); 401 #endif 402 403 /** 404 * [close_range(2)](https://man7.org/linux/man-pages/man2/close_range.2.html) 405 * performs an action (which depends on value of flags) on an inclusive range 406 * of file descriptors. 407 * 408 * Available since API level 34. 409 * 410 * Note: there is no emulation on too old kernels, hence this will fail with 411 * -1/ENOSYS on pre-5.9 kernels, -1/EINVAL for unsupported flags. In particular 412 * CLOSE_RANGE_CLOEXEC requires 5.11, though support was backported to Android 413 * Common Kernel 5.10-T. 414 * 415 * Returns 0 on success, and returns -1 and sets `errno` on failure. 416 */ 417 int close_range(unsigned int __min_fd, unsigned int __max_fd, int __flags) __INTRODUCED_IN(34); 418 419 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS) 420 #define _UNISTD_H_ 421 #include <bits/fortify/unistd.h> 422 #undef _UNISTD_H_ 423 #endif 424 425 __END_DECLS 426 427 #include <android/legacy_unistd_inlines.h> 428