blob: 7326ab13728f0726463e34b734e4ee41c64367b4 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes681338d2020-04-06 09:15:35 -070017#pragma once
18
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019/* this file contains system-dependent definitions used by ADB
20 * they're related to threads, sockets and file descriptors
21 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022
23#ifdef __CYGWIN__
24# undef _WIN32
25#endif
26
Elliott Hughes381cfa92015-07-23 17:12:58 -070027#include <errno.h>
28
Spencer Low5200c662015-07-30 23:07:55 -070029#include <string>
Josh Gao0f29cbc2018-12-12 16:12:28 -080030#include <string_view>
Spencer Lowa30b79a2015-11-15 16:29:36 -080031#include <vector>
Spencer Low5200c662015-07-30 23:07:55 -070032
Josh Gao13ea01d2016-05-13 14:52:06 -070033// Include this before open/close/unlink are defined as macros below.
Josh Gao3b3e10d2016-02-09 14:59:09 -080034#include <android-base/errors.h>
Elliott Hughes3df2fa62017-11-28 18:05:27 -080035#include <android-base/macros.h>
Adrian Roos68c14d12019-08-19 14:37:19 +020036#include <android-base/off64_t.h>
Josh Gao13ea01d2016-05-13 14:52:06 -070037#include <android-base/unique_fd.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080038#include <android-base/utf8.h>
Spencer Lowd21dc822015-11-12 15:20:15 -080039
Josh Gao27241a72019-04-25 14:04:57 -070040#include "adb_unique_fd.h"
Josh Gaoa3577e12016-12-05 13:24:48 -080041#include "sysdeps/errno.h"
Josh Gao46de1d72017-04-12 13:57:06 -070042#include "sysdeps/network.h"
Josh Gaof551ea02016-07-28 18:09:48 -070043#include "sysdeps/stat.h"
44
Josh Gao0aafa0f2020-04-03 10:12:44 -070045#if defined(__APPLE__)
Elliott Hughes681338d2020-04-06 09:15:35 -070046static inline void* mempcpy(void* dst, const void* src, size_t n) {
Josh Gao0aafa0f2020-04-03 10:12:44 -070047 return static_cast<char*>(memcpy(dst, src, n)) + n;
48}
49#endif
50
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#ifdef _WIN32
52
Josh Gaoa166e712016-01-29 12:08:34 -080053// Clang-only nullability specifiers
54#define _Nonnull
55#define _Nullable
56
Dan Albert630b9af2014-11-24 23:34:35 -080057#include <ctype.h>
58#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070059#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080060#include <errno.h>
61#include <fcntl.h>
62#include <io.h>
63#include <process.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070064#include <stdint.h>
Dan Albert630b9af2014-11-24 23:34:35 -080065#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070066#include <utime.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070067#include <windows.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070068#include <winsock2.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080070
Spencer Low2122c7a2015-08-26 18:46:09 -070071#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080072#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040073
Elliott Hughes5c742702015-07-30 17:42:01 -070074#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075#define OS_PATH_SEPARATOR '\\'
76#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070077#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Elliott Hughes681338d2020-04-06 09:15:35 -070079static inline bool adb_is_separator(char c) {
Josh Gao07db1192015-11-07 15:38:19 -080080 return c == '\\' || c == '/';
81}
82
Spencer Low50beee32018-09-03 16:03:22 -070083extern int adb_thread_setname(const std::string& name);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070084
Elliott Hughes681338d2020-04-06 09:15:35 -070085static inline void close_on_exec(borrowed_fd fd) {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020086 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087}
88
Josh Gao27241a72019-04-25 14:04:57 -070089extern int adb_unlink(const char* path);
90#undef unlink
91#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
Spencer Lowcf4ff642015-05-11 01:08:48 -070093extern int adb_mkdir(const std::string& path, int mode);
Josh Gao27241a72019-04-25 14:04:57 -070094#undef mkdir
95#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096
Joshua Duongd85f5c02019-11-20 14:18:43 -080097extern int adb_rename(const char* oldpath, const char* newpath);
98
Spencer Low6ac5d7d2015-05-22 20:09:06 -070099// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -0700100extern int adb_open(const char* path, int options);
101extern int adb_creat(const char* path, int mode);
Josh Gao27241a72019-04-25 14:04:57 -0700102extern int adb_read(borrowed_fd fd, void* buf, int len);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700103extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
Josh Gao27241a72019-04-25 14:04:57 -0700104extern int adb_write(borrowed_fd fd, const void* buf, int len);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700105extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
Josh Gao27241a72019-04-25 14:04:57 -0700106extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
107extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
Josh Gao2e1e7892018-03-23 13:03:28 -0700108extern int adb_close(int fd);
109extern int adb_register_socket(SOCKET s);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700110extern HANDLE adb_get_os_handle(borrowed_fd fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111
Joshua Duongd85f5c02019-11-20 14:18:43 -0800112extern int adb_gethostname(char* name, size_t len);
113extern int adb_getlogin_r(char* buf, size_t bufsize);
114
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700115// See the comments for the !defined(_WIN32) version of unix_close().
Elliott Hughes681338d2020-04-06 09:15:35 -0700116static inline int unix_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 return close(fd);
118}
Josh Gao27241a72019-04-25 14:04:57 -0700119#undef close
120#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121
Spencer Low2e02dc62015-11-07 17:34:39 -0800122// Like unix_read(), but may return EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700123extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800124
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700125// See the comments for the !defined(_WIN32) version of unix_read().
Elliott Hughes681338d2020-04-06 09:15:35 -0700126static inline int unix_read(borrowed_fd fd, void* buf, size_t len) {
Spencer Low2e02dc62015-11-07 17:34:39 -0800127 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
128}
Spencer Low50184062015-03-01 15:06:21 -0800129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130#undef read
131#define read ___xxx_read
132
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700133#undef pread
134#define pread ___xxx_pread
135
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700136// See the comments for the !defined(_WIN32) version of unix_write().
Elliott Hughes681338d2020-04-06 09:15:35 -0700137static inline int unix_write(borrowed_fd fd, const void* buf, size_t len) {
Josh Gao27241a72019-04-25 14:04:57 -0700138 return write(fd.get(), buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139}
140#undef write
141#define write ___xxx_write
142
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700143#undef pwrite
144#define pwrite ___xxx_pwrite
145
Spencer Low40babf02018-09-02 19:19:39 -0700146// See the comments for the !defined(_WIN32) version of unix_lseek().
Elliott Hughes681338d2020-04-06 09:15:35 -0700147static inline int unix_lseek(borrowed_fd fd, int pos, int where) {
Josh Gao27241a72019-04-25 14:04:57 -0700148 return lseek(fd.get(), pos, where);
Spencer Low40babf02018-09-02 19:19:39 -0700149}
150#undef lseek
151#define lseek ___xxx_lseek
152
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700153// See the comments for the !defined(_WIN32) version of adb_open_mode().
Elliott Hughes681338d2020-04-06 09:15:35 -0700154static inline int adb_open_mode(const char* path, int options, int mode) {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200155 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156}
157
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700158// See the comments for the !defined(_WIN32) version of unix_open().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800159extern int unix_open(std::string_view path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160#define open ___xxx_unix_open
161
David Pursellc5b8ad82015-10-28 14:29:51 -0700162// Checks if |fd| corresponds to a console.
163// Standard Windows isatty() returns 1 for both console FDs and character
164// devices like NUL. unix_isatty() performs some extra checking to only match
165// console FDs.
166// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
167// will work but adb_open() FDs will not. Additionally the OS handle associated
168// with |fd| must have GENERIC_READ access (which console FDs have by default).
169// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
170// calling this function is unreliable and should not be used.
Josh Gao27241a72019-04-25 14:04:57 -0700171int unix_isatty(borrowed_fd fd);
David Pursellc5b8ad82015-10-28 14:29:51 -0700172#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Spencer Low5200c662015-07-30 23:07:55 -0700174int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700175
176inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
177 abort();
178}
179
180inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
181 abort();
182}
183
Spencer Low5200c662015-07-30 23:07:55 -0700184int network_connect(const std::string& host, int port, int type, int timeout,
185 std::string* error);
186
Josh Gao27241a72019-04-25 14:04:57 -0700187extern int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
189#undef accept
190#define accept ___xxx_accept
191
David Purselleaae97e2016-04-07 11:25:48 -0700192// Returns the local port number of a bound socket, or -1 on failure.
Josh Gao27241a72019-04-25 14:04:57 -0700193int adb_socket_get_local_port(borrowed_fd fd);
David Purselleaae97e2016-04-07 11:25:48 -0700194
Josh Gao27241a72019-04-25 14:04:57 -0700195extern int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
196 socklen_t optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800197
198#undef setsockopt
199#define setsockopt ___xxx_setsockopt
200
Josh Gao27241a72019-04-25 14:04:57 -0700201extern int adb_socketpair(int sv[2]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
Josh Gao3777d2e2016-02-16 17:34:53 -0800203struct adb_pollfd {
204 int fd;
205 short events;
206 short revents;
207};
208extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
209#define poll ___xxx_poll
210
Elliott Hughes681338d2020-04-06 09:15:35 -0700211static inline int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
213}
214
Spencer Lowcf4ff642015-05-11 01:08:48 -0700215// UTF-8 versions of POSIX APIs.
216extern DIR* adb_opendir(const char* dirname);
217extern struct dirent* adb_readdir(DIR* dir);
218extern int adb_closedir(DIR* dir);
219
220extern int adb_utime(const char *, struct utimbuf *);
221extern int adb_chmod(const char *, int);
222
Elliott Hughesd8a4c602018-06-26 13:06:15 -0700223extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
224 __attribute__((__format__(__printf__, 2, 0)));
225extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
226extern int adb_fprintf(FILE* stream, const char* format, ...)
227 __attribute__((__format__(__printf__, 2, 3)));
228extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700229
230extern int adb_fputs(const char* buf, FILE* stream);
231extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800232extern int adb_putchar(int ch);
233extern int adb_puts(const char* buf);
Josh Gao27241a72019-04-25 14:04:57 -0700234extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700235
236extern FILE* adb_fopen(const char* f, const char* m);
237
238extern char* adb_getenv(const char* name);
239
240extern char* adb_getcwd(char* buf, int size);
241
242// Remap calls to POSIX APIs to our UTF-8 versions.
243#define opendir adb_opendir
244#define readdir adb_readdir
245#define closedir adb_closedir
246#define rewinddir rewinddir_utf8_not_yet_implemented
247#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800248// Some compiler's C++ headers have members named seekdir, so we can't do the
249// macro technique and instead cause a link error if seekdir is called.
250inline void seekdir(DIR*, long) {
251 extern int seekdir_utf8_not_yet_implemented;
252 seekdir_utf8_not_yet_implemented = 1;
253}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700254
255#define utime adb_utime
256#define chmod adb_chmod
257
258#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800259#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700260#define fprintf adb_fprintf
261#define printf adb_printf
262#define fputs adb_fputs
263#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800264// putc may be a macro, so if so, undefine it, so that we can redefine it.
265#undef putc
266#define putc(c, s) adb_fputc(c, s)
267#define putchar adb_putchar
268#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700269#define fwrite adb_fwrite
270
271#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800272#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700273
274#define getenv adb_getenv
275#define putenv putenv_utf8_not_yet_implemented
276#define setenv setenv_utf8_not_yet_implemented
277#define unsetenv unsetenv_utf8_not_yet_implemented
278
279#define getcwd adb_getcwd
280
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800281// A very simple wrapper over a launched child process
282class Process {
283 public:
284 constexpr explicit Process(HANDLE h = nullptr) : h_(h) {}
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700285 constexpr Process(Process&& other) : h_(std::exchange(other.h_, nullptr)) {}
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800286 ~Process() { close(); }
287 constexpr explicit operator bool() const { return h_ != nullptr; }
288
289 void wait() {
290 if (*this) {
291 ::WaitForSingleObject(h_, INFINITE);
292 close();
293 }
294 }
295 void kill() {
296 if (*this) {
297 ::TerminateProcess(h_, -1);
298 }
299 }
300
301 private:
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700302 DISALLOW_COPY_AND_ASSIGN(Process);
303
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800304 void close() {
305 if (*this) {
306 ::CloseHandle(h_);
307 h_ = nullptr;
308 }
309 }
310
311 HANDLE h_;
312};
313
314Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
315 std::initializer_list<int> fds_to_inherit = {});
316
Spencer Lowcf4ff642015-05-11 01:08:48 -0700317// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
318// passed to main().
319class NarrowArgs {
320public:
321 NarrowArgs(int argc, wchar_t** argv);
322 ~NarrowArgs();
323
324 inline char** data() {
325 return narrow_args;
326 }
327
328private:
329 char** narrow_args;
330};
331
Spencer Low5c398d22015-08-08 15:07:07 -0700332// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
333// so they can fit in an int. To convert back, we just need to sign-extend.
334// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
335// Note that this does not make a HANDLE value work with APIs like open(), nor
336// does this make a value from open() passable to APIs taking a HANDLE. This
337// just lets you take a HANDLE, pass it around as an int, and then use it again
338// as a HANDLE.
339inline int cast_handle_to_int(const HANDLE h) {
340 // truncate
341 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
342}
343
344inline HANDLE cast_int_to_handle(const int fd) {
345 // sign-extend
346 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
347}
348
Spencer Low2122c7a2015-08-26 18:46:09 -0700349// Deleter for unique_handle. Adapted from many sources, including:
350// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
351// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
352class handle_deleter {
353public:
354 typedef HANDLE pointer;
355
356 void operator()(HANDLE h);
357};
358
359// Like std::unique_ptr, but for Windows HANDLE objects that should be
360// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
361// but does not check if the handle != INVALID_HANDLE_VALUE.
362typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
363
Spencer Lowa30b79a2015-11-15 16:29:36 -0800364namespace internal {
365
366size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
367
368}
369
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370#else /* !_WIN32 a.k.a. Unix */
371
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372#include <fcntl.h>
Spencer Low5200c662015-07-30 23:07:55 -0700373#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374#include <netinet/in.h>
375#include <netinet/tcp.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700376#include <poll.h>
377#include <pthread.h>
378#include <signal.h>
379#include <stdarg.h>
380#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381#include <string.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700382#include <sys/stat.h>
383#include <sys/wait.h>
Kenny Root73167412012-10-12 15:26:45 -0700384#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
Alex Vallée47d67c92015-05-06 16:26:00 -0400386#include <string>
387
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700388#include <cutils/sockets.h>
389
Elliott Hughes5c742702015-07-30 17:42:01 -0700390#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391#define OS_PATH_SEPARATOR '/'
392#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700393#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394
Elliott Hughes681338d2020-04-06 09:15:35 -0700395static inline bool adb_is_separator(char c) {
Josh Gao07db1192015-11-07 15:38:19 -0800396 return c == '/';
397}
398
Elliott Hughes681338d2020-04-06 09:15:35 -0700399static inline int get_fd_flags(borrowed_fd fd) {
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800400 return fcntl(fd.get(), F_GETFD);
401}
402
Elliott Hughes681338d2020-04-06 09:15:35 -0700403static inline void close_on_exec(borrowed_fd fd) {
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800404 int flags = get_fd_flags(fd);
405 if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
406 fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
407 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408}
409
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700410// Open a file and return a file descriptor that may be used with unix_read(),
411// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
412//
413// On Unix, this is based on open(), so the file descriptor is a real OS file
414// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
415// file descriptor that can only be used with C Runtime APIs (which are wrapped
416// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
417// configurable CR/LF translation which defaults to text mode, but is settable
418// with _setmode().
Elliott Hughes681338d2020-04-06 09:15:35 -0700419static inline int unix_open(std::string_view path, int options, ...) {
Josh Gao0f29cbc2018-12-12 16:12:28 -0800420 std::string zero_terminated(path.begin(), path.end());
421 if ((options & O_CREAT) == 0) {
422 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
423 } else {
424 int mode;
425 va_list args;
426 va_start(args, options);
427 mode = va_arg(args, int);
428 va_end(args);
429 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 }
431}
432
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700433// Similar to the two-argument adb_open(), but takes a mode parameter for file
434// creation. See adb_open() for more info.
Elliott Hughes681338d2020-04-06 09:15:35 -0700435static inline int adb_open_mode(const char* pathname, int options, int mode) {
Josh Gao27241a72019-04-25 14:04:57 -0700436 return TEMP_FAILURE_RETRY(open(pathname, options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437}
438
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700439// Open a file and return a file descriptor that may be used with adb_read(),
440// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
441//
442// On Unix, this is based on open(), but the Windows implementation (in
443// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
444// and its CR/LF translation. The returned file descriptor should be used with
445// adb_read(), adb_write(), adb_close(), etc.
Elliott Hughes681338d2020-04-06 09:15:35 -0700446static inline int adb_open(const char* pathname, int options) {
Josh Gao27241a72019-04-25 14:04:57 -0700447 int fd = TEMP_FAILURE_RETRY(open(pathname, options));
448 if (fd < 0) return -1;
449 close_on_exec(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 return fd;
451}
Josh Gao27241a72019-04-25 14:04:57 -0700452#undef open
453#define open ___xxx_open
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454
Elliott Hughes681338d2020-04-06 09:15:35 -0700455static inline int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
Josh Gao27241a72019-04-25 14:04:57 -0700456 return shutdown(fd.get(), direction);
David Pursell1ed57f02015-10-06 15:30:03 -0700457}
Josh Gao2e1e7892018-03-23 13:03:28 -0700458
Josh Gao27241a72019-04-25 14:04:57 -0700459#undef shutdown
460#define shutdown ____xxx_shutdown
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400461
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700462// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
463// not designed to take a file descriptor from unix_open(). See the comments
464// for adb_open() for more info.
Elliott Hughes681338d2020-04-06 09:15:35 -0700465inline int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 return close(fd);
467}
Josh Gao27241a72019-04-25 14:04:57 -0700468#undef close
469#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700471// On Windows, ADB has an indirection layer for file descriptors. If we get a
472// Win32 SOCKET object from an external library, we have to map it in to that
473// indirection layer, which this does.
Elliott Hughes681338d2020-04-06 09:15:35 -0700474inline int adb_register_socket(int s) {
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700475 return s;
476}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477
Elliott Hughes681338d2020-04-06 09:15:35 -0700478static inline int adb_gethostname(char* name, size_t len) {
Joshua Duongd85f5c02019-11-20 14:18:43 -0800479 return gethostname(name, len);
480}
481
Elliott Hughes681338d2020-04-06 09:15:35 -0700482static inline int adb_getlogin_r(char* buf, size_t bufsize) {
Joshua Duongd85f5c02019-11-20 14:18:43 -0800483 return getlogin_r(buf, bufsize);
484}
485
Elliott Hughes681338d2020-04-06 09:15:35 -0700486static inline int adb_read(borrowed_fd fd, void* buf, size_t len) {
Josh Gao27241a72019-04-25 14:04:57 -0700487 return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488}
489
Elliott Hughes681338d2020-04-06 09:15:35 -0700490static inline int adb_pread(borrowed_fd fd, void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700491#if defined(__APPLE__)
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800492 return TEMP_FAILURE_RETRY(pread(fd.get(), buf, len, offset));
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700493#else
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800494 return TEMP_FAILURE_RETRY(pread64(fd.get(), buf, len, offset));
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700495#endif
496}
497
Spencer Low2e02dc62015-11-07 17:34:39 -0800498// Like unix_read(), but does not handle EINTR.
Elliott Hughes681338d2020-04-06 09:15:35 -0700499static inline int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
Josh Gao27241a72019-04-25 14:04:57 -0700500 return read(fd.get(), buf, len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800501}
502
Josh Gao27241a72019-04-25 14:04:57 -0700503#undef read
504#define read ___xxx_read
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700505#undef pread
506#define pread ___xxx_pread
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507
Elliott Hughes681338d2020-04-06 09:15:35 -0700508static inline int adb_write(borrowed_fd fd, const void* buf, size_t len) {
Josh Gao27241a72019-04-25 14:04:57 -0700509 return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510}
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700511
Elliott Hughes681338d2020-04-06 09:15:35 -0700512static inline int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700513#if defined(__APPLE__)
514 return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
515#else
516 return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
517#endif
518}
519
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520#undef write
521#define write ___xxx_write
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700522#undef pwrite
523#define pwrite ___xxx_pwrite
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524
Elliott Hughes681338d2020-04-06 09:15:35 -0700525static inline int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700526#if defined(__APPLE__)
Josh Gao27241a72019-04-25 14:04:57 -0700527 return lseek(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700528#else
Josh Gao27241a72019-04-25 14:04:57 -0700529 return lseek64(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700530#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531}
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700532#undef lseek
533#define lseek ___xxx_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534
Elliott Hughes681338d2020-04-06 09:15:35 -0700535static inline int adb_unlink(const char* path) {
Josh Gao27241a72019-04-25 14:04:57 -0700536 return unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537}
Josh Gao27241a72019-04-25 14:04:57 -0700538#undef unlink
539#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540
Elliott Hughes681338d2020-04-06 09:15:35 -0700541static inline int adb_creat(const char* path, int mode) {
Josh Gao27241a72019-04-25 14:04:57 -0700542 int fd = TEMP_FAILURE_RETRY(creat(path, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543
Josh Gao27241a72019-04-25 14:04:57 -0700544 if (fd < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545
546 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200547 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548}
Josh Gao27241a72019-04-25 14:04:57 -0700549#undef creat
550#define creat ___xxx_creat
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551
Elliott Hughes681338d2020-04-06 09:15:35 -0700552static inline int unix_isatty(borrowed_fd fd) {
Josh Gao27241a72019-04-25 14:04:57 -0700553 return isatty(fd.get());
David Pursellc5b8ad82015-10-28 14:29:51 -0700554}
Josh Gao27241a72019-04-25 14:04:57 -0700555#define isatty ___xxx_isatty
David Pursellc5b8ad82015-10-28 14:29:51 -0700556
Spencer Low5200c662015-07-30 23:07:55 -0700557// Helper for network_* functions.
558inline int _fd_set_error_str(int fd, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700559 if (fd == -1) {
560 *error = strerror(errno);
561 }
562 return fd;
Spencer Low5200c662015-07-30 23:07:55 -0700563}
564
Spencer Low5200c662015-07-30 23:07:55 -0700565inline int network_inaddr_any_server(int port, int type, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700566 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700567}
568
Josh Gaocfb21412016-08-24 18:38:44 -0700569inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
570 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
571}
572
573inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
574 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700575}
576
Josh Gao45e3e952018-08-10 16:03:09 -0700577int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700578
Elliott Hughes681338d2020-04-06 09:15:35 -0700579static inline int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
580 socklen_t* addrlen) {
Benoit Goby95ef8282011-02-01 18:57:41 -0800581 int fd;
582
Josh Gao27241a72019-04-25 14:04:57 -0700583 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
584 if (fd >= 0) close_on_exec(fd);
Benoit Goby95ef8282011-02-01 18:57:41 -0800585
586 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587}
588
Josh Gao27241a72019-04-25 14:04:57 -0700589#undef accept
590#define accept ___xxx_accept
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591
Josh Gao27241a72019-04-25 14:04:57 -0700592inline int adb_socket_get_local_port(borrowed_fd fd) {
593 return socket_get_local_port(fd.get());
David Purselleaae97e2016-04-07 11:25:48 -0700594}
595
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700596// Operate on a file descriptor returned from unix_open() or a well-known file
597// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
598//
599// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
600// adb_write(), adb_close() (which all map to Unix system calls), but the
601// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
602// into the C Runtime and its configurable CR/LF translation (which is settable
603// via _setmode()).
Josh Gao27241a72019-04-25 14:04:57 -0700604#define unix_read adb_read
605#define unix_write adb_write
Spencer Low40babf02018-09-02 19:19:39 -0700606#define unix_lseek adb_lseek
Josh Gao27241a72019-04-25 14:04:57 -0700607#define unix_close adb_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608
Elliott Hughes681338d2020-04-06 09:15:35 -0700609static inline int adb_thread_setname(const std::string& name) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700610#ifdef __APPLE__
611 return pthread_setname_np(name.c_str());
612#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700613 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
614 // glibc doesn't have strlcpy, so we have to fake it.
615 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
616 strncpy(buf, name.c_str(), sizeof(buf) - 1);
617 buf[sizeof(buf) - 1] = '\0';
618 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700619#endif
620}
621
Elliott Hughes681338d2020-04-06 09:15:35 -0700622static inline int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
623 socklen_t optlen) {
Josh Gao27241a72019-04-25 14:04:57 -0700624 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800625}
626
Josh Gao27241a72019-04-25 14:04:57 -0700627#undef setsockopt
628#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629
Elliott Hughes681338d2020-04-06 09:15:35 -0700630static inline int unix_socketpair(int d, int type, int protocol, int sv[2]) {
Josh Gao27241a72019-04-25 14:04:57 -0700631 return socketpair(d, type, protocol, sv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632}
633
Elliott Hughes681338d2020-04-06 09:15:35 -0700634static inline int adb_socketpair(int sv[2]) {
Josh Gao27241a72019-04-25 14:04:57 -0700635 int rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636
Josh Gao27241a72019-04-25 14:04:57 -0700637 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
638 if (rc < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639
Josh Gao27241a72019-04-25 14:04:57 -0700640 close_on_exec(sv[0]);
641 close_on_exec(sv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 return 0;
643}
644
Josh Gao27241a72019-04-25 14:04:57 -0700645#undef socketpair
646#define socketpair ___xxx_socketpair
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647
Josh Gao3777d2e2016-02-16 17:34:53 -0800648typedef struct pollfd adb_pollfd;
Elliott Hughes681338d2020-04-06 09:15:35 -0700649static inline int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
Josh Gao3777d2e2016-02-16 17:34:53 -0800650 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
651}
652
653#define poll ___xxx_poll
654
Elliott Hughes681338d2020-04-06 09:15:35 -0700655static inline int adb_mkdir(const std::string& path, int mode) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400656 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657}
Alex Vallée47d67c92015-05-06 16:26:00 -0400658
Josh Gao27241a72019-04-25 14:04:57 -0700659#undef mkdir
660#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661
Elliott Hughes681338d2020-04-06 09:15:35 -0700662static inline int adb_rename(const char* oldpath, const char* newpath) {
Joshua Duongd85f5c02019-11-20 14:18:43 -0800663 return rename(oldpath, newpath);
664}
665
Elliott Hughes681338d2020-04-06 09:15:35 -0700666static inline int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 return path[0] == '/';
668}
669
Elliott Hughes681338d2020-04-06 09:15:35 -0700670static inline int adb_get_os_handle(borrowed_fd fd) {
Alex Buynytskyy665f3ff2019-09-16 12:10:54 -0700671 return fd.get();
672}
673
Elliott Hughes681338d2020-04-06 09:15:35 -0700674static inline int cast_handle_to_int(int fd) {
Yurii Zubrytskyie3e64b82020-03-26 18:16:36 -0700675 return fd;
676}
677
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800678// A very simple wrapper over a launched child process
679class Process {
680 public:
681 constexpr explicit Process(pid_t pid) : pid_(pid) {}
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700682 constexpr Process(Process&& other) : pid_(std::exchange(other.pid_, -1)) {}
683
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800684 constexpr explicit operator bool() const { return pid_ >= 0; }
685
686 void wait() {
687 if (*this) {
688 int status;
689 ::waitpid(pid_, &status, 0);
690 pid_ = -1;
691 }
692 }
693 void kill() {
694 if (*this) {
695 ::kill(pid_, SIGTERM);
696 }
697 }
698
699 private:
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700700 DISALLOW_COPY_AND_ASSIGN(Process);
701
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800702 pid_t pid_;
703};
704
705Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
706 std::initializer_list<int> fds_to_inherit = {});
707
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708#endif /* !_WIN32 */
709
Josh Gao27241a72019-04-25 14:04:57 -0700710static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800711 int off = 1;
Josh Gao27241a72019-04-25 14:04:57 -0700712 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800713}
714
David Pursellbfd95032016-02-22 14:27:23 -0800715// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
716// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
717// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao27241a72019-04-25 14:04:57 -0700718bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellbfd95032016-02-22 14:27:23 -0800719
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700720#if defined(_WIN32)
721// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
722#undef ERROR
723#endif