blob: b0e7fa09713403647db0ee7e26fdeec628706179 [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>
Josh Gao13ea01d2016-05-13 14:52:06 -070036#include <android-base/unique_fd.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/utf8.h>
Spencer Lowd21dc822015-11-12 15:20:15 -080038
Josh Gao27241a72019-04-25 14:04:57 -070039#include "adb_unique_fd.h"
Josh Gaoa3577e12016-12-05 13:24:48 -080040#include "sysdeps/errno.h"
Josh Gao46de1d72017-04-12 13:57:06 -070041#include "sysdeps/network.h"
Josh Gaof551ea02016-07-28 18:09:48 -070042#include "sysdeps/stat.h"
43
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#ifdef _WIN32
45
Josh Gaoa166e712016-01-29 12:08:34 -080046// Clang-only nullability specifiers
47#define _Nonnull
48#define _Nullable
49
Dan Albert630b9af2014-11-24 23:34:35 -080050#include <ctype.h>
51#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070052#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080053#include <errno.h>
54#include <fcntl.h>
55#include <io.h>
56#include <process.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070057#include <stdint.h>
Dan Albert630b9af2014-11-24 23:34:35 -080058#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070059#include <utime.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070060#include <windows.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070061#include <winsock2.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080063
Spencer Low2122c7a2015-08-26 18:46:09 -070064#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080065#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040066
Elliott Hughes5c742702015-07-30 17:42:01 -070067#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068#define OS_PATH_SEPARATOR '\\'
69#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070070#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Josh Gao07db1192015-11-07 15:38:19 -080072static __inline__ bool adb_is_separator(char c) {
73 return c == '\\' || c == '/';
74}
75
Spencer Low50beee32018-09-03 16:03:22 -070076extern int adb_thread_setname(const std::string& name);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070077
Josh Gao27241a72019-04-25 14:04:57 -070078static __inline__ void close_on_exec(borrowed_fd fd) {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020079 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080}
81
Josh Gao27241a72019-04-25 14:04:57 -070082extern int adb_unlink(const char* path);
83#undef unlink
84#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085
Spencer Lowcf4ff642015-05-11 01:08:48 -070086extern int adb_mkdir(const std::string& path, int mode);
Josh Gao27241a72019-04-25 14:04:57 -070087#undef mkdir
88#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
Spencer Low6ac5d7d2015-05-22 20:09:06 -070090// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -070091extern int adb_open(const char* path, int options);
92extern int adb_creat(const char* path, int mode);
Josh Gao27241a72019-04-25 14:04:57 -070093extern int adb_read(borrowed_fd fd, void* buf, int len);
94extern int adb_write(borrowed_fd fd, const void* buf, int len);
95extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
96extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
Josh Gao2e1e7892018-03-23 13:03:28 -070097extern int adb_close(int fd);
98extern int adb_register_socket(SOCKET s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700100// See the comments for the !defined(_WIN32) version of unix_close().
Josh Gao27241a72019-04-25 14:04:57 -0700101static __inline__ int unix_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 return close(fd);
103}
Josh Gao27241a72019-04-25 14:04:57 -0700104#undef close
105#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
Spencer Low2e02dc62015-11-07 17:34:39 -0800107// Like unix_read(), but may return EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700108extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800109
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700110// See the comments for the !defined(_WIN32) version of unix_read().
Josh Gao27241a72019-04-25 14:04:57 -0700111static __inline__ int unix_read(borrowed_fd fd, void* buf, size_t len) {
Spencer Low2e02dc62015-11-07 17:34:39 -0800112 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
113}
Spencer Low50184062015-03-01 15:06:21 -0800114
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115#undef read
116#define read ___xxx_read
117
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700118// See the comments for the !defined(_WIN32) version of unix_write().
Josh Gao27241a72019-04-25 14:04:57 -0700119static __inline__ int unix_write(borrowed_fd fd, const void* buf, size_t len) {
120 return write(fd.get(), buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121}
122#undef write
123#define write ___xxx_write
124
Spencer Low40babf02018-09-02 19:19:39 -0700125// See the comments for the !defined(_WIN32) version of unix_lseek().
Josh Gao27241a72019-04-25 14:04:57 -0700126static __inline__ int unix_lseek(borrowed_fd fd, int pos, int where) {
127 return lseek(fd.get(), pos, where);
Spencer Low40babf02018-09-02 19:19:39 -0700128}
129#undef lseek
130#define lseek ___xxx_lseek
131
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700132// See the comments for the !defined(_WIN32) version of adb_open_mode().
Josh Gao27241a72019-04-25 14:04:57 -0700133static __inline__ int adb_open_mode(const char* path, int options, int mode) {
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200134 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135}
136
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700137// See the comments for the !defined(_WIN32) version of unix_open().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800138extern int unix_open(std::string_view path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139#define open ___xxx_unix_open
140
David Pursellc5b8ad82015-10-28 14:29:51 -0700141// Checks if |fd| corresponds to a console.
142// Standard Windows isatty() returns 1 for both console FDs and character
143// devices like NUL. unix_isatty() performs some extra checking to only match
144// console FDs.
145// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
146// will work but adb_open() FDs will not. Additionally the OS handle associated
147// with |fd| must have GENERIC_READ access (which console FDs have by default).
148// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
149// calling this function is unreliable and should not be used.
Josh Gao27241a72019-04-25 14:04:57 -0700150int unix_isatty(borrowed_fd fd);
David Pursellc5b8ad82015-10-28 14:29:51 -0700151#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Spencer Low5200c662015-07-30 23:07:55 -0700153int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700154
155inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
156 abort();
157}
158
159inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
160 abort();
161}
162
Spencer Low5200c662015-07-30 23:07:55 -0700163int network_connect(const std::string& host, int port, int type, int timeout,
164 std::string* error);
165
Josh Gao27241a72019-04-25 14:04:57 -0700166extern int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
168#undef accept
169#define accept ___xxx_accept
170
David Purselleaae97e2016-04-07 11:25:48 -0700171// Returns the local port number of a bound socket, or -1 on failure.
Josh Gao27241a72019-04-25 14:04:57 -0700172int adb_socket_get_local_port(borrowed_fd fd);
David Purselleaae97e2016-04-07 11:25:48 -0700173
Josh Gao27241a72019-04-25 14:04:57 -0700174extern int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
175 socklen_t optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800176
177#undef setsockopt
178#define setsockopt ___xxx_setsockopt
179
Josh Gao27241a72019-04-25 14:04:57 -0700180extern int adb_socketpair(int sv[2]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Josh Gao3777d2e2016-02-16 17:34:53 -0800182struct adb_pollfd {
183 int fd;
184 short events;
185 short revents;
186};
187extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
188#define poll ___xxx_poll
189
Elliott Hughes5c742702015-07-30 17:42:01 -0700190static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
192}
193
Spencer Lowcf4ff642015-05-11 01:08:48 -0700194// UTF-8 versions of POSIX APIs.
195extern DIR* adb_opendir(const char* dirname);
196extern struct dirent* adb_readdir(DIR* dir);
197extern int adb_closedir(DIR* dir);
198
199extern int adb_utime(const char *, struct utimbuf *);
200extern int adb_chmod(const char *, int);
201
Elliott Hughesd8a4c602018-06-26 13:06:15 -0700202extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
203 __attribute__((__format__(__printf__, 2, 0)));
204extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
205extern int adb_fprintf(FILE* stream, const char* format, ...)
206 __attribute__((__format__(__printf__, 2, 3)));
207extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700208
209extern int adb_fputs(const char* buf, FILE* stream);
210extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800211extern int adb_putchar(int ch);
212extern int adb_puts(const char* buf);
Josh Gao27241a72019-04-25 14:04:57 -0700213extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700214
215extern FILE* adb_fopen(const char* f, const char* m);
216
217extern char* adb_getenv(const char* name);
218
219extern char* adb_getcwd(char* buf, int size);
220
221// Remap calls to POSIX APIs to our UTF-8 versions.
222#define opendir adb_opendir
223#define readdir adb_readdir
224#define closedir adb_closedir
225#define rewinddir rewinddir_utf8_not_yet_implemented
226#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800227// Some compiler's C++ headers have members named seekdir, so we can't do the
228// macro technique and instead cause a link error if seekdir is called.
229inline void seekdir(DIR*, long) {
230 extern int seekdir_utf8_not_yet_implemented;
231 seekdir_utf8_not_yet_implemented = 1;
232}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700233
234#define utime adb_utime
235#define chmod adb_chmod
236
237#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800238#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700239#define fprintf adb_fprintf
240#define printf adb_printf
241#define fputs adb_fputs
242#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800243// putc may be a macro, so if so, undefine it, so that we can redefine it.
244#undef putc
245#define putc(c, s) adb_fputc(c, s)
246#define putchar adb_putchar
247#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700248#define fwrite adb_fwrite
249
250#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800251#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700252
253#define getenv adb_getenv
254#define putenv putenv_utf8_not_yet_implemented
255#define setenv setenv_utf8_not_yet_implemented
256#define unsetenv unsetenv_utf8_not_yet_implemented
257
258#define getcwd adb_getcwd
259
Spencer Lowcf4ff642015-05-11 01:08:48 -0700260// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
261// passed to main().
262class NarrowArgs {
263public:
264 NarrowArgs(int argc, wchar_t** argv);
265 ~NarrowArgs();
266
267 inline char** data() {
268 return narrow_args;
269 }
270
271private:
272 char** narrow_args;
273};
274
Spencer Low5c398d22015-08-08 15:07:07 -0700275// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
276// so they can fit in an int. To convert back, we just need to sign-extend.
277// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
278// Note that this does not make a HANDLE value work with APIs like open(), nor
279// does this make a value from open() passable to APIs taking a HANDLE. This
280// just lets you take a HANDLE, pass it around as an int, and then use it again
281// as a HANDLE.
282inline int cast_handle_to_int(const HANDLE h) {
283 // truncate
284 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
285}
286
287inline HANDLE cast_int_to_handle(const int fd) {
288 // sign-extend
289 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
290}
291
Spencer Low2122c7a2015-08-26 18:46:09 -0700292// Deleter for unique_handle. Adapted from many sources, including:
293// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
294// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
295class handle_deleter {
296public:
297 typedef HANDLE pointer;
298
299 void operator()(HANDLE h);
300};
301
302// Like std::unique_ptr, but for Windows HANDLE objects that should be
303// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
304// but does not check if the handle != INVALID_HANDLE_VALUE.
305typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
306
Spencer Lowa30b79a2015-11-15 16:29:36 -0800307namespace internal {
308
309size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
310
311}
312
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313#else /* !_WIN32 a.k.a. Unix */
314
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315#include <fcntl.h>
Spencer Low5200c662015-07-30 23:07:55 -0700316#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317#include <netinet/in.h>
318#include <netinet/tcp.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700319#include <poll.h>
320#include <pthread.h>
321#include <signal.h>
322#include <stdarg.h>
323#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324#include <string.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700325#include <sys/stat.h>
326#include <sys/wait.h>
Kenny Root73167412012-10-12 15:26:45 -0700327#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328
Alex Vallée47d67c92015-05-06 16:26:00 -0400329#include <string>
330
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700331#include <cutils/sockets.h>
332
Elliott Hughes5c742702015-07-30 17:42:01 -0700333#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334#define OS_PATH_SEPARATOR '/'
335#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700336#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337
Josh Gao07db1192015-11-07 15:38:19 -0800338static __inline__ bool adb_is_separator(char c) {
339 return c == '/';
340}
341
Josh Gao27241a72019-04-25 14:04:57 -0700342static __inline__ void close_on_exec(borrowed_fd fd) {
343 fcntl(fd.get(), F_SETFD, FD_CLOEXEC);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344}
345
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700346// Open a file and return a file descriptor that may be used with unix_read(),
347// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
348//
349// On Unix, this is based on open(), so the file descriptor is a real OS file
350// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
351// file descriptor that can only be used with C Runtime APIs (which are wrapped
352// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
353// configurable CR/LF translation which defaults to text mode, but is settable
354// with _setmode().
Josh Gao0f29cbc2018-12-12 16:12:28 -0800355static __inline__ int unix_open(std::string_view path, int options, ...) {
356 std::string zero_terminated(path.begin(), path.end());
357 if ((options & O_CREAT) == 0) {
358 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
359 } else {
360 int mode;
361 va_list args;
362 va_start(args, options);
363 mode = va_arg(args, int);
364 va_end(args);
365 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 }
367}
368
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700369// Similar to the two-argument adb_open(), but takes a mode parameter for file
370// creation. See adb_open() for more info.
Josh Gao27241a72019-04-25 14:04:57 -0700371static __inline__ int adb_open_mode(const char* pathname, int options, int mode) {
372 return TEMP_FAILURE_RETRY(open(pathname, options, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373}
374
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700375// Open a file and return a file descriptor that may be used with adb_read(),
376// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
377//
378// On Unix, this is based on open(), but the Windows implementation (in
379// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
380// and its CR/LF translation. The returned file descriptor should be used with
381// adb_read(), adb_write(), adb_close(), etc.
Josh Gao27241a72019-04-25 14:04:57 -0700382static __inline__ int adb_open(const char* pathname, int options) {
383 int fd = TEMP_FAILURE_RETRY(open(pathname, options));
384 if (fd < 0) return -1;
385 close_on_exec(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 return fd;
387}
Josh Gao27241a72019-04-25 14:04:57 -0700388#undef open
389#define open ___xxx_open
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390
Josh Gao27241a72019-04-25 14:04:57 -0700391static __inline__ int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
392 return shutdown(fd.get(), direction);
David Pursell1ed57f02015-10-06 15:30:03 -0700393}
Josh Gao2e1e7892018-03-23 13:03:28 -0700394
Josh Gao27241a72019-04-25 14:04:57 -0700395#undef shutdown
396#define shutdown ____xxx_shutdown
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400397
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700398// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
399// not designed to take a file descriptor from unix_open(). See the comments
400// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800401__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 return close(fd);
403}
Josh Gao27241a72019-04-25 14:04:57 -0700404#undef close
405#define close ____xxx_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700407// On Windows, ADB has an indirection layer for file descriptors. If we get a
408// Win32 SOCKET object from an external library, we have to map it in to that
409// indirection layer, which this does.
Josh Gao27241a72019-04-25 14:04:57 -0700410__inline__ int adb_register_socket(int s) {
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700411 return s;
412}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413
Josh Gao27241a72019-04-25 14:04:57 -0700414static __inline__ int adb_read(borrowed_fd fd, void* buf, size_t len) {
415 return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416}
417
Spencer Low2e02dc62015-11-07 17:34:39 -0800418// Like unix_read(), but does not handle EINTR.
Josh Gao27241a72019-04-25 14:04:57 -0700419static __inline__ int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
420 return read(fd.get(), buf, len);
Spencer Low2e02dc62015-11-07 17:34:39 -0800421}
422
Josh Gao27241a72019-04-25 14:04:57 -0700423#undef read
424#define read ___xxx_read
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425
Josh Gao27241a72019-04-25 14:04:57 -0700426static __inline__ int adb_write(borrowed_fd fd, const void* buf, size_t len) {
427 return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428}
429#undef write
430#define write ___xxx_write
431
Josh Gao27241a72019-04-25 14:04:57 -0700432static __inline__ int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700433#if defined(__APPLE__)
Josh Gao27241a72019-04-25 14:04:57 -0700434 return lseek(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700435#else
Josh Gao27241a72019-04-25 14:04:57 -0700436 return lseek64(fd.get(), pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700437#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438}
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700439#undef lseek
440#define lseek ___xxx_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441
Josh Gao27241a72019-04-25 14:04:57 -0700442static __inline__ int adb_unlink(const char* path) {
443 return unlink(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444}
Josh Gao27241a72019-04-25 14:04:57 -0700445#undef unlink
446#define unlink ___xxx_unlink
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447
Josh Gao27241a72019-04-25 14:04:57 -0700448static __inline__ int adb_creat(const char* path, int mode) {
449 int fd = TEMP_FAILURE_RETRY(creat(path, mode));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450
Josh Gao27241a72019-04-25 14:04:57 -0700451 if (fd < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452
453 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200454 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455}
Josh Gao27241a72019-04-25 14:04:57 -0700456#undef creat
457#define creat ___xxx_creat
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Josh Gao27241a72019-04-25 14:04:57 -0700459static __inline__ int unix_isatty(borrowed_fd fd) {
460 return isatty(fd.get());
David Pursellc5b8ad82015-10-28 14:29:51 -0700461}
Josh Gao27241a72019-04-25 14:04:57 -0700462#define isatty ___xxx_isatty
David Pursellc5b8ad82015-10-28 14:29:51 -0700463
Spencer Low5200c662015-07-30 23:07:55 -0700464// Helper for network_* functions.
465inline int _fd_set_error_str(int fd, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700466 if (fd == -1) {
467 *error = strerror(errno);
468 }
469 return fd;
Spencer Low5200c662015-07-30 23:07:55 -0700470}
471
Spencer Low5200c662015-07-30 23:07:55 -0700472inline int network_inaddr_any_server(int port, int type, std::string* error) {
Josh Gao27241a72019-04-25 14:04:57 -0700473 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700474}
475
Josh Gaocfb21412016-08-24 18:38:44 -0700476inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
477 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
478}
479
480inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
481 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700482}
483
Josh Gao45e3e952018-08-10 16:03:09 -0700484int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700485
Josh Gao27241a72019-04-25 14:04:57 -0700486static __inline__ int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
487 socklen_t* addrlen) {
Benoit Goby95ef8282011-02-01 18:57:41 -0800488 int fd;
489
Josh Gao27241a72019-04-25 14:04:57 -0700490 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
491 if (fd >= 0) close_on_exec(fd);
Benoit Goby95ef8282011-02-01 18:57:41 -0800492
493 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494}
495
Josh Gao27241a72019-04-25 14:04:57 -0700496#undef accept
497#define accept ___xxx_accept
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498
Josh Gao27241a72019-04-25 14:04:57 -0700499inline int adb_socket_get_local_port(borrowed_fd fd) {
500 return socket_get_local_port(fd.get());
David Purselleaae97e2016-04-07 11:25:48 -0700501}
502
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700503// Operate on a file descriptor returned from unix_open() or a well-known file
504// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
505//
506// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
507// adb_write(), adb_close() (which all map to Unix system calls), but the
508// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
509// into the C Runtime and its configurable CR/LF translation (which is settable
510// via _setmode()).
Josh Gao27241a72019-04-25 14:04:57 -0700511#define unix_read adb_read
512#define unix_write adb_write
Spencer Low40babf02018-09-02 19:19:39 -0700513#define unix_lseek adb_lseek
Josh Gao27241a72019-04-25 14:04:57 -0700514#define unix_close adb_close
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700516static __inline__ int adb_thread_setname(const std::string& name) {
517#ifdef __APPLE__
518 return pthread_setname_np(name.c_str());
519#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700520 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
521 // glibc doesn't have strlcpy, so we have to fake it.
522 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
523 strncpy(buf, name.c_str(), sizeof(buf) - 1);
524 buf[sizeof(buf) - 1] = '\0';
525 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700526#endif
527}
528
Josh Gao27241a72019-04-25 14:04:57 -0700529static __inline__ int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
530 socklen_t optlen) {
531 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Lowf055c192015-01-25 14:40:16 -0800532}
533
Josh Gao27241a72019-04-25 14:04:57 -0700534#undef setsockopt
535#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536
Josh Gao27241a72019-04-25 14:04:57 -0700537static __inline__ int unix_socketpair(int d, int type, int protocol, int sv[2]) {
538 return socketpair(d, type, protocol, sv);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539}
540
Josh Gao27241a72019-04-25 14:04:57 -0700541static __inline__ int adb_socketpair(int sv[2]) {
542 int rc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543
Josh Gao27241a72019-04-25 14:04:57 -0700544 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
545 if (rc < 0) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546
Josh Gao27241a72019-04-25 14:04:57 -0700547 close_on_exec(sv[0]);
548 close_on_exec(sv[1]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800549 return 0;
550}
551
Josh Gao27241a72019-04-25 14:04:57 -0700552#undef socketpair
553#define socketpair ___xxx_socketpair
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554
Josh Gao3777d2e2016-02-16 17:34:53 -0800555typedef struct pollfd adb_pollfd;
556static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
557 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
558}
559
560#define poll ___xxx_poll
561
Josh Gao27241a72019-04-25 14:04:57 -0700562static __inline__ int adb_mkdir(const std::string& path, int mode) {
Alex Vallée47d67c92015-05-06 16:26:00 -0400563 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564}
Alex Vallée47d67c92015-05-06 16:26:00 -0400565
Josh Gao27241a72019-04-25 14:04:57 -0700566#undef mkdir
567#define mkdir ___xxx_mkdir
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568
Alex Vallée47d67c92015-05-06 16:26:00 -0400569static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800570 return path[0] == '/';
571}
572
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573#endif /* !_WIN32 */
574
Josh Gao27241a72019-04-25 14:04:57 -0700575static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800576 int off = 1;
Josh Gao27241a72019-04-25 14:04:57 -0700577 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800578}
579
David Pursellbfd95032016-02-22 14:27:23 -0800580// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
581// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
582// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao27241a72019-04-25 14:04:57 -0700583bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellbfd95032016-02-22 14:27:23 -0800584
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700585#if defined(_WIN32)
586// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
587#undef ERROR
588#endif
589
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590#endif /* _ADB_SYSDEPS_H */