blob: 0abade46fd415963511d96fad661438d56e51254 [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>
30
Spencer Lowd21dc822015-11-12 15:20:15 -080031// Include this before open/unlink are defined as macros below.
Elliott Hughes4f713192015-12-04 22:00:26 -080032#include <android-base/utf8.h>
Spencer Lowd21dc822015-11-12 15:20:15 -080033
Dan Albertcc731cc2015-02-24 21:26:58 -080034/*
35 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
36 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
37 * not already defined, then define it here.
38 */
39#ifndef TEMP_FAILURE_RETRY
40/* Used to retry syscalls that can return EINTR. */
41#define TEMP_FAILURE_RETRY(exp) ({ \
42 typeof (exp) _rc; \
43 do { \
44 _rc = (exp); \
45 } while (_rc == -1 && errno == EINTR); \
46 _rc; })
47#endif
48
Spencer Lowcf4ff642015-05-11 01:08:48 -070049// Some printf-like functions are implemented in terms of
50// android::base::StringAppendV, so they should use the same attribute for
51// compile-time format string checking. On Windows, if the mingw version of
52// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
53// and PRIu64 (and related) to be recognized by the compile-time checking.
54#define ADB_FORMAT_ARCHETYPE __printf__
55#ifdef __USE_MINGW_ANSI_STDIO
56#if __USE_MINGW_ANSI_STDIO
57#undef ADB_FORMAT_ARCHETYPE
58#define ADB_FORMAT_ARCHETYPE gnu_printf
59#endif
60#endif
61
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062#ifdef _WIN32
63
Dan Albert630b9af2014-11-24 23:34:35 -080064#include <ctype.h>
65#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070066#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080067#include <errno.h>
68#include <fcntl.h>
69#include <io.h>
70#include <process.h>
71#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070072#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070074#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080076
Spencer Low2122c7a2015-08-26 18:46:09 -070077#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080078#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040079
Dan Albert630b9af2014-11-24 23:34:35 -080080#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
Elliott Hughes5c742702015-07-30 17:42:01 -070082#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083#define OS_PATH_SEPARATOR '\\'
84#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070085#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086
Josh Gao07db1192015-11-07 15:38:19 -080087static __inline__ bool adb_is_separator(char c) {
88 return c == '\\' || c == '/';
89}
90
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091typedef CRITICAL_SECTION adb_mutex_t;
92
93#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
94
95/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070096/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097#define ADB_MUTEX(x) extern adb_mutex_t x;
98#include "mutex_list.h"
99
100extern void adb_sysdeps_init(void);
101
102static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
103{
104 EnterCriticalSection( lock );
105}
106
107static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
108{
109 LeaveCriticalSection( lock );
110}
111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112typedef void* (*adb_thread_func_t)(void* arg);
113
114typedef void (*win_thread_func_t)(void* arg);
115
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700116static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) {
Elliott Hughes2e4a2ee2015-05-05 14:34:41 -0700117 uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg);
118 return (tid != static_cast<uintptr_t>(-1L));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119}
120
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700121static __inline__ int adb_thread_setname(const std::string& name) {
122 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
123 // the thread name in Windows. Unfortunately, it only works during debugging, but
124 // our build process doesn't generate PDB files needed for debugging.
125 return 0;
126}
127
leozwangcbf02672014-08-15 09:51:27 -0700128static __inline__ unsigned long adb_thread_id()
129{
130 return GetCurrentThreadId();
131}
132
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133static __inline__ void close_on_exec(int fd)
134{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200135 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136}
137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138#define lstat stat /* no symlinks on Win32 */
139
140#define S_ISLNK(m) 0 /* no symlinks on Win32 */
141
Spencer Lowcf4ff642015-05-11 01:08:48 -0700142extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143#undef unlink
144#define unlink ___xxx_unlink
145
Spencer Lowcf4ff642015-05-11 01:08:48 -0700146extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147#undef mkdir
148#define mkdir ___xxx_mkdir
149
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700150// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151extern int adb_open(const char* path, int options);
152extern int adb_creat(const char* path, int mode);
153extern int adb_read(int fd, void* buf, int len);
154extern int adb_write(int fd, const void* buf, int len);
155extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400156extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157extern int adb_close(int fd);
158
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700159// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160static __inline__ int unix_close(int fd)
161{
162 return close(fd);
163}
164#undef close
165#define close ____xxx_close
166
Spencer Low2e02dc62015-11-07 17:34:39 -0800167// Like unix_read(), but may return EINTR.
168extern int unix_read_interruptible(int fd, void* buf, size_t len);
169
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700170// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800171static __inline__ int unix_read(int fd, void* buf, size_t len) {
172 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
173}
Spencer Low50184062015-03-01 15:06:21 -0800174
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175#undef read
176#define read ___xxx_read
177
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700178// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179static __inline__ int unix_write(int fd, const void* buf, size_t len)
180{
181 return write(fd, buf, len);
182}
183#undef write
184#define write ___xxx_write
185
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700186// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187static __inline__ int adb_open_mode(const char* path, int options, int mode)
188{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200189 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190}
191
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700192// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700193extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194#define open ___xxx_unix_open
195
David Pursellc5b8ad82015-10-28 14:29:51 -0700196// Checks if |fd| corresponds to a console.
197// Standard Windows isatty() returns 1 for both console FDs and character
198// devices like NUL. unix_isatty() performs some extra checking to only match
199// console FDs.
200// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
201// will work but adb_open() FDs will not. Additionally the OS handle associated
202// with |fd| must have GENERIC_READ access (which console FDs have by default).
203// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
204// calling this function is unreliable and should not be used.
205int unix_isatty(int fd);
206#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
208/* normally provided by <cutils/misc.h> */
209extern void* load_file(const char* pathname, unsigned* psize);
210
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200211/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212
213#define FDE_READ 0x0001
214#define FDE_WRITE 0x0002
215#define FDE_ERROR 0x0004
216#define FDE_DONT_CLOSE 0x0080
217
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218typedef void (*fd_func)(int fd, unsigned events, void *userdata);
219
220fdevent *fdevent_create(int fd, fd_func func, void *arg);
221void fdevent_destroy(fdevent *fde);
222void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
223void fdevent_remove(fdevent *item);
224void fdevent_set(fdevent *fde, unsigned events);
225void fdevent_add(fdevent *fde, unsigned events);
226void fdevent_del(fdevent *fde, unsigned events);
227void fdevent_loop();
228
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229static __inline__ void adb_sleep_ms( int mseconds )
230{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200231 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232}
233
Spencer Low5200c662015-07-30 23:07:55 -0700234int network_loopback_client(int port, int type, std::string* error);
235int network_loopback_server(int port, int type, std::string* error);
236int network_inaddr_any_server(int port, int type, std::string* error);
237int network_connect(const std::string& host, int port, int type, int timeout,
238 std::string* error);
239
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
241
242#undef accept
243#define accept ___xxx_accept
244
Spencer Lowf055c192015-01-25 14:40:16 -0800245extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
246
247#undef setsockopt
248#define setsockopt ___xxx_setsockopt
249
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250extern int adb_socketpair( int sv[2] );
251
Elliott Hughes5c742702015-07-30 17:42:01 -0700252static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
254}
255
Spencer Low5200c662015-07-30 23:07:55 -0700256// Like strerror(), but for Win32 error codes.
257std::string SystemErrorCodeToString(DWORD error_code);
258
Spencer Lowcf4ff642015-05-11 01:08:48 -0700259// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
260// struct stat s;
261// stat(filename, &s);
262// To turn into the following:
263// struct adb_stat s;
264// adb_stat(filename, &s);
265// To get this to work, we need to make 'struct adb_stat' the same as
266// 'struct stat'. Note that this definition of 'struct adb_stat' uses the
267// *current* macro definition of stat, so it may actually be inheriting from
268// struct _stat32i64 (or some other remapping).
269struct adb_stat : public stat {};
270
271static_assert(sizeof(struct adb_stat) == sizeof(struct stat),
272 "structures should be the same");
273
274extern int adb_stat(const char* f, struct adb_stat* s);
275
276// stat is already a macro, undefine it so we can redefine it.
277#undef stat
278#define stat adb_stat
279
280// UTF-8 versions of POSIX APIs.
281extern DIR* adb_opendir(const char* dirname);
282extern struct dirent* adb_readdir(DIR* dir);
283extern int adb_closedir(DIR* dir);
284
285extern int adb_utime(const char *, struct utimbuf *);
286extern int adb_chmod(const char *, int);
287
288extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
289 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
290extern int adb_fprintf(FILE *stream, const char *format, ...)
291 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
292extern int adb_printf(const char *format, ...)
293 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
294
295extern int adb_fputs(const char* buf, FILE* stream);
296extern int adb_fputc(int ch, FILE* stream);
297extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
298 FILE* stream);
299
300extern FILE* adb_fopen(const char* f, const char* m);
301
302extern char* adb_getenv(const char* name);
303
304extern char* adb_getcwd(char* buf, int size);
305
306// Remap calls to POSIX APIs to our UTF-8 versions.
307#define opendir adb_opendir
308#define readdir adb_readdir
309#define closedir adb_closedir
310#define rewinddir rewinddir_utf8_not_yet_implemented
311#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800312// Some compiler's C++ headers have members named seekdir, so we can't do the
313// macro technique and instead cause a link error if seekdir is called.
314inline void seekdir(DIR*, long) {
315 extern int seekdir_utf8_not_yet_implemented;
316 seekdir_utf8_not_yet_implemented = 1;
317}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700318
319#define utime adb_utime
320#define chmod adb_chmod
321
322#define vfprintf adb_vfprintf
323#define fprintf adb_fprintf
324#define printf adb_printf
325#define fputs adb_fputs
326#define fputc adb_fputc
327#define fwrite adb_fwrite
328
329#define fopen adb_fopen
330
331#define getenv adb_getenv
332#define putenv putenv_utf8_not_yet_implemented
333#define setenv setenv_utf8_not_yet_implemented
334#define unsetenv unsetenv_utf8_not_yet_implemented
335
336#define getcwd adb_getcwd
337
Spencer Low0a796002015-10-18 16:45:09 -0700338char* adb_strerror(int err);
339#define strerror adb_strerror
340
Spencer Lowcf4ff642015-05-11 01:08:48 -0700341// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
342// passed to main().
343class NarrowArgs {
344public:
345 NarrowArgs(int argc, wchar_t** argv);
346 ~NarrowArgs();
347
348 inline char** data() {
349 return narrow_args;
350 }
351
352private:
353 char** narrow_args;
354};
355
Spencer Low5c398d22015-08-08 15:07:07 -0700356// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
357// so they can fit in an int. To convert back, we just need to sign-extend.
358// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
359// Note that this does not make a HANDLE value work with APIs like open(), nor
360// does this make a value from open() passable to APIs taking a HANDLE. This
361// just lets you take a HANDLE, pass it around as an int, and then use it again
362// as a HANDLE.
363inline int cast_handle_to_int(const HANDLE h) {
364 // truncate
365 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
366}
367
368inline HANDLE cast_int_to_handle(const int fd) {
369 // sign-extend
370 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
371}
372
Spencer Low2122c7a2015-08-26 18:46:09 -0700373// Deleter for unique_handle. Adapted from many sources, including:
374// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
375// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
376class handle_deleter {
377public:
378 typedef HANDLE pointer;
379
380 void operator()(HANDLE h);
381};
382
383// Like std::unique_ptr, but for Windows HANDLE objects that should be
384// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
385// but does not check if the handle != INVALID_HANDLE_VALUE.
386typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
387
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388#else /* !_WIN32 a.k.a. Unix */
389
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200390#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391#include <cutils/misc.h>
Spencer Low5200c662015-07-30 23:07:55 -0700392#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700393#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394#include <signal.h>
395#include <sys/wait.h>
396#include <sys/stat.h>
397#include <fcntl.h>
398
399#include <pthread.h>
400#include <unistd.h>
401#include <fcntl.h>
402#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700403#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404#include <netinet/in.h>
405#include <netinet/tcp.h>
406#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700407#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408
Alex Vallée47d67c92015-05-06 16:26:00 -0400409#include <string>
410
Elliott Hughes5c742702015-07-30 17:42:01 -0700411#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412#define OS_PATH_SEPARATOR '/'
413#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700414#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415
Josh Gao07db1192015-11-07 15:38:19 -0800416static __inline__ bool adb_is_separator(char c) {
417 return c == '/';
418}
419
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700421
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
423#define adb_mutex_init pthread_mutex_init
424#define adb_mutex_lock pthread_mutex_lock
425#define adb_mutex_unlock pthread_mutex_unlock
426#define adb_mutex_destroy pthread_mutex_destroy
427
JP Abgrall408fa572011-03-16 15:57:42 -0700428#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429
430#define adb_cond_t pthread_cond_t
431#define adb_cond_init pthread_cond_init
432#define adb_cond_wait pthread_cond_wait
433#define adb_cond_broadcast pthread_cond_broadcast
434#define adb_cond_signal pthread_cond_signal
435#define adb_cond_destroy pthread_cond_destroy
436
JP Abgrall408fa572011-03-16 15:57:42 -0700437/* declare all mutexes */
438#define ADB_MUTEX(x) extern adb_mutex_t x;
439#include "mutex_list.h"
440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441static __inline__ void close_on_exec(int fd)
442{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200443 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444}
445
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700446// Open a file and return a file descriptor that may be used with unix_read(),
447// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
448//
449// On Unix, this is based on open(), so the file descriptor is a real OS file
450// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
451// file descriptor that can only be used with C Runtime APIs (which are wrapped
452// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
453// configurable CR/LF translation which defaults to text mode, but is settable
454// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455static __inline__ int unix_open(const char* path, int options,...)
456{
457 if ((options & O_CREAT) == 0)
458 {
Kenny Root73167412012-10-12 15:26:45 -0700459 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 }
461 else
462 {
463 int mode;
464 va_list args;
465 va_start( args, options );
466 mode = va_arg( args, int );
467 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700468 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 }
470}
471
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700472// Similar to the two-argument adb_open(), but takes a mode parameter for file
473// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
475{
Kenny Root73167412012-10-12 15:26:45 -0700476 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477}
478
479
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700480// Open a file and return a file descriptor that may be used with adb_read(),
481// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
482//
483// On Unix, this is based on open(), but the Windows implementation (in
484// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
485// and its CR/LF translation. The returned file descriptor should be used with
486// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487static __inline__ int adb_open( const char* pathname, int options )
488{
Kenny Root73167412012-10-12 15:26:45 -0700489 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 if (fd < 0)
491 return -1;
492 close_on_exec( fd );
493 return fd;
494}
495#undef open
496#define open ___xxx_open
497
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400498static __inline__ int adb_shutdown(int fd)
499{
500 return shutdown(fd, SHUT_RDWR);
501}
David Pursell1ed57f02015-10-06 15:30:03 -0700502static __inline__ int adb_shutdown(int fd, int direction)
503{
504 return shutdown(fd, direction);
505}
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400506#undef shutdown
507#define shutdown ____xxx_shutdown
508
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700509// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
510// not designed to take a file descriptor from unix_open(). See the comments
511// for adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512static __inline__ int adb_close(int fd)
513{
514 return close(fd);
515}
516#undef close
517#define close ____xxx_close
518
519
520static __inline__ int adb_read(int fd, void* buf, size_t len)
521{
Kenny Root73167412012-10-12 15:26:45 -0700522 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523}
524
Spencer Low2e02dc62015-11-07 17:34:39 -0800525// Like unix_read(), but does not handle EINTR.
526static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
527 return read(fd, buf, len);
528}
529
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530#undef read
531#define read ___xxx_read
532
533static __inline__ int adb_write(int fd, const void* buf, size_t len)
534{
Kenny Root73167412012-10-12 15:26:45 -0700535 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536}
537#undef write
538#define write ___xxx_write
539
540static __inline__ int adb_lseek(int fd, int pos, int where)
541{
542 return lseek(fd, pos, where);
543}
544#undef lseek
545#define lseek ___xxx_lseek
546
547static __inline__ int adb_unlink(const char* path)
548{
549 return unlink(path);
550}
551#undef unlink
552#define unlink ___xxx_unlink
553
554static __inline__ int adb_creat(const char* path, int mode)
555{
Kenny Root73167412012-10-12 15:26:45 -0700556 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200558 if ( fd < 0 )
559 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560
561 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200562 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563}
564#undef creat
565#define creat ___xxx_creat
566
David Pursellc5b8ad82015-10-28 14:29:51 -0700567static __inline__ int unix_isatty(int fd) {
568 return isatty(fd);
569}
570#define isatty ___xxx_isatty
571
Spencer Low5200c662015-07-30 23:07:55 -0700572// Helper for network_* functions.
573inline int _fd_set_error_str(int fd, std::string* error) {
574 if (fd == -1) {
575 *error = strerror(errno);
576 }
577 return fd;
578}
579
580inline int network_loopback_client(int port, int type, std::string* error) {
581 return _fd_set_error_str(socket_loopback_client(port, type), error);
582}
583
584inline int network_loopback_server(int port, int type, std::string* error) {
585 return _fd_set_error_str(socket_loopback_server(port, type), error);
586}
587
588inline int network_inaddr_any_server(int port, int type, std::string* error) {
589 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
590}
591
592inline int network_local_server(const char *name, int namespace_id, int type,
593 std::string* error) {
594 return _fd_set_error_str(socket_local_server(name, namespace_id, type),
595 error);
596}
597
598inline int network_connect(const std::string& host, int port, int type,
599 int timeout, std::string* error) {
600 int getaddrinfo_error = 0;
601 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
602 &getaddrinfo_error);
603 if (fd != -1) {
604 return fd;
605 }
606 if (getaddrinfo_error != 0) {
607 *error = gai_strerror(getaddrinfo_error);
608 } else {
609 *error = strerror(errno);
610 }
611 return -1;
612}
613
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
615{
Benoit Goby95ef8282011-02-01 18:57:41 -0800616 int fd;
617
Kenny Root73167412012-10-12 15:26:45 -0700618 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800619 if (fd >= 0)
620 close_on_exec(fd);
621
622 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623}
624
625#undef accept
626#define accept ___xxx_accept
627
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700628// Operate on a file descriptor returned from unix_open() or a well-known file
629// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
630//
631// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
632// adb_write(), adb_close() (which all map to Unix system calls), but the
633// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
634// into the C Runtime and its configurable CR/LF translation (which is settable
635// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636#define unix_read adb_read
637#define unix_write adb_write
638#define unix_close adb_close
639
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640typedef void* (*adb_thread_func_t)( void* arg );
641
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700642static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) {
643 pthread_attr_t attr;
644 pthread_attr_init(&attr);
645 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700647 pthread_t thread;
648 errno = pthread_create(&thread, &attr, start, arg);
649 return (errno == 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650}
651
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700652static __inline__ int adb_thread_setname(const std::string& name) {
653#ifdef __APPLE__
654 return pthread_setname_np(name.c_str());
655#else
656 const char *s = name.c_str();
657
658 // pthread_setname_np fails rather than truncating long strings.
659 const int max_task_comm_len = 16; // including the null terminator
660 if (name.length() > (max_task_comm_len - 1)) {
661 char buf[max_task_comm_len];
662 strncpy(buf, name.c_str(), sizeof(buf) - 1);
663 buf[sizeof(buf) - 1] = '\0';
664 s = buf;
665 }
666
667 return pthread_setname_np(pthread_self(), s) ;
668#endif
669}
670
Spencer Lowf055c192015-01-25 14:40:16 -0800671static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
672{
673 return setsockopt( fd, level, optname, optval, optlen );
674}
675
676#undef setsockopt
677#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678
679static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
680{
681 return socketpair( d, type, protocol, sv );
682}
683
684static __inline__ int adb_socketpair( int sv[2] )
685{
686 int rc;
687
688 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
689 if (rc < 0)
690 return -1;
691
692 close_on_exec( sv[0] );
693 close_on_exec( sv[1] );
694 return 0;
695}
696
697#undef socketpair
698#define socketpair ___xxx_socketpair
699
700static __inline__ void adb_sleep_ms( int mseconds )
701{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200702 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703}
704
Alex Vallée47d67c92015-05-06 16:26:00 -0400705static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706{
Alex Vallée47d67c92015-05-06 16:26:00 -0400707 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708}
Alex Vallée47d67c92015-05-06 16:26:00 -0400709
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710#undef mkdir
711#define mkdir ___xxx_mkdir
712
713static __inline__ void adb_sysdeps_init(void)
714{
715}
716
Alex Vallée47d67c92015-05-06 16:26:00 -0400717static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 return path[0] == '/';
719}
720
leozwangcbf02672014-08-15 09:51:27 -0700721static __inline__ unsigned long adb_thread_id()
722{
Spencer Low8d8126a2015-07-21 02:06:26 -0700723 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700724}
725
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726#endif /* !_WIN32 */
727
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800728static inline void disable_tcp_nagle(int fd) {
729 int off = 1;
730 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
731}
732
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733#endif /* _ADB_SYSDEPS_H */