blob: 3a3ffda93701b69e823537bdd06ada4d6097e7a1 [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
Dan Albertcc731cc2015-02-24 21:26:58 -080029/*
30 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
31 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
32 * not already defined, then define it here.
33 */
34#ifndef TEMP_FAILURE_RETRY
35/* Used to retry syscalls that can return EINTR. */
36#define TEMP_FAILURE_RETRY(exp) ({ \
37 typeof (exp) _rc; \
38 do { \
39 _rc = (exp); \
40 } while (_rc == -1 && errno == EINTR); \
41 _rc; })
42#endif
43
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#ifdef _WIN32
45
Dan Albert630b9af2014-11-24 23:34:35 -080046#include <ctype.h>
47#include <direct.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <io.h>
51#include <process.h>
52#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070054#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080056
Alex Vallée47d67c92015-05-06 16:26:00 -040057#include <string>
58
Dan Albert630b9af2014-11-24 23:34:35 -080059#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
61#define OS_PATH_SEPARATOR '\\'
62#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070063#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
65typedef CRITICAL_SECTION adb_mutex_t;
66
67#define ADB_MUTEX_DEFINE(x) adb_mutex_t x
68
69/* declare all mutexes */
JP Abgrall408fa572011-03-16 15:57:42 -070070/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071#define ADB_MUTEX(x) extern adb_mutex_t x;
72#include "mutex_list.h"
73
74extern void adb_sysdeps_init(void);
75
76static __inline__ void adb_mutex_lock( adb_mutex_t* lock )
77{
78 EnterCriticalSection( lock );
79}
80
81static __inline__ void adb_mutex_unlock( adb_mutex_t* lock )
82{
83 LeaveCriticalSection( lock );
84}
85
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086typedef void* (*adb_thread_func_t)(void* arg);
87
88typedef void (*win_thread_func_t)(void* arg);
89
Elliott Hughes9b0f3542015-05-05 13:41:21 -070090static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) {
Elliott Hughes2e4a2ee2015-05-05 14:34:41 -070091 uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg);
92 return (tid != static_cast<uintptr_t>(-1L));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093}
94
leozwangcbf02672014-08-15 09:51:27 -070095static __inline__ unsigned long adb_thread_id()
96{
97 return GetCurrentThreadId();
98}
99
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100static __inline__ void close_on_exec(int fd)
101{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200102 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103}
104
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105#define lstat stat /* no symlinks on Win32 */
106
107#define S_ISLNK(m) 0 /* no symlinks on Win32 */
108
109static __inline__ int adb_unlink(const char* path)
110{
111 int rc = unlink(path);
112
113 if (rc == -1 && errno == EACCES) {
114 /* unlink returns EACCES when the file is read-only, so we first */
115 /* try to make it writable, then unlink again... */
116 rc = chmod(path, _S_IREAD|_S_IWRITE );
117 if (rc == 0)
118 rc = unlink(path);
119 }
120 return rc;
121}
122#undef unlink
123#define unlink ___xxx_unlink
124
Alex Vallée47d67c92015-05-06 16:26:00 -0400125static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126{
JP Abgrall0e7c4272011-02-23 18:44:39 -0800127 return _mkdir(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128}
129#undef mkdir
130#define mkdir ___xxx_mkdir
131
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700132// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133extern int adb_open(const char* path, int options);
134extern int adb_creat(const char* path, int mode);
135extern int adb_read(int fd, void* buf, int len);
136extern int adb_write(int fd, const void* buf, int len);
137extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400138extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139extern int adb_close(int fd);
140
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700141// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142static __inline__ int unix_close(int fd)
143{
144 return close(fd);
145}
146#undef close
147#define close ____xxx_close
148
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700149// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low50184062015-03-01 15:06:21 -0800150extern int unix_read(int fd, void* buf, size_t len);
151
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152#undef read
153#define read ___xxx_read
154
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700155// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156static __inline__ int unix_write(int fd, const void* buf, size_t len)
157{
158 return write(fd, buf, len);
159}
160#undef write
161#define write ___xxx_write
162
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700163// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164static __inline__ int adb_open_mode(const char* path, int options, int mode)
165{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200166 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167}
168
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700169// See the comments for the !defined(_WIN32) version of unix_open().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170static __inline__ int unix_open(const char* path, int options,...)
171{
172 if ((options & O_CREAT) == 0)
173 {
174 return open(path, options);
175 }
176 else
177 {
178 int mode;
179 va_list args;
180 va_start( args, options );
181 mode = va_arg( args, int );
182 va_end( args );
183 return open(path, options, mode);
184 }
185}
186#define open ___xxx_unix_open
187
188
189/* normally provided by <cutils/misc.h> */
190extern void* load_file(const char* pathname, unsigned* psize);
191
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200192/* normally provided by "fdevent.h" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
194#define FDE_READ 0x0001
195#define FDE_WRITE 0x0002
196#define FDE_ERROR 0x0004
197#define FDE_DONT_CLOSE 0x0080
198
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199typedef void (*fd_func)(int fd, unsigned events, void *userdata);
200
201fdevent *fdevent_create(int fd, fd_func func, void *arg);
202void fdevent_destroy(fdevent *fde);
203void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
204void fdevent_remove(fdevent *item);
205void fdevent_set(fdevent *fde, unsigned events);
206void fdevent_add(fdevent *fde, unsigned events);
207void fdevent_del(fdevent *fde, unsigned events);
208void fdevent_loop();
209
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210static __inline__ void adb_sleep_ms( int mseconds )
211{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200212 Sleep( mseconds );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213}
214
215extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
216
217#undef accept
218#define accept ___xxx_accept
219
Spencer Lowf055c192015-01-25 14:40:16 -0800220extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
221
222#undef setsockopt
223#define setsockopt ___xxx_setsockopt
224
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
226{
227 int opt = bufsize;
Spencer Lowf055c192015-01-25 14:40:16 -0800228 return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt));
229}
230
231static __inline__ void disable_tcp_nagle( int fd )
232{
233 int on = 1;
234 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void*)&on, sizeof(on));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235}
236
237extern int adb_socketpair( int sv[2] );
238
Alex Vallée47d67c92015-05-06 16:26:00 -0400239static __inline__ size_t adb_dirstart(const std::string& path, size_t pos = 0) {
240 size_t p = path.find('/', pos);
241 size_t p2 = path.find('\\', pos);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242
Alex Vallée47d67c92015-05-06 16:26:00 -0400243 if ( p == std::string::npos )
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 p = p2;
Alex Vallée47d67c92015-05-06 16:26:00 -0400245 else if ( p2 != std::string::npos && p2 > p )
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 p = p2;
247
248 return p;
249}
250
Alex Vallée47d67c92015-05-06 16:26:00 -0400251static __inline__ size_t adb_dirstop(const std::string& path) {
252 size_t p = path.rfind('/');
253 size_t p2 = path.rfind('\\');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254
Alex Vallée47d67c92015-05-06 16:26:00 -0400255 if ( p == std::string::npos )
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 p = p2;
Alex Vallée47d67c92015-05-06 16:26:00 -0400257 else if ( p2 != std::string::npos && p2 > p )
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 p = p2;
259
260 return p;
261}
262
263static __inline__ int adb_is_absolute_host_path( const char* path )
264{
265 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
266}
267
268#else /* !_WIN32 a.k.a. Unix */
269
David 'Digit' Turner414ff7d2009-05-18 17:07:46 +0200270#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271#include <cutils/misc.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700272#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273#include <signal.h>
274#include <sys/wait.h>
275#include <sys/stat.h>
276#include <fcntl.h>
277
278#include <pthread.h>
279#include <unistd.h>
280#include <fcntl.h>
281#include <stdarg.h>
282#include <netinet/in.h>
283#include <netinet/tcp.h>
284#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700285#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286
Alex Vallée47d67c92015-05-06 16:26:00 -0400287#include <string>
288
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289#define OS_PATH_SEPARATOR '/'
290#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700291#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292
293typedef pthread_mutex_t adb_mutex_t;
JP Abgrall408fa572011-03-16 15:57:42 -0700294
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
296#define adb_mutex_init pthread_mutex_init
297#define adb_mutex_lock pthread_mutex_lock
298#define adb_mutex_unlock pthread_mutex_unlock
299#define adb_mutex_destroy pthread_mutex_destroy
300
JP Abgrall408fa572011-03-16 15:57:42 -0700301#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302
303#define adb_cond_t pthread_cond_t
304#define adb_cond_init pthread_cond_init
305#define adb_cond_wait pthread_cond_wait
306#define adb_cond_broadcast pthread_cond_broadcast
307#define adb_cond_signal pthread_cond_signal
308#define adb_cond_destroy pthread_cond_destroy
309
JP Abgrall408fa572011-03-16 15:57:42 -0700310/* declare all mutexes */
311#define ADB_MUTEX(x) extern adb_mutex_t x;
312#include "mutex_list.h"
313
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314static __inline__ void close_on_exec(int fd)
315{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200316 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317}
318
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700319// Open a file and return a file descriptor that may be used with unix_read(),
320// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
321//
322// On Unix, this is based on open(), so the file descriptor is a real OS file
323// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
324// file descriptor that can only be used with C Runtime APIs (which are wrapped
325// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
326// configurable CR/LF translation which defaults to text mode, but is settable
327// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328static __inline__ int unix_open(const char* path, int options,...)
329{
330 if ((options & O_CREAT) == 0)
331 {
Kenny Root73167412012-10-12 15:26:45 -0700332 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 }
334 else
335 {
336 int mode;
337 va_list args;
338 va_start( args, options );
339 mode = va_arg( args, int );
340 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700341 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 }
343}
344
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700345// Similar to the two-argument adb_open(), but takes a mode parameter for file
346// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
348{
Kenny Root73167412012-10-12 15:26:45 -0700349 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350}
351
352
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700353// Open a file and return a file descriptor that may be used with adb_read(),
354// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
355//
356// On Unix, this is based on open(), but the Windows implementation (in
357// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
358// and its CR/LF translation. The returned file descriptor should be used with
359// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360static __inline__ int adb_open( const char* pathname, int options )
361{
Kenny Root73167412012-10-12 15:26:45 -0700362 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 if (fd < 0)
364 return -1;
365 close_on_exec( fd );
366 return fd;
367}
368#undef open
369#define open ___xxx_open
370
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400371static __inline__ int adb_shutdown(int fd)
372{
373 return shutdown(fd, SHUT_RDWR);
374}
375#undef shutdown
376#define shutdown ____xxx_shutdown
377
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700378// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
379// not designed to take a file descriptor from unix_open(). See the comments
380// for adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381static __inline__ int adb_close(int fd)
382{
383 return close(fd);
384}
385#undef close
386#define close ____xxx_close
387
388
389static __inline__ int adb_read(int fd, void* buf, size_t len)
390{
Kenny Root73167412012-10-12 15:26:45 -0700391 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392}
393
394#undef read
395#define read ___xxx_read
396
397static __inline__ int adb_write(int fd, const void* buf, size_t len)
398{
Kenny Root73167412012-10-12 15:26:45 -0700399 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400}
401#undef write
402#define write ___xxx_write
403
404static __inline__ int adb_lseek(int fd, int pos, int where)
405{
406 return lseek(fd, pos, where);
407}
408#undef lseek
409#define lseek ___xxx_lseek
410
411static __inline__ int adb_unlink(const char* path)
412{
413 return unlink(path);
414}
415#undef unlink
416#define unlink ___xxx_unlink
417
418static __inline__ int adb_creat(const char* path, int mode)
419{
Kenny Root73167412012-10-12 15:26:45 -0700420 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200422 if ( fd < 0 )
423 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424
425 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200426 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427}
428#undef creat
429#define creat ___xxx_creat
430
431static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
432{
Benoit Goby95ef8282011-02-01 18:57:41 -0800433 int fd;
434
Kenny Root73167412012-10-12 15:26:45 -0700435 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800436 if (fd >= 0)
437 close_on_exec(fd);
438
439 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440}
441
442#undef accept
443#define accept ___xxx_accept
444
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700445// Operate on a file descriptor returned from unix_open() or a well-known file
446// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
447//
448// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
449// adb_write(), adb_close() (which all map to Unix system calls), but the
450// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
451// into the C Runtime and its configurable CR/LF translation (which is settable
452// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453#define unix_read adb_read
454#define unix_write adb_write
455#define unix_close adb_close
456
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457typedef void* (*adb_thread_func_t)( void* arg );
458
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700459static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) {
460 pthread_attr_t attr;
461 pthread_attr_init(&attr);
462 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700464 pthread_t thread;
465 errno = pthread_create(&thread, &attr, start, arg);
466 return (errno == 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467}
468
469static __inline__ int adb_socket_setbufsize( int fd, int bufsize )
470{
471 int opt = bufsize;
472 return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
473}
474
475static __inline__ void disable_tcp_nagle(int fd)
476{
477 int on = 1;
478 setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
479}
480
Spencer Lowf055c192015-01-25 14:40:16 -0800481static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
482{
483 return setsockopt( fd, level, optname, optval, optlen );
484}
485
486#undef setsockopt
487#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488
489static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
490{
491 return socketpair( d, type, protocol, sv );
492}
493
494static __inline__ int adb_socketpair( int sv[2] )
495{
496 int rc;
497
498 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
499 if (rc < 0)
500 return -1;
501
502 close_on_exec( sv[0] );
503 close_on_exec( sv[1] );
504 return 0;
505}
506
507#undef socketpair
508#define socketpair ___xxx_socketpair
509
510static __inline__ void adb_sleep_ms( int mseconds )
511{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200512 usleep( mseconds*1000 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513}
514
Alex Vallée47d67c92015-05-06 16:26:00 -0400515static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516{
Alex Vallée47d67c92015-05-06 16:26:00 -0400517 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518}
Alex Vallée47d67c92015-05-06 16:26:00 -0400519
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520#undef mkdir
521#define mkdir ___xxx_mkdir
522
523static __inline__ void adb_sysdeps_init(void)
524{
525}
526
Alex Vallée47d67c92015-05-06 16:26:00 -0400527static __inline__ size_t adb_dirstart(const std::string& path, size_t pos = 0) {
528 return path.find('/', pos);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529}
530
Alex Vallée47d67c92015-05-06 16:26:00 -0400531static __inline__ size_t adb_dirstop(const std::string& path) {
532 return path.rfind('/');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533}
534
Alex Vallée47d67c92015-05-06 16:26:00 -0400535static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 return path[0] == '/';
537}
538
leozwangcbf02672014-08-15 09:51:27 -0700539static __inline__ unsigned long adb_thread_id()
540{
Spencer Low8d8126a2015-07-21 02:06:26 -0700541 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700542}
543
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544#endif /* !_WIN32 */
545
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546#endif /* _ADB_SYSDEPS_H */