blob: 9f4012ade571eb5c480b0e7fef14c04e04ea1c62 [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
Dan Albertcc731cc2015-02-24 21:26:58 -080031/*
32 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
33 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
34 * not already defined, then define it here.
35 */
36#ifndef TEMP_FAILURE_RETRY
37/* Used to retry syscalls that can return EINTR. */
38#define TEMP_FAILURE_RETRY(exp) ({ \
39 typeof (exp) _rc; \
40 do { \
41 _rc = (exp); \
42 } while (_rc == -1 && errno == EINTR); \
43 _rc; })
44#endif
45
Spencer Lowcf4ff642015-05-11 01:08:48 -070046// Some printf-like functions are implemented in terms of
47// android::base::StringAppendV, so they should use the same attribute for
48// compile-time format string checking. On Windows, if the mingw version of
49// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
50// and PRIu64 (and related) to be recognized by the compile-time checking.
51#define ADB_FORMAT_ARCHETYPE __printf__
52#ifdef __USE_MINGW_ANSI_STDIO
53#if __USE_MINGW_ANSI_STDIO
54#undef ADB_FORMAT_ARCHETYPE
55#define ADB_FORMAT_ARCHETYPE gnu_printf
56#endif
57#endif
58
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059#ifdef _WIN32
60
Dan Albert630b9af2014-11-24 23:34:35 -080061#include <ctype.h>
62#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070063#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080064#include <errno.h>
65#include <fcntl.h>
66#include <io.h>
67#include <process.h>
68#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070069#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070071#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080073
Spencer Low2122c7a2015-08-26 18:46:09 -070074#include <memory> // unique_ptr
Spencer Lowcf4ff642015-05-11 01:08:48 -070075#include <string> // Prototypes for narrow() and widen() use std::(w)string.
Alex Vallée47d67c92015-05-06 16:26:00 -040076
Dan Albert630b9af2014-11-24 23:34:35 -080077#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Elliott Hughes5c742702015-07-30 17:42:01 -070079#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080#define OS_PATH_SEPARATOR '\\'
81#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070082#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083
84typedef CRITICAL_SECTION adb_mutex_t;
85
86#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
87
88/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070089/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090#define ADB_MUTEX(x) extern adb_mutex_t x;
91#include "mutex_list.h"
92
93extern void adb_sysdeps_init(void);
94
95static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
96{
97 EnterCriticalSection( lock );
98}
99
100static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
101{
102 LeaveCriticalSection( lock );
103}
104
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105typedef void* (*adb_thread_func_t)(void* arg);
106
107typedef void (*win_thread_func_t)(void* arg);
108
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700109static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) {
Elliott Hughes2e4a2ee2015-05-05 14:34:41 -0700110 uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg);
111 return (tid != static_cast<uintptr_t>(-1L));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112}
113
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700114static __inline__ int adb_thread_setname(const std::string& name) {
115 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
116 // the thread name in Windows. Unfortunately, it only works during debugging, but
117 // our build process doesn't generate PDB files needed for debugging.
118 return 0;
119}
120
leozwangcbf02672014-08-15 09:51:27 -0700121static __inline__ unsigned long adb_thread_id()
122{
123 return GetCurrentThreadId();
124}
125
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126static __inline__ void close_on_exec(int fd)
127{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200128 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129}
130
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131#define lstat stat /* no symlinks on Win32 */
132
133#define S_ISLNK(m) 0 /* no symlinks on Win32 */
134
Spencer Lowcf4ff642015-05-11 01:08:48 -0700135extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136#undef unlink
137#define unlink ___xxx_unlink
138
Spencer Lowcf4ff642015-05-11 01:08:48 -0700139extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140#undef mkdir
141#define mkdir ___xxx_mkdir
142
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700143// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144extern int adb_open(const char* path, int options);
145extern int adb_creat(const char* path, int mode);
146extern int adb_read(int fd, void* buf, int len);
147extern int adb_write(int fd, const void* buf, int len);
148extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400149extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150extern int adb_close(int fd);
151
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700152// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153static __inline__ int unix_close(int fd)
154{
155 return close(fd);
156}
157#undef close
158#define close ____xxx_close
159
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700160// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low50184062015-03-01 15:06:21 -0800161extern int unix_read(int fd, void* buf, size_t len);
162
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163#undef read
164#define read ___xxx_read
165
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700166// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167static __inline__ int unix_write(int fd, const void* buf, size_t len)
168{
169 return write(fd, buf, len);
170}
171#undef write
172#define write ___xxx_write
173
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700174// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175static __inline__ int adb_open_mode(const char* path, int options, int mode)
176{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200177 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178}
179
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700180// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700181extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182#define open ___xxx_unix_open
183
David Pursellc5b8ad82015-10-28 14:29:51 -0700184// Checks if |fd| corresponds to a console.
185// Standard Windows isatty() returns 1 for both console FDs and character
186// devices like NUL. unix_isatty() performs some extra checking to only match
187// console FDs.
188// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
189// will work but adb_open() FDs will not. Additionally the OS handle associated
190// with |fd| must have GENERIC_READ access (which console FDs have by default).
191// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
192// calling this function is unreliable and should not be used.
193int unix_isatty(int fd);
194#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
196/* normally provided by <cutils/misc.h> */
197extern void* load_file(const char* pathname, unsigned* psize);
198
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200199/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
201#define FDE_READ 0x0001
202#define FDE_WRITE 0x0002
203#define FDE_ERROR 0x0004
204#define FDE_DONT_CLOSE 0x0080
205
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206typedef void (*fd_func)(int fd, unsigned events, void *userdata);
207
208fdevent *fdevent_create(int fd, fd_func func, void *arg);
209void fdevent_destroy(fdevent *fde);
210void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
211void fdevent_remove(fdevent *item);
212void fdevent_set(fdevent *fde, unsigned events);
213void fdevent_add(fdevent *fde, unsigned events);
214void fdevent_del(fdevent *fde, unsigned events);
215void fdevent_loop();
216
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217static __inline__ void adb_sleep_ms( int mseconds )
218{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200219 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220}
221
Spencer Low5200c662015-07-30 23:07:55 -0700222int network_loopback_client(int port, int type, std::string* error);
223int network_loopback_server(int port, int type, std::string* error);
224int network_inaddr_any_server(int port, int type, std::string* error);
225int network_connect(const std::string& host, int port, int type, int timeout,
226 std::string* error);
227
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
229
230#undef accept
231#define accept ___xxx_accept
232
Spencer Lowf055c192015-01-25 14:40:16 -0800233extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
234
235#undef setsockopt
236#define setsockopt ___xxx_setsockopt
237
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
239{
240 int opt = bufsize;
Spencer Lowf055c192015-01-25 14:40:16 -0800241 return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
242}
243
244static __inline__ void disable_tcp_nagle( int fd )
245{
246 int on = 1;
247 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248}
249
250extern 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// Convert from UTF-8 to UTF-16, typically used to convert char strings into
342// wchar_t strings that can be passed to wchar_t-based OS and C Runtime APIs
343// on Windows.
344extern std::wstring widen(const std::string& utf8);
345extern std::wstring widen(const char* utf8);
346
347// Convert from UTF-16 to UTF-8, typically used to convert strings from OS and
348// C Runtime APIs that return wchar_t, to a format for our char-based data
349// structures.
350extern std::string narrow(const std::wstring& utf16);
351extern std::string narrow(const wchar_t* utf16);
352
353// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
354// passed to main().
355class NarrowArgs {
356public:
357 NarrowArgs(int argc, wchar_t** argv);
358 ~NarrowArgs();
359
360 inline char** data() {
361 return narrow_args;
362 }
363
364private:
365 char** narrow_args;
366};
367
Spencer Low5c398d22015-08-08 15:07:07 -0700368// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
369// so they can fit in an int. To convert back, we just need to sign-extend.
370// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
371// Note that this does not make a HANDLE value work with APIs like open(), nor
372// does this make a value from open() passable to APIs taking a HANDLE. This
373// just lets you take a HANDLE, pass it around as an int, and then use it again
374// as a HANDLE.
375inline int cast_handle_to_int(const HANDLE h) {
376 // truncate
377 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
378}
379
380inline HANDLE cast_int_to_handle(const int fd) {
381 // sign-extend
382 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
383}
384
Spencer Low2122c7a2015-08-26 18:46:09 -0700385// Deleter for unique_handle. Adapted from many sources, including:
386// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
387// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
388class handle_deleter {
389public:
390 typedef HANDLE pointer;
391
392 void operator()(HANDLE h);
393};
394
395// Like std::unique_ptr, but for Windows HANDLE objects that should be
396// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
397// but does not check if the handle != INVALID_HANDLE_VALUE.
398typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
399
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400#else /* !_WIN32 a.k.a. Unix */
401
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200402#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403#include <cutils/misc.h>
Spencer Low5200c662015-07-30 23:07:55 -0700404#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700405#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406#include <signal.h>
407#include <sys/wait.h>
408#include <sys/stat.h>
409#include <fcntl.h>
410
411#include <pthread.h>
412#include <unistd.h>
413#include <fcntl.h>
414#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700415#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416#include <netinet/in.h>
417#include <netinet/tcp.h>
418#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700419#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420
Alex Vallée47d67c92015-05-06 16:26:00 -0400421#include <string>
422
Elliott Hughes5c742702015-07-30 17:42:01 -0700423#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424#define OS_PATH_SEPARATOR '/'
425#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700426#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427
428typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700429
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
431#define adb_mutex_init pthread_mutex_init
432#define adb_mutex_lock pthread_mutex_lock
433#define adb_mutex_unlock pthread_mutex_unlock
434#define adb_mutex_destroy pthread_mutex_destroy
435
JP Abgrall408fa572011-03-16 15:57:42 -0700436#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437
438#define adb_cond_t pthread_cond_t
439#define adb_cond_init pthread_cond_init
440#define adb_cond_wait pthread_cond_wait
441#define adb_cond_broadcast pthread_cond_broadcast
442#define adb_cond_signal pthread_cond_signal
443#define adb_cond_destroy pthread_cond_destroy
444
JP Abgrall408fa572011-03-16 15:57:42 -0700445/* declare all mutexes */
446#define ADB_MUTEX(x) extern adb_mutex_t x;
447#include "mutex_list.h"
448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449static __inline__ void close_on_exec(int fd)
450{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200451 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452}
453
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700454// Open a file and return a file descriptor that may be used with unix_read(),
455// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
456//
457// On Unix, this is based on open(), so the file descriptor is a real OS file
458// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
459// file descriptor that can only be used with C Runtime APIs (which are wrapped
460// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
461// configurable CR/LF translation which defaults to text mode, but is settable
462// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463static __inline__ int unix_open(const char* path, int options,...)
464{
465 if ((options & O_CREAT) == 0)
466 {
Kenny Root73167412012-10-12 15:26:45 -0700467 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 }
469 else
470 {
471 int mode;
472 va_list args;
473 va_start( args, options );
474 mode = va_arg( args, int );
475 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700476 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 }
478}
479
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700480// Similar to the two-argument adb_open(), but takes a mode parameter for file
481// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
483{
Kenny Root73167412012-10-12 15:26:45 -0700484 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485}
486
487
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700488// Open a file and return a file descriptor that may be used with adb_read(),
489// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
490//
491// On Unix, this is based on open(), but the Windows implementation (in
492// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
493// and its CR/LF translation. The returned file descriptor should be used with
494// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495static __inline__ int adb_open( const char* pathname, int options )
496{
Kenny Root73167412012-10-12 15:26:45 -0700497 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 if (fd < 0)
499 return -1;
500 close_on_exec( fd );
501 return fd;
502}
503#undef open
504#define open ___xxx_open
505
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400506static __inline__ int adb_shutdown(int fd)
507{
508 return shutdown(fd, SHUT_RDWR);
509}
David Pursell1ed57f02015-10-06 15:30:03 -0700510static __inline__ int adb_shutdown(int fd, int direction)
511{
512 return shutdown(fd, direction);
513}
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400514#undef shutdown
515#define shutdown ____xxx_shutdown
516
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700517// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
518// not designed to take a file descriptor from unix_open(). See the comments
519// for adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520static __inline__ int adb_close(int fd)
521{
522 return close(fd);
523}
524#undef close
525#define close ____xxx_close
526
527
528static __inline__ int adb_read(int fd, void* buf, size_t len)
529{
Kenny Root73167412012-10-12 15:26:45 -0700530 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531}
532
533#undef read
534#define read ___xxx_read
535
536static __inline__ int adb_write(int fd, const void* buf, size_t len)
537{
Kenny Root73167412012-10-12 15:26:45 -0700538 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539}
540#undef write
541#define write ___xxx_write
542
543static __inline__ int adb_lseek(int fd, int pos, int where)
544{
545 return lseek(fd, pos, where);
546}
547#undef lseek
548#define lseek ___xxx_lseek
549
550static __inline__ int adb_unlink(const char* path)
551{
552 return unlink(path);
553}
554#undef unlink
555#define unlink ___xxx_unlink
556
557static __inline__ int adb_creat(const char* path, int mode)
558{
Kenny Root73167412012-10-12 15:26:45 -0700559 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200561 if ( fd < 0 )
562 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563
564 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200565 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566}
567#undef creat
568#define creat ___xxx_creat
569
David Pursellc5b8ad82015-10-28 14:29:51 -0700570static __inline__ int unix_isatty(int fd) {
571 return isatty(fd);
572}
573#define isatty ___xxx_isatty
574
Spencer Low5200c662015-07-30 23:07:55 -0700575// Helper for network_* functions.
576inline int _fd_set_error_str(int fd, std::string* error) {
577 if (fd == -1) {
578 *error = strerror(errno);
579 }
580 return fd;
581}
582
583inline int network_loopback_client(int port, int type, std::string* error) {
584 return _fd_set_error_str(socket_loopback_client(port, type), error);
585}
586
587inline int network_loopback_server(int port, int type, std::string* error) {
588 return _fd_set_error_str(socket_loopback_server(port, type), error);
589}
590
591inline int network_inaddr_any_server(int port, int type, std::string* error) {
592 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
593}
594
595inline int network_local_server(const char *name, int namespace_id, int type,
596 std::string* error) {
597 return _fd_set_error_str(socket_local_server(name, namespace_id, type),
598 error);
599}
600
601inline int network_connect(const std::string& host, int port, int type,
602 int timeout, std::string* error) {
603 int getaddrinfo_error = 0;
604 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
605 &getaddrinfo_error);
606 if (fd != -1) {
607 return fd;
608 }
609 if (getaddrinfo_error != 0) {
610 *error = gai_strerror(getaddrinfo_error);
611 } else {
612 *error = strerror(errno);
613 }
614 return -1;
615}
616
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
618{
Benoit Goby95ef8282011-02-01 18:57:41 -0800619 int fd;
620
Kenny Root73167412012-10-12 15:26:45 -0700621 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800622 if (fd >= 0)
623 close_on_exec(fd);
624
625 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626}
627
628#undef accept
629#define accept ___xxx_accept
630
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700631// Operate on a file descriptor returned from unix_open() or a well-known file
632// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
633//
634// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
635// adb_write(), adb_close() (which all map to Unix system calls), but the
636// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
637// into the C Runtime and its configurable CR/LF translation (which is settable
638// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639#define unix_read adb_read
640#define unix_write adb_write
641#define unix_close adb_close
642
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643typedef void* (*adb_thread_func_t)( void* arg );
644
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700645static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) {
646 pthread_attr_t attr;
647 pthread_attr_init(&attr);
648 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700650 pthread_t thread;
651 errno = pthread_create(&thread, &attr, start, arg);
652 return (errno == 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653}
654
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700655static __inline__ int adb_thread_setname(const std::string& name) {
656#ifdef __APPLE__
657 return pthread_setname_np(name.c_str());
658#else
659 const char *s = name.c_str();
660
661 // pthread_setname_np fails rather than truncating long strings.
662 const int max_task_comm_len = 16; // including the null terminator
663 if (name.length() > (max_task_comm_len - 1)) {
664 char buf[max_task_comm_len];
665 strncpy(buf, name.c_str(), sizeof(buf) - 1);
666 buf[sizeof(buf) - 1] = '\0';
667 s = buf;
668 }
669
670 return pthread_setname_np(pthread_self(), s) ;
671#endif
672}
673
674static __inline__ int adb_socket_setbufsize(int fd, int bufsize )
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675{
676 int opt = bufsize;
677 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
678}
679
680static __inline__ void disable_tcp_nagle(int fd)
681{
682 int on = 1;
683 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
684}
685
Spencer Lowf055c192015-01-25 14:40:16 -0800686static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
687{
688 return setsockopt( fd, level, optname, optval, optlen );
689}
690
691#undef setsockopt
692#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693
694static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
695{
696 return socketpair( d, type, protocol, sv );
697}
698
699static __inline__ int adb_socketpair( int sv[2] )
700{
701 int rc;
702
703 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
704 if (rc < 0)
705 return -1;
706
707 close_on_exec( sv[0] );
708 close_on_exec( sv[1] );
709 return 0;
710}
711
712#undef socketpair
713#define socketpair ___xxx_socketpair
714
715static __inline__ void adb_sleep_ms( int mseconds )
716{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200717 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718}
719
Alex Vallée47d67c92015-05-06 16:26:00 -0400720static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721{
Alex Vallée47d67c92015-05-06 16:26:00 -0400722 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723}
Alex Vallée47d67c92015-05-06 16:26:00 -0400724
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725#undef mkdir
726#define mkdir ___xxx_mkdir
727
728static __inline__ void adb_sysdeps_init(void)
729{
730}
731
Alex Vallée47d67c92015-05-06 16:26:00 -0400732static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733 return path[0] == '/';
734}
735
leozwangcbf02672014-08-15 09:51:27 -0700736static __inline__ unsigned long adb_thread_id()
737{
Spencer Low8d8126a2015-07-21 02:06:26 -0700738 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700739}
740
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741#endif /* !_WIN32 */
742
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743#endif /* _ADB_SYSDEPS_H */