blob: 4efbc02c3145d020eca1a6e4ff0ca19a47f2d28a [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
17/* this file contains system-dependent definitions used by ADB
18 * they're related to threads, sockets and file descriptors
19 */
20#ifndef _ADB_SYSDEPS_H
21#define _ADB_SYSDEPS_H
22
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045#ifdef _WIN32
46
Josh Gaoa166e712016-01-29 12:08:34 -080047// Clang-only nullability specifiers
48#define _Nonnull
49#define _Nullable
50
Dan Albert630b9af2014-11-24 23:34:35 -080051#include <ctype.h>
52#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070053#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080054#include <errno.h>
55#include <fcntl.h>
56#include <io.h>
57#include <process.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070058#include <stdint.h>
Dan Albert630b9af2014-11-24 23:34:35 -080059#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070060#include <utime.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070061#include <windows.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070062#include <winsock2.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080064
Spencer Low2122c7a2015-08-26 18:46:09 -070065#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080066#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040067
Elliott Hughes5c742702015-07-30 17:42:01 -070068#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069#define OS_PATH_SEPARATOR '\\'
70#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070071#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Josh Gao07db1192015-11-07 15:38:19 -080073static __inline__ bool adb_is_separator(char c) {
74 return c == '\\' || c == '/';
75}
76
Spencer Low50beee32018-09-03 16:03:22 -070077extern int adb_thread_setname(const std::string& name);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070078
Josh Gao27241a72019-04-25 14:04:57 -070079static __inline__ void close_on_exec(borrowed_fd fd) {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020080 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081}
82
Josh Gao27241a72019-04-25 14:04:57 -070083extern int adb_unlink(const char* path);
84#undef unlink
85#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086
Spencer Lowcf4ff642015-05-11 01:08:48 -070087extern int adb_mkdir(const std::string& path, int mode);
Josh Gao27241a72019-04-25 14:04:57 -070088#undef mkdir
89#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Joshua Duongd85f5c02019-11-20 14:18:43 -080091extern int adb_rename(const char* oldpath, const char* newpath);
92
Spencer Low6ac5d7d2015-05-22 20:09:06 -070093// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -070094extern int adb_open(const char* path, int options);
95extern int adb_creat(const char* path, int mode);
Josh Gao27241a72019-04-25 14:04:57 -070096extern int adb_read(borrowed_fd fd, void* buf, int len);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -070097extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
Josh Gao27241a72019-04-25 14:04:57 -070098extern int adb_write(borrowed_fd fd, const void* buf, int len);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -070099extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
Josh Gao27241a72019-04-25 14:04:57 -0700100extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
101extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
Josh Gao2e1e7892018-03-23 13:03:28 -0700102extern int adb_close(int fd);
103extern int adb_register_socket(SOCKET s);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700104extern HANDLE adb_get_os_handle(borrowed_fd fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105
Joshua Duongd85f5c02019-11-20 14:18:43 -0800106extern int adb_gethostname(char* name, size_t len);
107extern int adb_getlogin_r(char* buf, size_t bufsize);
108
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700109// See the comments for the !defined(_WIN32) version of unix_close().
Josh Gao27241a72019-04-25 14:04:57 -0700110static __inline__ int unix_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 return close(fd);
112}
Josh Gao27241a72019-04-25 14:04:57 -0700113#undef close
114#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115
Spencer Low2e02dc62015-11-07 17:34:39 -0800116// Like unix_read(), but may return EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700117extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800118
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700119// See the comments for the !defined(_WIN32) version of unix_read().
Josh Gao27241a72019-04-25 14:04:57 -0700120static __inline__ int unix_read(borrowed_fd fd, void* buf, size_t len) {
Spencer Low2e02dc62015-11-07 17:34:39 -0800121 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
122}
Spencer Low50184062015-03-01 15:06:21 -0800123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124#undef read
125#define read ___xxx_read
126
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700127#undef pread
128#define pread ___xxx_pread
129
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700130// See the comments for the !defined(_WIN32) version of unix_write().
Josh Gao27241a72019-04-25 14:04:57 -0700131static __inline__ int unix_write(borrowed_fd fd, const void* buf, size_t len) {
132 return write(fd.get(), buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133}
134#undef write
135#define write ___xxx_write
136
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700137#undef pwrite
138#define pwrite ___xxx_pwrite
139
Spencer Low40babf02018-09-02 19:19:39 -0700140// See the comments for the !defined(_WIN32) version of unix_lseek().
Josh Gao27241a72019-04-25 14:04:57 -0700141static __inline__ int unix_lseek(borrowed_fd fd, int pos, int where) {
142 return lseek(fd.get(), pos, where);
Spencer Low40babf02018-09-02 19:19:39 -0700143}
144#undef lseek
145#define lseek ___xxx_lseek
146
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700147// See the comments for the !defined(_WIN32) version of adb_open_mode().
Josh Gao27241a72019-04-25 14:04:57 -0700148static __inline__ int adb_open_mode(const char* path, int options, int mode) {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200149 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150}
151
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700152// See the comments for the !defined(_WIN32) version of unix_open().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800153extern int unix_open(std::string_view path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154#define open ___xxx_unix_open
155
David Pursellc5b8ad82015-10-28 14:29:51 -0700156// Checks if |fd| corresponds to a console.
157// Standard Windows isatty() returns 1 for both console FDs and character
158// devices like NUL. unix_isatty() performs some extra checking to only match
159// console FDs.
160// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
161// will work but adb_open() FDs will not. Additionally the OS handle associated
162// with |fd| must have GENERIC_READ access (which console FDs have by default).
163// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
164// calling this function is unreliable and should not be used.
Josh Gao27241a72019-04-25 14:04:57 -0700165int unix_isatty(borrowed_fd fd);
David Pursellc5b8ad82015-10-28 14:29:51 -0700166#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Spencer Low5200c662015-07-30 23:07:55 -0700168int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700169
170inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
171 abort();
172}
173
174inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
175 abort();
176}
177
Spencer Low5200c662015-07-30 23:07:55 -0700178int network_connect(const std::string& host, int port, int type, int timeout,
179 std::string* error);
180
Josh Gao27241a72019-04-25 14:04:57 -0700181extern int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
183#undef accept
184#define accept ___xxx_accept
185
David Purselleaae97e2016-04-07 11:25:48 -0700186// Returns the local port number of a bound socket, or -1 on failure.
Josh Gao27241a72019-04-25 14:04:57 -0700187int adb_socket_get_local_port(borrowed_fd fd);
David Purselleaae97e2016-04-07 11:25:48 -0700188
Josh Gao27241a72019-04-25 14:04:57 -0700189extern int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
190 socklen_t optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800191
192#undef setsockopt
193#define setsockopt ___xxx_setsockopt
194
Josh Gao27241a72019-04-25 14:04:57 -0700195extern int adb_socketpair(int sv[2]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Josh Gao3777d2e2016-02-16 17:34:53 -0800197struct adb_pollfd {
198 int fd;
199 short events;
200 short revents;
201};
202extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
203#define poll ___xxx_poll
204
Elliott Hughes5c742702015-07-30 17:42:01 -0700205static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
207}
208
Spencer Lowcf4ff642015-05-11 01:08:48 -0700209// UTF-8 versions of POSIX APIs.
210extern DIR* adb_opendir(const char* dirname);
211extern struct dirent* adb_readdir(DIR* dir);
212extern int adb_closedir(DIR* dir);
213
214extern int adb_utime(const char *, struct utimbuf *);
215extern int adb_chmod(const char *, int);
216
Elliott Hughesd8a4c602018-06-26 13:06:15 -0700217extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
218 __attribute__((__format__(__printf__, 2, 0)));
219extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
220extern int adb_fprintf(FILE* stream, const char* format, ...)
221 __attribute__((__format__(__printf__, 2, 3)));
222extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700223
224extern int adb_fputs(const char* buf, FILE* stream);
225extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800226extern int adb_putchar(int ch);
227extern int adb_puts(const char* buf);
Josh Gao27241a72019-04-25 14:04:57 -0700228extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700229
230extern FILE* adb_fopen(const char* f, const char* m);
231
232extern char* adb_getenv(const char* name);
233
234extern char* adb_getcwd(char* buf, int size);
235
236// Remap calls to POSIX APIs to our UTF-8 versions.
237#define opendir adb_opendir
238#define readdir adb_readdir
239#define closedir adb_closedir
240#define rewinddir rewinddir_utf8_not_yet_implemented
241#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800242// Some compiler's C++ headers have members named seekdir, so we can't do the
243// macro technique and instead cause a link error if seekdir is called.
244inline void seekdir(DIR*, long) {
245 extern int seekdir_utf8_not_yet_implemented;
246 seekdir_utf8_not_yet_implemented = 1;
247}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700248
249#define utime adb_utime
250#define chmod adb_chmod
251
252#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800253#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700254#define fprintf adb_fprintf
255#define printf adb_printf
256#define fputs adb_fputs
257#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800258// putc may be a macro, so if so, undefine it, so that we can redefine it.
259#undef putc
260#define putc(c, s) adb_fputc(c, s)
261#define putchar adb_putchar
262#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700263#define fwrite adb_fwrite
264
265#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800266#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700267
268#define getenv adb_getenv
269#define putenv putenv_utf8_not_yet_implemented
270#define setenv setenv_utf8_not_yet_implemented
271#define unsetenv unsetenv_utf8_not_yet_implemented
272
273#define getcwd adb_getcwd
274
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800275// A very simple wrapper over a launched child process
276class Process {
277 public:
278 constexpr explicit Process(HANDLE h = nullptr) : h_(h) {}
279 ~Process() { close(); }
280 constexpr explicit operator bool() const { return h_ != nullptr; }
281
282 void wait() {
283 if (*this) {
284 ::WaitForSingleObject(h_, INFINITE);
285 close();
286 }
287 }
288 void kill() {
289 if (*this) {
290 ::TerminateProcess(h_, -1);
291 }
292 }
293
294 private:
295 void close() {
296 if (*this) {
297 ::CloseHandle(h_);
298 h_ = nullptr;
299 }
300 }
301
302 HANDLE h_;
303};
304
305Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
306 std::initializer_list<int> fds_to_inherit = {});
307
Spencer Lowcf4ff642015-05-11 01:08:48 -0700308// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
309// passed to main().
310class NarrowArgs {
311public:
312 NarrowArgs(int argc, wchar_t** argv);
313 ~NarrowArgs();
314
315 inline char** data() {
316 return narrow_args;
317 }
318
319private:
320 char** narrow_args;
321};
322
Spencer Low5c398d22015-08-08 15:07:07 -0700323// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
324// so they can fit in an int. To convert back, we just need to sign-extend.
325// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
326// Note that this does not make a HANDLE value work with APIs like open(), nor
327// does this make a value from open() passable to APIs taking a HANDLE. This
328// just lets you take a HANDLE, pass it around as an int, and then use it again
329// as a HANDLE.
330inline int cast_handle_to_int(const HANDLE h) {
331 // truncate
332 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
333}
334
335inline HANDLE cast_int_to_handle(const int fd) {
336 // sign-extend
337 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
338}
339
Spencer Low2122c7a2015-08-26 18:46:09 -0700340// Deleter for unique_handle. Adapted from many sources, including:
341// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
342// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
343class handle_deleter {
344public:
345 typedef HANDLE pointer;
346
347 void operator()(HANDLE h);
348};
349
350// Like std::unique_ptr, but for Windows HANDLE objects that should be
351// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
352// but does not check if the handle != INVALID_HANDLE_VALUE.
353typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
354
Spencer Lowa30b79a2015-11-15 16:29:36 -0800355namespace internal {
356
357size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
358
359}
360
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361#else /* !_WIN32 a.k.a. Unix */
362
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363#include <fcntl.h>
Spencer Low5200c662015-07-30 23:07:55 -0700364#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365#include <netinet/in.h>
366#include <netinet/tcp.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700367#include <poll.h>
368#include <pthread.h>
369#include <signal.h>
370#include <stdarg.h>
371#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372#include <string.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700373#include <sys/stat.h>
374#include <sys/wait.h>
Kenny Root73167412012-10-12 15:26:45 -0700375#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Alex Vallée47d67c92015-05-06 16:26:00 -0400377#include <string>
378
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700379#include <cutils/sockets.h>
380
Elliott Hughes5c742702015-07-30 17:42:01 -0700381#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382#define OS_PATH_SEPARATOR '/'
383#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700384#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
Josh Gao07db1192015-11-07 15:38:19 -0800386static __inline__ bool adb_is_separator(char c) {
387 return c == '/';
388}
389
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800390static __inline__ int get_fd_flags(borrowed_fd fd) {
391 return fcntl(fd.get(), F_GETFD);
392}
393
Josh Gao27241a72019-04-25 14:04:57 -0700394static __inline__ void close_on_exec(borrowed_fd fd) {
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800395 int flags = get_fd_flags(fd);
396 if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
397 fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
398 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399}
400
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700401// Open a file and return a file descriptor that may be used with unix_read(),
402// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
403//
404// On Unix, this is based on open(), so the file descriptor is a real OS file
405// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
406// file descriptor that can only be used with C Runtime APIs (which are wrapped
407// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
408// configurable CR/LF translation which defaults to text mode, but is settable
409// with _setmode().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800410static __inline__ int unix_open(std::string_view path, int options, ...) {
411 std::string zero_terminated(path.begin(), path.end());
412 if ((options & O_CREAT) == 0) {
413 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
414 } else {
415 int mode;
416 va_list args;
417 va_start(args, options);
418 mode = va_arg(args, int);
419 va_end(args);
420 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 }
422}
423
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700424// Similar to the two-argument adb_open(), but takes a mode parameter for file
425// creation. See adb_open() for more info.
Josh Gao27241a72019-04-25 14:04:57 -0700426static __inline__ int adb_open_mode(const char* pathname, int options, int mode) {
427 return TEMP_FAILURE_RETRY(open(pathname, options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428}
429
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700430// Open a file and return a file descriptor that may be used with adb_read(),
431// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
432//
433// On Unix, this is based on open(), but the Windows implementation (in
434// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
435// and its CR/LF translation. The returned file descriptor should be used with
436// adb_read(), adb_write(), adb_close(), etc.
Josh Gao27241a72019-04-25 14:04:57 -0700437static __inline__ int adb_open(const char* pathname, int options) {
438 int fd = TEMP_FAILURE_RETRY(open(pathname, options));
439 if (fd < 0) return -1;
440 close_on_exec(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 return fd;
442}
Josh Gao27241a72019-04-25 14:04:57 -0700443#undef open
444#define open ___xxx_open
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445
Josh Gao27241a72019-04-25 14:04:57 -0700446static __inline__ int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
447 return shutdown(fd.get(), direction);
David Pursell1ed57f02015-10-06 15:30:03 -0700448}
Josh Gao2e1e7892018-03-23 13:03:28 -0700449
Josh Gao27241a72019-04-25 14:04:57 -0700450#undef shutdown
451#define shutdown ____xxx_shutdown
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400452
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700453// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
454// not designed to take a file descriptor from unix_open(). See the comments
455// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800456__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 return close(fd);
458}
Josh Gao27241a72019-04-25 14:04:57 -0700459#undef close
460#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700462// On Windows, ADB has an indirection layer for file descriptors. If we get a
463// Win32 SOCKET object from an external library, we have to map it in to that
464// indirection layer, which this does.
Josh Gao27241a72019-04-25 14:04:57 -0700465__inline__ int adb_register_socket(int s) {
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700466 return s;
467}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468
Joshua Duongd85f5c02019-11-20 14:18:43 -0800469static __inline__ int adb_gethostname(char* name, size_t len) {
470 return gethostname(name, len);
471}
472
473static __inline__ int adb_getlogin_r(char* buf, size_t bufsize) {
474 return getlogin_r(buf, bufsize);
475}
476
Josh Gao27241a72019-04-25 14:04:57 -0700477static __inline__ int adb_read(borrowed_fd fd, void* buf, size_t len) {
478 return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479}
480
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800481static __inline__ int adb_pread(borrowed_fd fd, void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700482#if defined(__APPLE__)
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800483 return TEMP_FAILURE_RETRY(pread(fd.get(), buf, len, offset));
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700484#else
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800485 return TEMP_FAILURE_RETRY(pread64(fd.get(), buf, len, offset));
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700486#endif
487}
488
Spencer Low2e02dc62015-11-07 17:34:39 -0800489// Like unix_read(), but does not handle EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700490static __inline__ int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
491 return read(fd.get(), buf, len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800492}
493
Josh Gao27241a72019-04-25 14:04:57 -0700494#undef read
495#define read ___xxx_read
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700496#undef pread
497#define pread ___xxx_pread
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498
Josh Gao27241a72019-04-25 14:04:57 -0700499static __inline__ int adb_write(borrowed_fd fd, const void* buf, size_t len) {
500 return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501}
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700502
503static __inline__ int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
504#if defined(__APPLE__)
505 return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
506#else
507 return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
508#endif
509}
510
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511#undef write
512#define write ___xxx_write
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700513#undef pwrite
514#define pwrite ___xxx_pwrite
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515
Josh Gao27241a72019-04-25 14:04:57 -0700516static __inline__ int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700517#if defined(__APPLE__)
Josh Gao27241a72019-04-25 14:04:57 -0700518 return lseek(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700519#else
Josh Gao27241a72019-04-25 14:04:57 -0700520 return lseek64(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700521#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522}
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700523#undef lseek
524#define lseek ___xxx_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525
Josh Gao27241a72019-04-25 14:04:57 -0700526static __inline__ int adb_unlink(const char* path) {
527 return unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528}
Josh Gao27241a72019-04-25 14:04:57 -0700529#undef unlink
530#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531
Josh Gao27241a72019-04-25 14:04:57 -0700532static __inline__ int adb_creat(const char* path, int mode) {
533 int fd = TEMP_FAILURE_RETRY(creat(path, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534
Josh Gao27241a72019-04-25 14:04:57 -0700535 if (fd < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536
537 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200538 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539}
Josh Gao27241a72019-04-25 14:04:57 -0700540#undef creat
541#define creat ___xxx_creat
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542
Josh Gao27241a72019-04-25 14:04:57 -0700543static __inline__ int unix_isatty(borrowed_fd fd) {
544 return isatty(fd.get());
David Pursellc5b8ad82015-10-28 14:29:51 -0700545}
Josh Gao27241a72019-04-25 14:04:57 -0700546#define isatty ___xxx_isatty
David Pursellc5b8ad82015-10-28 14:29:51 -0700547
Spencer Low5200c662015-07-30 23:07:55 -0700548// Helper for network_* functions.
549inline int _fd_set_error_str(int fd, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700550 if (fd == -1) {
551 *error = strerror(errno);
552 }
553 return fd;
Spencer Low5200c662015-07-30 23:07:55 -0700554}
555
Spencer Low5200c662015-07-30 23:07:55 -0700556inline int network_inaddr_any_server(int port, int type, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700557 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700558}
559
Josh Gaocfb21412016-08-24 18:38:44 -0700560inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
561 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
562}
563
564inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
565 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700566}
567
Josh Gao45e3e952018-08-10 16:03:09 -0700568int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700569
Josh Gao27241a72019-04-25 14:04:57 -0700570static __inline__ int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
571 socklen_t* addrlen) {
Benoit Goby95ef8282011-02-01 18:57:41 -0800572 int fd;
573
Josh Gao27241a72019-04-25 14:04:57 -0700574 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
575 if (fd >= 0) close_on_exec(fd);
Benoit Goby95ef8282011-02-01 18:57:41 -0800576
577 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800578}
579
Josh Gao27241a72019-04-25 14:04:57 -0700580#undef accept
581#define accept ___xxx_accept
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582
Josh Gao27241a72019-04-25 14:04:57 -0700583inline int adb_socket_get_local_port(borrowed_fd fd) {
584 return socket_get_local_port(fd.get());
David Purselleaae97e2016-04-07 11:25:48 -0700585}
586
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700587// Operate on a file descriptor returned from unix_open() or a well-known file
588// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
589//
590// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
591// adb_write(), adb_close() (which all map to Unix system calls), but the
592// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
593// into the C Runtime and its configurable CR/LF translation (which is settable
594// via _setmode()).
Josh Gao27241a72019-04-25 14:04:57 -0700595#define unix_read adb_read
596#define unix_write adb_write
Spencer Low40babf02018-09-02 19:19:39 -0700597#define unix_lseek adb_lseek
Josh Gao27241a72019-04-25 14:04:57 -0700598#define unix_close adb_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700600static __inline__ int adb_thread_setname(const std::string& name) {
601#ifdef __APPLE__
602 return pthread_setname_np(name.c_str());
603#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700604 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
605 // glibc doesn't have strlcpy, so we have to fake it.
606 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
607 strncpy(buf, name.c_str(), sizeof(buf) - 1);
608 buf[sizeof(buf) - 1] = '\0';
609 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700610#endif
611}
612
Josh Gao27241a72019-04-25 14:04:57 -0700613static __inline__ int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
614 socklen_t optlen) {
615 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800616}
617
Josh Gao27241a72019-04-25 14:04:57 -0700618#undef setsockopt
619#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620
Josh Gao27241a72019-04-25 14:04:57 -0700621static __inline__ int unix_socketpair(int d, int type, int protocol, int sv[2]) {
622 return socketpair(d, type, protocol, sv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623}
624
Josh Gao27241a72019-04-25 14:04:57 -0700625static __inline__ int adb_socketpair(int sv[2]) {
626 int rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627
Josh Gao27241a72019-04-25 14:04:57 -0700628 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
629 if (rc < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630
Josh Gao27241a72019-04-25 14:04:57 -0700631 close_on_exec(sv[0]);
632 close_on_exec(sv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 return 0;
634}
635
Josh Gao27241a72019-04-25 14:04:57 -0700636#undef socketpair
637#define socketpair ___xxx_socketpair
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638
Josh Gao3777d2e2016-02-16 17:34:53 -0800639typedef struct pollfd adb_pollfd;
640static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
641 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
642}
643
644#define poll ___xxx_poll
645
Josh Gao27241a72019-04-25 14:04:57 -0700646static __inline__ int adb_mkdir(const std::string& path, int mode) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400647 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648}
Alex Vallée47d67c92015-05-06 16:26:00 -0400649
Josh Gao27241a72019-04-25 14:04:57 -0700650#undef mkdir
651#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800652
Joshua Duongd85f5c02019-11-20 14:18:43 -0800653static __inline__ int adb_rename(const char* oldpath, const char* newpath) {
654 return rename(oldpath, newpath);
655}
656
Alex Vallée47d67c92015-05-06 16:26:00 -0400657static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658 return path[0] == '/';
659}
660
Alex Buynytskyy665f3ff2019-09-16 12:10:54 -0700661static __inline__ int adb_get_os_handle(borrowed_fd fd) {
662 return fd.get();
663}
664
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800665// A very simple wrapper over a launched child process
666class Process {
667 public:
668 constexpr explicit Process(pid_t pid) : pid_(pid) {}
669 constexpr explicit operator bool() const { return pid_ >= 0; }
670
671 void wait() {
672 if (*this) {
673 int status;
674 ::waitpid(pid_, &status, 0);
675 pid_ = -1;
676 }
677 }
678 void kill() {
679 if (*this) {
680 ::kill(pid_, SIGTERM);
681 }
682 }
683
684 private:
685 pid_t pid_;
686};
687
688Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
689 std::initializer_list<int> fds_to_inherit = {});
690
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691#endif /* !_WIN32 */
692
Josh Gao27241a72019-04-25 14:04:57 -0700693static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800694 int off = 1;
Josh Gao27241a72019-04-25 14:04:57 -0700695 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800696}
697
David Pursellbfd95032016-02-22 14:27:23 -0800698// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
699// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
700// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao27241a72019-04-25 14:04:57 -0700701bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellbfd95032016-02-22 14:27:23 -0800702
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700703#if defined(_WIN32)
704// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
705#undef ERROR
706#endif
707
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708#endif /* _ADB_SYSDEPS_H */