blob: 322f99256ca302441a975d8e7f9188a98f000906 [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 Gaof551ea02016-07-28 18:09:48 -070037#include "sysdeps/stat.h"
38
Dan Albertcc731cc2015-02-24 21:26:58 -080039/*
40 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
41 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
42 * not already defined, then define it here.
43 */
44#ifndef TEMP_FAILURE_RETRY
45/* Used to retry syscalls that can return EINTR. */
46#define TEMP_FAILURE_RETRY(exp) ({ \
47 typeof (exp) _rc; \
48 do { \
49 _rc = (exp); \
50 } while (_rc == -1 && errno == EINTR); \
51 _rc; })
52#endif
53
Spencer Lowcf4ff642015-05-11 01:08:48 -070054// Some printf-like functions are implemented in terms of
55// android::base::StringAppendV, so they should use the same attribute for
56// compile-time format string checking. On Windows, if the mingw version of
57// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
58// and PRIu64 (and related) to be recognized by the compile-time checking.
59#define ADB_FORMAT_ARCHETYPE __printf__
60#ifdef __USE_MINGW_ANSI_STDIO
61#if __USE_MINGW_ANSI_STDIO
62#undef ADB_FORMAT_ARCHETYPE
63#define ADB_FORMAT_ARCHETYPE gnu_printf
64#endif
65#endif
66
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067#ifdef _WIN32
68
Josh Gaoa166e712016-01-29 12:08:34 -080069// Clang-only nullability specifiers
70#define _Nonnull
71#define _Nullable
72
Dan Albert630b9af2014-11-24 23:34:35 -080073#include <ctype.h>
74#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070075#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080076#include <errno.h>
77#include <fcntl.h>
78#include <io.h>
79#include <process.h>
80#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070081#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070083#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080085
Spencer Low2122c7a2015-08-26 18:46:09 -070086#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080087#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040088
Dan Albert630b9af2014-11-24 23:34:35 -080089#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Elliott Hughes5c742702015-07-30 17:42:01 -070091#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092#define OS_PATH_SEPARATOR '\\'
93#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070094#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
Josh Gao07db1192015-11-07 15:38:19 -080096static __inline__ bool adb_is_separator(char c) {
97 return c == '\\' || c == '/';
98}
99
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100typedef CRITICAL_SECTION adb_mutex_t;
101
102#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
103
104/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -0700105/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106#define ADB_MUTEX(x) extern adb_mutex_t x;
107#include "mutex_list.h"
108
109extern void adb_sysdeps_init(void);
110
111static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
112{
113 EnterCriticalSection( lock );
114}
115
116static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
117{
118 LeaveCriticalSection( lock );
119}
120
Josh Gaob5fea142016-02-12 14:31:15 -0800121typedef void (*adb_thread_func_t)(void* arg);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800122typedef HANDLE adb_thread_t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123
Josh Gaob5fea142016-02-12 14:31:15 -0800124struct adb_winthread_args {
Josh Gao3b3e10d2016-02-09 14:59:09 -0800125 adb_thread_func_t func;
126 void* arg;
127};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128
Josh Gaob5fea142016-02-12 14:31:15 -0800129static unsigned __stdcall adb_winthread_wrapper(void* heap_args) {
130 // Move the arguments from the heap onto the thread's stack.
131 adb_winthread_args thread_args = *static_cast<adb_winthread_args*>(heap_args);
132 delete static_cast<adb_winthread_args*>(heap_args);
133 thread_args.func(thread_args.arg);
134 return 0;
Josh Gao3b3e10d2016-02-09 14:59:09 -0800135}
136
137static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg,
138 adb_thread_t* thread = nullptr) {
Josh Gaob5fea142016-02-12 14:31:15 -0800139 adb_winthread_args* args = new adb_winthread_args{.func = func, .arg = arg};
140 uintptr_t handle = _beginthreadex(nullptr, 0, adb_winthread_wrapper, args, 0, nullptr);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800141 if (handle != static_cast<uintptr_t>(0)) {
142 if (thread) {
143 *thread = reinterpret_cast<HANDLE>(handle);
144 } else {
145 CloseHandle(thread);
146 }
147 return true;
148 }
149 return false;
150}
151
152static __inline__ bool adb_thread_join(adb_thread_t thread) {
153 switch (WaitForSingleObject(thread, INFINITE)) {
154 case WAIT_OBJECT_0:
155 CloseHandle(thread);
156 return true;
157
158 case WAIT_FAILED:
159 fprintf(stderr, "adb_thread_join failed: %s\n",
160 android::base::SystemErrorCodeToString(GetLastError()).c_str());
161 break;
162
163 default:
164 abort();
165 }
166
167 return false;
168}
169
170static __inline__ bool adb_thread_detach(adb_thread_t thread) {
171 CloseHandle(thread);
172 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173}
174
Josh Gaob5fea142016-02-12 14:31:15 -0800175static __inline__ void __attribute__((noreturn)) adb_thread_exit() {
Josh Gaod7b37492016-02-12 15:45:04 -0800176 _endthreadex(0);
Josh Gaob5fea142016-02-12 14:31:15 -0800177}
178
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700179static __inline__ int adb_thread_setname(const std::string& name) {
180 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
181 // the thread name in Windows. Unfortunately, it only works during debugging, but
182 // our build process doesn't generate PDB files needed for debugging.
183 return 0;
184}
185
Josh Gao3777d2e2016-02-16 17:34:53 -0800186static __inline__ adb_thread_t adb_thread_self() {
187 return GetCurrentThread();
188}
189
190static __inline__ bool adb_thread_equal(adb_thread_t lhs, adb_thread_t rhs) {
191 return GetThreadId(lhs) == GetThreadId(rhs);
192}
193
leozwangcbf02672014-08-15 09:51:27 -0700194static __inline__ unsigned long adb_thread_id()
195{
196 return GetCurrentThreadId();
197}
198
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199static __inline__ void close_on_exec(int fd)
200{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200201 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202}
203
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204#define S_ISLNK(m) 0 /* no symlinks on Win32 */
205
Spencer Lowcf4ff642015-05-11 01:08:48 -0700206extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207#undef unlink
208#define unlink ___xxx_unlink
209
Spencer Lowcf4ff642015-05-11 01:08:48 -0700210extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211#undef mkdir
212#define mkdir ___xxx_mkdir
213
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700214// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215extern int adb_open(const char* path, int options);
216extern int adb_creat(const char* path, int mode);
217extern int adb_read(int fd, void* buf, int len);
218extern int adb_write(int fd, const void* buf, int len);
219extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400220extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221extern int adb_close(int fd);
222
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700223// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224static __inline__ int unix_close(int fd)
225{
226 return close(fd);
227}
228#undef close
229#define close ____xxx_close
230
Spencer Low2e02dc62015-11-07 17:34:39 -0800231// Like unix_read(), but may return EINTR.
232extern int unix_read_interruptible(int fd, void* buf, size_t len);
233
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700234// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800235static __inline__ int unix_read(int fd, void* buf, size_t len) {
236 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
237}
Spencer Low50184062015-03-01 15:06:21 -0800238
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239#undef read
240#define read ___xxx_read
241
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700242// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243static __inline__ int unix_write(int fd, const void* buf, size_t len)
244{
245 return write(fd, buf, len);
246}
247#undef write
248#define write ___xxx_write
249
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700250// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251static __inline__ int adb_open_mode(const char* path, int options, int mode)
252{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200253 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254}
255
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700256// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700257extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258#define open ___xxx_unix_open
259
David Pursellc5b8ad82015-10-28 14:29:51 -0700260// Checks if |fd| corresponds to a console.
261// Standard Windows isatty() returns 1 for both console FDs and character
262// devices like NUL. unix_isatty() performs some extra checking to only match
263// console FDs.
264// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
265// will work but adb_open() FDs will not. Additionally the OS handle associated
266// with |fd| must have GENERIC_READ access (which console FDs have by default).
267// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
268// calling this function is unreliable and should not be used.
269int unix_isatty(int fd);
270#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272static __inline__ void adb_sleep_ms( int mseconds )
273{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200274 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275}
276
Spencer Low5200c662015-07-30 23:07:55 -0700277int network_loopback_client(int port, int type, std::string* error);
278int network_loopback_server(int port, int type, std::string* error);
279int network_inaddr_any_server(int port, int type, std::string* error);
280int network_connect(const std::string& host, int port, int type, int timeout,
281 std::string* error);
282
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
284
285#undef accept
286#define accept ___xxx_accept
287
Josh Gao59901912016-08-23 15:28:43 -0700288int adb_getsockname(int fd, struct sockaddr* sockaddr, socklen_t* optlen);
289#undef getsockname
290#define getsockname(...) ___xxx_getsockname(__VA__ARGS__)
291
David Purselleaae97e2016-04-07 11:25:48 -0700292// Returns the local port number of a bound socket, or -1 on failure.
293int adb_socket_get_local_port(int fd);
294
Spencer Lowf055c192015-01-25 14:40:16 -0800295extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
296
297#undef setsockopt
298#define setsockopt ___xxx_setsockopt
299
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300extern int adb_socketpair( int sv[2] );
301
Josh Gao3777d2e2016-02-16 17:34:53 -0800302struct adb_pollfd {
303 int fd;
304 short events;
305 short revents;
306};
307extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
308#define poll ___xxx_poll
309
Elliott Hughes5c742702015-07-30 17:42:01 -0700310static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
312}
313
Spencer Lowcf4ff642015-05-11 01:08:48 -0700314// UTF-8 versions of POSIX APIs.
315extern DIR* adb_opendir(const char* dirname);
316extern struct dirent* adb_readdir(DIR* dir);
317extern int adb_closedir(DIR* dir);
318
319extern int adb_utime(const char *, struct utimbuf *);
320extern int adb_chmod(const char *, int);
321
322extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
323 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
Spencer Lowa30b79a2015-11-15 16:29:36 -0800324extern int adb_vprintf(const char *format, va_list ap)
325 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700326extern int adb_fprintf(FILE *stream, const char *format, ...)
327 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
328extern int adb_printf(const char *format, ...)
329 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
330
331extern int adb_fputs(const char* buf, FILE* stream);
332extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800333extern int adb_putchar(int ch);
334extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700335extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
336 FILE* stream);
337
338extern FILE* adb_fopen(const char* f, const char* m);
339
340extern char* adb_getenv(const char* name);
341
342extern char* adb_getcwd(char* buf, int size);
343
344// Remap calls to POSIX APIs to our UTF-8 versions.
345#define opendir adb_opendir
346#define readdir adb_readdir
347#define closedir adb_closedir
348#define rewinddir rewinddir_utf8_not_yet_implemented
349#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800350// Some compiler's C++ headers have members named seekdir, so we can't do the
351// macro technique and instead cause a link error if seekdir is called.
352inline void seekdir(DIR*, long) {
353 extern int seekdir_utf8_not_yet_implemented;
354 seekdir_utf8_not_yet_implemented = 1;
355}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700356
357#define utime adb_utime
358#define chmod adb_chmod
359
360#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800361#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700362#define fprintf adb_fprintf
363#define printf adb_printf
364#define fputs adb_fputs
365#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800366// putc may be a macro, so if so, undefine it, so that we can redefine it.
367#undef putc
368#define putc(c, s) adb_fputc(c, s)
369#define putchar adb_putchar
370#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700371#define fwrite adb_fwrite
372
373#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800374#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700375
376#define getenv adb_getenv
377#define putenv putenv_utf8_not_yet_implemented
378#define setenv setenv_utf8_not_yet_implemented
379#define unsetenv unsetenv_utf8_not_yet_implemented
380
381#define getcwd adb_getcwd
382
Spencer Low0a796002015-10-18 16:45:09 -0700383char* adb_strerror(int err);
384#define strerror adb_strerror
385
Spencer Lowcf4ff642015-05-11 01:08:48 -0700386// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
387// passed to main().
388class NarrowArgs {
389public:
390 NarrowArgs(int argc, wchar_t** argv);
391 ~NarrowArgs();
392
393 inline char** data() {
394 return narrow_args;
395 }
396
397private:
398 char** narrow_args;
399};
400
Spencer Low5c398d22015-08-08 15:07:07 -0700401// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
402// so they can fit in an int. To convert back, we just need to sign-extend.
403// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
404// Note that this does not make a HANDLE value work with APIs like open(), nor
405// does this make a value from open() passable to APIs taking a HANDLE. This
406// just lets you take a HANDLE, pass it around as an int, and then use it again
407// as a HANDLE.
408inline int cast_handle_to_int(const HANDLE h) {
409 // truncate
410 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
411}
412
413inline HANDLE cast_int_to_handle(const int fd) {
414 // sign-extend
415 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
416}
417
Spencer Low2122c7a2015-08-26 18:46:09 -0700418// Deleter for unique_handle. Adapted from many sources, including:
419// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
420// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
421class handle_deleter {
422public:
423 typedef HANDLE pointer;
424
425 void operator()(HANDLE h);
426};
427
428// Like std::unique_ptr, but for Windows HANDLE objects that should be
429// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
430// but does not check if the handle != INVALID_HANDLE_VALUE.
431typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
432
Spencer Lowa30b79a2015-11-15 16:29:36 -0800433namespace internal {
434
435size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
436
437}
438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439#else /* !_WIN32 a.k.a. Unix */
440
Spencer Low5200c662015-07-30 23:07:55 -0700441#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700442#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443#include <fcntl.h>
Josh Gao3777d2e2016-02-16 17:34:53 -0800444#include <poll.h>
445#include <signal.h>
446#include <sys/stat.h>
447#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
449#include <pthread.h>
450#include <unistd.h>
451#include <fcntl.h>
452#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700453#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454#include <netinet/in.h>
455#include <netinet/tcp.h>
456#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700457#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
Alex Vallée47d67c92015-05-06 16:26:00 -0400459#include <string>
460
Elliott Hughes5c742702015-07-30 17:42:01 -0700461#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462#define OS_PATH_SEPARATOR '/'
463#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700464#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465
Josh Gao07db1192015-11-07 15:38:19 -0800466static __inline__ bool adb_is_separator(char c) {
467 return c == '/';
468}
469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700471
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
473#define adb_mutex_init pthread_mutex_init
474#define adb_mutex_lock pthread_mutex_lock
475#define adb_mutex_unlock pthread_mutex_unlock
476#define adb_mutex_destroy pthread_mutex_destroy
477
JP Abgrall408fa572011-03-16 15:57:42 -0700478#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479
480#define adb_cond_t pthread_cond_t
481#define adb_cond_init pthread_cond_init
482#define adb_cond_wait pthread_cond_wait
483#define adb_cond_broadcast pthread_cond_broadcast
484#define adb_cond_signal pthread_cond_signal
485#define adb_cond_destroy pthread_cond_destroy
486
JP Abgrall408fa572011-03-16 15:57:42 -0700487/* declare all mutexes */
488#define ADB_MUTEX(x) extern adb_mutex_t x;
489#include "mutex_list.h"
490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491static __inline__ void close_on_exec(int fd)
492{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200493 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494}
495
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700496// Open a file and return a file descriptor that may be used with unix_read(),
497// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
498//
499// On Unix, this is based on open(), so the file descriptor is a real OS file
500// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
501// file descriptor that can only be used with C Runtime APIs (which are wrapped
502// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
503// configurable CR/LF translation which defaults to text mode, but is settable
504// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505static __inline__ int unix_open(const char* path, int options,...)
506{
507 if ((options & O_CREAT) == 0)
508 {
Kenny Root73167412012-10-12 15:26:45 -0700509 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 }
511 else
512 {
513 int mode;
514 va_list args;
515 va_start( args, options );
516 mode = va_arg( args, int );
517 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700518 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 }
520}
521
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700522// Similar to the two-argument adb_open(), but takes a mode parameter for file
523// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
525{
Kenny Root73167412012-10-12 15:26:45 -0700526 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527}
528
529
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700530// Open a file and return a file descriptor that may be used with adb_read(),
531// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
532//
533// On Unix, this is based on open(), but the Windows implementation (in
534// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
535// and its CR/LF translation. The returned file descriptor should be used with
536// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537static __inline__ int adb_open( const char* pathname, int options )
538{
Kenny Root73167412012-10-12 15:26:45 -0700539 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 if (fd < 0)
541 return -1;
542 close_on_exec( fd );
543 return fd;
544}
545#undef open
546#define open ___xxx_open
547
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400548static __inline__ int adb_shutdown(int fd)
549{
550 return shutdown(fd, SHUT_RDWR);
551}
David Pursell1ed57f02015-10-06 15:30:03 -0700552static __inline__ int adb_shutdown(int fd, int direction)
553{
554 return shutdown(fd, direction);
555}
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400556#undef shutdown
557#define shutdown ____xxx_shutdown
558
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700559// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
560// not designed to take a file descriptor from unix_open(). See the comments
561// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800562__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 return close(fd);
564}
565#undef close
566#define close ____xxx_close
567
568
569static __inline__ int adb_read(int fd, void* buf, size_t len)
570{
Kenny Root73167412012-10-12 15:26:45 -0700571 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572}
573
Spencer Low2e02dc62015-11-07 17:34:39 -0800574// Like unix_read(), but does not handle EINTR.
575static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
576 return read(fd, buf, len);
577}
578
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800579#undef read
580#define read ___xxx_read
581
582static __inline__ int adb_write(int fd, const void* buf, size_t len)
583{
Kenny Root73167412012-10-12 15:26:45 -0700584 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585}
586#undef write
587#define write ___xxx_write
588
589static __inline__ int adb_lseek(int fd, int pos, int where)
590{
591 return lseek(fd, pos, where);
592}
593#undef lseek
594#define lseek ___xxx_lseek
595
596static __inline__ int adb_unlink(const char* path)
597{
598 return unlink(path);
599}
600#undef unlink
601#define unlink ___xxx_unlink
602
603static __inline__ int adb_creat(const char* path, int mode)
604{
Kenny Root73167412012-10-12 15:26:45 -0700605 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200607 if ( fd < 0 )
608 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609
610 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200611 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612}
613#undef creat
614#define creat ___xxx_creat
615
David Pursellc5b8ad82015-10-28 14:29:51 -0700616static __inline__ int unix_isatty(int fd) {
617 return isatty(fd);
618}
619#define isatty ___xxx_isatty
620
Spencer Low5200c662015-07-30 23:07:55 -0700621// Helper for network_* functions.
622inline int _fd_set_error_str(int fd, std::string* error) {
623 if (fd == -1) {
624 *error = strerror(errno);
625 }
626 return fd;
627}
628
629inline int network_loopback_client(int port, int type, std::string* error) {
630 return _fd_set_error_str(socket_loopback_client(port, type), error);
631}
632
633inline int network_loopback_server(int port, int type, std::string* error) {
634 return _fd_set_error_str(socket_loopback_server(port, type), error);
635}
636
637inline int network_inaddr_any_server(int port, int type, std::string* error) {
638 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
639}
640
641inline int network_local_server(const char *name, int namespace_id, int type,
642 std::string* error) {
643 return _fd_set_error_str(socket_local_server(name, namespace_id, type),
644 error);
645}
646
647inline int network_connect(const std::string& host, int port, int type,
648 int timeout, std::string* error) {
649 int getaddrinfo_error = 0;
650 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
651 &getaddrinfo_error);
652 if (fd != -1) {
653 return fd;
654 }
655 if (getaddrinfo_error != 0) {
656 *error = gai_strerror(getaddrinfo_error);
657 } else {
658 *error = strerror(errno);
659 }
660 return -1;
661}
662
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
664{
Benoit Goby95ef8282011-02-01 18:57:41 -0800665 int fd;
666
Kenny Root73167412012-10-12 15:26:45 -0700667 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800668 if (fd >= 0)
669 close_on_exec(fd);
670
671 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672}
673
674#undef accept
675#define accept ___xxx_accept
676
David Purselleaae97e2016-04-07 11:25:48 -0700677inline int adb_socket_get_local_port(int fd) {
678 return socket_get_local_port(fd);
679}
680
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700681// Operate on a file descriptor returned from unix_open() or a well-known file
682// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
683//
684// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
685// adb_write(), adb_close() (which all map to Unix system calls), but the
686// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
687// into the C Runtime and its configurable CR/LF translation (which is settable
688// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689#define unix_read adb_read
690#define unix_write adb_write
691#define unix_close adb_close
692
Josh Gaob5fea142016-02-12 14:31:15 -0800693// Win32 is limited to DWORDs for thread return values; limit the POSIX systems to this as well to
694// ensure compatibility.
695typedef void (*adb_thread_func_t)(void* arg);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800696typedef pthread_t adb_thread_t;
697
Josh Gaob5fea142016-02-12 14:31:15 -0800698struct adb_pthread_args {
699 adb_thread_func_t func;
700 void* arg;
701};
702
703static void* adb_pthread_wrapper(void* heap_args) {
704 // Move the arguments from the heap onto the thread's stack.
705 adb_pthread_args thread_args = *reinterpret_cast<adb_pthread_args*>(heap_args);
706 delete static_cast<adb_pthread_args*>(heap_args);
707 thread_args.func(thread_args.arg);
708 return nullptr;
709}
710
Josh Gao3b3e10d2016-02-09 14:59:09 -0800711static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg,
712 adb_thread_t* thread = nullptr) {
713 pthread_t temp;
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700714 pthread_attr_t attr;
715 pthread_attr_init(&attr);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800716 pthread_attr_setdetachstate(&attr, thread ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED);
Josh Gaob5fea142016-02-12 14:31:15 -0800717 auto* pthread_args = new adb_pthread_args{.func = start, .arg = arg};
718 errno = pthread_create(&temp, &attr, adb_pthread_wrapper, pthread_args);
Josh Gao3b3e10d2016-02-09 14:59:09 -0800719 if (errno == 0) {
720 if (thread) {
721 *thread = temp;
722 }
723 return true;
724 }
725 return false;
726}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727
Josh Gao3b3e10d2016-02-09 14:59:09 -0800728static __inline__ bool adb_thread_join(adb_thread_t thread) {
729 errno = pthread_join(thread, nullptr);
730 return errno == 0;
731}
732
733static __inline__ bool adb_thread_detach(adb_thread_t thread) {
734 errno = pthread_detach(thread);
735 return errno == 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736}
737
Josh Gaob5fea142016-02-12 14:31:15 -0800738static __inline__ void __attribute__((noreturn)) adb_thread_exit() {
739 pthread_exit(nullptr);
740}
741
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700742static __inline__ int adb_thread_setname(const std::string& name) {
743#ifdef __APPLE__
744 return pthread_setname_np(name.c_str());
745#else
746 const char *s = name.c_str();
747
748 // pthread_setname_np fails rather than truncating long strings.
749 const int max_task_comm_len = 16; // including the null terminator
750 if (name.length() > (max_task_comm_len - 1)) {
751 char buf[max_task_comm_len];
752 strncpy(buf, name.c_str(), sizeof(buf) - 1);
753 buf[sizeof(buf) - 1] = '\0';
754 s = buf;
755 }
756
757 return pthread_setname_np(pthread_self(), s) ;
758#endif
759}
760
Spencer Lowf055c192015-01-25 14:40:16 -0800761static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
762{
763 return setsockopt( fd, level, optname, optval, optlen );
764}
765
766#undef setsockopt
767#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800768
769static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
770{
771 return socketpair( d, type, protocol, sv );
772}
773
774static __inline__ int adb_socketpair( int sv[2] )
775{
776 int rc;
777
778 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
779 if (rc < 0)
780 return -1;
781
782 close_on_exec( sv[0] );
783 close_on_exec( sv[1] );
784 return 0;
785}
786
787#undef socketpair
788#define socketpair ___xxx_socketpair
789
Josh Gao3777d2e2016-02-16 17:34:53 -0800790typedef struct pollfd adb_pollfd;
791static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
792 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
793}
794
795#define poll ___xxx_poll
796
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797static __inline__ void adb_sleep_ms( int mseconds )
798{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200799 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800800}
801
Alex Vallée47d67c92015-05-06 16:26:00 -0400802static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800803{
Alex Vallée47d67c92015-05-06 16:26:00 -0400804 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805}
Alex Vallée47d67c92015-05-06 16:26:00 -0400806
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807#undef mkdir
808#define mkdir ___xxx_mkdir
809
810static __inline__ void adb_sysdeps_init(void)
811{
812}
813
Alex Vallée47d67c92015-05-06 16:26:00 -0400814static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815 return path[0] == '/';
816}
817
leozwangcbf02672014-08-15 09:51:27 -0700818static __inline__ unsigned long adb_thread_id()
819{
Spencer Low8d8126a2015-07-21 02:06:26 -0700820 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700821}
822
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800823#endif /* !_WIN32 */
824
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800825static inline void disable_tcp_nagle(int fd) {
826 int off = 1;
827 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
828}
829
David Pursellbfd95032016-02-22 14:27:23 -0800830// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
831// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
832// configured to drop after 10 missed keepalives. Returns true on success.
833bool set_tcp_keepalive(int fd, int interval_sec);
834
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700835#if defined(_WIN32)
836// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
837#undef ERROR
838#endif
839
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840#endif /* _ADB_SYSDEPS_H */