sysdeps.h revision c5b8ad88ce6e74216198f14c6e33d55cb1681370
1/*
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
27#include <errno.h>
28
29#include <string>
30
31/*
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
46// 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
59#ifdef _WIN32
60
61#include <ctype.h>
62#include <direct.h>
63#include <dirent.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <io.h>
67#include <process.h>
68#include <sys/stat.h>
69#include <utime.h>
70#include <winsock2.h>
71#include <windows.h>
72#include <ws2tcpip.h>
73
74#include <memory>   // unique_ptr
75#include <string>   // Prototypes for narrow() and widen() use std::(w)string.
76
77#include "fdevent.h"
78
79#define OS_PATH_SEPARATORS "\\/"
80#define OS_PATH_SEPARATOR '\\'
81#define OS_PATH_SEPARATOR_STR "\\"
82#define ENV_PATH_SEPARATOR_STR ";"
83
84typedef CRITICAL_SECTION          adb_mutex_t;
85
86#define  ADB_MUTEX_DEFINE(x)     adb_mutex_t   x
87
88/* declare all mutexes */
89/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
90#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
105typedef  void*  (*adb_thread_func_t)(void*  arg);
106
107typedef  void (*win_thread_func_t)(void*  arg);
108
109static __inline__ bool adb_thread_create(adb_thread_func_t func, void* arg) {
110    uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg);
111    return (tid != static_cast<uintptr_t>(-1L));
112}
113
114static __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
121static __inline__  unsigned long adb_thread_id()
122{
123    return GetCurrentThreadId();
124}
125
126static __inline__ void  close_on_exec(int  fd)
127{
128    /* nothing really */
129}
130
131#define  lstat    stat   /* no symlinks on Win32 */
132
133#define  S_ISLNK(m)   0   /* no symlinks on Win32 */
134
135extern int  adb_unlink(const char*  path);
136#undef  unlink
137#define unlink  ___xxx_unlink
138
139extern int adb_mkdir(const std::string& path, int mode);
140#undef   mkdir
141#define  mkdir  ___xxx_mkdir
142
143// See the comments for the !defined(_WIN32) versions of adb_*().
144extern 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);
149extern int  adb_shutdown(int  fd);
150extern int  adb_close(int  fd);
151
152// See the comments for the !defined(_WIN32) version of unix_close().
153static __inline__ int  unix_close(int fd)
154{
155    return close(fd);
156}
157#undef   close
158#define  close   ____xxx_close
159
160// See the comments for the !defined(_WIN32) version of unix_read().
161extern int  unix_read(int  fd, void*  buf, size_t  len);
162
163#undef   read
164#define  read  ___xxx_read
165
166// See the comments for the !defined(_WIN32) version of unix_write().
167static __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
174// See the comments for the !defined(_WIN32) version of adb_open_mode().
175static __inline__ int  adb_open_mode(const char* path, int options, int mode)
176{
177    return adb_open(path, options);
178}
179
180// See the comments for the !defined(_WIN32) version of unix_open().
181extern int unix_open(const char* path, int options, ...);
182#define  open    ___xxx_unix_open
183
184// 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
195
196/* normally provided by <cutils/misc.h> */
197extern void*  load_file(const char*  pathname, unsigned*  psize);
198
199/* normally provided by "fdevent.h" */
200
201#define FDE_READ              0x0001
202#define FDE_WRITE             0x0002
203#define FDE_ERROR             0x0004
204#define FDE_DONT_CLOSE        0x0080
205
206typedef 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
217static __inline__ void  adb_sleep_ms( int  mseconds )
218{
219    Sleep( mseconds );
220}
221
222int 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
228extern int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen);
229
230#undef   accept
231#define  accept  ___xxx_accept
232
233extern 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
238static __inline__  int  adb_socket_setbufsize( int   fd, int  bufsize )
239{
240    int opt = bufsize;
241    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));
248}
249
250extern int  adb_socketpair( int  sv[2] );
251
252static __inline__ int adb_is_absolute_host_path(const char* path) {
253    return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
254}
255
256// Like strerror(), but for Win32 error codes.
257std::string SystemErrorCodeToString(DWORD error_code);
258
259// 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
312#define seekdir seekdir_utf8_not_yet_implemented
313
314#define utime adb_utime
315#define chmod adb_chmod
316
317#define vfprintf adb_vfprintf
318#define fprintf adb_fprintf
319#define printf adb_printf
320#define fputs adb_fputs
321#define fputc adb_fputc
322#define fwrite adb_fwrite
323
324#define fopen adb_fopen
325
326#define getenv adb_getenv
327#define putenv putenv_utf8_not_yet_implemented
328#define setenv setenv_utf8_not_yet_implemented
329#define unsetenv unsetenv_utf8_not_yet_implemented
330
331#define getcwd adb_getcwd
332
333char* adb_strerror(int err);
334#define strerror adb_strerror
335
336// Convert from UTF-8 to UTF-16, typically used to convert char strings into
337// wchar_t strings that can be passed to wchar_t-based OS and C Runtime APIs
338// on Windows.
339extern std::wstring widen(const std::string& utf8);
340extern std::wstring widen(const char* utf8);
341
342// Convert from UTF-16 to UTF-8, typically used to convert strings from OS and
343// C Runtime APIs that return wchar_t, to a format for our char-based data
344// structures.
345extern std::string narrow(const std::wstring& utf16);
346extern std::string narrow(const wchar_t* utf16);
347
348// 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
363// 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
380// 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
395#else /* !_WIN32 a.k.a. Unix */
396
397#include "fdevent.h"
398#include <cutils/misc.h>
399#include <cutils/sockets.h>
400#include <cutils/threads.h>
401#include <signal.h>
402#include <sys/wait.h>
403#include <sys/stat.h>
404#include <fcntl.h>
405
406#include <pthread.h>
407#include <unistd.h>
408#include <fcntl.h>
409#include <stdarg.h>
410#include <netdb.h>
411#include <netinet/in.h>
412#include <netinet/tcp.h>
413#include <string.h>
414#include <unistd.h>
415
416#include <string>
417
418#define OS_PATH_SEPARATORS "/"
419#define OS_PATH_SEPARATOR '/'
420#define OS_PATH_SEPARATOR_STR "/"
421#define ENV_PATH_SEPARATOR_STR ":"
422
423typedef  pthread_mutex_t          adb_mutex_t;
424
425#define  ADB_MUTEX_INITIALIZER    PTHREAD_MUTEX_INITIALIZER
426#define  adb_mutex_init           pthread_mutex_init
427#define  adb_mutex_lock           pthread_mutex_lock
428#define  adb_mutex_unlock         pthread_mutex_unlock
429#define  adb_mutex_destroy        pthread_mutex_destroy
430
431#define  ADB_MUTEX_DEFINE(m)      adb_mutex_t   m = PTHREAD_MUTEX_INITIALIZER
432
433#define  adb_cond_t               pthread_cond_t
434#define  adb_cond_init            pthread_cond_init
435#define  adb_cond_wait            pthread_cond_wait
436#define  adb_cond_broadcast       pthread_cond_broadcast
437#define  adb_cond_signal          pthread_cond_signal
438#define  adb_cond_destroy         pthread_cond_destroy
439
440/* declare all mutexes */
441#define  ADB_MUTEX(x)   extern adb_mutex_t  x;
442#include "mutex_list.h"
443
444static __inline__ void  close_on_exec(int  fd)
445{
446    fcntl( fd, F_SETFD, FD_CLOEXEC );
447}
448
449// Open a file and return a file descriptor that may be used with unix_read(),
450// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
451//
452// On Unix, this is based on open(), so the file descriptor is a real OS file
453// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
454// file descriptor that can only be used with C Runtime APIs (which are wrapped
455// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
456// configurable CR/LF translation which defaults to text mode, but is settable
457// with _setmode().
458static __inline__ int  unix_open(const char*  path, int options,...)
459{
460    if ((options & O_CREAT) == 0)
461    {
462        return  TEMP_FAILURE_RETRY( open(path, options) );
463    }
464    else
465    {
466        int      mode;
467        va_list  args;
468        va_start( args, options );
469        mode = va_arg( args, int );
470        va_end( args );
471        return TEMP_FAILURE_RETRY( open( path, options, mode ) );
472    }
473}
474
475// Similar to the two-argument adb_open(), but takes a mode parameter for file
476// creation. See adb_open() for more info.
477static __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode )
478{
479    return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
480}
481
482
483// Open a file and return a file descriptor that may be used with adb_read(),
484// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
485//
486// On Unix, this is based on open(), but the Windows implementation (in
487// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
488// and its CR/LF translation. The returned file descriptor should be used with
489// adb_read(), adb_write(), adb_close(), etc.
490static __inline__ int  adb_open( const char*  pathname, int  options )
491{
492    int  fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
493    if (fd < 0)
494        return -1;
495    close_on_exec( fd );
496    return fd;
497}
498#undef   open
499#define  open    ___xxx_open
500
501static __inline__ int  adb_shutdown(int fd)
502{
503    return shutdown(fd, SHUT_RDWR);
504}
505static __inline__ int  adb_shutdown(int fd, int direction)
506{
507    return shutdown(fd, direction);
508}
509#undef   shutdown
510#define  shutdown   ____xxx_shutdown
511
512// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
513// not designed to take a file descriptor from unix_open(). See the comments
514// for adb_open() for more info.
515static __inline__ int  adb_close(int fd)
516{
517    return close(fd);
518}
519#undef   close
520#define  close   ____xxx_close
521
522
523static __inline__  int  adb_read(int  fd, void*  buf, size_t  len)
524{
525    return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
526}
527
528#undef   read
529#define  read  ___xxx_read
530
531static __inline__  int  adb_write(int  fd, const void*  buf, size_t  len)
532{
533    return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
534}
535#undef   write
536#define  write  ___xxx_write
537
538static __inline__ int   adb_lseek(int  fd, int  pos, int  where)
539{
540    return lseek(fd, pos, where);
541}
542#undef   lseek
543#define  lseek   ___xxx_lseek
544
545static __inline__  int    adb_unlink(const char*  path)
546{
547    return  unlink(path);
548}
549#undef  unlink
550#define unlink  ___xxx_unlink
551
552static __inline__  int  adb_creat(const char*  path, int  mode)
553{
554    int  fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
555
556    if ( fd < 0 )
557        return -1;
558
559    close_on_exec(fd);
560    return fd;
561}
562#undef   creat
563#define  creat  ___xxx_creat
564
565static __inline__ int unix_isatty(int fd) {
566    return isatty(fd);
567}
568#define  isatty  ___xxx_isatty
569
570// Helper for network_* functions.
571inline int _fd_set_error_str(int fd, std::string* error) {
572  if (fd == -1) {
573    *error = strerror(errno);
574  }
575  return fd;
576}
577
578inline int network_loopback_client(int port, int type, std::string* error) {
579  return _fd_set_error_str(socket_loopback_client(port, type), error);
580}
581
582inline int network_loopback_server(int port, int type, std::string* error) {
583  return _fd_set_error_str(socket_loopback_server(port, type), error);
584}
585
586inline int network_inaddr_any_server(int port, int type, std::string* error) {
587  return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
588}
589
590inline int network_local_server(const char *name, int namespace_id, int type,
591                                std::string* error) {
592  return _fd_set_error_str(socket_local_server(name, namespace_id, type),
593                           error);
594}
595
596inline int network_connect(const std::string& host, int port, int type,
597                           int timeout, std::string* error) {
598  int getaddrinfo_error = 0;
599  int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
600                                         &getaddrinfo_error);
601  if (fd != -1) {
602    return fd;
603  }
604  if (getaddrinfo_error != 0) {
605    *error = gai_strerror(getaddrinfo_error);
606  } else {
607    *error = strerror(errno);
608  }
609  return -1;
610}
611
612static __inline__ int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen)
613{
614    int fd;
615
616    fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
617    if (fd >= 0)
618        close_on_exec(fd);
619
620    return fd;
621}
622
623#undef   accept
624#define  accept  ___xxx_accept
625
626// Operate on a file descriptor returned from unix_open() or a well-known file
627// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
628//
629// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
630// adb_write(), adb_close() (which all map to Unix system calls), but the
631// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
632// into the C Runtime and its configurable CR/LF translation (which is settable
633// via _setmode()).
634#define  unix_read   adb_read
635#define  unix_write  adb_write
636#define  unix_close  adb_close
637
638typedef void*  (*adb_thread_func_t)( void*  arg );
639
640static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) {
641    pthread_attr_t attr;
642    pthread_attr_init(&attr);
643    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
644
645    pthread_t thread;
646    errno = pthread_create(&thread, &attr, start, arg);
647    return (errno == 0);
648}
649
650static __inline__ int adb_thread_setname(const std::string& name) {
651#ifdef __APPLE__
652    return pthread_setname_np(name.c_str());
653#else
654    const char *s = name.c_str();
655
656    // pthread_setname_np fails rather than truncating long strings.
657    const int max_task_comm_len = 16; // including the null terminator
658    if (name.length() > (max_task_comm_len - 1)) {
659        char buf[max_task_comm_len];
660        strncpy(buf, name.c_str(), sizeof(buf) - 1);
661        buf[sizeof(buf) - 1] = '\0';
662        s = buf;
663    }
664
665    return pthread_setname_np(pthread_self(), s) ;
666#endif
667}
668
669static __inline__  int  adb_socket_setbufsize(int fd, int  bufsize )
670{
671    int opt = bufsize;
672    return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
673}
674
675static __inline__ void  disable_tcp_nagle(int fd)
676{
677    int  on = 1;
678    setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
679}
680
681static __inline__ int  adb_setsockopt( int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen )
682{
683    return setsockopt( fd, level, optname, optval, optlen );
684}
685
686#undef   setsockopt
687#define  setsockopt  ___xxx_setsockopt
688
689static __inline__ int  unix_socketpair( int  d, int  type, int  protocol, int sv[2] )
690{
691    return socketpair( d, type, protocol, sv );
692}
693
694static __inline__ int  adb_socketpair( int  sv[2] )
695{
696    int  rc;
697
698    rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
699    if (rc < 0)
700        return -1;
701
702    close_on_exec( sv[0] );
703    close_on_exec( sv[1] );
704    return 0;
705}
706
707#undef   socketpair
708#define  socketpair   ___xxx_socketpair
709
710static __inline__ void  adb_sleep_ms( int  mseconds )
711{
712    usleep( mseconds*1000 );
713}
714
715static __inline__ int  adb_mkdir(const std::string& path, int mode)
716{
717    return mkdir(path.c_str(), mode);
718}
719
720#undef   mkdir
721#define  mkdir  ___xxx_mkdir
722
723static __inline__ void  adb_sysdeps_init(void)
724{
725}
726
727static __inline__ int adb_is_absolute_host_path(const char* path) {
728    return path[0] == '/';
729}
730
731static __inline__ unsigned long adb_thread_id()
732{
733    return (unsigned long)gettid();
734}
735
736#endif /* !_WIN32 */
737
738#endif /* _ADB_SYSDEPS_H */
739