blob: 5cd0cd1accb334831868dc8eaba836d4e3efcb5d [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>
Spencer Lowa30b79a2015-11-15 16:29:36 -080030#include <vector>
Spencer Low5200c662015-07-30 23:07:55 -070031
Josh Gao13ea01d2016-05-13 14:52:06 -070032// Include this before open/close/unlink are defined as macros below.
Josh Gao3b3e10d2016-02-09 14:59:09 -080033#include <android-base/errors.h>
Josh Gao13ea01d2016-05-13 14:52:06 -070034#include <android-base/unique_fd.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/utf8.h>
Spencer Lowd21dc822015-11-12 15:20:15 -080036
Josh Gaoa3577e12016-12-05 13:24:48 -080037#include "sysdeps/errno.h"
Josh Gao46de1d72017-04-12 13:57:06 -070038#include "sysdeps/network.h"
Josh Gaof551ea02016-07-28 18:09:48 -070039#include "sysdeps/stat.h"
40
Dan Albertcc731cc2015-02-24 21:26:58 -080041/*
42 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
43 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
44 * not already defined, then define it here.
45 */
46#ifndef TEMP_FAILURE_RETRY
47/* Used to retry syscalls that can return EINTR. */
48#define TEMP_FAILURE_RETRY(exp) ({ \
49 typeof (exp) _rc; \
50 do { \
51 _rc = (exp); \
52 } while (_rc == -1 && errno == EINTR); \
53 _rc; })
54#endif
55
Spencer Lowcf4ff642015-05-11 01:08:48 -070056// Some printf-like functions are implemented in terms of
57// android::base::StringAppendV, so they should use the same attribute for
58// compile-time format string checking. On Windows, if the mingw version of
59// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
60// and PRIu64 (and related) to be recognized by the compile-time checking.
61#define ADB_FORMAT_ARCHETYPE __printf__
62#ifdef __USE_MINGW_ANSI_STDIO
63#if __USE_MINGW_ANSI_STDIO
64#undef ADB_FORMAT_ARCHETYPE
65#define ADB_FORMAT_ARCHETYPE gnu_printf
66#endif
67#endif
68
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069#ifdef _WIN32
70
Josh Gaoa166e712016-01-29 12:08:34 -080071// Clang-only nullability specifiers
72#define _Nonnull
73#define _Nullable
74
Dan Albert630b9af2014-11-24 23:34:35 -080075#include <ctype.h>
76#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070077#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080078#include <errno.h>
79#include <fcntl.h>
80#include <io.h>
81#include <process.h>
82#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070083#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070085#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080087
Spencer Low2122c7a2015-08-26 18:46:09 -070088#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080089#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040090
Dan Albert630b9af2014-11-24 23:34:35 -080091#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
Elliott Hughes5c742702015-07-30 17:42:01 -070093#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094#define OS_PATH_SEPARATOR '\\'
95#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070096#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Josh Gao07db1192015-11-07 15:38:19 -080098static __inline__ bool adb_is_separator(char c) {
99 return c == '\\' || c == '/';
100}
101
Josh Gaob5fea142016-02-12 14:31:15 -0800102typedef void (*adb_thread_func_t)(void* arg);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800103typedef HANDLE adb_thread_t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Josh Gaob5fea142016-02-12 14:31:15 -0800105struct adb_winthread_args {
Josh Gao3b3e10d2016-02-09 14:59:09 -0800106 adb_thread_func_t func;
107 void* arg;
108};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109
Josh Gaob5fea142016-02-12 14:31:15 -0800110static unsigned __stdcall adb_winthread_wrapper(void* heap_args) {
111 // Move the arguments from the heap onto the thread's stack.
112 adb_winthread_args thread_args = *static_cast<adb_winthread_args*>(heap_args);
113 delete static_cast<adb_winthread_args*>(heap_args);
114 thread_args.func(thread_args.arg);
115 return 0;
Josh Gao3b3e10d2016-02-09 14:59:09 -0800116}
117
118static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg,
119 adb_thread_t* thread = nullptr) {
Josh Gaob5fea142016-02-12 14:31:15 -0800120 adb_winthread_args* args = new adb_winthread_args{.func = func, .arg = arg};
121 uintptr_t handle = _beginthreadex(nullptr, 0, adb_winthread_wrapper, args, 0, nullptr);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800122 if (handle != static_cast<uintptr_t>(0)) {
123 if (thread) {
124 *thread = reinterpret_cast<HANDLE>(handle);
125 } else {
126 CloseHandle(thread);
127 }
128 return true;
129 }
130 return false;
131}
132
133static __inline__ bool adb_thread_join(adb_thread_t thread) {
134 switch (WaitForSingleObject(thread, INFINITE)) {
135 case WAIT_OBJECT_0:
136 CloseHandle(thread);
137 return true;
138
139 case WAIT_FAILED:
140 fprintf(stderr, "adb_thread_join failed: %s\n",
141 android::base::SystemErrorCodeToString(GetLastError()).c_str());
142 break;
143
144 default:
145 abort();
146 }
147
148 return false;
149}
150
151static __inline__ bool adb_thread_detach(adb_thread_t thread) {
152 CloseHandle(thread);
153 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
155
Josh Gaob5fea142016-02-12 14:31:15 -0800156static __inline__ void __attribute__((noreturn)) adb_thread_exit() {
Josh Gaod7b37492016-02-12 15:45:04 -0800157 _endthreadex(0);
Josh Gaob5fea142016-02-12 14:31:15 -0800158}
159
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700160static __inline__ int adb_thread_setname(const std::string& name) {
161 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
162 // the thread name in Windows. Unfortunately, it only works during debugging, but
163 // our build process doesn't generate PDB files needed for debugging.
164 return 0;
165}
166
Josh Gao3777d2e2016-02-16 17:34:53 -0800167static __inline__ adb_thread_t adb_thread_self() {
168 return GetCurrentThread();
169}
170
171static __inline__ bool adb_thread_equal(adb_thread_t lhs, adb_thread_t rhs) {
172 return GetThreadId(lhs) == GetThreadId(rhs);
173}
174
leozwangcbf02672014-08-15 09:51:27 -0700175static __inline__ unsigned long adb_thread_id()
176{
177 return GetCurrentThreadId();
178}
179
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180static __inline__ void close_on_exec(int fd)
181{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200182 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183}
184
Spencer Lowcf4ff642015-05-11 01:08:48 -0700185extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186#undef unlink
187#define unlink ___xxx_unlink
188
Spencer Lowcf4ff642015-05-11 01:08:48 -0700189extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190#undef mkdir
191#define mkdir ___xxx_mkdir
192
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700193// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194extern int adb_open(const char* path, int options);
195extern int adb_creat(const char* path, int mode);
196extern int adb_read(int fd, void* buf, int len);
197extern int adb_write(int fd, const void* buf, int len);
198extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400199extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200extern int adb_close(int fd);
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700201extern int adb_register_socket(SOCKET s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700203// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204static __inline__ int unix_close(int fd)
205{
206 return close(fd);
207}
208#undef close
209#define close ____xxx_close
210
Spencer Low2e02dc62015-11-07 17:34:39 -0800211// Like unix_read(), but may return EINTR.
212extern int unix_read_interruptible(int fd, void* buf, size_t len);
213
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700214// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800215static __inline__ int unix_read(int fd, void* buf, size_t len) {
216 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
217}
Spencer Low50184062015-03-01 15:06:21 -0800218
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219#undef read
220#define read ___xxx_read
221
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700222// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223static __inline__ int unix_write(int fd, const void* buf, size_t len)
224{
225 return write(fd, buf, len);
226}
227#undef write
228#define write ___xxx_write
229
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700230// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231static __inline__ int adb_open_mode(const char* path, int options, int mode)
232{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200233 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234}
235
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700236// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700237extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238#define open ___xxx_unix_open
239
David Pursellc5b8ad82015-10-28 14:29:51 -0700240// Checks if |fd| corresponds to a console.
241// Standard Windows isatty() returns 1 for both console FDs and character
242// devices like NUL. unix_isatty() performs some extra checking to only match
243// console FDs.
244// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
245// will work but adb_open() FDs will not. Additionally the OS handle associated
246// with |fd| must have GENERIC_READ access (which console FDs have by default).
247// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
248// calling this function is unreliable and should not be used.
249int unix_isatty(int fd);
250#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251
Spencer Low5200c662015-07-30 23:07:55 -0700252int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700253
254inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
255 abort();
256}
257
258inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
259 abort();
260}
261
Spencer Low5200c662015-07-30 23:07:55 -0700262int network_connect(const std::string& host, int port, int type, int timeout,
263 std::string* error);
264
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
266
267#undef accept
268#define accept ___xxx_accept
269
David Purselleaae97e2016-04-07 11:25:48 -0700270// Returns the local port number of a bound socket, or -1 on failure.
271int adb_socket_get_local_port(int fd);
272
Spencer Lowf055c192015-01-25 14:40:16 -0800273extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
274
275#undef setsockopt
276#define setsockopt ___xxx_setsockopt
277
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278extern int adb_socketpair( int sv[2] );
279
Josh Gao3777d2e2016-02-16 17:34:53 -0800280struct adb_pollfd {
281 int fd;
282 short events;
283 short revents;
284};
285extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
286#define poll ___xxx_poll
287
Elliott Hughes5c742702015-07-30 17:42:01 -0700288static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
290}
291
Spencer Lowcf4ff642015-05-11 01:08:48 -0700292// UTF-8 versions of POSIX APIs.
293extern DIR* adb_opendir(const char* dirname);
294extern struct dirent* adb_readdir(DIR* dir);
295extern int adb_closedir(DIR* dir);
296
297extern int adb_utime(const char *, struct utimbuf *);
298extern int adb_chmod(const char *, int);
299
300extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
301 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
Spencer Lowa30b79a2015-11-15 16:29:36 -0800302extern int adb_vprintf(const char *format, va_list ap)
303 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700304extern int adb_fprintf(FILE *stream, const char *format, ...)
305 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
306extern int adb_printf(const char *format, ...)
307 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
308
309extern int adb_fputs(const char* buf, FILE* stream);
310extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800311extern int adb_putchar(int ch);
312extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700313extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
314 FILE* stream);
315
316extern FILE* adb_fopen(const char* f, const char* m);
317
318extern char* adb_getenv(const char* name);
319
320extern char* adb_getcwd(char* buf, int size);
321
322// Remap calls to POSIX APIs to our UTF-8 versions.
323#define opendir adb_opendir
324#define readdir adb_readdir
325#define closedir adb_closedir
326#define rewinddir rewinddir_utf8_not_yet_implemented
327#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800328// Some compiler's C++ headers have members named seekdir, so we can't do the
329// macro technique and instead cause a link error if seekdir is called.
330inline void seekdir(DIR*, long) {
331 extern int seekdir_utf8_not_yet_implemented;
332 seekdir_utf8_not_yet_implemented = 1;
333}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700334
335#define utime adb_utime
336#define chmod adb_chmod
337
338#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800339#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700340#define fprintf adb_fprintf
341#define printf adb_printf
342#define fputs adb_fputs
343#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800344// putc may be a macro, so if so, undefine it, so that we can redefine it.
345#undef putc
346#define putc(c, s) adb_fputc(c, s)
347#define putchar adb_putchar
348#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700349#define fwrite adb_fwrite
350
351#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800352#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700353
354#define getenv adb_getenv
355#define putenv putenv_utf8_not_yet_implemented
356#define setenv setenv_utf8_not_yet_implemented
357#define unsetenv unsetenv_utf8_not_yet_implemented
358
359#define getcwd adb_getcwd
360
Spencer Lowcf4ff642015-05-11 01:08:48 -0700361// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
362// passed to main().
363class NarrowArgs {
364public:
365 NarrowArgs(int argc, wchar_t** argv);
366 ~NarrowArgs();
367
368 inline char** data() {
369 return narrow_args;
370 }
371
372private:
373 char** narrow_args;
374};
375
Spencer Low5c398d22015-08-08 15:07:07 -0700376// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
377// so they can fit in an int. To convert back, we just need to sign-extend.
378// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
379// Note that this does not make a HANDLE value work with APIs like open(), nor
380// does this make a value from open() passable to APIs taking a HANDLE. This
381// just lets you take a HANDLE, pass it around as an int, and then use it again
382// as a HANDLE.
383inline int cast_handle_to_int(const HANDLE h) {
384 // truncate
385 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
386}
387
388inline HANDLE cast_int_to_handle(const int fd) {
389 // sign-extend
390 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
391}
392
Spencer Low2122c7a2015-08-26 18:46:09 -0700393// Deleter for unique_handle. Adapted from many sources, including:
394// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
395// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
396class handle_deleter {
397public:
398 typedef HANDLE pointer;
399
400 void operator()(HANDLE h);
401};
402
403// Like std::unique_ptr, but for Windows HANDLE objects that should be
404// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
405// but does not check if the handle != INVALID_HANDLE_VALUE.
406typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
407
Spencer Lowa30b79a2015-11-15 16:29:36 -0800408namespace internal {
409
410size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
411
412}
413
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414#else /* !_WIN32 a.k.a. Unix */
415
Spencer Low5200c662015-07-30 23:07:55 -0700416#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700417#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418#include <fcntl.h>
Josh Gao3777d2e2016-02-16 17:34:53 -0800419#include <poll.h>
420#include <signal.h>
421#include <sys/stat.h>
422#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423
424#include <pthread.h>
425#include <unistd.h>
426#include <fcntl.h>
427#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700428#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429#include <netinet/in.h>
430#include <netinet/tcp.h>
431#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700432#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433
Alex Vallée47d67c92015-05-06 16:26:00 -0400434#include <string>
435
Elliott Hughes5c742702015-07-30 17:42:01 -0700436#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437#define OS_PATH_SEPARATOR '/'
438#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700439#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
Josh Gao07db1192015-11-07 15:38:19 -0800441static __inline__ bool adb_is_separator(char c) {
442 return c == '/';
443}
444
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445static __inline__ void close_on_exec(int fd)
446{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200447 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448}
449
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700450// Open a file and return a file descriptor that may be used with unix_read(),
451// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
452//
453// On Unix, this is based on open(), so the file descriptor is a real OS file
454// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
455// file descriptor that can only be used with C Runtime APIs (which are wrapped
456// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
457// configurable CR/LF translation which defaults to text mode, but is settable
458// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459static __inline__ int unix_open(const char* path, int options,...)
460{
461 if ((options & O_CREAT) == 0)
462 {
Kenny Root73167412012-10-12 15:26:45 -0700463 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 }
465 else
466 {
467 int mode;
468 va_list args;
469 va_start( args, options );
470 mode = va_arg( args, int );
471 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700472 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 }
474}
475
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700476// Similar to the two-argument adb_open(), but takes a mode parameter for file
477// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
479{
Kenny Root73167412012-10-12 15:26:45 -0700480 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481}
482
483
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700484// Open a file and return a file descriptor that may be used with adb_read(),
485// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
486//
487// On Unix, this is based on open(), but the Windows implementation (in
488// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
489// and its CR/LF translation. The returned file descriptor should be used with
490// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491static __inline__ int adb_open( const char* pathname, int options )
492{
Kenny Root73167412012-10-12 15:26:45 -0700493 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 if (fd < 0)
495 return -1;
496 close_on_exec( fd );
497 return fd;
498}
499#undef open
500#define open ___xxx_open
501
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400502static __inline__ int adb_shutdown(int fd)
503{
504 return shutdown(fd, SHUT_RDWR);
505}
David Pursell1ed57f02015-10-06 15:30:03 -0700506static __inline__ int adb_shutdown(int fd, int direction)
507{
508 return shutdown(fd, direction);
509}
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400510#undef shutdown
511#define shutdown ____xxx_shutdown
512
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700513// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
514// not designed to take a file descriptor from unix_open(). See the comments
515// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800516__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 return close(fd);
518}
519#undef close
520#define close ____xxx_close
521
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700522// On Windows, ADB has an indirection layer for file descriptors. If we get a
523// Win32 SOCKET object from an external library, we have to map it in to that
524// indirection layer, which this does.
525__inline__ int adb_register_socket(int s) {
526 return s;
527}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528
529static __inline__ int adb_read(int fd, void* buf, size_t len)
530{
Kenny Root73167412012-10-12 15:26:45 -0700531 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532}
533
Spencer Low2e02dc62015-11-07 17:34:39 -0800534// Like unix_read(), but does not handle EINTR.
535static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
536 return read(fd, buf, len);
537}
538
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539#undef read
540#define read ___xxx_read
541
542static __inline__ int adb_write(int fd, const void* buf, size_t len)
543{
Kenny Root73167412012-10-12 15:26:45 -0700544 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545}
546#undef write
547#define write ___xxx_write
548
549static __inline__ int adb_lseek(int fd, int pos, int where)
550{
551 return lseek(fd, pos, where);
552}
553#undef lseek
554#define lseek ___xxx_lseek
555
556static __inline__ int adb_unlink(const char* path)
557{
558 return unlink(path);
559}
560#undef unlink
561#define unlink ___xxx_unlink
562
563static __inline__ int adb_creat(const char* path, int mode)
564{
Kenny Root73167412012-10-12 15:26:45 -0700565 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200567 if ( fd < 0 )
568 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800569
570 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200571 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572}
573#undef creat
574#define creat ___xxx_creat
575
David Pursellc5b8ad82015-10-28 14:29:51 -0700576static __inline__ int unix_isatty(int fd) {
577 return isatty(fd);
578}
579#define isatty ___xxx_isatty
580
Spencer Low5200c662015-07-30 23:07:55 -0700581// Helper for network_* functions.
582inline int _fd_set_error_str(int fd, std::string* error) {
583 if (fd == -1) {
584 *error = strerror(errno);
585 }
586 return fd;
587}
588
Spencer Low5200c662015-07-30 23:07:55 -0700589inline int network_inaddr_any_server(int port, int type, std::string* error) {
590 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
591}
592
Josh Gaocfb21412016-08-24 18:38:44 -0700593inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
594 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
595}
596
597inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
598 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700599}
600
601inline int network_connect(const std::string& host, int port, int type,
602 int timeout, std::string* error) {
603 int getaddrinfo_error = 0;
604 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
605 &getaddrinfo_error);
606 if (fd != -1) {
607 return fd;
608 }
609 if (getaddrinfo_error != 0) {
610 *error = gai_strerror(getaddrinfo_error);
611 } else {
612 *error = strerror(errno);
613 }
614 return -1;
615}
616
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
618{
Benoit Goby95ef8282011-02-01 18:57:41 -0800619 int fd;
620
Kenny Root73167412012-10-12 15:26:45 -0700621 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800622 if (fd >= 0)
623 close_on_exec(fd);
624
625 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626}
627
628#undef accept
629#define accept ___xxx_accept
630
David Purselleaae97e2016-04-07 11:25:48 -0700631inline int adb_socket_get_local_port(int fd) {
632 return socket_get_local_port(fd);
633}
634
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700635// Operate on a file descriptor returned from unix_open() or a well-known file
636// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
637//
638// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
639// adb_write(), adb_close() (which all map to Unix system calls), but the
640// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
641// into the C Runtime and its configurable CR/LF translation (which is settable
642// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643#define unix_read adb_read
644#define unix_write adb_write
645#define unix_close adb_close
646
Josh Gaob5fea142016-02-12 14:31:15 -0800647// Win32 is limited to DWORDs for thread return values; limit the POSIX systems to this as well to
648// ensure compatibility.
649typedef void (*adb_thread_func_t)(void* arg);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800650typedef pthread_t adb_thread_t;
651
Josh Gaob5fea142016-02-12 14:31:15 -0800652struct adb_pthread_args {
653 adb_thread_func_t func;
654 void* arg;
655};
656
657static void* adb_pthread_wrapper(void* heap_args) {
658 // Move the arguments from the heap onto the thread's stack.
659 adb_pthread_args thread_args = *reinterpret_cast<adb_pthread_args*>(heap_args);
660 delete static_cast<adb_pthread_args*>(heap_args);
661 thread_args.func(thread_args.arg);
662 return nullptr;
663}
664
Josh Gao3b3e10d2016-02-09 14:59:09 -0800665static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg,
666 adb_thread_t* thread = nullptr) {
667 pthread_t temp;
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700668 pthread_attr_t attr;
669 pthread_attr_init(&attr);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800670 pthread_attr_setdetachstate(&attr, thread ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED);
Josh Gaob5fea142016-02-12 14:31:15 -0800671 auto* pthread_args = new adb_pthread_args{.func = start, .arg = arg};
672 errno = pthread_create(&temp, &attr, adb_pthread_wrapper, pthread_args);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800673 if (errno == 0) {
674 if (thread) {
675 *thread = temp;
676 }
677 return true;
678 }
679 return false;
680}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681
Josh Gao3b3e10d2016-02-09 14:59:09 -0800682static __inline__ bool adb_thread_join(adb_thread_t thread) {
683 errno = pthread_join(thread, nullptr);
684 return errno == 0;
685}
686
687static __inline__ bool adb_thread_detach(adb_thread_t thread) {
688 errno = pthread_detach(thread);
689 return errno == 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690}
691
Josh Gaob5fea142016-02-12 14:31:15 -0800692static __inline__ void __attribute__((noreturn)) adb_thread_exit() {
693 pthread_exit(nullptr);
694}
695
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700696static __inline__ int adb_thread_setname(const std::string& name) {
697#ifdef __APPLE__
698 return pthread_setname_np(name.c_str());
699#else
700 const char *s = name.c_str();
701
702 // pthread_setname_np fails rather than truncating long strings.
703 const int max_task_comm_len = 16; // including the null terminator
704 if (name.length() > (max_task_comm_len - 1)) {
705 char buf[max_task_comm_len];
706 strncpy(buf, name.c_str(), sizeof(buf) - 1);
707 buf[sizeof(buf) - 1] = '\0';
708 s = buf;
709 }
710
711 return pthread_setname_np(pthread_self(), s) ;
712#endif
713}
714
Spencer Lowf055c192015-01-25 14:40:16 -0800715static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
716{
717 return setsockopt( fd, level, optname, optval, optlen );
718}
719
720#undef setsockopt
721#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722
723static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
724{
725 return socketpair( d, type, protocol, sv );
726}
727
728static __inline__ int adb_socketpair( int sv[2] )
729{
730 int rc;
731
732 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
733 if (rc < 0)
734 return -1;
735
736 close_on_exec( sv[0] );
737 close_on_exec( sv[1] );
738 return 0;
739}
740
741#undef socketpair
742#define socketpair ___xxx_socketpair
743
Josh Gao3777d2e2016-02-16 17:34:53 -0800744typedef struct pollfd adb_pollfd;
745static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
746 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
747}
748
749#define poll ___xxx_poll
750
Alex Vallée47d67c92015-05-06 16:26:00 -0400751static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752{
Alex Vallée47d67c92015-05-06 16:26:00 -0400753 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754}
Alex Vallée47d67c92015-05-06 16:26:00 -0400755
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756#undef mkdir
757#define mkdir ___xxx_mkdir
758
Alex Vallée47d67c92015-05-06 16:26:00 -0400759static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 return path[0] == '/';
761}
762
leozwangcbf02672014-08-15 09:51:27 -0700763static __inline__ unsigned long adb_thread_id()
764{
Spencer Low8d8126a2015-07-21 02:06:26 -0700765 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700766}
767
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800768#endif /* !_WIN32 */
769
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800770static inline void disable_tcp_nagle(int fd) {
771 int off = 1;
772 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
773}
774
David Pursellbfd95032016-02-22 14:27:23 -0800775// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
776// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
777// configured to drop after 10 missed keepalives. Returns true on success.
778bool set_tcp_keepalive(int fd, int interval_sec);
779
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700780#if defined(_WIN32)
781// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
782#undef ERROR
783#endif
784
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785#endif /* _ADB_SYSDEPS_H */