blob: 6f3c44302091c7fbdd104cebd0d38b362341b4c5 [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 Lowcf4ff642015-05-11 01:08:48 -070074#include <string> // Prototypes for narrow() and widen() use std::(w)string.
Alex Vallée47d67c92015-05-06 16:26:00 -040075
Dan Albert630b9af2014-11-24 23:34:35 -080076#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
Elliott Hughes5c742702015-07-30 17:42:01 -070078#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079#define OS_PATH_SEPARATOR '\\'
80#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070081#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082
83typedef CRITICAL_SECTION adb_mutex_t;
84
85#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
86
87/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070088/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089#define ADB_MUTEX(x) extern adb_mutex_t x;
90#include "mutex_list.h"
91
92extern void adb_sysdeps_init(void);
93
94static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
95{
96 EnterCriticalSection( lock );
97}
98
99static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
100{
101 LeaveCriticalSection( lock );
102}
103
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104typedef void* (*adb_thread_func_t)(void* arg);
105
106typedef void (*win_thread_func_t)(void* arg);
107
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700108static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) {
Elliott Hughes2e4a2ee2015-05-05 14:34:41 -0700109 uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg);
110 return (tid != static_cast<uintptr_t>(-1L));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111}
112
leozwangcbf02672014-08-15 09:51:27 -0700113static __inline__ unsigned long adb_thread_id()
114{
115 return GetCurrentThreadId();
116}
117
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118static __inline__ void close_on_exec(int fd)
119{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200120 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121}
122
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123#define lstat stat /* no symlinks on Win32 */
124
125#define S_ISLNK(m) 0 /* no symlinks on Win32 */
126
Spencer Lowcf4ff642015-05-11 01:08:48 -0700127extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128#undef unlink
129#define unlink ___xxx_unlink
130
Spencer Lowcf4ff642015-05-11 01:08:48 -0700131extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132#undef mkdir
133#define mkdir ___xxx_mkdir
134
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700135// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136extern int adb_open(const char* path, int options);
137extern int adb_creat(const char* path, int mode);
138extern int adb_read(int fd, void* buf, int len);
139extern int adb_write(int fd, const void* buf, int len);
140extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400141extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142extern int adb_close(int fd);
143
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700144// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145static __inline__ int unix_close(int fd)
146{
147 return close(fd);
148}
149#undef close
150#define close ____xxx_close
151
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700152// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low50184062015-03-01 15:06:21 -0800153extern int unix_read(int fd, void* buf, size_t len);
154
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155#undef read
156#define read ___xxx_read
157
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700158// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159static __inline__ int unix_write(int fd, const void* buf, size_t len)
160{
161 return write(fd, buf, len);
162}
163#undef write
164#define write ___xxx_write
165
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700166// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167static __inline__ int adb_open_mode(const char* path, int options, int mode)
168{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200169 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170}
171
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700172// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700173extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174#define open ___xxx_unix_open
175
176
177/* normally provided by <cutils/misc.h> */
178extern void* load_file(const char* pathname, unsigned* psize);
179
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200180/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
182#define FDE_READ 0x0001
183#define FDE_WRITE 0x0002
184#define FDE_ERROR 0x0004
185#define FDE_DONT_CLOSE 0x0080
186
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187typedef void (*fd_func)(int fd, unsigned events, void *userdata);
188
189fdevent *fdevent_create(int fd, fd_func func, void *arg);
190void fdevent_destroy(fdevent *fde);
191void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
192void fdevent_remove(fdevent *item);
193void fdevent_set(fdevent *fde, unsigned events);
194void fdevent_add(fdevent *fde, unsigned events);
195void fdevent_del(fdevent *fde, unsigned events);
196void fdevent_loop();
197
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198static __inline__ void adb_sleep_ms( int mseconds )
199{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200200 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201}
202
Spencer Low5200c662015-07-30 23:07:55 -0700203int network_loopback_client(int port, int type, std::string* error);
204int network_loopback_server(int port, int type, std::string* error);
205int network_inaddr_any_server(int port, int type, std::string* error);
206int network_connect(const std::string& host, int port, int type, int timeout,
207 std::string* error);
208
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
210
211#undef accept
212#define accept ___xxx_accept
213
Spencer Lowf055c192015-01-25 14:40:16 -0800214extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
215
216#undef setsockopt
217#define setsockopt ___xxx_setsockopt
218
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
220{
221 int opt = bufsize;
Spencer Lowf055c192015-01-25 14:40:16 -0800222 return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
223}
224
225static __inline__ void disable_tcp_nagle( int fd )
226{
227 int on = 1;
228 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229}
230
231extern int adb_socketpair( int sv[2] );
232
Elliott Hughes5c742702015-07-30 17:42:01 -0700233static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
235}
236
Spencer Low5200c662015-07-30 23:07:55 -0700237// Like strerror(), but for Win32 error codes.
238std::string SystemErrorCodeToString(DWORD error_code);
239
Spencer Lowcf4ff642015-05-11 01:08:48 -0700240// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
241// struct stat s;
242// stat(filename, &s);
243// To turn into the following:
244// struct adb_stat s;
245// adb_stat(filename, &s);
246// To get this to work, we need to make 'struct adb_stat' the same as
247// 'struct stat'. Note that this definition of 'struct adb_stat' uses the
248// *current* macro definition of stat, so it may actually be inheriting from
249// struct _stat32i64 (or some other remapping).
250struct adb_stat : public stat {};
251
252static_assert(sizeof(struct adb_stat) == sizeof(struct stat),
253 "structures should be the same");
254
255extern int adb_stat(const char* f, struct adb_stat* s);
256
257// stat is already a macro, undefine it so we can redefine it.
258#undef stat
259#define stat adb_stat
260
261// UTF-8 versions of POSIX APIs.
262extern DIR* adb_opendir(const char* dirname);
263extern struct dirent* adb_readdir(DIR* dir);
264extern int adb_closedir(DIR* dir);
265
266extern int adb_utime(const char *, struct utimbuf *);
267extern int adb_chmod(const char *, int);
268
269extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
270 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
271extern int adb_fprintf(FILE *stream, const char *format, ...)
272 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
273extern int adb_printf(const char *format, ...)
274 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
275
276extern int adb_fputs(const char* buf, FILE* stream);
277extern int adb_fputc(int ch, FILE* stream);
278extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
279 FILE* stream);
280
281extern FILE* adb_fopen(const char* f, const char* m);
282
283extern char* adb_getenv(const char* name);
284
285extern char* adb_getcwd(char* buf, int size);
286
287// Remap calls to POSIX APIs to our UTF-8 versions.
288#define opendir adb_opendir
289#define readdir adb_readdir
290#define closedir adb_closedir
291#define rewinddir rewinddir_utf8_not_yet_implemented
292#define telldir telldir_utf8_not_yet_implemented
293#define seekdir seekdir_utf8_not_yet_implemented
294
295#define utime adb_utime
296#define chmod adb_chmod
297
298#define vfprintf adb_vfprintf
299#define fprintf adb_fprintf
300#define printf adb_printf
301#define fputs adb_fputs
302#define fputc adb_fputc
303#define fwrite adb_fwrite
304
305#define fopen adb_fopen
306
307#define getenv adb_getenv
308#define putenv putenv_utf8_not_yet_implemented
309#define setenv setenv_utf8_not_yet_implemented
310#define unsetenv unsetenv_utf8_not_yet_implemented
311
312#define getcwd adb_getcwd
313
314// Convert from UTF-8 to UTF-16, typically used to convert char strings into
315// wchar_t strings that can be passed to wchar_t-based OS and C Runtime APIs
316// on Windows.
317extern std::wstring widen(const std::string& utf8);
318extern std::wstring widen(const char* utf8);
319
320// Convert from UTF-16 to UTF-8, typically used to convert strings from OS and
321// C Runtime APIs that return wchar_t, to a format for our char-based data
322// structures.
323extern std::string narrow(const std::wstring& utf16);
324extern std::string narrow(const wchar_t* utf16);
325
326// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
327// passed to main().
328class NarrowArgs {
329public:
330 NarrowArgs(int argc, wchar_t** argv);
331 ~NarrowArgs();
332
333 inline char** data() {
334 return narrow_args;
335 }
336
337private:
338 char** narrow_args;
339};
340
Spencer Low5c398d22015-08-08 15:07:07 -0700341// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
342// so they can fit in an int. To convert back, we just need to sign-extend.
343// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
344// Note that this does not make a HANDLE value work with APIs like open(), nor
345// does this make a value from open() passable to APIs taking a HANDLE. This
346// just lets you take a HANDLE, pass it around as an int, and then use it again
347// as a HANDLE.
348inline int cast_handle_to_int(const HANDLE h) {
349 // truncate
350 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
351}
352
353inline HANDLE cast_int_to_handle(const int fd) {
354 // sign-extend
355 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
356}
357
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358#else /* !_WIN32 a.k.a. Unix */
359
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200360#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361#include <cutils/misc.h>
Spencer Low5200c662015-07-30 23:07:55 -0700362#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700363#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364#include <signal.h>
365#include <sys/wait.h>
366#include <sys/stat.h>
367#include <fcntl.h>
368
369#include <pthread.h>
370#include <unistd.h>
371#include <fcntl.h>
372#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700373#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374#include <netinet/in.h>
375#include <netinet/tcp.h>
376#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700377#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378
Alex Vallée47d67c92015-05-06 16:26:00 -0400379#include <string>
380
Elliott Hughes5c742702015-07-30 17:42:01 -0700381#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382#define OS_PATH_SEPARATOR '/'
383#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700384#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
386typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700387
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
389#define adb_mutex_init pthread_mutex_init
390#define adb_mutex_lock pthread_mutex_lock
391#define adb_mutex_unlock pthread_mutex_unlock
392#define adb_mutex_destroy pthread_mutex_destroy
393
JP Abgrall408fa572011-03-16 15:57:42 -0700394#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395
396#define adb_cond_t pthread_cond_t
397#define adb_cond_init pthread_cond_init
398#define adb_cond_wait pthread_cond_wait
399#define adb_cond_broadcast pthread_cond_broadcast
400#define adb_cond_signal pthread_cond_signal
401#define adb_cond_destroy pthread_cond_destroy
402
JP Abgrall408fa572011-03-16 15:57:42 -0700403/* declare all mutexes */
404#define ADB_MUTEX(x) extern adb_mutex_t x;
405#include "mutex_list.h"
406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407static __inline__ void close_on_exec(int fd)
408{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200409 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410}
411
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700412// Open a file and return a file descriptor that may be used with unix_read(),
413// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
414//
415// On Unix, this is based on open(), so the file descriptor is a real OS file
416// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
417// file descriptor that can only be used with C Runtime APIs (which are wrapped
418// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
419// configurable CR/LF translation which defaults to text mode, but is settable
420// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421static __inline__ int unix_open(const char* path, int options,...)
422{
423 if ((options & O_CREAT) == 0)
424 {
Kenny Root73167412012-10-12 15:26:45 -0700425 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 }
427 else
428 {
429 int mode;
430 va_list args;
431 va_start( args, options );
432 mode = va_arg( args, int );
433 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700434 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 }
436}
437
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700438// Similar to the two-argument adb_open(), but takes a mode parameter for file
439// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
441{
Kenny Root73167412012-10-12 15:26:45 -0700442 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443}
444
445
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700446// Open a file and return a file descriptor that may be used with adb_read(),
447// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
448//
449// On Unix, this is based on open(), but the Windows implementation (in
450// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
451// and its CR/LF translation. The returned file descriptor should be used with
452// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453static __inline__ int adb_open( const char* pathname, int options )
454{
Kenny Root73167412012-10-12 15:26:45 -0700455 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 if (fd < 0)
457 return -1;
458 close_on_exec( fd );
459 return fd;
460}
461#undef open
462#define open ___xxx_open
463
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400464static __inline__ int adb_shutdown(int fd)
465{
466 return shutdown(fd, SHUT_RDWR);
467}
468#undef shutdown
469#define shutdown ____xxx_shutdown
470
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700471// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
472// not designed to take a file descriptor from unix_open(). See the comments
473// for adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474static __inline__ int adb_close(int fd)
475{
476 return close(fd);
477}
478#undef close
479#define close ____xxx_close
480
481
482static __inline__ int adb_read(int fd, void* buf, size_t len)
483{
Kenny Root73167412012-10-12 15:26:45 -0700484 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485}
486
487#undef read
488#define read ___xxx_read
489
490static __inline__ int adb_write(int fd, const void* buf, size_t len)
491{
Kenny Root73167412012-10-12 15:26:45 -0700492 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493}
494#undef write
495#define write ___xxx_write
496
497static __inline__ int adb_lseek(int fd, int pos, int where)
498{
499 return lseek(fd, pos, where);
500}
501#undef lseek
502#define lseek ___xxx_lseek
503
504static __inline__ int adb_unlink(const char* path)
505{
506 return unlink(path);
507}
508#undef unlink
509#define unlink ___xxx_unlink
510
511static __inline__ int adb_creat(const char* path, int mode)
512{
Kenny Root73167412012-10-12 15:26:45 -0700513 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200515 if ( fd < 0 )
516 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517
518 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200519 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520}
521#undef creat
522#define creat ___xxx_creat
523
Spencer Low5200c662015-07-30 23:07:55 -0700524// Helper for network_* functions.
525inline int _fd_set_error_str(int fd, std::string* error) {
526 if (fd == -1) {
527 *error = strerror(errno);
528 }
529 return fd;
530}
531
532inline int network_loopback_client(int port, int type, std::string* error) {
533 return _fd_set_error_str(socket_loopback_client(port, type), error);
534}
535
536inline int network_loopback_server(int port, int type, std::string* error) {
537 return _fd_set_error_str(socket_loopback_server(port, type), error);
538}
539
540inline int network_inaddr_any_server(int port, int type, std::string* error) {
541 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
542}
543
544inline int network_local_server(const char *name, int namespace_id, int type,
545 std::string* error) {
546 return _fd_set_error_str(socket_local_server(name, namespace_id, type),
547 error);
548}
549
550inline int network_connect(const std::string& host, int port, int type,
551 int timeout, std::string* error) {
552 int getaddrinfo_error = 0;
553 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
554 &getaddrinfo_error);
555 if (fd != -1) {
556 return fd;
557 }
558 if (getaddrinfo_error != 0) {
559 *error = gai_strerror(getaddrinfo_error);
560 } else {
561 *error = strerror(errno);
562 }
563 return -1;
564}
565
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
567{
Benoit Goby95ef8282011-02-01 18:57:41 -0800568 int fd;
569
Kenny Root73167412012-10-12 15:26:45 -0700570 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800571 if (fd >= 0)
572 close_on_exec(fd);
573
574 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575}
576
577#undef accept
578#define accept ___xxx_accept
579
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700580// Operate on a file descriptor returned from unix_open() or a well-known file
581// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
582//
583// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
584// adb_write(), adb_close() (which all map to Unix system calls), but the
585// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
586// into the C Runtime and its configurable CR/LF translation (which is settable
587// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588#define unix_read adb_read
589#define unix_write adb_write
590#define unix_close adb_close
591
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592typedef void* (*adb_thread_func_t)( void* arg );
593
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700594static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) {
595 pthread_attr_t attr;
596 pthread_attr_init(&attr);
597 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700599 pthread_t thread;
600 errno = pthread_create(&thread, &attr, start, arg);
601 return (errno == 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602}
603
604static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
605{
606 int opt = bufsize;
607 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
608}
609
610static __inline__ void disable_tcp_nagle(int fd)
611{
612 int on = 1;
613 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
614}
615
Spencer Lowf055c192015-01-25 14:40:16 -0800616static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
617{
618 return setsockopt( fd, level, optname, optval, optlen );
619}
620
621#undef setsockopt
622#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623
624static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
625{
626 return socketpair( d, type, protocol, sv );
627}
628
629static __inline__ int adb_socketpair( int sv[2] )
630{
631 int rc;
632
633 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
634 if (rc < 0)
635 return -1;
636
637 close_on_exec( sv[0] );
638 close_on_exec( sv[1] );
639 return 0;
640}
641
642#undef socketpair
643#define socketpair ___xxx_socketpair
644
645static __inline__ void adb_sleep_ms( int mseconds )
646{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200647 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648}
649
Alex Vallée47d67c92015-05-06 16:26:00 -0400650static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651{
Alex Vallée47d67c92015-05-06 16:26:00 -0400652 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653}
Alex Vallée47d67c92015-05-06 16:26:00 -0400654
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655#undef mkdir
656#define mkdir ___xxx_mkdir
657
658static __inline__ void adb_sysdeps_init(void)
659{
660}
661
Alex Vallée47d67c92015-05-06 16:26:00 -0400662static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663 return path[0] == '/';
664}
665
leozwangcbf02672014-08-15 09:51:27 -0700666static __inline__ unsigned long adb_thread_id()
667{
Spencer Low8d8126a2015-07-21 02:06:26 -0700668 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700669}
670
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671#endif /* !_WIN32 */
672
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673#endif /* _ADB_SYSDEPS_H */