blob: 761a4c79954a464444f1fd4fe3768aa05d2670e1 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Hughes43df1092015-07-23 17:12:58 -070027#include <errno.h>
28
Spencer Low753d4852015-07-30 23:07:55 -070029#include <string>
Spencer Lowf373c352015-11-15 16:29:36 -080030#include <vector>
Spencer Low753d4852015-07-30 23:07:55 -070031
Spencer Low50f5bf12015-11-12 15:20:15 -080032// Include this before open/unlink are defined as macros below.
Josh Gaoa4f5e032016-02-09 14:59:09 -080033#include <android-base/errors.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080034#include <android-base/utf8.h>
Spencer Low50f5bf12015-11-12 15:20:15 -080035
Dan Albert66a91b02015-02-24 21:26:58 -080036/*
37 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
38 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
39 * not already defined, then define it here.
40 */
41#ifndef TEMP_FAILURE_RETRY
42/* Used to retry syscalls that can return EINTR. */
43#define TEMP_FAILURE_RETRY(exp) ({ \
44 typeof (exp) _rc; \
45 do { \
46 _rc = (exp); \
47 } while (_rc == -1 && errno == EINTR); \
48 _rc; })
49#endif
50
Spencer Low6815c072015-05-11 01:08:48 -070051// Some printf-like functions are implemented in terms of
52// android::base::StringAppendV, so they should use the same attribute for
53// compile-time format string checking. On Windows, if the mingw version of
54// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
55// and PRIu64 (and related) to be recognized by the compile-time checking.
56#define ADB_FORMAT_ARCHETYPE __printf__
57#ifdef __USE_MINGW_ANSI_STDIO
58#if __USE_MINGW_ANSI_STDIO
59#undef ADB_FORMAT_ARCHETYPE
60#define ADB_FORMAT_ARCHETYPE gnu_printf
61#endif
62#endif
63
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064#ifdef _WIN32
65
Josh Gao23f6f2b2016-01-29 12:08:34 -080066// Clang-only nullability specifiers
67#define _Nonnull
68#define _Nullable
69
Dan Albertbbbbea62014-11-24 23:34:35 -080070#include <ctype.h>
71#include <direct.h>
Spencer Low6815c072015-05-11 01:08:48 -070072#include <dirent.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080073#include <errno.h>
74#include <fcntl.h>
75#include <io.h>
76#include <process.h>
77#include <sys/stat.h>
Spencer Low6815c072015-05-11 01:08:48 -070078#include <utime.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080079#include <winsock2.h>
Stephen Hinesb1170852014-10-01 17:37:06 -070080#include <windows.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080081#include <ws2tcpip.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080082
Spencer Low2bbb3a92015-08-26 18:46:09 -070083#include <memory> // unique_ptr
Spencer Low50f5bf12015-11-12 15:20:15 -080084#include <string>
Alex Vallée28d1f8d2015-05-06 16:26:00 -040085
Dan Albertbbbbea62014-11-24 23:34:35 -080086#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080087
Elliott Hughesd189cfb2015-07-30 17:42:01 -070088#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080089#define OS_PATH_SEPARATOR '\\'
90#define OS_PATH_SEPARATOR_STR "\\"
Benoit Goby2cc19e42012-04-12 12:23:49 -070091#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092
Josh Gao8acf06c2015-11-07 15:38:19 -080093static __inline__ bool adb_is_separator(char c) {
94 return c == '\\' || c == '/';
95}
96
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080097typedef CRITICAL_SECTION adb_mutex_t;
98
99#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
100
101/* declare all mutexes */
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700102/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800103#define ADB_MUTEX(x) extern adb_mutex_t x;
104#include "mutex_list.h"
105
106extern void adb_sysdeps_init(void);
107
108static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
109{
110 EnterCriticalSection( lock );
111}
112
113static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
114{
115 LeaveCriticalSection( lock );
116}
117
Josh Gaoa4f5e032016-02-09 14:59:09 -0800118typedef void* (*adb_thread_func_t)(void* arg);
119typedef HANDLE adb_thread_t;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800120
Josh Gaoa4f5e032016-02-09 14:59:09 -0800121struct win_thread_args {
122 adb_thread_func_t func;
123 void* arg;
124};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800125
Josh Gaoa4f5e032016-02-09 14:59:09 -0800126static unsigned __stdcall win_thread_wrapper(void* args) {
127 win_thread_args thread_args = *static_cast<win_thread_args*>(args);
128 delete static_cast<win_thread_args*>(args);
129 void* result = thread_args.func(thread_args.arg);
130 return reinterpret_cast<unsigned>(result);
131}
132
133static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg,
134 adb_thread_t* thread = nullptr) {
135 win_thread_args* args = new win_thread_args{.func = func, .arg = arg};
136 uintptr_t handle = _beginthreadex(nullptr, 0, win_thread_wrapper, args, 0, nullptr);
137 if (handle != static_cast<uintptr_t>(0)) {
138 if (thread) {
139 *thread = reinterpret_cast<HANDLE>(handle);
140 } else {
141 CloseHandle(thread);
142 }
143 return true;
144 }
145 return false;
146}
147
148static __inline__ bool adb_thread_join(adb_thread_t thread) {
149 switch (WaitForSingleObject(thread, INFINITE)) {
150 case WAIT_OBJECT_0:
151 CloseHandle(thread);
152 return true;
153
154 case WAIT_FAILED:
155 fprintf(stderr, "adb_thread_join failed: %s\n",
156 android::base::SystemErrorCodeToString(GetLastError()).c_str());
157 break;
158
159 default:
160 abort();
161 }
162
163 return false;
164}
165
166static __inline__ bool adb_thread_detach(adb_thread_t thread) {
167 CloseHandle(thread);
168 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800169}
170
Siva Velusamy2669cf92015-08-28 16:37:29 -0700171static __inline__ int adb_thread_setname(const std::string& name) {
172 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
173 // the thread name in Windows. Unfortunately, it only works during debugging, but
174 // our build process doesn't generate PDB files needed for debugging.
175 return 0;
176}
177
leozwang1be54622014-08-15 09:51:27 -0700178static __inline__ unsigned long adb_thread_id()
179{
180 return GetCurrentThreadId();
181}
182
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800183static __inline__ void close_on_exec(int fd)
184{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200185 /* nothing really */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186}
187
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188#define lstat stat /* no symlinks on Win32 */
189
190#define S_ISLNK(m) 0 /* no symlinks on Win32 */
191
Spencer Low6815c072015-05-11 01:08:48 -0700192extern int adb_unlink(const char* path);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800193#undef unlink
194#define unlink ___xxx_unlink
195
Spencer Low6815c072015-05-11 01:08:48 -0700196extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800197#undef mkdir
198#define mkdir ___xxx_mkdir
199
Spencer Low3a2421b2015-05-22 20:09:06 -0700200// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201extern int adb_open(const char* path, int options);
202extern int adb_creat(const char* path, int mode);
203extern int adb_read(int fd, void* buf, int len);
204extern int adb_write(int fd, const void* buf, int len);
205extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood81ffe172009-10-11 23:04:18 -0400206extern int adb_shutdown(int fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800207extern int adb_close(int fd);
208
Spencer Low3a2421b2015-05-22 20:09:06 -0700209// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210static __inline__ int unix_close(int fd)
211{
212 return close(fd);
213}
214#undef close
215#define close ____xxx_close
216
Spencer Low55441402015-11-07 17:34:39 -0800217// Like unix_read(), but may return EINTR.
218extern int unix_read_interruptible(int fd, void* buf, size_t len);
219
Spencer Low3a2421b2015-05-22 20:09:06 -0700220// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low55441402015-11-07 17:34:39 -0800221static __inline__ int unix_read(int fd, void* buf, size_t len) {
222 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
223}
Spencer Lowbeb61982015-03-01 15:06:21 -0800224
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800225#undef read
226#define read ___xxx_read
227
Spencer Low3a2421b2015-05-22 20:09:06 -0700228// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800229static __inline__ int unix_write(int fd, const void* buf, size_t len)
230{
231 return write(fd, buf, len);
232}
233#undef write
234#define write ___xxx_write
235
Spencer Low3a2421b2015-05-22 20:09:06 -0700236// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800237static __inline__ int adb_open_mode(const char* path, int options, int mode)
238{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200239 return adb_open(path, options);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240}
241
Spencer Low3a2421b2015-05-22 20:09:06 -0700242// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Low6815c072015-05-11 01:08:48 -0700243extern int unix_open(const char* path, int options, ...);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800244#define open ___xxx_unix_open
245
David Pursell58805362015-10-28 14:29:51 -0700246// Checks if |fd| corresponds to a console.
247// Standard Windows isatty() returns 1 for both console FDs and character
248// devices like NUL. unix_isatty() performs some extra checking to only match
249// console FDs.
250// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
251// will work but adb_open() FDs will not. Additionally the OS handle associated
252// with |fd| must have GENERIC_READ access (which console FDs have by default).
253// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
254// calling this function is unreliable and should not be used.
255int unix_isatty(int fd);
256#define isatty ___xxx_isatty
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800257
258/* normally provided by <cutils/misc.h> */
259extern void* load_file(const char* pathname, unsigned* psize);
260
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200261/* normally provided by "fdevent.h" */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262
263#define FDE_READ 0x0001
264#define FDE_WRITE 0x0002
265#define FDE_ERROR 0x0004
266#define FDE_DONT_CLOSE 0x0080
267
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800268typedef void (*fd_func)(int fd, unsigned events, void *userdata);
269
270fdevent *fdevent_create(int fd, fd_func func, void *arg);
271void fdevent_destroy(fdevent *fde);
272void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
273void fdevent_remove(fdevent *item);
274void fdevent_set(fdevent *fde, unsigned events);
275void fdevent_add(fdevent *fde, unsigned events);
276void fdevent_del(fdevent *fde, unsigned events);
277void fdevent_loop();
278
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279static __inline__ void adb_sleep_ms( int mseconds )
280{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200281 Sleep( mseconds );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800282}
283
Spencer Low753d4852015-07-30 23:07:55 -0700284int network_loopback_client(int port, int type, std::string* error);
285int network_loopback_server(int port, int type, std::string* error);
286int network_inaddr_any_server(int port, int type, std::string* error);
287int network_connect(const std::string& host, int port, int type, int timeout,
288 std::string* error);
289
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800290extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
291
292#undef accept
293#define accept ___xxx_accept
294
Spencer Low31aafa62015-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 Project9ca14dc2009-03-03 19:32:55 -0800300extern int adb_socketpair( int sv[2] );
301
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700302static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800303 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
304}
305
Spencer Low6815c072015-05-11 01:08:48 -0700306// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
307// struct stat s;
308// stat(filename, &s);
309// To turn into the following:
310// struct adb_stat s;
311// adb_stat(filename, &s);
312// To get this to work, we need to make 'struct adb_stat' the same as
313// 'struct stat'. Note that this definition of 'struct adb_stat' uses the
314// *current* macro definition of stat, so it may actually be inheriting from
315// struct _stat32i64 (or some other remapping).
316struct adb_stat : public stat {};
317
318static_assert(sizeof(struct adb_stat) == sizeof(struct stat),
319 "structures should be the same");
320
321extern int adb_stat(const char* f, struct adb_stat* s);
322
323// stat is already a macro, undefine it so we can redefine it.
324#undef stat
325#define stat adb_stat
326
327// UTF-8 versions of POSIX APIs.
328extern DIR* adb_opendir(const char* dirname);
329extern struct dirent* adb_readdir(DIR* dir);
330extern int adb_closedir(DIR* dir);
331
332extern int adb_utime(const char *, struct utimbuf *);
333extern int adb_chmod(const char *, int);
334
335extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
336 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
Spencer Lowf373c352015-11-15 16:29:36 -0800337extern int adb_vprintf(const char *format, va_list ap)
338 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
Spencer Low6815c072015-05-11 01:08:48 -0700339extern int adb_fprintf(FILE *stream, const char *format, ...)
340 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
341extern int adb_printf(const char *format, ...)
342 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
343
344extern int adb_fputs(const char* buf, FILE* stream);
345extern int adb_fputc(int ch, FILE* stream);
Spencer Lowf373c352015-11-15 16:29:36 -0800346extern int adb_putchar(int ch);
347extern int adb_puts(const char* buf);
Spencer Low6815c072015-05-11 01:08:48 -0700348extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
349 FILE* stream);
350
351extern FILE* adb_fopen(const char* f, const char* m);
352
353extern char* adb_getenv(const char* name);
354
355extern char* adb_getcwd(char* buf, int size);
356
357// Remap calls to POSIX APIs to our UTF-8 versions.
358#define opendir adb_opendir
359#define readdir adb_readdir
360#define closedir adb_closedir
361#define rewinddir rewinddir_utf8_not_yet_implemented
362#define telldir telldir_utf8_not_yet_implemented
Spencer Low28bc2cb2015-11-07 18:51:54 -0800363// Some compiler's C++ headers have members named seekdir, so we can't do the
364// macro technique and instead cause a link error if seekdir is called.
365inline void seekdir(DIR*, long) {
366 extern int seekdir_utf8_not_yet_implemented;
367 seekdir_utf8_not_yet_implemented = 1;
368}
Spencer Low6815c072015-05-11 01:08:48 -0700369
370#define utime adb_utime
371#define chmod adb_chmod
372
373#define vfprintf adb_vfprintf
Spencer Lowf373c352015-11-15 16:29:36 -0800374#define vprintf adb_vprintf
Spencer Low6815c072015-05-11 01:08:48 -0700375#define fprintf adb_fprintf
376#define printf adb_printf
377#define fputs adb_fputs
378#define fputc adb_fputc
Spencer Lowf373c352015-11-15 16:29:36 -0800379// putc may be a macro, so if so, undefine it, so that we can redefine it.
380#undef putc
381#define putc(c, s) adb_fputc(c, s)
382#define putchar adb_putchar
383#define puts adb_puts
Spencer Low6815c072015-05-11 01:08:48 -0700384#define fwrite adb_fwrite
385
386#define fopen adb_fopen
Spencer Lowf373c352015-11-15 16:29:36 -0800387#define freopen freopen_utf8_not_yet_implemented
Spencer Low6815c072015-05-11 01:08:48 -0700388
389#define getenv adb_getenv
390#define putenv putenv_utf8_not_yet_implemented
391#define setenv setenv_utf8_not_yet_implemented
392#define unsetenv unsetenv_utf8_not_yet_implemented
393
394#define getcwd adb_getcwd
395
Spencer Low028e1592015-10-18 16:45:09 -0700396char* adb_strerror(int err);
397#define strerror adb_strerror
398
Spencer Low6815c072015-05-11 01:08:48 -0700399// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
400// passed to main().
401class NarrowArgs {
402public:
403 NarrowArgs(int argc, wchar_t** argv);
404 ~NarrowArgs();
405
406 inline char** data() {
407 return narrow_args;
408 }
409
410private:
411 char** narrow_args;
412};
413
Spencer Lowb28812f2015-08-08 15:07:07 -0700414// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
415// so they can fit in an int. To convert back, we just need to sign-extend.
416// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
417// Note that this does not make a HANDLE value work with APIs like open(), nor
418// does this make a value from open() passable to APIs taking a HANDLE. This
419// just lets you take a HANDLE, pass it around as an int, and then use it again
420// as a HANDLE.
421inline int cast_handle_to_int(const HANDLE h) {
422 // truncate
423 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
424}
425
426inline HANDLE cast_int_to_handle(const int fd) {
427 // sign-extend
428 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
429}
430
Spencer Low2bbb3a92015-08-26 18:46:09 -0700431// Deleter for unique_handle. Adapted from many sources, including:
432// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
433// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
434class handle_deleter {
435public:
436 typedef HANDLE pointer;
437
438 void operator()(HANDLE h);
439};
440
441// Like std::unique_ptr, but for Windows HANDLE objects that should be
442// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
443// but does not check if the handle != INVALID_HANDLE_VALUE.
444typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
445
Spencer Lowf373c352015-11-15 16:29:36 -0800446namespace internal {
447
448size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
449
450}
451
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800452#else /* !_WIN32 a.k.a. Unix */
453
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200454#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800455#include <cutils/misc.h>
Spencer Low753d4852015-07-30 23:07:55 -0700456#include <cutils/sockets.h>
Spencer Low5c761bd2015-07-21 02:06:26 -0700457#include <cutils/threads.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458#include <signal.h>
459#include <sys/wait.h>
460#include <sys/stat.h>
461#include <fcntl.h>
462
463#include <pthread.h>
464#include <unistd.h>
465#include <fcntl.h>
466#include <stdarg.h>
Spencer Low753d4852015-07-30 23:07:55 -0700467#include <netdb.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800468#include <netinet/in.h>
469#include <netinet/tcp.h>
470#include <string.h>
Kenny Rooteac025c2012-10-12 15:26:45 -0700471#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800472
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400473#include <string>
474
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700475#define OS_PATH_SEPARATORS "/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800476#define OS_PATH_SEPARATOR '/'
477#define OS_PATH_SEPARATOR_STR "/"
Benoit Goby2cc19e42012-04-12 12:23:49 -0700478#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800479
Josh Gao8acf06c2015-11-07 15:38:19 -0800480static __inline__ bool adb_is_separator(char c) {
481 return c == '/';
482}
483
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800484typedef pthread_mutex_t adb_mutex_t;
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700485
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800486#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
487#define adb_mutex_init pthread_mutex_init
488#define adb_mutex_lock pthread_mutex_lock
489#define adb_mutex_unlock pthread_mutex_unlock
490#define adb_mutex_destroy pthread_mutex_destroy
491
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700492#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800493
494#define adb_cond_t pthread_cond_t
495#define adb_cond_init pthread_cond_init
496#define adb_cond_wait pthread_cond_wait
497#define adb_cond_broadcast pthread_cond_broadcast
498#define adb_cond_signal pthread_cond_signal
499#define adb_cond_destroy pthread_cond_destroy
500
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700501/* declare all mutexes */
502#define ADB_MUTEX(x) extern adb_mutex_t x;
503#include "mutex_list.h"
504
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800505static __inline__ void close_on_exec(int fd)
506{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200507 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800508}
509
Spencer Low3a2421b2015-05-22 20:09:06 -0700510// Open a file and return a file descriptor that may be used with unix_read(),
511// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
512//
513// On Unix, this is based on open(), so the file descriptor is a real OS file
514// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
515// file descriptor that can only be used with C Runtime APIs (which are wrapped
516// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
517// configurable CR/LF translation which defaults to text mode, but is settable
518// with _setmode().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800519static __inline__ int unix_open(const char* path, int options,...)
520{
521 if ((options & O_CREAT) == 0)
522 {
Kenny Rooteac025c2012-10-12 15:26:45 -0700523 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800524 }
525 else
526 {
527 int mode;
528 va_list args;
529 va_start( args, options );
530 mode = va_arg( args, int );
531 va_end( args );
Kenny Rooteac025c2012-10-12 15:26:45 -0700532 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800533 }
534}
535
Spencer Low3a2421b2015-05-22 20:09:06 -0700536// Similar to the two-argument adb_open(), but takes a mode parameter for file
537// creation. See adb_open() for more info.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800538static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
539{
Kenny Rooteac025c2012-10-12 15:26:45 -0700540 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800541}
542
543
Spencer Low3a2421b2015-05-22 20:09:06 -0700544// Open a file and return a file descriptor that may be used with adb_read(),
545// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
546//
547// On Unix, this is based on open(), but the Windows implementation (in
548// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
549// and its CR/LF translation. The returned file descriptor should be used with
550// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800551static __inline__ int adb_open( const char* pathname, int options )
552{
Kenny Rooteac025c2012-10-12 15:26:45 -0700553 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800554 if (fd < 0)
555 return -1;
556 close_on_exec( fd );
557 return fd;
558}
559#undef open
560#define open ___xxx_open
561
Mike Lockwood81ffe172009-10-11 23:04:18 -0400562static __inline__ int adb_shutdown(int fd)
563{
564 return shutdown(fd, SHUT_RDWR);
565}
David Pursell3fe11f62015-10-06 15:30:03 -0700566static __inline__ int adb_shutdown(int fd, int direction)
567{
568 return shutdown(fd, direction);
569}
Mike Lockwood81ffe172009-10-11 23:04:18 -0400570#undef shutdown
571#define shutdown ____xxx_shutdown
572
Spencer Low3a2421b2015-05-22 20:09:06 -0700573// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
574// not designed to take a file descriptor from unix_open(). See the comments
575// for adb_open() for more info.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800576static __inline__ int adb_close(int fd)
577{
578 return close(fd);
579}
580#undef close
581#define close ____xxx_close
582
583
584static __inline__ int adb_read(int fd, void* buf, size_t len)
585{
Kenny Rooteac025c2012-10-12 15:26:45 -0700586 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800587}
588
Spencer Low55441402015-11-07 17:34:39 -0800589// Like unix_read(), but does not handle EINTR.
590static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
591 return read(fd, buf, len);
592}
593
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800594#undef read
595#define read ___xxx_read
596
597static __inline__ int adb_write(int fd, const void* buf, size_t len)
598{
Kenny Rooteac025c2012-10-12 15:26:45 -0700599 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800600}
601#undef write
602#define write ___xxx_write
603
604static __inline__ int adb_lseek(int fd, int pos, int where)
605{
606 return lseek(fd, pos, where);
607}
608#undef lseek
609#define lseek ___xxx_lseek
610
611static __inline__ int adb_unlink(const char* path)
612{
613 return unlink(path);
614}
615#undef unlink
616#define unlink ___xxx_unlink
617
618static __inline__ int adb_creat(const char* path, int mode)
619{
Kenny Rooteac025c2012-10-12 15:26:45 -0700620 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800621
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200622 if ( fd < 0 )
623 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800624
625 close_on_exec(fd);
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200626 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800627}
628#undef creat
629#define creat ___xxx_creat
630
David Pursell58805362015-10-28 14:29:51 -0700631static __inline__ int unix_isatty(int fd) {
632 return isatty(fd);
633}
634#define isatty ___xxx_isatty
635
Spencer Low753d4852015-07-30 23:07:55 -0700636// Helper for network_* functions.
637inline int _fd_set_error_str(int fd, std::string* error) {
638 if (fd == -1) {
639 *error = strerror(errno);
640 }
641 return fd;
642}
643
644inline int network_loopback_client(int port, int type, std::string* error) {
645 return _fd_set_error_str(socket_loopback_client(port, type), error);
646}
647
648inline int network_loopback_server(int port, int type, std::string* error) {
649 return _fd_set_error_str(socket_loopback_server(port, type), error);
650}
651
652inline int network_inaddr_any_server(int port, int type, std::string* error) {
653 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
654}
655
656inline int network_local_server(const char *name, int namespace_id, int type,
657 std::string* error) {
658 return _fd_set_error_str(socket_local_server(name, namespace_id, type),
659 error);
660}
661
662inline int network_connect(const std::string& host, int port, int type,
663 int timeout, std::string* error) {
664 int getaddrinfo_error = 0;
665 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
666 &getaddrinfo_error);
667 if (fd != -1) {
668 return fd;
669 }
670 if (getaddrinfo_error != 0) {
671 *error = gai_strerror(getaddrinfo_error);
672 } else {
673 *error = strerror(errno);
674 }
675 return -1;
676}
677
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800678static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
679{
Benoit Gobyf4ded742011-02-01 18:57:41 -0800680 int fd;
681
Kenny Rooteac025c2012-10-12 15:26:45 -0700682 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Gobyf4ded742011-02-01 18:57:41 -0800683 if (fd >= 0)
684 close_on_exec(fd);
685
686 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800687}
688
689#undef accept
690#define accept ___xxx_accept
691
Spencer Low3a2421b2015-05-22 20:09:06 -0700692// Operate on a file descriptor returned from unix_open() or a well-known file
693// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
694//
695// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
696// adb_write(), adb_close() (which all map to Unix system calls), but the
697// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
698// into the C Runtime and its configurable CR/LF translation (which is settable
699// via _setmode()).
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800700#define unix_read adb_read
701#define unix_write adb_write
702#define unix_close adb_close
703
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800704typedef void* (*adb_thread_func_t)( void* arg );
705
Josh Gaoa4f5e032016-02-09 14:59:09 -0800706typedef pthread_t adb_thread_t;
707
708static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg,
709 adb_thread_t* thread = nullptr) {
710 pthread_t temp;
Elliott Hughesf2517142015-05-05 13:41:21 -0700711 pthread_attr_t attr;
712 pthread_attr_init(&attr);
Josh Gaoa4f5e032016-02-09 14:59:09 -0800713 pthread_attr_setdetachstate(&attr, thread ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED);
714 errno = pthread_create(&temp, &attr, start, arg);
715 if (errno == 0) {
716 if (thread) {
717 *thread = temp;
718 }
719 return true;
720 }
721 return false;
722}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800723
Josh Gaoa4f5e032016-02-09 14:59:09 -0800724static __inline__ bool adb_thread_join(adb_thread_t thread) {
725 errno = pthread_join(thread, nullptr);
726 return errno == 0;
727}
728
729static __inline__ bool adb_thread_detach(adb_thread_t thread) {
730 errno = pthread_detach(thread);
731 return errno == 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800732}
733
Siva Velusamy2669cf92015-08-28 16:37:29 -0700734static __inline__ int adb_thread_setname(const std::string& name) {
735#ifdef __APPLE__
736 return pthread_setname_np(name.c_str());
737#else
738 const char *s = name.c_str();
739
740 // pthread_setname_np fails rather than truncating long strings.
741 const int max_task_comm_len = 16; // including the null terminator
742 if (name.length() > (max_task_comm_len - 1)) {
743 char buf[max_task_comm_len];
744 strncpy(buf, name.c_str(), sizeof(buf) - 1);
745 buf[sizeof(buf) - 1] = '\0';
746 s = buf;
747 }
748
749 return pthread_setname_np(pthread_self(), s) ;
750#endif
751}
752
Spencer Low31aafa62015-01-25 14:40:16 -0800753static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
754{
755 return setsockopt( fd, level, optname, optval, optlen );
756}
757
758#undef setsockopt
759#define setsockopt ___xxx_setsockopt
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800760
761static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
762{
763 return socketpair( d, type, protocol, sv );
764}
765
766static __inline__ int adb_socketpair( int sv[2] )
767{
768 int rc;
769
770 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
771 if (rc < 0)
772 return -1;
773
774 close_on_exec( sv[0] );
775 close_on_exec( sv[1] );
776 return 0;
777}
778
779#undef socketpair
780#define socketpair ___xxx_socketpair
781
782static __inline__ void adb_sleep_ms( int mseconds )
783{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200784 usleep( mseconds*1000 );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800785}
786
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400787static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800788{
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400789 return mkdir(path.c_str(), mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800790}
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400791
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800792#undef mkdir
793#define mkdir ___xxx_mkdir
794
795static __inline__ void adb_sysdeps_init(void)
796{
797}
798
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400799static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800800 return path[0] == '/';
801}
802
leozwang1be54622014-08-15 09:51:27 -0700803static __inline__ unsigned long adb_thread_id()
804{
Spencer Low5c761bd2015-07-21 02:06:26 -0700805 return (unsigned long)gettid();
leozwang1be54622014-08-15 09:51:27 -0700806}
807
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800808#endif /* !_WIN32 */
809
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800810static inline void disable_tcp_nagle(int fd) {
811 int off = 1;
812 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
813}
814
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800815#endif /* _ADB_SYSDEPS_H */