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