30#if __has_include("google/protobuf/io/coded_stream.h")
31#include "google/protobuf/io/coded_stream.h"
42#include <android-base/errors.h>
43#include <android-base/logging.h>
44#include <android-base/macros.h>
46#include <android-base/properties.h>
47#include <android-base/unique_fd.h>
48#include <android-base/utf8.h>
49#if __has_include(<print>)
59static inline void* mempcpy(
void* dst,
const void* src,
size_t n) {
60 return static_cast<char*
>(
memcpy(dst, src, n)) + n;
86#define OS_PATH_SEPARATORS "\\/"
87#define OS_PATH_SEPARATOR '\\'
88#define OS_PATH_SEPARATOR_STR "\\"
89#define ENV_PATH_SEPARATOR_STR ";"
92 return c ==
'\\' || c ==
'/';
103#define unlink ___xxx_unlink
105extern int adb_mkdir(
const std::string& path,
int mode);
107#define mkdir ___xxx_mkdir
109extern int adb_rename(
const char* oldpath,
const char* newpath);
112extern int adb_open(
const char* path,
int options);
113extern int adb_creat(
const char* path,
int mode);
114extern int adb_read(borrowed_fd fd,
void* buf,
int len);
116extern int adb_write(borrowed_fd fd,
const void* buf,
int len);
118extern int64_t
adb_lseek(borrowed_fd fd, int64_t pos,
int where);
119extern int adb_shutdown(borrowed_fd fd,
int direction = SHUT_RDWR);
132#define close ____xxx_close
138static inline int unix_read(borrowed_fd fd,
void* buf,
size_t len) {
143#define read ___xxx_read
146#define pread ___xxx_pread
149static inline int unix_write(borrowed_fd fd,
const void* buf,
size_t len) {
150 return write(fd.get(), buf, len);
153#define write ___xxx_write
156#define pwrite ___xxx_pwrite
159static inline int unix_lseek(borrowed_fd fd,
int pos,
int where) {
160 return lseek(fd.get(), pos, where);
163#define lseek ___xxx_lseek
166static inline int adb_open_mode(
const char* path,
int options,
int mode) {
171extern int unix_open(std::string_view path,
int options, ...);
172#define open ___xxx_unix_open
184#define isatty ___xxx_isatty
199extern int adb_socket_accept(borrowed_fd serverfd,
struct sockaddr* addr, socklen_t* addrlen);
202#define accept ___xxx_accept
204int adb_getsockname(borrowed_fd fd,
struct sockaddr* sockaddr, socklen_t* addrlen);
209extern int adb_setsockopt(borrowed_fd fd,
int level,
int optname,
const void* optval,
213#define setsockopt ___xxx_setsockopt
219extern int adb_bind(borrowed_fd fd,
const sockaddr* addr,
int namelen);
226 socklen_t msg_namelen;
230 size_t msg_controllen;
249#define poll ___xxx_poll
252 return isalpha(path[0]) && path[1] ==
':' && path[2] ==
'\\';
256extern DIR* adb_opendir(
const char* dirname);
257extern struct dirent* adb_readdir(DIR* dir);
258extern int adb_closedir(DIR* dir);
260extern int adb_utime(
const char *,
struct utimbuf *);
261extern int adb_chmod(
const char *,
int);
263extern int adb_vfprintf(FILE* stream,
const char*
format, va_list ap)
265extern int adb_vprintf(
const char*
format, va_list ap)
__attribute__((__format__(__printf__, 1, 0)));
266extern int adb_fprintf(FILE* stream,
const char*
format, ...)
268extern
int adb_printf(const
char*
format, ...)
__attribute__((__format__(__printf__, 1, 2)));
270extern
int adb_fputs(const
char* buf, FILE* stream);
271extern
int adb_fputc(
int ch, FILE* stream);
272extern
int adb_putchar(
int ch);
273extern
int adb_puts(const
char* buf);
274extern
size_t adb_fwrite(const
void* ptr,
size_t size,
size_t nmemb, FILE* stream);
276extern FILE* adb_fopen(const
char* f, const
char* m);
278extern
char* adb_getenv(const
char* name);
280extern
char* adb_getcwd(
char* buf,
int size);
283#define opendir adb_opendir
284#define readdir adb_readdir
285#define closedir adb_closedir
286#define rewinddir rewinddir_utf8_not_yet_implemented
287#define telldir telldir_utf8_not_yet_implemented
290inline void seekdir(DIR*,
long) {
291 extern int seekdir_utf8_not_yet_implemented;
292 seekdir_utf8_not_yet_implemented = 1;
295#define utime adb_utime
296#define chmod adb_chmod
298#define vfprintf adb_vfprintf
299#define vprintf adb_vprintf
300#define fprintf adb_fprintf
301#define printf adb_printf
302#define fputs adb_fputs
303#define fputc adb_fputc
306#define putc(c, s) adb_fputc(c, s)
307#define putchar adb_putchar
309#define fwrite adb_fwrite
311#define fopen adb_fopen
312#define freopen freopen_utf8_not_yet_implemented
314#define getenv adb_getenv
315#define putenv putenv_utf8_not_yet_implemented
316#define setenv setenv_utf8_not_yet_implemented
317#define unsetenv unsetenv_utf8_not_yet_implemented
319#define getcwd adb_getcwd
324 constexpr explicit Process(HANDLE h =
nullptr) : h_(h) {}
325 constexpr Process(
Process&& other) : h_(std::exchange(other.h_, nullptr)) {}
327 constexpr explicit operator bool()
const {
return h_ !=
nullptr; }
331 ::WaitForSingleObject(h_, INFINITE);
337 ::TerminateProcess(h_, -1);
355 std::initializer_list<int> fds_to_inherit = {});
361 NarrowArgs(
int argc,
wchar_t** argv);
364 inline char**
data() {
381 return static_cast<int>(
reinterpret_cast<INT_PTR
>(h));
384inline HANDLE cast_int_to_handle(
const int fd) {
386 return reinterpret_cast<HANDLE
>(
static_cast<INT_PTR
>(fd));
392class handle_deleter {
394 typedef HANDLE pointer;
396 void operator()(HANDLE h);
402typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
406size_t ParseCompleteUTF8(
const char* first,
const char* last, std::vector<char>* remaining_bytes);
414#include <netinet/in.h>
415#include <netinet/tcp.h>
428#include <cutils/sockets.h>
430#define OS_PATH_SEPARATORS "/"
431#define OS_PATH_SEPARATOR '/'
432#define OS_PATH_SEPARATOR_STR "/"
433#define ENV_PATH_SEPARATOR_STR ":"
440 return fcntl(fd.get(), F_GETFD);
445 if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
446 fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
459static inline int unix_open(std::string_view path,
int options, ...) {
460 std::string zero_terminated(path.begin(), path.end());
461 if ((options & O_CREAT) == 0) {
466 va_start(args, options);
467 mode = va_arg(args,
int);
475static inline int adb_open_mode(
const char* pathname,
int options,
int mode) {
486static inline int adb_open(
const char* pathname,
int options) {
488 if (fd < 0)
return -1;
493#define open ___xxx_open
495static inline int adb_shutdown(borrowed_fd fd,
int direction = SHUT_RDWR) {
496 return shutdown(fd.get(), direction);
500#define shutdown ____xxx_shutdown
509#define close ____xxx_close
519 return gethostname(name, len);
523 return getlogin_r(buf, bufsize);
526static inline int adb_read(borrowed_fd fd,
void* buf,
size_t len) {
531#if defined(__APPLE__)
540 return read(fd.get(), buf, len);
544#define read ___xxx_read
546#define pread ___xxx_pread
548static inline int adb_write(borrowed_fd fd,
const void* buf,
size_t len) {
553#if defined(__APPLE__)
561#define write ___xxx_write
563#define pwrite ___xxx_pwrite
565static inline int64_t
adb_lseek(borrowed_fd fd, int64_t pos,
int where) {
566#if defined(__APPLE__)
567 return lseek(fd.get(), pos, where);
569 return lseek64(fd.get(), pos, where);
573#define lseek ___xxx_lseek
579#define unlink ___xxx_unlink
581static inline int adb_creat(
const char* path,
int mode) {
584 if (fd < 0)
return -1;
590#define creat ___xxx_creat
595#define isatty ___xxx_isatty
600 *
error = strerror(errno);
613 return android::base::GetIntProperty(
"persist.adb.local_connect_timeout_ms", 500);
627 socklen_t* addrlen) {
637#define accept ___xxx_accept
639inline int adb_getsockname(borrowed_fd fd,
struct sockaddr* sockaddr, socklen_t* addrlen) {
640 return getsockname(fd.get(), sockaddr, addrlen);
644 return socket_get_local_port(fd.get());
655#define unix_read adb_read
656#define unix_write adb_write
657#define unix_lseek adb_lseek
658#define unix_close adb_close
662 return pthread_setname_np(name.c_str());
667 strncpy(buf, name.c_str(),
sizeof(buf) - 1);
668 buf[
sizeof(buf) - 1] =
'\0';
669 return pthread_setname_np(pthread_self(), buf);
673static inline int adb_setsockopt(borrowed_fd fd,
int level,
int optname,
const void* optval,
675 return setsockopt(fd.get(), level, optname, optval, optlen);
679#define setsockopt ___xxx_setsockopt
682 return socket(domain,
type, protocol);
685static inline int adb_bind(borrowed_fd fd,
const sockaddr* addr,
int namelen) {
686 return bind(fd.get(), addr, namelen);
697 if (rc < 0)
return -1;
705#define socketpair ___xxx_socketpair
709 return sendmsg(fd.get(), msg, flags);
713 ssize_t ret = recvmsg(fd.get(), msg, flags);
715 PLOG(ERROR) <<
"adb_recmsg error";
723 return CMSG_FIRSTHDR(msgh);
727 return CMSG_NXTHDR(msgh, cmsg);
731 return CMSG_DATA(cmsg);
739#define poll ___xxx_poll
741static inline int adb_mkdir(
const std::string& path,
int mode) {
742 return mkdir(path.c_str(), mode);
746#define mkdir ___xxx_mkdir
748static inline int adb_rename(
const char* oldpath,
const char* newpath) {
749 return rename(oldpath, newpath);
753 return path[0] ==
'/';
770 constexpr explicit operator bool()
const {
return pid_ >= 0; }
792 std::initializer_list<int> fds_to_inherit = {});
798 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off,
sizeof(off));
#define TEMP_FAILURE_RETRY(expression)
Definition: cdefs.h:38
Definition: sysdeps.h:765
pid_t pid_
Definition: sysdeps.h:788
void wait()
Definition: sysdeps.h:772
constexpr Process(pid_t pid)
Definition: sysdeps.h:767
void kill()
Definition: sysdeps.h:779
constexpr Process(Process &&other)
Definition: sysdeps.h:768
DISALLOW_COPY_AND_ASSIGN(Process)
uint32_t size
Definition: io.h:2
#define error(format, args...)
Definition: fec_private.h:199
int status()
Definition: health.cpp:44
int SOCKET
Definition: in_process_tpm.cpp:31
char data[Size]
Definition: incremental_server.cpp:1
void * memcpy(void *dest, const void *src, size_t n)
Definition: mem_overrides.cpp:8
EventFormat format
Definition: kernel_log_server.cc:61
class incremental::File __attribute__
off_t off64_t
Definition: off64_t.h:21
uint8_t type
Definition: pairing_connection.h:0
ssize_t adb_sendmsg(borrowed_fd fd, const adb_msghdr *msg, int flags)
Definition: sysdeps.h:708
static int adb_pread(borrowed_fd fd, void *buf, size_t len, off64_t offset)
Definition: sysdeps.h:530
cmsghdr adb_cmsghdr
Definition: sysdeps.h:720
ssize_t adb_recvmsg(borrowed_fd fd, adb_msghdr *msg, int flags)
Definition: sysdeps.h:712
#define creat
Definition: sysdeps.h:590
static int adb_rename(const char *oldpath, const char *newpath)
Definition: sysdeps.h:748
#define lseek
Definition: sysdeps.h:573
static bool adb_is_separator(char c)
Definition: sysdeps.h:435
static int adb_is_absolute_host_path(const char *path)
Definition: sysdeps.h:752
#define unlink
Definition: sysdeps.h:579
int network_connect(const std::string &host, int port, int type, int timeout, std::string *error)
Definition: network.cpp:136
static int adb_pwrite(int fd, const void *buf, size_t len, off64_t offset)
Definition: sysdeps.h:552
struct pollfd adb_pollfd
Definition: sysdeps.h:734
int adb_register_socket(int s)
Definition: sysdeps.h:514
static int adb_thread_setname(const std::string &name)
Definition: sysdeps.h:660
#define mkdir
Definition: sysdeps.h:746
#define setsockopt
Definition: sysdeps.h:679
#define accept
Definition: sysdeps.h:637
struct msghdr adb_msghdr
Definition: sysdeps.h:707
int network_inaddr_any_server(int port, int type, std::string *error)
Definition: sysdeps.h:605
static int adb_read(borrowed_fd fd, void *buf, size_t len)
Definition: sysdeps.h:526
#define pwrite
Definition: sysdeps.h:563
adb_cmsghdr * adb_CMSG_NXTHDR(adb_msghdr *msgh, adb_cmsghdr *cmsg)
Definition: sysdeps.h:726
#define pread
Definition: sysdeps.h:546
unsigned char * adb_CMSG_DATA(adb_cmsghdr *cmsg)
Definition: sysdeps.h:730
static int adb_poll(adb_pollfd *fds, size_t nfds, int timeout)
Definition: sysdeps.h:735
static int adb_setsockopt(borrowed_fd fd, int level, int optname, const void *optval, socklen_t optlen)
Definition: sysdeps.h:673
#define unix_write
Definition: sysdeps.h:656
#define unix_close
Definition: sysdeps.h:658
int _fd_set_error_str(int fd, std::string *error)
Definition: sysdeps.h:598
int adb_socket_get_local_port(borrowed_fd fd)
Definition: sysdeps.h:643
static int adb_get_os_handle(borrowed_fd fd)
Definition: sysdeps.h:756
static int adb_open_mode(const char *pathname, int options, int mode)
Definition: sysdeps.h:475
static int adb_socket(int domain, int type, int protocol)
Definition: sysdeps.h:681
int network_local_client(const char *name, int namespace_id, int type, std::string *error)
Definition: sysdeps.h:616
std::optional< ssize_t > network_peek(borrowed_fd fd)
Definition: sysdeps_unix.cpp:112
static int adb_bind(borrowed_fd fd, const sockaddr *addr, int namelen)
Definition: sysdeps.h:685
#define read
Definition: sysdeps.h:544
adb_cmsghdr * adb_CMSG_FIRSTHDR(adb_msghdr *msgh)
Definition: sysdeps.h:722
static int unix_open(std::string_view path, int options,...)
Definition: sysdeps.h:459
bool set_tcp_keepalive(borrowed_fd fd, int interval_sec)
Definition: sysdeps_unix.cpp:24
std::string GetOSVersion()
Definition: sysdeps_unix.cpp:105
#define poll
Definition: sysdeps.h:739
static int unix_socketpair(int d, int type, int protocol, int sv[2])
Definition: sysdeps.h:689
static int adb_open(const char *pathname, int options)
Definition: sysdeps.h:486
#define unix_read
Definition: sysdeps.h:655
Process adb_launch_process(std::string_view executable, std::vector< std::string > args, std::initializer_list< int > fds_to_inherit={})
Definition: sysdeps_unix.cpp:73
static int get_adb_connect_timeout()
Definition: sysdeps.h:609
#define open
Definition: sysdeps.h:493
int adb_getsockname(borrowed_fd fd, struct sockaddr *sockaddr, socklen_t *addrlen)
Definition: sysdeps.h:639
static int unix_isatty(borrowed_fd fd)
Definition: sysdeps.h:592
static int cast_handle_to_int(int fd)
Definition: sysdeps.h:760
static int adb_creat(const char *path, int mode)
Definition: sysdeps.h:581
static void close_on_exec(borrowed_fd fd)
Definition: sysdeps.h:443
#define close
Definition: sysdeps.h:509
static int adb_unlink(const char *path)
Definition: sysdeps.h:575
int adb_close(int fd)
Definition: sysdeps.h:505
static int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where)
Definition: sysdeps.h:565
#define socketpair
Definition: sysdeps.h:705
#define shutdown
Definition: sysdeps.h:500
static int adb_socket_accept(borrowed_fd serverfd, struct sockaddr *addr, socklen_t *addrlen)
Definition: sysdeps.h:626
#define write
Definition: sysdeps.h:561
#define unix_lseek
Definition: sysdeps.h:657
static int adb_socketpair(int sv[2])
Definition: sysdeps.h:693
static int adb_mkdir(const std::string &path, int mode)
Definition: sysdeps.h:741
static int unix_read_interruptible(borrowed_fd fd, void *buf, size_t len)
Definition: sysdeps.h:539
static int adb_write(borrowed_fd fd, const void *buf, size_t len)
Definition: sysdeps.h:548
static void disable_tcp_nagle(borrowed_fd fd)
Definition: sysdeps.h:796
#define isatty
Definition: sysdeps.h:595
int network_local_server(const char *name, int namespace_id, int type, std::string *error)
Definition: sysdeps.h:620
static int get_fd_flags(borrowed_fd fd)
Definition: sysdeps.h:439
static int adb_gethostname(char *name, size_t len)
Definition: sysdeps.h:518
static int adb_getlogin_r(char *buf, size_t bufsize)
Definition: sysdeps.h:522
static int adb_shutdown(borrowed_fd fd, int direction=SHUT_RDWR)
Definition: sysdeps.h:495
iovec adb_iovec
Definition: uio.h:38