blob: 3e781b8ac8ef6e1d23ce208ae5c4b190257e0ada [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) {}
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700279 constexpr Process(Process&& other) : h_(std::exchange(other.h_, nullptr)) {}
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800280 ~Process() { close(); }
281 constexpr explicit operator bool() const { return h_ != nullptr; }
282
283 void wait() {
284 if (*this) {
285 ::WaitForSingleObject(h_, INFINITE);
286 close();
287 }
288 }
289 void kill() {
290 if (*this) {
291 ::TerminateProcess(h_, -1);
292 }
293 }
294
295 private:
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700296 DISALLOW_COPY_AND_ASSIGN(Process);
297
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800298 void close() {
299 if (*this) {
300 ::CloseHandle(h_);
301 h_ = nullptr;
302 }
303 }
304
305 HANDLE h_;
306};
307
308Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
309 std::initializer_list<int> fds_to_inherit = {});
310
Spencer Lowcf4ff642015-05-11 01:08:48 -0700311// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
312// passed to main().
313class NarrowArgs {
314public:
315 NarrowArgs(int argc, wchar_t** argv);
316 ~NarrowArgs();
317
318 inline char** data() {
319 return narrow_args;
320 }
321
322private:
323 char** narrow_args;
324};
325
Spencer Low5c398d22015-08-08 15:07:07 -0700326// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
327// so they can fit in an int. To convert back, we just need to sign-extend.
328// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
329// Note that this does not make a HANDLE value work with APIs like open(), nor
330// does this make a value from open() passable to APIs taking a HANDLE. This
331// just lets you take a HANDLE, pass it around as an int, and then use it again
332// as a HANDLE.
333inline int cast_handle_to_int(const HANDLE h) {
334 // truncate
335 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
336}
337
338inline HANDLE cast_int_to_handle(const int fd) {
339 // sign-extend
340 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
341}
342
Spencer Low2122c7a2015-08-26 18:46:09 -0700343// Deleter for unique_handle. Adapted from many sources, including:
344// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
345// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
346class handle_deleter {
347public:
348 typedef HANDLE pointer;
349
350 void operator()(HANDLE h);
351};
352
353// Like std::unique_ptr, but for Windows HANDLE objects that should be
354// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
355// but does not check if the handle != INVALID_HANDLE_VALUE.
356typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
357
Spencer Lowa30b79a2015-11-15 16:29:36 -0800358namespace internal {
359
360size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
361
362}
363
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364#else /* !_WIN32 a.k.a. Unix */
365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366#include <fcntl.h>
Spencer Low5200c662015-07-30 23:07:55 -0700367#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368#include <netinet/in.h>
369#include <netinet/tcp.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700370#include <poll.h>
371#include <pthread.h>
372#include <signal.h>
373#include <stdarg.h>
374#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375#include <string.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700376#include <sys/stat.h>
377#include <sys/wait.h>
Kenny Root73167412012-10-12 15:26:45 -0700378#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
Alex Vallée47d67c92015-05-06 16:26:00 -0400380#include <string>
381
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700382#include <cutils/sockets.h>
383
Elliott Hughes5c742702015-07-30 17:42:01 -0700384#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385#define OS_PATH_SEPARATOR '/'
386#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700387#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388
Josh Gao07db1192015-11-07 15:38:19 -0800389static __inline__ bool adb_is_separator(char c) {
390 return c == '/';
391}
392
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800393static __inline__ int get_fd_flags(borrowed_fd fd) {
394 return fcntl(fd.get(), F_GETFD);
395}
396
Josh Gao27241a72019-04-25 14:04:57 -0700397static __inline__ void close_on_exec(borrowed_fd fd) {
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800398 int flags = get_fd_flags(fd);
399 if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
400 fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
401 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402}
403
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700404// Open a file and return a file descriptor that may be used with unix_read(),
405// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
406//
407// On Unix, this is based on open(), so the file descriptor is a real OS file
408// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
409// file descriptor that can only be used with C Runtime APIs (which are wrapped
410// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
411// configurable CR/LF translation which defaults to text mode, but is settable
412// with _setmode().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800413static __inline__ int unix_open(std::string_view path, int options, ...) {
414 std::string zero_terminated(path.begin(), path.end());
415 if ((options & O_CREAT) == 0) {
416 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
417 } else {
418 int mode;
419 va_list args;
420 va_start(args, options);
421 mode = va_arg(args, int);
422 va_end(args);
423 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 }
425}
426
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700427// Similar to the two-argument adb_open(), but takes a mode parameter for file
428// creation. See adb_open() for more info.
Josh Gao27241a72019-04-25 14:04:57 -0700429static __inline__ int adb_open_mode(const char* pathname, int options, int mode) {
430 return TEMP_FAILURE_RETRY(open(pathname, options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431}
432
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700433// Open a file and return a file descriptor that may be used with adb_read(),
434// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
435//
436// On Unix, this is based on open(), but the Windows implementation (in
437// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
438// and its CR/LF translation. The returned file descriptor should be used with
439// adb_read(), adb_write(), adb_close(), etc.
Josh Gao27241a72019-04-25 14:04:57 -0700440static __inline__ int adb_open(const char* pathname, int options) {
441 int fd = TEMP_FAILURE_RETRY(open(pathname, options));
442 if (fd < 0) return -1;
443 close_on_exec(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 return fd;
445}
Josh Gao27241a72019-04-25 14:04:57 -0700446#undef open
447#define open ___xxx_open
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
Josh Gao27241a72019-04-25 14:04:57 -0700449static __inline__ int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
450 return shutdown(fd.get(), direction);
David Pursell1ed57f02015-10-06 15:30:03 -0700451}
Josh Gao2e1e7892018-03-23 13:03:28 -0700452
Josh Gao27241a72019-04-25 14:04:57 -0700453#undef shutdown
454#define shutdown ____xxx_shutdown
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400455
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700456// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
457// not designed to take a file descriptor from unix_open(). See the comments
458// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800459__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 return close(fd);
461}
Josh Gao27241a72019-04-25 14:04:57 -0700462#undef close
463#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700465// On Windows, ADB has an indirection layer for file descriptors. If we get a
466// Win32 SOCKET object from an external library, we have to map it in to that
467// indirection layer, which this does.
Josh Gao27241a72019-04-25 14:04:57 -0700468__inline__ int adb_register_socket(int s) {
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700469 return s;
470}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
Joshua Duongd85f5c02019-11-20 14:18:43 -0800472static __inline__ int adb_gethostname(char* name, size_t len) {
473 return gethostname(name, len);
474}
475
476static __inline__ int adb_getlogin_r(char* buf, size_t bufsize) {
477 return getlogin_r(buf, bufsize);
478}
479
Josh Gao27241a72019-04-25 14:04:57 -0700480static __inline__ int adb_read(borrowed_fd fd, void* buf, size_t len) {
481 return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482}
483
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800484static __inline__ int adb_pread(borrowed_fd fd, void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700485#if defined(__APPLE__)
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800486 return TEMP_FAILURE_RETRY(pread(fd.get(), buf, len, offset));
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700487#else
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800488 return TEMP_FAILURE_RETRY(pread64(fd.get(), buf, len, offset));
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700489#endif
490}
491
Spencer Low2e02dc62015-11-07 17:34:39 -0800492// Like unix_read(), but does not handle EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700493static __inline__ int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
494 return read(fd.get(), buf, len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800495}
496
Josh Gao27241a72019-04-25 14:04:57 -0700497#undef read
498#define read ___xxx_read
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700499#undef pread
500#define pread ___xxx_pread
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501
Josh Gao27241a72019-04-25 14:04:57 -0700502static __inline__ int adb_write(borrowed_fd fd, const void* buf, size_t len) {
503 return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504}
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700505
506static __inline__ int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
507#if defined(__APPLE__)
508 return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
509#else
510 return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
511#endif
512}
513
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514#undef write
515#define write ___xxx_write
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700516#undef pwrite
517#define pwrite ___xxx_pwrite
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518
Josh Gao27241a72019-04-25 14:04:57 -0700519static __inline__ int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700520#if defined(__APPLE__)
Josh Gao27241a72019-04-25 14:04:57 -0700521 return lseek(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700522#else
Josh Gao27241a72019-04-25 14:04:57 -0700523 return lseek64(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700524#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525}
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700526#undef lseek
527#define lseek ___xxx_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528
Josh Gao27241a72019-04-25 14:04:57 -0700529static __inline__ int adb_unlink(const char* path) {
530 return unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531}
Josh Gao27241a72019-04-25 14:04:57 -0700532#undef unlink
533#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534
Josh Gao27241a72019-04-25 14:04:57 -0700535static __inline__ int adb_creat(const char* path, int mode) {
536 int fd = TEMP_FAILURE_RETRY(creat(path, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537
Josh Gao27241a72019-04-25 14:04:57 -0700538 if (fd < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539
540 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200541 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542}
Josh Gao27241a72019-04-25 14:04:57 -0700543#undef creat
544#define creat ___xxx_creat
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545
Josh Gao27241a72019-04-25 14:04:57 -0700546static __inline__ int unix_isatty(borrowed_fd fd) {
547 return isatty(fd.get());
David Pursellc5b8ad82015-10-28 14:29:51 -0700548}
Josh Gao27241a72019-04-25 14:04:57 -0700549#define isatty ___xxx_isatty
David Pursellc5b8ad82015-10-28 14:29:51 -0700550
Spencer Low5200c662015-07-30 23:07:55 -0700551// Helper for network_* functions.
552inline int _fd_set_error_str(int fd, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700553 if (fd == -1) {
554 *error = strerror(errno);
555 }
556 return fd;
Spencer Low5200c662015-07-30 23:07:55 -0700557}
558
Spencer Low5200c662015-07-30 23:07:55 -0700559inline int network_inaddr_any_server(int port, int type, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700560 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700561}
562
Josh Gaocfb21412016-08-24 18:38:44 -0700563inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
564 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
565}
566
567inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
568 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700569}
570
Josh Gao45e3e952018-08-10 16:03:09 -0700571int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700572
Josh Gao27241a72019-04-25 14:04:57 -0700573static __inline__ int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
574 socklen_t* addrlen) {
Benoit Goby95ef8282011-02-01 18:57:41 -0800575 int fd;
576
Josh Gao27241a72019-04-25 14:04:57 -0700577 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
578 if (fd >= 0) close_on_exec(fd);
Benoit Goby95ef8282011-02-01 18:57:41 -0800579
580 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581}
582
Josh Gao27241a72019-04-25 14:04:57 -0700583#undef accept
584#define accept ___xxx_accept
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585
Josh Gao27241a72019-04-25 14:04:57 -0700586inline int adb_socket_get_local_port(borrowed_fd fd) {
587 return socket_get_local_port(fd.get());
David Purselleaae97e2016-04-07 11:25:48 -0700588}
589
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700590// Operate on a file descriptor returned from unix_open() or a well-known file
591// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
592//
593// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
594// adb_write(), adb_close() (which all map to Unix system calls), but the
595// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
596// into the C Runtime and its configurable CR/LF translation (which is settable
597// via _setmode()).
Josh Gao27241a72019-04-25 14:04:57 -0700598#define unix_read adb_read
599#define unix_write adb_write
Spencer Low40babf02018-09-02 19:19:39 -0700600#define unix_lseek adb_lseek
Josh Gao27241a72019-04-25 14:04:57 -0700601#define unix_close adb_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700603static __inline__ int adb_thread_setname(const std::string& name) {
604#ifdef __APPLE__
605 return pthread_setname_np(name.c_str());
606#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700607 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
608 // glibc doesn't have strlcpy, so we have to fake it.
609 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
610 strncpy(buf, name.c_str(), sizeof(buf) - 1);
611 buf[sizeof(buf) - 1] = '\0';
612 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700613#endif
614}
615
Josh Gao27241a72019-04-25 14:04:57 -0700616static __inline__ int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
617 socklen_t optlen) {
618 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800619}
620
Josh Gao27241a72019-04-25 14:04:57 -0700621#undef setsockopt
622#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623
Josh Gao27241a72019-04-25 14:04:57 -0700624static __inline__ int unix_socketpair(int d, int type, int protocol, int sv[2]) {
625 return socketpair(d, type, protocol, sv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626}
627
Josh Gao27241a72019-04-25 14:04:57 -0700628static __inline__ int adb_socketpair(int sv[2]) {
629 int rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630
Josh Gao27241a72019-04-25 14:04:57 -0700631 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
632 if (rc < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633
Josh Gao27241a72019-04-25 14:04:57 -0700634 close_on_exec(sv[0]);
635 close_on_exec(sv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 return 0;
637}
638
Josh Gao27241a72019-04-25 14:04:57 -0700639#undef socketpair
640#define socketpair ___xxx_socketpair
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641
Josh Gao3777d2e2016-02-16 17:34:53 -0800642typedef struct pollfd adb_pollfd;
643static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
644 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
645}
646
647#define poll ___xxx_poll
648
Josh Gao27241a72019-04-25 14:04:57 -0700649static __inline__ int adb_mkdir(const std::string& path, int mode) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400650 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651}
Alex Vallée47d67c92015-05-06 16:26:00 -0400652
Josh Gao27241a72019-04-25 14:04:57 -0700653#undef mkdir
654#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655
Joshua Duongd85f5c02019-11-20 14:18:43 -0800656static __inline__ int adb_rename(const char* oldpath, const char* newpath) {
657 return rename(oldpath, newpath);
658}
659
Alex Vallée47d67c92015-05-06 16:26:00 -0400660static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 return path[0] == '/';
662}
663
Alex Buynytskyy665f3ff2019-09-16 12:10:54 -0700664static __inline__ int adb_get_os_handle(borrowed_fd fd) {
665 return fd.get();
666}
667
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800668// A very simple wrapper over a launched child process
669class Process {
670 public:
671 constexpr explicit Process(pid_t pid) : pid_(pid) {}
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700672 constexpr Process(Process&& other) : pid_(std::exchange(other.pid_, -1)) {}
673
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800674 constexpr explicit operator bool() const { return pid_ >= 0; }
675
676 void wait() {
677 if (*this) {
678 int status;
679 ::waitpid(pid_, &status, 0);
680 pid_ = -1;
681 }
682 }
683 void kill() {
684 if (*this) {
685 ::kill(pid_, SIGTERM);
686 }
687 }
688
689 private:
Yurii Zubrytskyi2488c212020-03-18 15:49:45 -0700690 DISALLOW_COPY_AND_ASSIGN(Process);
691
Alex Buynytskyy96ff54b2020-02-13 06:52:04 -0800692 pid_t pid_;
693};
694
695Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
696 std::initializer_list<int> fds_to_inherit = {});
697
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698#endif /* !_WIN32 */
699
Josh Gao27241a72019-04-25 14:04:57 -0700700static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800701 int off = 1;
Josh Gao27241a72019-04-25 14:04:57 -0700702 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800703}
704
David Pursellbfd95032016-02-22 14:27:23 -0800705// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
706// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
707// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao27241a72019-04-25 14:04:57 -0700708bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellbfd95032016-02-22 14:27:23 -0800709
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700710#if defined(_WIN32)
711// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
712#undef ERROR
713#endif
714
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715#endif /* _ADB_SYSDEPS_H */