blob: 797d7bd60499adc95633d2a2d99313b6c25bb739 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* this file contains system-dependent definitions used by ADB
18 * they're related to threads, sockets and file descriptors
19 */
20#ifndef _ADB_SYSDEPS_H
21#define _ADB_SYSDEPS_H
22
23#ifdef __CYGWIN__
24# undef _WIN32
25#endif
26
Elliott Hughes381cfa92015-07-23 17:12:58 -070027#include <errno.h>
28
Spencer Low5200c662015-07-30 23:07:55 -070029#include <string>
Spencer Lowa30b79a2015-11-15 16:29:36 -080030#include <vector>
Spencer Low5200c662015-07-30 23:07:55 -070031
Spencer Lowd21dc822015-11-12 15:20:15 -080032// Include this before open/unlink are defined as macros below.
Elliott Hughes4f713192015-12-04 22:00:26 -080033#include <android-base/utf8.h>
Spencer Lowd21dc822015-11-12 15:20:15 -080034
Dan Albertcc731cc2015-02-24 21:26:58 -080035/*
36 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
37 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
38 * not already defined, then define it here.
39 */
40#ifndef TEMP_FAILURE_RETRY
41/* Used to retry syscalls that can return EINTR. */
42#define TEMP_FAILURE_RETRY(exp) ({ \
43 typeof (exp) _rc; \
44 do { \
45 _rc = (exp); \
46 } while (_rc == -1 && errno == EINTR); \
47 _rc; })
48#endif
49
Spencer Lowcf4ff642015-05-11 01:08:48 -070050// Some printf-like functions are implemented in terms of
51// android::base::StringAppendV, so they should use the same attribute for
52// compile-time format string checking. On Windows, if the mingw version of
53// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
54// and PRIu64 (and related) to be recognized by the compile-time checking.
55#define ADB_FORMAT_ARCHETYPE __printf__
56#ifdef __USE_MINGW_ANSI_STDIO
57#if __USE_MINGW_ANSI_STDIO
58#undef ADB_FORMAT_ARCHETYPE
59#define ADB_FORMAT_ARCHETYPE gnu_printf
60#endif
61#endif
62
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#ifdef _WIN32
64
Dan Albert630b9af2014-11-24 23:34:35 -080065#include <ctype.h>
66#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070067#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080068#include <errno.h>
69#include <fcntl.h>
70#include <io.h>
71#include <process.h>
72#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070073#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070075#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080077
Spencer Low2122c7a2015-08-26 18:46:09 -070078#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080079#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040080
Dan Albert630b9af2014-11-24 23:34:35 -080081#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
Elliott Hughes5c742702015-07-30 17:42:01 -070083#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084#define OS_PATH_SEPARATOR '\\'
85#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070086#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Josh Gao07db1192015-11-07 15:38:19 -080088static __inline__ bool adb_is_separator(char c) {
89 return c == '\\' || c == '/';
90}
91
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092typedef CRITICAL_SECTION adb_mutex_t;
93
94#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
95
96/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070097/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098#define ADB_MUTEX(x) extern adb_mutex_t x;
99#include "mutex_list.h"
100
101extern void adb_sysdeps_init(void);
102
103static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
104{
105 EnterCriticalSection( lock );
106}
107
108static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
109{
110 LeaveCriticalSection( lock );
111}
112
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113typedef void* (*adb_thread_func_t)(void* arg);
114
115typedef void (*win_thread_func_t)(void* arg);
116
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700117static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) {
Elliott Hughes2e4a2ee2015-05-05 14:34:41 -0700118 uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg);
119 return (tid != static_cast<uintptr_t>(-1L));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120}
121
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700122static __inline__ int adb_thread_setname(const std::string& name) {
123 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
124 // the thread name in Windows. Unfortunately, it only works during debugging, but
125 // our build process doesn't generate PDB files needed for debugging.
126 return 0;
127}
128
leozwangcbf02672014-08-15 09:51:27 -0700129static __inline__ unsigned long adb_thread_id()
130{
131 return GetCurrentThreadId();
132}
133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134static __inline__ void close_on_exec(int fd)
135{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200136 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137}
138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139#define lstat stat /* no symlinks on Win32 */
140
141#define S_ISLNK(m) 0 /* no symlinks on Win32 */
142
Spencer Lowcf4ff642015-05-11 01:08:48 -0700143extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144#undef unlink
145#define unlink ___xxx_unlink
146
Spencer Lowcf4ff642015-05-11 01:08:48 -0700147extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148#undef mkdir
149#define mkdir ___xxx_mkdir
150
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700151// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152extern int adb_open(const char* path, int options);
153extern int adb_creat(const char* path, int mode);
154extern int adb_read(int fd, void* buf, int len);
155extern int adb_write(int fd, const void* buf, int len);
156extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400157extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158extern int adb_close(int fd);
159
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700160// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161static __inline__ int unix_close(int fd)
162{
163 return close(fd);
164}
165#undef close
166#define close ____xxx_close
167
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700168// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low50184062015-03-01 15:06:21 -0800169extern int unix_read(int fd, void* buf, size_t len);
170
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171#undef read
172#define read ___xxx_read
173
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700174// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175static __inline__ int unix_write(int fd, const void* buf, size_t len)
176{
177 return write(fd, buf, len);
178}
179#undef write
180#define write ___xxx_write
181
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700182// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183static __inline__ int adb_open_mode(const char* path, int options, int mode)
184{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200185 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186}
187
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700188// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700189extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190#define open ___xxx_unix_open
191
David Pursellc5b8ad82015-10-28 14:29:51 -0700192// Checks if |fd| corresponds to a console.
193// Standard Windows isatty() returns 1 for both console FDs and character
194// devices like NUL. unix_isatty() performs some extra checking to only match
195// console FDs.
196// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
197// will work but adb_open() FDs will not. Additionally the OS handle associated
198// with |fd| must have GENERIC_READ access (which console FDs have by default).
199// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
200// calling this function is unreliable and should not be used.
201int unix_isatty(int fd);
202#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
204/* normally provided by <cutils/misc.h> */
205extern void* load_file(const char* pathname, unsigned* psize);
206
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200207/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208
209#define FDE_READ 0x0001
210#define FDE_WRITE 0x0002
211#define FDE_ERROR 0x0004
212#define FDE_DONT_CLOSE 0x0080
213
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214typedef void (*fd_func)(int fd, unsigned events, void *userdata);
215
216fdevent *fdevent_create(int fd, fd_func func, void *arg);
217void fdevent_destroy(fdevent *fde);
218void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
219void fdevent_remove(fdevent *item);
220void fdevent_set(fdevent *fde, unsigned events);
221void fdevent_add(fdevent *fde, unsigned events);
222void fdevent_del(fdevent *fde, unsigned events);
223void fdevent_loop();
224
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225static __inline__ void adb_sleep_ms( int mseconds )
226{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200227 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228}
229
Spencer Low5200c662015-07-30 23:07:55 -0700230int network_loopback_client(int port, int type, std::string* error);
231int network_loopback_server(int port, int type, std::string* error);
232int network_inaddr_any_server(int port, int type, std::string* error);
233int network_connect(const std::string& host, int port, int type, int timeout,
234 std::string* error);
235
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
237
238#undef accept
239#define accept ___xxx_accept
240
Spencer Lowf055c192015-01-25 14:40:16 -0800241extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
242
243#undef setsockopt
244#define setsockopt ___xxx_setsockopt
245
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246extern int adb_socketpair( int sv[2] );
247
Elliott Hughes5c742702015-07-30 17:42:01 -0700248static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
250}
251
Spencer Low5200c662015-07-30 23:07:55 -0700252// Like strerror(), but for Win32 error codes.
253std::string SystemErrorCodeToString(DWORD error_code);
254
Spencer Lowcf4ff642015-05-11 01:08:48 -0700255// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
256// struct stat s;
257// stat(filename, &s);
258// To turn into the following:
259// struct adb_stat s;
260// adb_stat(filename, &s);
261// To get this to work, we need to make 'struct adb_stat' the same as
262// 'struct stat'. Note that this definition of 'struct adb_stat' uses the
263// *current* macro definition of stat, so it may actually be inheriting from
264// struct _stat32i64 (or some other remapping).
265struct adb_stat : public stat {};
266
267static_assert(sizeof(struct adb_stat) == sizeof(struct stat),
268 "structures should be the same");
269
270extern int adb_stat(const char* f, struct adb_stat* s);
271
272// stat is already a macro, undefine it so we can redefine it.
273#undef stat
274#define stat adb_stat
275
276// UTF-8 versions of POSIX APIs.
277extern DIR* adb_opendir(const char* dirname);
278extern struct dirent* adb_readdir(DIR* dir);
279extern int adb_closedir(DIR* dir);
280
281extern int adb_utime(const char *, struct utimbuf *);
282extern int adb_chmod(const char *, int);
283
284extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
285 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
Spencer Lowa30b79a2015-11-15 16:29:36 -0800286extern int adb_vprintf(const char *format, va_list ap)
287 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700288extern int adb_fprintf(FILE *stream, const char *format, ...)
289 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
290extern int adb_printf(const char *format, ...)
291 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
292
293extern int adb_fputs(const char* buf, FILE* stream);
294extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800295extern int adb_putchar(int ch);
296extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700297extern 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
Spencer Lowa30b79a2015-11-15 16:29:36 -0800323#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700324#define fprintf adb_fprintf
325#define printf adb_printf
326#define fputs adb_fputs
327#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800328// putc may be a macro, so if so, undefine it, so that we can redefine it.
329#undef putc
330#define putc(c, s) adb_fputc(c, s)
331#define putchar adb_putchar
332#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700333#define fwrite adb_fwrite
334
335#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800336#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700337
338#define getenv adb_getenv
339#define putenv putenv_utf8_not_yet_implemented
340#define setenv setenv_utf8_not_yet_implemented
341#define unsetenv unsetenv_utf8_not_yet_implemented
342
343#define getcwd adb_getcwd
344
Spencer Low0a796002015-10-18 16:45:09 -0700345char* adb_strerror(int err);
346#define strerror adb_strerror
347
Spencer Lowcf4ff642015-05-11 01:08:48 -0700348// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
349// passed to main().
350class NarrowArgs {
351public:
352 NarrowArgs(int argc, wchar_t** argv);
353 ~NarrowArgs();
354
355 inline char** data() {
356 return narrow_args;
357 }
358
359private:
360 char** narrow_args;
361};
362
Spencer Low5c398d22015-08-08 15:07:07 -0700363// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
364// so they can fit in an int. To convert back, we just need to sign-extend.
365// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
366// Note that this does not make a HANDLE value work with APIs like open(), nor
367// does this make a value from open() passable to APIs taking a HANDLE. This
368// just lets you take a HANDLE, pass it around as an int, and then use it again
369// as a HANDLE.
370inline int cast_handle_to_int(const HANDLE h) {
371 // truncate
372 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
373}
374
375inline HANDLE cast_int_to_handle(const int fd) {
376 // sign-extend
377 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
378}
379
Spencer Low2122c7a2015-08-26 18:46:09 -0700380// Deleter for unique_handle. Adapted from many sources, including:
381// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
382// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
383class handle_deleter {
384public:
385 typedef HANDLE pointer;
386
387 void operator()(HANDLE h);
388};
389
390// Like std::unique_ptr, but for Windows HANDLE objects that should be
391// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
392// but does not check if the handle != INVALID_HANDLE_VALUE.
393typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
394
Spencer Lowa30b79a2015-11-15 16:29:36 -0800395namespace internal {
396
397size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
398
399}
400
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401#else /* !_WIN32 a.k.a. Unix */
402
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200403#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404#include <cutils/misc.h>
Spencer Low5200c662015-07-30 23:07:55 -0700405#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700406#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407#include <signal.h>
408#include <sys/wait.h>
409#include <sys/stat.h>
410#include <fcntl.h>
411
412#include <pthread.h>
413#include <unistd.h>
414#include <fcntl.h>
415#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700416#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417#include <netinet/in.h>
418#include <netinet/tcp.h>
419#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700420#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421
Alex Vallée47d67c92015-05-06 16:26:00 -0400422#include <string>
423
Elliott Hughes5c742702015-07-30 17:42:01 -0700424#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425#define OS_PATH_SEPARATOR '/'
426#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700427#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428
Josh Gao07db1192015-11-07 15:38:19 -0800429static __inline__ bool adb_is_separator(char c) {
430 return c == '/';
431}
432
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
436#define adb_mutex_init pthread_mutex_init
437#define adb_mutex_lock pthread_mutex_lock
438#define adb_mutex_unlock pthread_mutex_unlock
439#define adb_mutex_destroy pthread_mutex_destroy
440
JP Abgrall408fa572011-03-16 15:57:42 -0700441#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442
443#define adb_cond_t pthread_cond_t
444#define adb_cond_init pthread_cond_init
445#define adb_cond_wait pthread_cond_wait
446#define adb_cond_broadcast pthread_cond_broadcast
447#define adb_cond_signal pthread_cond_signal
448#define adb_cond_destroy pthread_cond_destroy
449
JP Abgrall408fa572011-03-16 15:57:42 -0700450/* declare all mutexes */
451#define ADB_MUTEX(x) extern adb_mutex_t x;
452#include "mutex_list.h"
453
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454static __inline__ void close_on_exec(int fd)
455{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200456 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457}
458
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700459// Open a file and return a file descriptor that may be used with unix_read(),
460// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
461//
462// On Unix, this is based on open(), so the file descriptor is a real OS file
463// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
464// file descriptor that can only be used with C Runtime APIs (which are wrapped
465// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
466// configurable CR/LF translation which defaults to text mode, but is settable
467// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468static __inline__ int unix_open(const char* path, int options,...)
469{
470 if ((options & O_CREAT) == 0)
471 {
Kenny Root73167412012-10-12 15:26:45 -0700472 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 }
474 else
475 {
476 int mode;
477 va_list args;
478 va_start( args, options );
479 mode = va_arg( args, int );
480 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700481 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 }
483}
484
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700485// Similar to the two-argument adb_open(), but takes a mode parameter for file
486// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
488{
Kenny Root73167412012-10-12 15:26:45 -0700489 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490}
491
492
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700493// Open a file and return a file descriptor that may be used with adb_read(),
494// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
495//
496// On Unix, this is based on open(), but the Windows implementation (in
497// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
498// and its CR/LF translation. The returned file descriptor should be used with
499// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500static __inline__ int adb_open( const char* pathname, int options )
501{
Kenny Root73167412012-10-12 15:26:45 -0700502 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 if (fd < 0)
504 return -1;
505 close_on_exec( fd );
506 return fd;
507}
508#undef open
509#define open ___xxx_open
510
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400511static __inline__ int adb_shutdown(int fd)
512{
513 return shutdown(fd, SHUT_RDWR);
514}
David Pursell1ed57f02015-10-06 15:30:03 -0700515static __inline__ int adb_shutdown(int fd, int direction)
516{
517 return shutdown(fd, direction);
518}
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400519#undef shutdown
520#define shutdown ____xxx_shutdown
521
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700522// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
523// not designed to take a file descriptor from unix_open(). See the comments
524// for adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525static __inline__ int adb_close(int fd)
526{
527 return close(fd);
528}
529#undef close
530#define close ____xxx_close
531
532
533static __inline__ int adb_read(int fd, void* buf, size_t len)
534{
Kenny Root73167412012-10-12 15:26:45 -0700535 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536}
537
538#undef read
539#define read ___xxx_read
540
541static __inline__ int adb_write(int fd, const void* buf, size_t len)
542{
Kenny Root73167412012-10-12 15:26:45 -0700543 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544}
545#undef write
546#define write ___xxx_write
547
548static __inline__ int adb_lseek(int fd, int pos, int where)
549{
550 return lseek(fd, pos, where);
551}
552#undef lseek
553#define lseek ___xxx_lseek
554
555static __inline__ int adb_unlink(const char* path)
556{
557 return unlink(path);
558}
559#undef unlink
560#define unlink ___xxx_unlink
561
562static __inline__ int adb_creat(const char* path, int mode)
563{
Kenny Root73167412012-10-12 15:26:45 -0700564 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200566 if ( fd < 0 )
567 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568
569 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200570 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571}
572#undef creat
573#define creat ___xxx_creat
574
David Pursellc5b8ad82015-10-28 14:29:51 -0700575static __inline__ int unix_isatty(int fd) {
576 return isatty(fd);
577}
578#define isatty ___xxx_isatty
579
Spencer Low5200c662015-07-30 23:07:55 -0700580// Helper for network_* functions.
581inline int _fd_set_error_str(int fd, std::string* error) {
582 if (fd == -1) {
583 *error = strerror(errno);
584 }
585 return fd;
586}
587
588inline int network_loopback_client(int port, int type, std::string* error) {
589 return _fd_set_error_str(socket_loopback_client(port, type), error);
590}
591
592inline int network_loopback_server(int port, int type, std::string* error) {
593 return _fd_set_error_str(socket_loopback_server(port, type), error);
594}
595
596inline int network_inaddr_any_server(int port, int type, std::string* error) {
597 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
598}
599
600inline int network_local_server(const char *name, int namespace_id, int type,
601 std::string* error) {
602 return _fd_set_error_str(socket_local_server(name, namespace_id, type),
603 error);
604}
605
606inline int network_connect(const std::string& host, int port, int type,
607 int timeout, std::string* error) {
608 int getaddrinfo_error = 0;
609 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
610 &getaddrinfo_error);
611 if (fd != -1) {
612 return fd;
613 }
614 if (getaddrinfo_error != 0) {
615 *error = gai_strerror(getaddrinfo_error);
616 } else {
617 *error = strerror(errno);
618 }
619 return -1;
620}
621
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
623{
Benoit Goby95ef8282011-02-01 18:57:41 -0800624 int fd;
625
Kenny Root73167412012-10-12 15:26:45 -0700626 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800627 if (fd >= 0)
628 close_on_exec(fd);
629
630 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631}
632
633#undef accept
634#define accept ___xxx_accept
635
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700636// Operate on a file descriptor returned from unix_open() or a well-known file
637// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
638//
639// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
640// adb_write(), adb_close() (which all map to Unix system calls), but the
641// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
642// into the C Runtime and its configurable CR/LF translation (which is settable
643// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644#define unix_read adb_read
645#define unix_write adb_write
646#define unix_close adb_close
647
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648typedef void* (*adb_thread_func_t)( void* arg );
649
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700650static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) {
651 pthread_attr_t attr;
652 pthread_attr_init(&attr);
653 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700655 pthread_t thread;
656 errno = pthread_create(&thread, &attr, start, arg);
657 return (errno == 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658}
659
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700660static __inline__ int adb_thread_setname(const std::string& name) {
661#ifdef __APPLE__
662 return pthread_setname_np(name.c_str());
663#else
664 const char *s = name.c_str();
665
666 // pthread_setname_np fails rather than truncating long strings.
667 const int max_task_comm_len = 16; // including the null terminator
668 if (name.length() > (max_task_comm_len - 1)) {
669 char buf[max_task_comm_len];
670 strncpy(buf, name.c_str(), sizeof(buf) - 1);
671 buf[sizeof(buf) - 1] = '\0';
672 s = buf;
673 }
674
675 return pthread_setname_np(pthread_self(), s) ;
676#endif
677}
678
Spencer Lowf055c192015-01-25 14:40:16 -0800679static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
680{
681 return setsockopt( fd, level, optname, optval, optlen );
682}
683
684#undef setsockopt
685#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686
687static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
688{
689 return socketpair( d, type, protocol, sv );
690}
691
692static __inline__ int adb_socketpair( int sv[2] )
693{
694 int rc;
695
696 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
697 if (rc < 0)
698 return -1;
699
700 close_on_exec( sv[0] );
701 close_on_exec( sv[1] );
702 return 0;
703}
704
705#undef socketpair
706#define socketpair ___xxx_socketpair
707
708static __inline__ void adb_sleep_ms( int mseconds )
709{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200710 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711}
712
Alex Vallée47d67c92015-05-06 16:26:00 -0400713static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714{
Alex Vallée47d67c92015-05-06 16:26:00 -0400715 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716}
Alex Vallée47d67c92015-05-06 16:26:00 -0400717
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718#undef mkdir
719#define mkdir ___xxx_mkdir
720
721static __inline__ void adb_sysdeps_init(void)
722{
723}
724
Alex Vallée47d67c92015-05-06 16:26:00 -0400725static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 return path[0] == '/';
727}
728
leozwangcbf02672014-08-15 09:51:27 -0700729static __inline__ unsigned long adb_thread_id()
730{
Spencer Low8d8126a2015-07-21 02:06:26 -0700731 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700732}
733
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734#endif /* !_WIN32 */
735
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800736static inline void disable_tcp_nagle(int fd) {
737 int off = 1;
738 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
739}
740
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741#endif /* _ADB_SYSDEPS_H */