blob: 0c5a6b4c1d20456f570b64d3d5fb73ee790a5a8d [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
Spencer Low6ac5d7d2015-05-22 20:09:06 -070091// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -070092extern int adb_open(const char* path, int options);
93extern int adb_creat(const char* path, int mode);
Josh Gao27241a72019-04-25 14:04:57 -070094extern int adb_read(borrowed_fd fd, void* buf, int len);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -070095extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
Josh Gao27241a72019-04-25 14:04:57 -070096extern int adb_write(borrowed_fd fd, const void* buf, int len);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -070097extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
Josh Gao27241a72019-04-25 14:04:57 -070098extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
99extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
Josh Gao2e1e7892018-03-23 13:03:28 -0700100extern int adb_close(int fd);
101extern int adb_register_socket(SOCKET s);
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700102extern HANDLE adb_get_os_handle(borrowed_fd fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700104// See the comments for the !defined(_WIN32) version of unix_close().
Josh Gao27241a72019-04-25 14:04:57 -0700105static __inline__ int unix_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 return close(fd);
107}
Josh Gao27241a72019-04-25 14:04:57 -0700108#undef close
109#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110
Spencer Low2e02dc62015-11-07 17:34:39 -0800111// Like unix_read(), but may return EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700112extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800113
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700114// See the comments for the !defined(_WIN32) version of unix_read().
Josh Gao27241a72019-04-25 14:04:57 -0700115static __inline__ int unix_read(borrowed_fd fd, void* buf, size_t len) {
Spencer Low2e02dc62015-11-07 17:34:39 -0800116 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
117}
Spencer Low50184062015-03-01 15:06:21 -0800118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119#undef read
120#define read ___xxx_read
121
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700122#undef pread
123#define pread ___xxx_pread
124
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700125// See the comments for the !defined(_WIN32) version of unix_write().
Josh Gao27241a72019-04-25 14:04:57 -0700126static __inline__ int unix_write(borrowed_fd fd, const void* buf, size_t len) {
127 return write(fd.get(), buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128}
129#undef write
130#define write ___xxx_write
131
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700132#undef pwrite
133#define pwrite ___xxx_pwrite
134
Spencer Low40babf02018-09-02 19:19:39 -0700135// See the comments for the !defined(_WIN32) version of unix_lseek().
Josh Gao27241a72019-04-25 14:04:57 -0700136static __inline__ int unix_lseek(borrowed_fd fd, int pos, int where) {
137 return lseek(fd.get(), pos, where);
Spencer Low40babf02018-09-02 19:19:39 -0700138}
139#undef lseek
140#define lseek ___xxx_lseek
141
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700142// See the comments for the !defined(_WIN32) version of adb_open_mode().
Josh Gao27241a72019-04-25 14:04:57 -0700143static __inline__ int adb_open_mode(const char* path, int options, int mode) {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200144 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145}
146
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700147// See the comments for the !defined(_WIN32) version of unix_open().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800148extern int unix_open(std::string_view path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149#define open ___xxx_unix_open
150
David Pursellc5b8ad82015-10-28 14:29:51 -0700151// Checks if |fd| corresponds to a console.
152// Standard Windows isatty() returns 1 for both console FDs and character
153// devices like NUL. unix_isatty() performs some extra checking to only match
154// console FDs.
155// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
156// will work but adb_open() FDs will not. Additionally the OS handle associated
157// with |fd| must have GENERIC_READ access (which console FDs have by default).
158// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
159// calling this function is unreliable and should not be used.
Josh Gao27241a72019-04-25 14:04:57 -0700160int unix_isatty(borrowed_fd fd);
David Pursellc5b8ad82015-10-28 14:29:51 -0700161#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Spencer Low5200c662015-07-30 23:07:55 -0700163int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700164
165inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
166 abort();
167}
168
169inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
170 abort();
171}
172
Spencer Low5200c662015-07-30 23:07:55 -0700173int network_connect(const std::string& host, int port, int type, int timeout,
174 std::string* error);
175
Josh Gao27241a72019-04-25 14:04:57 -0700176extern int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
178#undef accept
179#define accept ___xxx_accept
180
David Purselleaae97e2016-04-07 11:25:48 -0700181// Returns the local port number of a bound socket, or -1 on failure.
Josh Gao27241a72019-04-25 14:04:57 -0700182int adb_socket_get_local_port(borrowed_fd fd);
David Purselleaae97e2016-04-07 11:25:48 -0700183
Josh Gao27241a72019-04-25 14:04:57 -0700184extern int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
185 socklen_t optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800186
187#undef setsockopt
188#define setsockopt ___xxx_setsockopt
189
Josh Gao27241a72019-04-25 14:04:57 -0700190extern int adb_socketpair(int sv[2]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Josh Gao3777d2e2016-02-16 17:34:53 -0800192struct adb_pollfd {
193 int fd;
194 short events;
195 short revents;
196};
197extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
198#define poll ___xxx_poll
199
Elliott Hughes5c742702015-07-30 17:42:01 -0700200static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
202}
203
Spencer Lowcf4ff642015-05-11 01:08:48 -0700204// UTF-8 versions of POSIX APIs.
205extern DIR* adb_opendir(const char* dirname);
206extern struct dirent* adb_readdir(DIR* dir);
207extern int adb_closedir(DIR* dir);
208
209extern int adb_utime(const char *, struct utimbuf *);
210extern int adb_chmod(const char *, int);
211
Elliott Hughesd8a4c602018-06-26 13:06:15 -0700212extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
213 __attribute__((__format__(__printf__, 2, 0)));
214extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
215extern int adb_fprintf(FILE* stream, const char* format, ...)
216 __attribute__((__format__(__printf__, 2, 3)));
217extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700218
219extern int adb_fputs(const char* buf, FILE* stream);
220extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800221extern int adb_putchar(int ch);
222extern int adb_puts(const char* buf);
Josh Gao27241a72019-04-25 14:04:57 -0700223extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700224
225extern FILE* adb_fopen(const char* f, const char* m);
226
227extern char* adb_getenv(const char* name);
228
229extern char* adb_getcwd(char* buf, int size);
230
231// Remap calls to POSIX APIs to our UTF-8 versions.
232#define opendir adb_opendir
233#define readdir adb_readdir
234#define closedir adb_closedir
235#define rewinddir rewinddir_utf8_not_yet_implemented
236#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800237// Some compiler's C++ headers have members named seekdir, so we can't do the
238// macro technique and instead cause a link error if seekdir is called.
239inline void seekdir(DIR*, long) {
240 extern int seekdir_utf8_not_yet_implemented;
241 seekdir_utf8_not_yet_implemented = 1;
242}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700243
244#define utime adb_utime
245#define chmod adb_chmod
246
247#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800248#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700249#define fprintf adb_fprintf
250#define printf adb_printf
251#define fputs adb_fputs
252#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800253// putc may be a macro, so if so, undefine it, so that we can redefine it.
254#undef putc
255#define putc(c, s) adb_fputc(c, s)
256#define putchar adb_putchar
257#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700258#define fwrite adb_fwrite
259
260#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800261#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700262
263#define getenv adb_getenv
264#define putenv putenv_utf8_not_yet_implemented
265#define setenv setenv_utf8_not_yet_implemented
266#define unsetenv unsetenv_utf8_not_yet_implemented
267
268#define getcwd adb_getcwd
269
Spencer Lowcf4ff642015-05-11 01:08:48 -0700270// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
271// passed to main().
272class NarrowArgs {
273public:
274 NarrowArgs(int argc, wchar_t** argv);
275 ~NarrowArgs();
276
277 inline char** data() {
278 return narrow_args;
279 }
280
281private:
282 char** narrow_args;
283};
284
Spencer Low5c398d22015-08-08 15:07:07 -0700285// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
286// so they can fit in an int. To convert back, we just need to sign-extend.
287// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
288// Note that this does not make a HANDLE value work with APIs like open(), nor
289// does this make a value from open() passable to APIs taking a HANDLE. This
290// just lets you take a HANDLE, pass it around as an int, and then use it again
291// as a HANDLE.
292inline int cast_handle_to_int(const HANDLE h) {
293 // truncate
294 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
295}
296
297inline HANDLE cast_int_to_handle(const int fd) {
298 // sign-extend
299 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
300}
301
Spencer Low2122c7a2015-08-26 18:46:09 -0700302// Deleter for unique_handle. Adapted from many sources, including:
303// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
304// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
305class handle_deleter {
306public:
307 typedef HANDLE pointer;
308
309 void operator()(HANDLE h);
310};
311
312// Like std::unique_ptr, but for Windows HANDLE objects that should be
313// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
314// but does not check if the handle != INVALID_HANDLE_VALUE.
315typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
316
Spencer Lowa30b79a2015-11-15 16:29:36 -0800317namespace internal {
318
319size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
320
321}
322
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323#else /* !_WIN32 a.k.a. Unix */
324
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325#include <fcntl.h>
Spencer Low5200c662015-07-30 23:07:55 -0700326#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327#include <netinet/in.h>
328#include <netinet/tcp.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700329#include <poll.h>
330#include <pthread.h>
331#include <signal.h>
332#include <stdarg.h>
333#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334#include <string.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700335#include <sys/stat.h>
336#include <sys/wait.h>
Kenny Root73167412012-10-12 15:26:45 -0700337#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338
Alex Vallée47d67c92015-05-06 16:26:00 -0400339#include <string>
340
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700341#include <cutils/sockets.h>
342
Elliott Hughes5c742702015-07-30 17:42:01 -0700343#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344#define OS_PATH_SEPARATOR '/'
345#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700346#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347
Josh Gao07db1192015-11-07 15:38:19 -0800348static __inline__ bool adb_is_separator(char c) {
349 return c == '/';
350}
351
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800352static __inline__ int get_fd_flags(borrowed_fd fd) {
353 return fcntl(fd.get(), F_GETFD);
354}
355
Josh Gao27241a72019-04-25 14:04:57 -0700356static __inline__ void close_on_exec(borrowed_fd fd) {
Daniel Colascione3ec7be72019-11-13 17:49:37 -0800357 int flags = get_fd_flags(fd);
358 if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
359 fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
360 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361}
362
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700363// Open a file and return a file descriptor that may be used with unix_read(),
364// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
365//
366// On Unix, this is based on open(), so the file descriptor is a real OS file
367// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
368// file descriptor that can only be used with C Runtime APIs (which are wrapped
369// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
370// configurable CR/LF translation which defaults to text mode, but is settable
371// with _setmode().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800372static __inline__ int unix_open(std::string_view path, int options, ...) {
373 std::string zero_terminated(path.begin(), path.end());
374 if ((options & O_CREAT) == 0) {
375 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
376 } else {
377 int mode;
378 va_list args;
379 va_start(args, options);
380 mode = va_arg(args, int);
381 va_end(args);
382 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 }
384}
385
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700386// Similar to the two-argument adb_open(), but takes a mode parameter for file
387// creation. See adb_open() for more info.
Josh Gao27241a72019-04-25 14:04:57 -0700388static __inline__ int adb_open_mode(const char* pathname, int options, int mode) {
389 return TEMP_FAILURE_RETRY(open(pathname, options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390}
391
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700392// Open a file and return a file descriptor that may be used with adb_read(),
393// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
394//
395// On Unix, this is based on open(), but the Windows implementation (in
396// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
397// and its CR/LF translation. The returned file descriptor should be used with
398// adb_read(), adb_write(), adb_close(), etc.
Josh Gao27241a72019-04-25 14:04:57 -0700399static __inline__ int adb_open(const char* pathname, int options) {
400 int fd = TEMP_FAILURE_RETRY(open(pathname, options));
401 if (fd < 0) return -1;
402 close_on_exec(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 return fd;
404}
Josh Gao27241a72019-04-25 14:04:57 -0700405#undef open
406#define open ___xxx_open
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407
Josh Gao27241a72019-04-25 14:04:57 -0700408static __inline__ int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
409 return shutdown(fd.get(), direction);
David Pursell1ed57f02015-10-06 15:30:03 -0700410}
Josh Gao2e1e7892018-03-23 13:03:28 -0700411
Josh Gao27241a72019-04-25 14:04:57 -0700412#undef shutdown
413#define shutdown ____xxx_shutdown
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400414
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700415// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
416// not designed to take a file descriptor from unix_open(). See the comments
417// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800418__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 return close(fd);
420}
Josh Gao27241a72019-04-25 14:04:57 -0700421#undef close
422#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700424// On Windows, ADB has an indirection layer for file descriptors. If we get a
425// Win32 SOCKET object from an external library, we have to map it in to that
426// indirection layer, which this does.
Josh Gao27241a72019-04-25 14:04:57 -0700427__inline__ int adb_register_socket(int s) {
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700428 return s;
429}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430
Josh Gao27241a72019-04-25 14:04:57 -0700431static __inline__ int adb_read(borrowed_fd fd, void* buf, size_t len) {
432 return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433}
434
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700435static __inline__ int adb_pread(int fd, void* buf, size_t len, off64_t offset) {
436#if defined(__APPLE__)
437 return TEMP_FAILURE_RETRY(pread(fd, buf, len, offset));
438#else
439 return TEMP_FAILURE_RETRY(pread64(fd, buf, len, offset));
440#endif
441}
442
Spencer Low2e02dc62015-11-07 17:34:39 -0800443// Like unix_read(), but does not handle EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700444static __inline__ int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
445 return read(fd.get(), buf, len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800446}
447
Josh Gao27241a72019-04-25 14:04:57 -0700448#undef read
449#define read ___xxx_read
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700450#undef pread
451#define pread ___xxx_pread
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452
Josh Gao27241a72019-04-25 14:04:57 -0700453static __inline__ int adb_write(borrowed_fd fd, const void* buf, size_t len) {
454 return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455}
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700456
457static __inline__ int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
458#if defined(__APPLE__)
459 return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
460#else
461 return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
462#endif
463}
464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465#undef write
466#define write ___xxx_write
Yurii Zubrytskyi709dfc32019-07-10 17:59:34 -0700467#undef pwrite
468#define pwrite ___xxx_pwrite
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
Josh Gao27241a72019-04-25 14:04:57 -0700470static __inline__ int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700471#if defined(__APPLE__)
Josh Gao27241a72019-04-25 14:04:57 -0700472 return lseek(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700473#else
Josh Gao27241a72019-04-25 14:04:57 -0700474 return lseek64(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700475#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476}
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700477#undef lseek
478#define lseek ___xxx_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479
Josh Gao27241a72019-04-25 14:04:57 -0700480static __inline__ int adb_unlink(const char* path) {
481 return unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482}
Josh Gao27241a72019-04-25 14:04:57 -0700483#undef unlink
484#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485
Josh Gao27241a72019-04-25 14:04:57 -0700486static __inline__ int adb_creat(const char* path, int mode) {
487 int fd = TEMP_FAILURE_RETRY(creat(path, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488
Josh Gao27241a72019-04-25 14:04:57 -0700489 if (fd < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
491 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200492 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493}
Josh Gao27241a72019-04-25 14:04:57 -0700494#undef creat
495#define creat ___xxx_creat
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496
Josh Gao27241a72019-04-25 14:04:57 -0700497static __inline__ int unix_isatty(borrowed_fd fd) {
498 return isatty(fd.get());
David Pursellc5b8ad82015-10-28 14:29:51 -0700499}
Josh Gao27241a72019-04-25 14:04:57 -0700500#define isatty ___xxx_isatty
David Pursellc5b8ad82015-10-28 14:29:51 -0700501
Spencer Low5200c662015-07-30 23:07:55 -0700502// Helper for network_* functions.
503inline int _fd_set_error_str(int fd, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700504 if (fd == -1) {
505 *error = strerror(errno);
506 }
507 return fd;
Spencer Low5200c662015-07-30 23:07:55 -0700508}
509
Spencer Low5200c662015-07-30 23:07:55 -0700510inline int network_inaddr_any_server(int port, int type, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700511 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700512}
513
Josh Gaocfb21412016-08-24 18:38:44 -0700514inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
515 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
516}
517
518inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
519 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700520}
521
Josh Gao45e3e952018-08-10 16:03:09 -0700522int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700523
Josh Gao27241a72019-04-25 14:04:57 -0700524static __inline__ int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
525 socklen_t* addrlen) {
Benoit Goby95ef8282011-02-01 18:57:41 -0800526 int fd;
527
Josh Gao27241a72019-04-25 14:04:57 -0700528 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
529 if (fd >= 0) close_on_exec(fd);
Benoit Goby95ef8282011-02-01 18:57:41 -0800530
531 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532}
533
Josh Gao27241a72019-04-25 14:04:57 -0700534#undef accept
535#define accept ___xxx_accept
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536
Josh Gao27241a72019-04-25 14:04:57 -0700537inline int adb_socket_get_local_port(borrowed_fd fd) {
538 return socket_get_local_port(fd.get());
David Purselleaae97e2016-04-07 11:25:48 -0700539}
540
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700541// Operate on a file descriptor returned from unix_open() or a well-known file
542// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
543//
544// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
545// adb_write(), adb_close() (which all map to Unix system calls), but the
546// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
547// into the C Runtime and its configurable CR/LF translation (which is settable
548// via _setmode()).
Josh Gao27241a72019-04-25 14:04:57 -0700549#define unix_read adb_read
550#define unix_write adb_write
Spencer Low40babf02018-09-02 19:19:39 -0700551#define unix_lseek adb_lseek
Josh Gao27241a72019-04-25 14:04:57 -0700552#define unix_close adb_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700554static __inline__ int adb_thread_setname(const std::string& name) {
555#ifdef __APPLE__
556 return pthread_setname_np(name.c_str());
557#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700558 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
559 // glibc doesn't have strlcpy, so we have to fake it.
560 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
561 strncpy(buf, name.c_str(), sizeof(buf) - 1);
562 buf[sizeof(buf) - 1] = '\0';
563 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700564#endif
565}
566
Josh Gao27241a72019-04-25 14:04:57 -0700567static __inline__ int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
568 socklen_t optlen) {
569 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800570}
571
Josh Gao27241a72019-04-25 14:04:57 -0700572#undef setsockopt
573#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574
Josh Gao27241a72019-04-25 14:04:57 -0700575static __inline__ int unix_socketpair(int d, int type, int protocol, int sv[2]) {
576 return socketpair(d, type, protocol, sv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577}
578
Josh Gao27241a72019-04-25 14:04:57 -0700579static __inline__ int adb_socketpair(int sv[2]) {
580 int rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581
Josh Gao27241a72019-04-25 14:04:57 -0700582 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
583 if (rc < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584
Josh Gao27241a72019-04-25 14:04:57 -0700585 close_on_exec(sv[0]);
586 close_on_exec(sv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587 return 0;
588}
589
Josh Gao27241a72019-04-25 14:04:57 -0700590#undef socketpair
591#define socketpair ___xxx_socketpair
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592
Josh Gao3777d2e2016-02-16 17:34:53 -0800593typedef struct pollfd adb_pollfd;
594static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
595 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
596}
597
598#define poll ___xxx_poll
599
Josh Gao27241a72019-04-25 14:04:57 -0700600static __inline__ int adb_mkdir(const std::string& path, int mode) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400601 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602}
Alex Vallée47d67c92015-05-06 16:26:00 -0400603
Josh Gao27241a72019-04-25 14:04:57 -0700604#undef mkdir
605#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606
Alex Vallée47d67c92015-05-06 16:26:00 -0400607static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 return path[0] == '/';
609}
610
Alex Buynytskyy665f3ff2019-09-16 12:10:54 -0700611static __inline__ int adb_get_os_handle(borrowed_fd fd) {
612 return fd.get();
613}
614
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615#endif /* !_WIN32 */
616
Josh Gao27241a72019-04-25 14:04:57 -0700617static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800618 int off = 1;
Josh Gao27241a72019-04-25 14:04:57 -0700619 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800620}
621
David Pursellbfd95032016-02-22 14:27:23 -0800622// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
623// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
624// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao27241a72019-04-25 14:04:57 -0700625bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellbfd95032016-02-22 14:27:23 -0800626
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700627#if defined(_WIN32)
628// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
629#undef ERROR
630#endif
631
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632#endif /* _ADB_SYSDEPS_H */