2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* this file contains system-dependent definitions used by ADB
|
|
|
|
* they're related to threads, sockets and file descriptors
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# undef _WIN32
|
|
|
|
#endif
|
|
|
|
|
2015-07-24 02:12:58 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2015-07-31 08:07:55 +02:00
|
|
|
#include <string>
|
2018-12-13 01:12:28 +01:00
|
|
|
#include <string_view>
|
adb: win32: handle incomplete UTF-8 in console output, other fixes
Previously, the various adb_printf, adb_fwrite, etc. functions did not
correctly handle the case of the passed buffer ending with an incomplete
UTF-8 sequence. This is fixed by buffering up incomplete UTF-8 sequences
in g_console_output_buffer (protected by the mutex
g_console_output_buffer) and outputting it later once the full sequence
is available.
A unittest for the main worker function, ParseCompleteUTF8(), was added
to adb_test.
Other fixes:
- Fix the return value of number-of-chars written to be number of UTF-8
bytes instead of number of UTF-16 characters.
- Don't overwrite errno in success cases of various adb_printf, etc.
functions. This might be excessive, but might be useful in the case
when these functions are used for debugging/tracing.
- Add missing UTF-8 stdio functions that aren't currently used by adb,
but might be in the future: vprintf, putc, putchar, puts.
- stdin_raw_init: If we can't get the console handle, don't call
SetConsoleMode(). Not a big deal, but this will prevent erroneous
trace output.
Change-Id: I8730e8af92882c42b884ad921b39a17b54465085
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-11-16 01:29:36 +01:00
|
|
|
#include <vector>
|
2015-07-31 08:07:55 +02:00
|
|
|
|
2016-05-13 23:52:06 +02:00
|
|
|
// Include this before open/close/unlink are defined as macros below.
|
2016-02-09 23:59:09 +01:00
|
|
|
#include <android-base/errors.h>
|
2017-11-29 03:05:27 +01:00
|
|
|
#include <android-base/macros.h>
|
2019-08-19 14:37:19 +02:00
|
|
|
#include <android-base/off64_t.h>
|
2016-05-13 23:52:06 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/utf8.h>
|
2015-11-13 00:20:15 +01:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
#include "adb_unique_fd.h"
|
2016-12-05 22:24:48 +01:00
|
|
|
#include "sysdeps/errno.h"
|
2017-04-12 22:57:06 +02:00
|
|
|
#include "sysdeps/network.h"
|
2016-07-29 03:09:48 +02:00
|
|
|
#include "sysdeps/stat.h"
|
|
|
|
|
2020-04-03 19:12:44 +02:00
|
|
|
#if defined(__APPLE__)
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline void* mempcpy(void* dst, const void* src, size_t n) {
|
2020-04-03 19:12:44 +02:00
|
|
|
return static_cast<char*>(memcpy(dst, src, n)) + n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2016-01-29 21:08:34 +01:00
|
|
|
// Clang-only nullability specifiers
|
|
|
|
#define _Nonnull
|
|
|
|
#define _Nullable
|
|
|
|
|
2014-11-25 08:34:35 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <direct.h>
|
2015-05-11 10:08:48 +02:00
|
|
|
#include <dirent.h>
|
2014-11-25 08:34:35 +01:00
|
|
|
#include <errno.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
2014-11-25 08:34:35 +01:00
|
|
|
#include <process.h>
|
2018-09-20 22:59:49 +02:00
|
|
|
#include <stdint.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/stat.h>
|
2015-05-11 10:08:48 +02:00
|
|
|
#include <utime.h>
|
2014-11-25 08:34:35 +01:00
|
|
|
#include <windows.h>
|
2018-09-20 22:59:49 +02:00
|
|
|
#include <winsock2.h>
|
2014-11-25 08:34:35 +01:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
#include <memory> // unique_ptr
|
2015-11-13 00:20:15 +01:00
|
|
|
#include <string>
|
2015-05-06 22:26:00 +02:00
|
|
|
|
2015-07-31 02:42:01 +02:00
|
|
|
#define OS_PATH_SEPARATORS "\\/"
|
2009-03-04 04:32:55 +01:00
|
|
|
#define OS_PATH_SEPARATOR '\\'
|
|
|
|
#define OS_PATH_SEPARATOR_STR "\\"
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#define ENV_PATH_SEPARATOR_STR ";"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline bool adb_is_separator(char c) {
|
2015-11-08 00:38:19 +01:00
|
|
|
return c == '\\' || c == '/';
|
|
|
|
}
|
|
|
|
|
2018-09-04 01:03:22 +02:00
|
|
|
extern int adb_thread_setname(const std::string& name);
|
2015-08-29 01:37:29 +02:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline void close_on_exec(borrowed_fd fd) {
|
2009-05-18 17:36:28 +02:00
|
|
|
/* nothing really */
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int adb_unlink(const char* path);
|
|
|
|
#undef unlink
|
|
|
|
#define unlink ___xxx_unlink
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-11 10:08:48 +02:00
|
|
|
extern int adb_mkdir(const std::string& path, int mode);
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef mkdir
|
|
|
|
#define mkdir ___xxx_mkdir
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-11-20 23:18:43 +01:00
|
|
|
extern int adb_rename(const char* oldpath, const char* newpath);
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// See the comments for the !defined(_WIN32) versions of adb_*().
|
2018-03-23 21:03:28 +01:00
|
|
|
extern int adb_open(const char* path, int options);
|
|
|
|
extern int adb_creat(const char* path, int mode);
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int adb_read(borrowed_fd fd, void* buf, int len);
|
2019-07-11 02:59:34 +02:00
|
|
|
extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int adb_write(borrowed_fd fd, const void* buf, int len);
|
2019-07-11 02:59:34 +02:00
|
|
|
extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
|
|
|
|
extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
|
2018-03-23 21:03:28 +01:00
|
|
|
extern int adb_close(int fd);
|
|
|
|
extern int adb_register_socket(SOCKET s);
|
2019-07-11 02:59:34 +02:00
|
|
|
extern HANDLE adb_get_os_handle(borrowed_fd fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-11-20 23:18:43 +01:00
|
|
|
extern int adb_gethostname(char* name, size_t len);
|
|
|
|
extern int adb_getlogin_r(char* buf, size_t bufsize);
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// See the comments for the !defined(_WIN32) version of unix_close().
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_close(int fd) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return close(fd);
|
|
|
|
}
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef close
|
|
|
|
#define close ____xxx_close
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-11-08 02:34:39 +01:00
|
|
|
// Like unix_read(), but may return EINTR.
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
|
2015-11-08 02:34:39 +01:00
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// See the comments for the !defined(_WIN32) version of unix_read().
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_read(borrowed_fd fd, void* buf, size_t len) {
|
2015-11-08 02:34:39 +01:00
|
|
|
return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
|
|
|
|
}
|
2015-03-02 00:06:21 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#undef read
|
|
|
|
#define read ___xxx_read
|
|
|
|
|
2019-07-11 02:59:34 +02:00
|
|
|
#undef pread
|
|
|
|
#define pread ___xxx_pread
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// See the comments for the !defined(_WIN32) version of unix_write().
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_write(borrowed_fd fd, const void* buf, size_t len) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return write(fd.get(), buf, len);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
#undef write
|
|
|
|
#define write ___xxx_write
|
|
|
|
|
2019-07-11 02:59:34 +02:00
|
|
|
#undef pwrite
|
|
|
|
#define pwrite ___xxx_pwrite
|
|
|
|
|
2018-09-03 04:19:39 +02:00
|
|
|
// See the comments for the !defined(_WIN32) version of unix_lseek().
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_lseek(borrowed_fd fd, int pos, int where) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return lseek(fd.get(), pos, where);
|
2018-09-03 04:19:39 +02:00
|
|
|
}
|
|
|
|
#undef lseek
|
|
|
|
#define lseek ___xxx_lseek
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// See the comments for the !defined(_WIN32) version of adb_open_mode().
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_open_mode(const char* path, int options, int mode) {
|
2009-05-18 17:36:28 +02:00
|
|
|
return adb_open(path, options);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// See the comments for the !defined(_WIN32) version of unix_open().
|
2018-12-13 01:12:28 +01:00
|
|
|
extern int unix_open(std::string_view path, int options, ...);
|
2009-03-04 04:32:55 +01:00
|
|
|
#define open ___xxx_unix_open
|
|
|
|
|
2015-10-28 22:29:51 +01:00
|
|
|
// Checks if |fd| corresponds to a console.
|
|
|
|
// Standard Windows isatty() returns 1 for both console FDs and character
|
|
|
|
// devices like NUL. unix_isatty() performs some extra checking to only match
|
|
|
|
// console FDs.
|
|
|
|
// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
|
|
|
|
// will work but adb_open() FDs will not. Additionally the OS handle associated
|
|
|
|
// with |fd| must have GENERIC_READ access (which console FDs have by default).
|
|
|
|
// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
|
|
|
|
// calling this function is unreliable and should not be used.
|
2019-04-25 23:04:57 +02:00
|
|
|
int unix_isatty(borrowed_fd fd);
|
2015-10-28 22:29:51 +01:00
|
|
|
#define isatty ___xxx_isatty
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-31 08:07:55 +02:00
|
|
|
int network_inaddr_any_server(int port, int type, std::string* error);
|
2016-08-25 03:38:44 +02:00
|
|
|
|
|
|
|
inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2015-07-31 08:07:55 +02:00
|
|
|
int network_connect(const std::string& host, int port, int type, int timeout,
|
|
|
|
std::string* error);
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#undef accept
|
|
|
|
#define accept ___xxx_accept
|
|
|
|
|
2016-04-07 20:25:48 +02:00
|
|
|
// Returns the local port number of a bound socket, or -1 on failure.
|
2019-04-25 23:04:57 +02:00
|
|
|
int adb_socket_get_local_port(borrowed_fd fd);
|
2016-04-07 20:25:48 +02:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
|
|
|
|
socklen_t optlen);
|
2015-01-25 23:40:16 +01:00
|
|
|
|
|
|
|
#undef setsockopt
|
|
|
|
#define setsockopt ___xxx_setsockopt
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
extern int adb_socketpair(int sv[2]);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-02-17 02:34:53 +01:00
|
|
|
struct adb_pollfd {
|
|
|
|
int fd;
|
|
|
|
short events;
|
|
|
|
short revents;
|
|
|
|
};
|
|
|
|
extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
|
|
|
|
#define poll ___xxx_poll
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_is_absolute_host_path(const char* path) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:08:48 +02:00
|
|
|
// UTF-8 versions of POSIX APIs.
|
|
|
|
extern DIR* adb_opendir(const char* dirname);
|
|
|
|
extern struct dirent* adb_readdir(DIR* dir);
|
|
|
|
extern int adb_closedir(DIR* dir);
|
|
|
|
|
|
|
|
extern int adb_utime(const char *, struct utimbuf *);
|
|
|
|
extern int adb_chmod(const char *, int);
|
|
|
|
|
2018-06-26 22:06:15 +02:00
|
|
|
extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
|
|
|
|
__attribute__((__format__(__printf__, 2, 0)));
|
|
|
|
extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
|
|
|
|
extern int adb_fprintf(FILE* stream, const char* format, ...)
|
|
|
|
__attribute__((__format__(__printf__, 2, 3)));
|
|
|
|
extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
|
2015-05-11 10:08:48 +02:00
|
|
|
|
|
|
|
extern int adb_fputs(const char* buf, FILE* stream);
|
|
|
|
extern int adb_fputc(int ch, FILE* stream);
|
adb: win32: handle incomplete UTF-8 in console output, other fixes
Previously, the various adb_printf, adb_fwrite, etc. functions did not
correctly handle the case of the passed buffer ending with an incomplete
UTF-8 sequence. This is fixed by buffering up incomplete UTF-8 sequences
in g_console_output_buffer (protected by the mutex
g_console_output_buffer) and outputting it later once the full sequence
is available.
A unittest for the main worker function, ParseCompleteUTF8(), was added
to adb_test.
Other fixes:
- Fix the return value of number-of-chars written to be number of UTF-8
bytes instead of number of UTF-16 characters.
- Don't overwrite errno in success cases of various adb_printf, etc.
functions. This might be excessive, but might be useful in the case
when these functions are used for debugging/tracing.
- Add missing UTF-8 stdio functions that aren't currently used by adb,
but might be in the future: vprintf, putc, putchar, puts.
- stdin_raw_init: If we can't get the console handle, don't call
SetConsoleMode(). Not a big deal, but this will prevent erroneous
trace output.
Change-Id: I8730e8af92882c42b884ad921b39a17b54465085
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-11-16 01:29:36 +01:00
|
|
|
extern int adb_putchar(int ch);
|
|
|
|
extern int adb_puts(const char* buf);
|
2019-04-25 23:04:57 +02:00
|
|
|
extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
|
2015-05-11 10:08:48 +02:00
|
|
|
|
|
|
|
extern FILE* adb_fopen(const char* f, const char* m);
|
|
|
|
|
|
|
|
extern char* adb_getenv(const char* name);
|
|
|
|
|
|
|
|
extern char* adb_getcwd(char* buf, int size);
|
|
|
|
|
|
|
|
// Remap calls to POSIX APIs to our UTF-8 versions.
|
|
|
|
#define opendir adb_opendir
|
|
|
|
#define readdir adb_readdir
|
|
|
|
#define closedir adb_closedir
|
|
|
|
#define rewinddir rewinddir_utf8_not_yet_implemented
|
|
|
|
#define telldir telldir_utf8_not_yet_implemented
|
2015-11-08 03:51:54 +01:00
|
|
|
// Some compiler's C++ headers have members named seekdir, so we can't do the
|
|
|
|
// macro technique and instead cause a link error if seekdir is called.
|
|
|
|
inline void seekdir(DIR*, long) {
|
|
|
|
extern int seekdir_utf8_not_yet_implemented;
|
|
|
|
seekdir_utf8_not_yet_implemented = 1;
|
|
|
|
}
|
2015-05-11 10:08:48 +02:00
|
|
|
|
|
|
|
#define utime adb_utime
|
|
|
|
#define chmod adb_chmod
|
|
|
|
|
|
|
|
#define vfprintf adb_vfprintf
|
adb: win32: handle incomplete UTF-8 in console output, other fixes
Previously, the various adb_printf, adb_fwrite, etc. functions did not
correctly handle the case of the passed buffer ending with an incomplete
UTF-8 sequence. This is fixed by buffering up incomplete UTF-8 sequences
in g_console_output_buffer (protected by the mutex
g_console_output_buffer) and outputting it later once the full sequence
is available.
A unittest for the main worker function, ParseCompleteUTF8(), was added
to adb_test.
Other fixes:
- Fix the return value of number-of-chars written to be number of UTF-8
bytes instead of number of UTF-16 characters.
- Don't overwrite errno in success cases of various adb_printf, etc.
functions. This might be excessive, but might be useful in the case
when these functions are used for debugging/tracing.
- Add missing UTF-8 stdio functions that aren't currently used by adb,
but might be in the future: vprintf, putc, putchar, puts.
- stdin_raw_init: If we can't get the console handle, don't call
SetConsoleMode(). Not a big deal, but this will prevent erroneous
trace output.
Change-Id: I8730e8af92882c42b884ad921b39a17b54465085
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-11-16 01:29:36 +01:00
|
|
|
#define vprintf adb_vprintf
|
2015-05-11 10:08:48 +02:00
|
|
|
#define fprintf adb_fprintf
|
|
|
|
#define printf adb_printf
|
|
|
|
#define fputs adb_fputs
|
|
|
|
#define fputc adb_fputc
|
adb: win32: handle incomplete UTF-8 in console output, other fixes
Previously, the various adb_printf, adb_fwrite, etc. functions did not
correctly handle the case of the passed buffer ending with an incomplete
UTF-8 sequence. This is fixed by buffering up incomplete UTF-8 sequences
in g_console_output_buffer (protected by the mutex
g_console_output_buffer) and outputting it later once the full sequence
is available.
A unittest for the main worker function, ParseCompleteUTF8(), was added
to adb_test.
Other fixes:
- Fix the return value of number-of-chars written to be number of UTF-8
bytes instead of number of UTF-16 characters.
- Don't overwrite errno in success cases of various adb_printf, etc.
functions. This might be excessive, but might be useful in the case
when these functions are used for debugging/tracing.
- Add missing UTF-8 stdio functions that aren't currently used by adb,
but might be in the future: vprintf, putc, putchar, puts.
- stdin_raw_init: If we can't get the console handle, don't call
SetConsoleMode(). Not a big deal, but this will prevent erroneous
trace output.
Change-Id: I8730e8af92882c42b884ad921b39a17b54465085
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-11-16 01:29:36 +01:00
|
|
|
// putc may be a macro, so if so, undefine it, so that we can redefine it.
|
|
|
|
#undef putc
|
|
|
|
#define putc(c, s) adb_fputc(c, s)
|
|
|
|
#define putchar adb_putchar
|
|
|
|
#define puts adb_puts
|
2015-05-11 10:08:48 +02:00
|
|
|
#define fwrite adb_fwrite
|
|
|
|
|
|
|
|
#define fopen adb_fopen
|
adb: win32: handle incomplete UTF-8 in console output, other fixes
Previously, the various adb_printf, adb_fwrite, etc. functions did not
correctly handle the case of the passed buffer ending with an incomplete
UTF-8 sequence. This is fixed by buffering up incomplete UTF-8 sequences
in g_console_output_buffer (protected by the mutex
g_console_output_buffer) and outputting it later once the full sequence
is available.
A unittest for the main worker function, ParseCompleteUTF8(), was added
to adb_test.
Other fixes:
- Fix the return value of number-of-chars written to be number of UTF-8
bytes instead of number of UTF-16 characters.
- Don't overwrite errno in success cases of various adb_printf, etc.
functions. This might be excessive, but might be useful in the case
when these functions are used for debugging/tracing.
- Add missing UTF-8 stdio functions that aren't currently used by adb,
but might be in the future: vprintf, putc, putchar, puts.
- stdin_raw_init: If we can't get the console handle, don't call
SetConsoleMode(). Not a big deal, but this will prevent erroneous
trace output.
Change-Id: I8730e8af92882c42b884ad921b39a17b54465085
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-11-16 01:29:36 +01:00
|
|
|
#define freopen freopen_utf8_not_yet_implemented
|
2015-05-11 10:08:48 +02:00
|
|
|
|
|
|
|
#define getenv adb_getenv
|
|
|
|
#define putenv putenv_utf8_not_yet_implemented
|
|
|
|
#define setenv setenv_utf8_not_yet_implemented
|
|
|
|
#define unsetenv unsetenv_utf8_not_yet_implemented
|
|
|
|
|
|
|
|
#define getcwd adb_getcwd
|
|
|
|
|
2020-02-13 15:52:04 +01:00
|
|
|
// A very simple wrapper over a launched child process
|
|
|
|
class Process {
|
|
|
|
public:
|
|
|
|
constexpr explicit Process(HANDLE h = nullptr) : h_(h) {}
|
2020-03-18 23:49:45 +01:00
|
|
|
constexpr Process(Process&& other) : h_(std::exchange(other.h_, nullptr)) {}
|
2020-02-13 15:52:04 +01:00
|
|
|
~Process() { close(); }
|
|
|
|
constexpr explicit operator bool() const { return h_ != nullptr; }
|
|
|
|
|
|
|
|
void wait() {
|
|
|
|
if (*this) {
|
|
|
|
::WaitForSingleObject(h_, INFINITE);
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void kill() {
|
|
|
|
if (*this) {
|
|
|
|
::TerminateProcess(h_, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-18 23:49:45 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Process);
|
|
|
|
|
2020-02-13 15:52:04 +01:00
|
|
|
void close() {
|
|
|
|
if (*this) {
|
|
|
|
::CloseHandle(h_);
|
|
|
|
h_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE h_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
|
|
|
|
std::initializer_list<int> fds_to_inherit = {});
|
|
|
|
|
2015-05-11 10:08:48 +02:00
|
|
|
// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
|
|
|
|
// passed to main().
|
|
|
|
class NarrowArgs {
|
|
|
|
public:
|
|
|
|
NarrowArgs(int argc, wchar_t** argv);
|
|
|
|
~NarrowArgs();
|
|
|
|
|
|
|
|
inline char** data() {
|
|
|
|
return narrow_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char** narrow_args;
|
|
|
|
};
|
|
|
|
|
2015-08-09 00:07:07 +02:00
|
|
|
// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
|
|
|
|
// so they can fit in an int. To convert back, we just need to sign-extend.
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
|
|
|
|
// Note that this does not make a HANDLE value work with APIs like open(), nor
|
|
|
|
// does this make a value from open() passable to APIs taking a HANDLE. This
|
|
|
|
// just lets you take a HANDLE, pass it around as an int, and then use it again
|
|
|
|
// as a HANDLE.
|
|
|
|
inline int cast_handle_to_int(const HANDLE h) {
|
|
|
|
// truncate
|
|
|
|
return static_cast<int>(reinterpret_cast<INT_PTR>(h));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline HANDLE cast_int_to_handle(const int fd) {
|
|
|
|
// sign-extend
|
|
|
|
return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:46:09 +02:00
|
|
|
// Deleter for unique_handle. Adapted from many sources, including:
|
|
|
|
// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
|
|
|
|
// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
|
|
|
|
class handle_deleter {
|
|
|
|
public:
|
|
|
|
typedef HANDLE pointer;
|
|
|
|
|
|
|
|
void operator()(HANDLE h);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Like std::unique_ptr, but for Windows HANDLE objects that should be
|
|
|
|
// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
|
|
|
|
// but does not check if the handle != INVALID_HANDLE_VALUE.
|
|
|
|
typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
|
|
|
|
|
adb: win32: handle incomplete UTF-8 in console output, other fixes
Previously, the various adb_printf, adb_fwrite, etc. functions did not
correctly handle the case of the passed buffer ending with an incomplete
UTF-8 sequence. This is fixed by buffering up incomplete UTF-8 sequences
in g_console_output_buffer (protected by the mutex
g_console_output_buffer) and outputting it later once the full sequence
is available.
A unittest for the main worker function, ParseCompleteUTF8(), was added
to adb_test.
Other fixes:
- Fix the return value of number-of-chars written to be number of UTF-8
bytes instead of number of UTF-16 characters.
- Don't overwrite errno in success cases of various adb_printf, etc.
functions. This might be excessive, but might be useful in the case
when these functions are used for debugging/tracing.
- Add missing UTF-8 stdio functions that aren't currently used by adb,
but might be in the future: vprintf, putc, putchar, puts.
- stdin_raw_init: If we can't get the console handle, don't call
SetConsoleMode(). Not a big deal, but this will prevent erroneous
trace output.
Change-Id: I8730e8af92882c42b884ad921b39a17b54465085
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
2015-11-16 01:29:36 +01:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#else /* !_WIN32 a.k.a. Unix */
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2015-07-31 08:07:55 +02:00
|
|
|
#include <netdb.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
2018-09-20 22:59:49 +02:00
|
|
|
#include <poll.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
2018-09-20 22:59:49 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
2012-10-13 00:26:45 +02:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-05-06 22:26:00 +02:00
|
|
|
#include <string>
|
|
|
|
|
2018-09-20 22:59:49 +02:00
|
|
|
#include <cutils/sockets.h>
|
|
|
|
|
2015-07-31 02:42:01 +02:00
|
|
|
#define OS_PATH_SEPARATORS "/"
|
2009-03-04 04:32:55 +01:00
|
|
|
#define OS_PATH_SEPARATOR '/'
|
|
|
|
#define OS_PATH_SEPARATOR_STR "/"
|
adb: Add public key authentification
Secure adb using a public key authentication, to allow USB debugging
only from authorized hosts.
When a device is connected to an unauthorized host, the adb daemon sends
the user public key to the device. A popup is shown to ask the user to
allow debugging once or permanantly from the host. The public key is
installed on the device in the later case. Other keys may be installed
at build time.
On the host, the user public/private key pair is automatically generated,
if it does not exist, when the adb daemon starts and is stored in
$HOME/.android/adb_key(.pub) or in $ANDROID_SDK_HOME on windows. If needed,
the ADB_KEYS_PATH env variable may be set to a :-separated (; under
Windows) list of private keys, e.g. company-wide or vendor keys.
On the device, vendors public keys are installed at build time in
/adb_keys. User-installed keys are stored in /data/misc/adb/adb_keys.
ADB Protocol change:
If the device needs to authenticate the host, it replies to CNXN
packets with an AUTH packet. The AUTH packet payload is a random token.
The host signs the token with one of its private keys and sends an AUTH(0)
packet. If the signature verification succeeds, the device replies with
a CNXN packet. Otherwise, it sends a new AUTH packet with a new token so
that the host can retry with another private key. Once the host has tried
all its keys, it can send an AUTH(1) packet with a public key as
payload. adbd then sends the public key to the framework (if it has been
started) for confirmation.
Change-Id: I4e84d7621da956f66ff657245901bdaefead8395
2012-04-12 21:23:49 +02:00
|
|
|
#define ENV_PATH_SEPARATOR_STR ":"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline bool adb_is_separator(char c) {
|
2015-11-08 00:38:19 +01:00
|
|
|
return c == '/';
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int get_fd_flags(borrowed_fd fd) {
|
2019-11-14 02:49:37 +01:00
|
|
|
return fcntl(fd.get(), F_GETFD);
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline void close_on_exec(borrowed_fd fd) {
|
2019-11-14 02:49:37 +01:00
|
|
|
int flags = get_fd_flags(fd);
|
|
|
|
if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
|
|
|
|
fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// Open a file and return a file descriptor that may be used with unix_read(),
|
|
|
|
// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
|
|
|
|
//
|
|
|
|
// On Unix, this is based on open(), so the file descriptor is a real OS file
|
|
|
|
// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
|
|
|
|
// file descriptor that can only be used with C Runtime APIs (which are wrapped
|
|
|
|
// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
|
|
|
|
// configurable CR/LF translation which defaults to text mode, but is settable
|
|
|
|
// with _setmode().
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_open(std::string_view path, int options, ...) {
|
2018-12-13 01:12:28 +01:00
|
|
|
std::string zero_terminated(path.begin(), path.end());
|
|
|
|
if ((options & O_CREAT) == 0) {
|
|
|
|
return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
|
|
|
|
} else {
|
|
|
|
int mode;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, options);
|
|
|
|
mode = va_arg(args, int);
|
|
|
|
va_end(args);
|
|
|
|
return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// Similar to the two-argument adb_open(), but takes a mode parameter for file
|
|
|
|
// creation. See adb_open() for more info.
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_open_mode(const char* pathname, int options, int mode) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return TEMP_FAILURE_RETRY(open(pathname, options, mode));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// Open a file and return a file descriptor that may be used with adb_read(),
|
|
|
|
// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
|
|
|
|
//
|
|
|
|
// On Unix, this is based on open(), but the Windows implementation (in
|
|
|
|
// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
|
|
|
|
// and its CR/LF translation. The returned file descriptor should be used with
|
|
|
|
// adb_read(), adb_write(), adb_close(), etc.
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_open(const char* pathname, int options) {
|
2019-04-25 23:04:57 +02:00
|
|
|
int fd = TEMP_FAILURE_RETRY(open(pathname, options));
|
|
|
|
if (fd < 0) return -1;
|
|
|
|
close_on_exec(fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
return fd;
|
|
|
|
}
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef open
|
|
|
|
#define open ___xxx_open
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return shutdown(fd.get(), direction);
|
2015-10-07 00:30:03 +02:00
|
|
|
}
|
2018-03-23 21:03:28 +01:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef shutdown
|
|
|
|
#define shutdown ____xxx_shutdown
|
2009-10-12 05:04:18 +02:00
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
|
|
|
|
// not designed to take a file descriptor from unix_open(). See the comments
|
|
|
|
// for adb_open() for more info.
|
2020-04-06 18:15:35 +02:00
|
|
|
inline int adb_close(int fd) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return close(fd);
|
|
|
|
}
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef close
|
|
|
|
#define close ____xxx_close
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-09-21 23:03:39 +02:00
|
|
|
// On Windows, ADB has an indirection layer for file descriptors. If we get a
|
|
|
|
// Win32 SOCKET object from an external library, we have to map it in to that
|
|
|
|
// indirection layer, which this does.
|
2020-04-06 18:15:35 +02:00
|
|
|
inline int adb_register_socket(int s) {
|
2016-09-21 23:03:39 +02:00
|
|
|
return s;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_gethostname(char* name, size_t len) {
|
2019-11-20 23:18:43 +01:00
|
|
|
return gethostname(name, len);
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_getlogin_r(char* buf, size_t bufsize) {
|
2019-11-20 23:18:43 +01:00
|
|
|
return getlogin_r(buf, bufsize);
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_read(borrowed_fd fd, void* buf, size_t len) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_pread(borrowed_fd fd, void* buf, size_t len, off64_t offset) {
|
2019-07-11 02:59:34 +02:00
|
|
|
#if defined(__APPLE__)
|
2020-02-13 15:52:04 +01:00
|
|
|
return TEMP_FAILURE_RETRY(pread(fd.get(), buf, len, offset));
|
2019-07-11 02:59:34 +02:00
|
|
|
#else
|
2020-02-13 15:52:04 +01:00
|
|
|
return TEMP_FAILURE_RETRY(pread64(fd.get(), buf, len, offset));
|
2019-07-11 02:59:34 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-11-08 02:34:39 +01:00
|
|
|
// Like unix_read(), but does not handle EINTR.
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return read(fd.get(), buf, len);
|
2015-11-08 02:34:39 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef read
|
|
|
|
#define read ___xxx_read
|
2019-07-11 02:59:34 +02:00
|
|
|
#undef pread
|
|
|
|
#define pread ___xxx_pread
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_write(borrowed_fd fd, const void* buf, size_t len) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2019-07-11 02:59:34 +02:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
|
2019-07-11 02:59:34 +02:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
|
|
|
|
#else
|
|
|
|
return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#undef write
|
|
|
|
#define write ___xxx_write
|
2019-07-11 02:59:34 +02:00
|
|
|
#undef pwrite
|
|
|
|
#define pwrite ___xxx_pwrite
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
|
2018-09-20 22:59:49 +02:00
|
|
|
#if defined(__APPLE__)
|
2019-04-25 23:04:57 +02:00
|
|
|
return lseek(fd.get(), pos, where);
|
2018-09-20 22:59:49 +02:00
|
|
|
#else
|
2019-04-25 23:04:57 +02:00
|
|
|
return lseek64(fd.get(), pos, where);
|
2018-09-20 22:59:49 +02:00
|
|
|
#endif
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-09-20 22:59:49 +02:00
|
|
|
#undef lseek
|
|
|
|
#define lseek ___xxx_lseek
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_unlink(const char* path) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return unlink(path);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef unlink
|
|
|
|
#define unlink ___xxx_unlink
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_creat(const char* path, int mode) {
|
2019-04-25 23:04:57 +02:00
|
|
|
int fd = TEMP_FAILURE_RETRY(creat(path, mode));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
if (fd < 0) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
close_on_exec(fd);
|
2009-05-18 17:36:28 +02:00
|
|
|
return fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef creat
|
|
|
|
#define creat ___xxx_creat
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_isatty(borrowed_fd fd) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return isatty(fd.get());
|
2015-10-28 22:29:51 +01:00
|
|
|
}
|
2019-04-25 23:04:57 +02:00
|
|
|
#define isatty ___xxx_isatty
|
2015-10-28 22:29:51 +01:00
|
|
|
|
2015-07-31 08:07:55 +02:00
|
|
|
// Helper for network_* functions.
|
|
|
|
inline int _fd_set_error_str(int fd, std::string* error) {
|
2019-04-25 23:04:57 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
*error = strerror(errno);
|
|
|
|
}
|
|
|
|
return fd;
|
2015-07-31 08:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int network_inaddr_any_server(int port, int type, std::string* error) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
|
2015-07-31 08:07:55 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 03:38:44 +02:00
|
|
|
inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
|
|
|
|
return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
|
|
|
|
return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
|
2015-07-31 08:07:55 +02:00
|
|
|
}
|
|
|
|
|
2018-08-11 01:03:09 +02:00
|
|
|
int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
|
2015-07-31 08:07:55 +02:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
|
|
|
|
socklen_t* addrlen) {
|
2011-02-02 03:57:41 +01:00
|
|
|
int fd;
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
|
|
|
|
if (fd >= 0) close_on_exec(fd);
|
2011-02-02 03:57:41 +01:00
|
|
|
|
|
|
|
return fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef accept
|
|
|
|
#define accept ___xxx_accept
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
inline int adb_socket_get_local_port(borrowed_fd fd) {
|
|
|
|
return socket_get_local_port(fd.get());
|
2016-04-07 20:25:48 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 05:09:06 +02:00
|
|
|
// Operate on a file descriptor returned from unix_open() or a well-known file
|
|
|
|
// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
|
|
|
|
//
|
|
|
|
// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
|
|
|
|
// adb_write(), adb_close() (which all map to Unix system calls), but the
|
|
|
|
// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
|
|
|
|
// into the C Runtime and its configurable CR/LF translation (which is settable
|
|
|
|
// via _setmode()).
|
2019-04-25 23:04:57 +02:00
|
|
|
#define unix_read adb_read
|
|
|
|
#define unix_write adb_write
|
2018-09-03 04:19:39 +02:00
|
|
|
#define unix_lseek adb_lseek
|
2019-04-25 23:04:57 +02:00
|
|
|
#define unix_close adb_close
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_thread_setname(const std::string& name) {
|
2015-08-29 01:37:29 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
return pthread_setname_np(name.c_str());
|
|
|
|
#else
|
2017-08-02 22:22:38 +02:00
|
|
|
// Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
|
|
|
|
// glibc doesn't have strlcpy, so we have to fake it.
|
|
|
|
char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
|
|
|
|
strncpy(buf, name.c_str(), sizeof(buf) - 1);
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
return pthread_setname_np(pthread_self(), buf);
|
2015-08-29 01:37:29 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
|
|
|
|
socklen_t optlen) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return setsockopt(fd.get(), level, optname, optval, optlen);
|
2015-01-25 23:40:16 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef setsockopt
|
|
|
|
#define setsockopt ___xxx_setsockopt
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int unix_socketpair(int d, int type, int protocol, int sv[2]) {
|
2019-04-25 23:04:57 +02:00
|
|
|
return socketpair(d, type, protocol, sv);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_socketpair(int sv[2]) {
|
2019-04-25 23:04:57 +02:00
|
|
|
int rc;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
|
|
|
|
if (rc < 0) return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
close_on_exec(sv[0]);
|
|
|
|
close_on_exec(sv[1]);
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef socketpair
|
|
|
|
#define socketpair ___xxx_socketpair
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-02-17 02:34:53 +01:00
|
|
|
typedef struct pollfd adb_pollfd;
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
|
2016-02-17 02:34:53 +01:00
|
|
|
return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define poll ___xxx_poll
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_mkdir(const std::string& path, int mode) {
|
2015-05-06 22:26:00 +02:00
|
|
|
return mkdir(path.c_str(), mode);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-05-06 22:26:00 +02:00
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
#undef mkdir
|
|
|
|
#define mkdir ___xxx_mkdir
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_rename(const char* oldpath, const char* newpath) {
|
2019-11-20 23:18:43 +01:00
|
|
|
return rename(oldpath, newpath);
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_is_absolute_host_path(const char* path) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return path[0] == '/';
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int adb_get_os_handle(borrowed_fd fd) {
|
2019-09-16 21:10:54 +02:00
|
|
|
return fd.get();
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:15:35 +02:00
|
|
|
static inline int cast_handle_to_int(int fd) {
|
2020-03-27 02:16:36 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-02-13 15:52:04 +01:00
|
|
|
// A very simple wrapper over a launched child process
|
|
|
|
class Process {
|
|
|
|
public:
|
|
|
|
constexpr explicit Process(pid_t pid) : pid_(pid) {}
|
2020-03-18 23:49:45 +01:00
|
|
|
constexpr Process(Process&& other) : pid_(std::exchange(other.pid_, -1)) {}
|
|
|
|
|
2020-02-13 15:52:04 +01:00
|
|
|
constexpr explicit operator bool() const { return pid_ >= 0; }
|
|
|
|
|
|
|
|
void wait() {
|
|
|
|
if (*this) {
|
|
|
|
int status;
|
|
|
|
::waitpid(pid_, &status, 0);
|
|
|
|
pid_ = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void kill() {
|
|
|
|
if (*this) {
|
|
|
|
::kill(pid_, SIGTERM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-18 23:49:45 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Process);
|
|
|
|
|
2020-02-13 15:52:04 +01:00
|
|
|
pid_t pid_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
|
|
|
|
std::initializer_list<int> fds_to_inherit = {});
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif /* !_WIN32 */
|
|
|
|
|
2019-04-25 23:04:57 +02:00
|
|
|
static inline void disable_tcp_nagle(borrowed_fd fd) {
|
2015-11-21 07:01:06 +01:00
|
|
|
int off = 1;
|
2019-04-25 23:04:57 +02:00
|
|
|
adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
|
2015-11-21 07:01:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 23:27:23 +01:00
|
|
|
// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
|
|
|
|
// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
|
|
|
|
// configured to drop after 10 missed keepalives. Returns true on success.
|
2019-04-25 23:04:57 +02:00
|
|
|
bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
|
2016-02-22 23:27:23 +01:00
|
|
|
|
2016-06-30 02:42:01 +02:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
|
|
|
|
#undef ERROR
|
|
|
|
#endif
|