19270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/*
29270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * Copyright (C) 2007 The Android Open Source Project
39270a20a801403c9f60d6a701b39eae70d380403Doug Zongker *
49270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * Licensed under the Apache License, Version 2.0 (the "License");
59270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * you may not use this file except in compliance with the License.
69270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * You may obtain a copy of the License at
79270a20a801403c9f60d6a701b39eae70d380403Doug Zongker *
89270a20a801403c9f60d6a701b39eae70d380403Doug Zongker *      http://www.apache.org/licenses/LICENSE-2.0
99270a20a801403c9f60d6a701b39eae70d380403Doug Zongker *
109270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * Unless required by applicable law or agreed to in writing, software
119270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * distributed under the License is distributed on an "AS IS" BASIS,
129270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * See the License for the specific language governing permissions and
149270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * limitations under the License.
159270a20a801403c9f60d6a701b39eae70d380403Doug Zongker */
169270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
179270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/* this file contains system-dependent definitions used by ADB
189270a20a801403c9f60d6a701b39eae70d380403Doug Zongker * they're related to threads, sockets and file descriptors
199270a20a801403c9f60d6a701b39eae70d380403Doug Zongker */
209270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#ifndef _ADB_SYSDEPS_H
219270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define _ADB_SYSDEPS_H
229270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
239270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#ifdef __CYGWIN__
249270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#  undef _WIN32
259270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#endif
269270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
279270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#ifdef _WIN32
289270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
299270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <windows.h>
309270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <winsock2.h>
319270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <ws2tcpip.h>
329270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <process.h>
339270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <fcntl.h>
349270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <io.h>
359270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <sys/stat.h>
369270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <errno.h>
379270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <ctype.h>
389270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
399270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define OS_PATH_SEPARATOR '\\'
409270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define OS_PATH_SEPARATOR_STR "\\"
419270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
429270a20a801403c9f60d6a701b39eae70d380403Doug Zongkertypedef CRITICAL_SECTION          adb_mutex_t;
439270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
449270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  ADB_MUTEX_DEFINE(x)     adb_mutex_t   x
459270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
469270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/* declare all mutexes */
479270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
489270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  ADB_MUTEX(x)   extern adb_mutex_t  x;
499270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include "mutex_list.h"
509270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
519270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern void  adb_sysdeps_init(void);
529270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
539270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ void adb_mutex_lock( adb_mutex_t*  lock )
549270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
559270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    EnterCriticalSection( lock );
569270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
579270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
589270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ void  adb_mutex_unlock( adb_mutex_t*  lock )
599270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
609270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    LeaveCriticalSection( lock );
619270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
629270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
639270a20a801403c9f60d6a701b39eae70d380403Doug Zongkertypedef struct { unsigned  tid; }  adb_thread_t;
649270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
659270a20a801403c9f60d6a701b39eae70d380403Doug Zongkertypedef  void*  (*adb_thread_func_t)(void*  arg);
669270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
679270a20a801403c9f60d6a701b39eae70d380403Doug Zongkertypedef  void (*win_thread_func_t)(void*  arg);
689270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
699270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  adb_thread_create( adb_thread_t  *thread, adb_thread_func_t  func, void*  arg)
709270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
719270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
729270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    if (thread->tid == (unsigned)-1L) {
739270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        return -1;
749270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    }
759270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return 0;
769270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
779270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
789270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ void  close_on_exec(int  fd)
799270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
809270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    /* nothing really */
819270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
829270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
839270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern void  disable_tcp_nagle(int  fd);
849270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
859270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  lstat    stat   /* no symlinks on Win32 */
869270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
879270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  S_ISLNK(m)   0   /* no symlinks on Win32 */
889270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
899270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__  int    adb_unlink(const char*  path)
909270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
919270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    int  rc = unlink(path);
929270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
939270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    if (rc == -1 && errno == EACCES) {
949270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        /* unlink returns EACCES when the file is read-only, so we first */
959270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        /* try to make it writable, then unlink again...                  */
969270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        rc = chmod(path, _S_IREAD|_S_IWRITE );
979270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        if (rc == 0)
989270a20a801403c9f60d6a701b39eae70d380403Doug Zongker            rc = unlink(path);
999270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    }
1009270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return rc;
1019270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
1029270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef  unlink
1039270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define unlink  ___xxx_unlink
1049270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1059270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  adb_mkdir(const char*  path, int mode)
1069270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
1079270a20a801403c9f60d6a701b39eae70d380403Doug Zongker	return _mkdir(path);
1089270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
1099270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef   mkdir
1109270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  mkdir  ___xxx_mkdir
1119270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1129270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_open(const char*  path, int  options);
1139270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_creat(const char*  path, int  mode);
1149270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_read(int  fd, void* buf, int len);
1159270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_write(int  fd, const void*  buf, int  len);
1169270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_lseek(int  fd, int  pos, int  where);
1179270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_shutdown(int  fd);
1189270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_close(int  fd);
1199270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1209270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  unix_close(int fd)
1219270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
1229270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return close(fd);
1239270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
1249270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef   close
1259270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  close   ____xxx_close
1269270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1279270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__  int  unix_read(int  fd, void*  buf, size_t  len)
1289270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
1299270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return read(fd, buf, len);
1309270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
1319270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef   read
1329270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  read  ___xxx_read
1339270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1349270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__  int  unix_write(int  fd, const void*  buf, size_t  len)
1359270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
1369270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return write(fd, buf, len);
1379270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
1389270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef   write
1399270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  write  ___xxx_write
1409270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1419270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  adb_open_mode(const char* path, int options, int mode)
1429270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
1439270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return adb_open(path, options);
1449270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
1459270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1469270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  unix_open(const char*  path, int options,...)
1479270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
1489270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    if ((options & O_CREAT) == 0)
1499270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    {
1509270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        return  open(path, options);
1519270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    }
1529270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    else
1539270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    {
1549270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        int      mode;
1559270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        va_list  args;
1569270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        va_start( args, options );
1579270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        mode = va_arg( args, int );
1589270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        va_end( args );
1599270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        return open(path, options, mode);
1609270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    }
1619270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
1629270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  open    ___xxx_unix_open
1639270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1649270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1659270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/* normally provided by <cutils/misc.h> */
1669270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern void*  load_file(const char*  pathname, unsigned*  psize);
1679270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1689270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/* normally provided by <cutils/sockets.h> */
1699270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int socket_loopback_client(int port, int type);
1709270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int socket_network_client(const char *host, int port, int type);
1719270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int socket_loopback_server(int port, int type);
1729270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int socket_inaddr_any_server(int port, int type);
1739270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1749270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/* normally provided by "fdevent.h" */
1759270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1769270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define FDE_READ              0x0001
1779270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define FDE_WRITE             0x0002
1789270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define FDE_ERROR             0x0004
1799270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define FDE_DONT_CLOSE        0x0080
1809270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1819270a20a801403c9f60d6a701b39eae70d380403Doug Zongkertypedef struct fdevent fdevent;
1829270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1839270a20a801403c9f60d6a701b39eae70d380403Doug Zongkertypedef void (*fd_func)(int fd, unsigned events, void *userdata);
1849270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1859270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerfdevent *fdevent_create(int fd, fd_func func, void *arg);
1869270a20a801403c9f60d6a701b39eae70d380403Doug Zongkervoid     fdevent_destroy(fdevent *fde);
1879270a20a801403c9f60d6a701b39eae70d380403Doug Zongkervoid     fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
1889270a20a801403c9f60d6a701b39eae70d380403Doug Zongkervoid     fdevent_remove(fdevent *item);
1899270a20a801403c9f60d6a701b39eae70d380403Doug Zongkervoid     fdevent_set(fdevent *fde, unsigned events);
1909270a20a801403c9f60d6a701b39eae70d380403Doug Zongkervoid     fdevent_add(fdevent *fde, unsigned events);
1919270a20a801403c9f60d6a701b39eae70d380403Doug Zongkervoid     fdevent_del(fdevent *fde, unsigned events);
1929270a20a801403c9f60d6a701b39eae70d380403Doug Zongkervoid     fdevent_loop();
1939270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1949270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstruct fdevent {
1959270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    fdevent *next;
1969270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    fdevent *prev;
1979270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
1989270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    int fd;
1999270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    int force_eof;
2009270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2019270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    unsigned short state;
2029270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    unsigned short events;
2039270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2049270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    fd_func func;
2059270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    void *arg;
2069270a20a801403c9f60d6a701b39eae70d380403Doug Zongker};
2079270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2089270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ void  adb_sleep_ms( int  mseconds )
2099270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
2109270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    Sleep( mseconds );
2119270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
2129270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2139270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen);
2149270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2159270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef   accept
2169270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  accept  ___xxx_accept
2179270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2189270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__  int  adb_socket_setbufsize( int   fd, int  bufsize )
2199270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
2209270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    int opt = bufsize;
2219270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
2229270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
2239270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2249270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerextern int  adb_socketpair( int  sv[2] );
2259270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2269270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__  char*  adb_dirstart( const char*  path )
2279270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
2289270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    char*  p  = strchr(path, '/');
2299270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    char*  p2 = strchr(path, '\\');
2309270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2319270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    if ( !p )
2329270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        p = p2;
2339270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    else if ( p2 && p2 > p )
2349270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        p = p2;
2359270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2369270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return p;
2379270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
2389270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2399270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__  char*  adb_dirstop( const char*  path )
2409270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
2419270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    char*  p  = strrchr(path, '/');
2429270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    char*  p2 = strrchr(path, '\\');
2439270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2449270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    if ( !p )
2459270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        p = p2;
2469270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    else if ( p2 && p2 > p )
2479270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        p = p2;
2489270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2499270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return p;
2509270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
2519270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2529270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__  int  adb_is_absolute_host_path( const char*  path )
2539270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
2549270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
2559270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
2569270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2579270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#else /* !_WIN32 a.k.a. Unix */
2589270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2599270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include "fdevent.h"
2609270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <cutils/sockets.h>
2619270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <cutils/properties.h>
2629270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <cutils/misc.h>
2639270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <signal.h>
2649270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <sys/wait.h>
2659270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <sys/stat.h>
2669270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <fcntl.h>
2679270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2689270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <pthread.h>
2699270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <unistd.h>
2709270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <fcntl.h>
2719270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <stdarg.h>
2729270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <netinet/in.h>
2739270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <netinet/tcp.h>
2749270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include <string.h>
2759270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2769270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define OS_PATH_SEPARATOR '/'
2779270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define OS_PATH_SEPARATOR_STR "/"
2789270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2799270a20a801403c9f60d6a701b39eae70d380403Doug Zongkertypedef  pthread_mutex_t          adb_mutex_t;
2809270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2819270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  ADB_MUTEX_INITIALIZER    PTHREAD_MUTEX_INITIALIZER
2829270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_mutex_init           pthread_mutex_init
2839270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_mutex_lock           pthread_mutex_lock
2849270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_mutex_unlock         pthread_mutex_unlock
2859270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_mutex_destroy        pthread_mutex_destroy
2869270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2879270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  ADB_MUTEX_DEFINE(m)      adb_mutex_t   m = PTHREAD_MUTEX_INITIALIZER
2889270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2899270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_cond_t               pthread_cond_t
2909270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_cond_init            pthread_cond_init
2919270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_cond_wait            pthread_cond_wait
2929270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_cond_broadcast       pthread_cond_broadcast
2939270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_cond_signal          pthread_cond_signal
2949270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  adb_cond_destroy         pthread_cond_destroy
2959270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
2969270a20a801403c9f60d6a701b39eae70d380403Doug Zongker/* declare all mutexes */
2979270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  ADB_MUTEX(x)   extern adb_mutex_t  x;
2989270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#include "mutex_list.h"
2999270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
3009270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ void  close_on_exec(int  fd)
3019270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
3029270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    fcntl( fd, F_SETFD, FD_CLOEXEC );
3039270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
3049270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
3059270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  unix_open(const char*  path, int options,...)
3069270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
3079270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    if ((options & O_CREAT) == 0)
3089270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    {
3099270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        return  open(path, options);
3109270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    }
3119270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    else
3129270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    {
3139270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        int      mode;
3149270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        va_list  args;
3159270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        va_start( args, options );
3169270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        mode = va_arg( args, int );
3179270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        va_end( args );
3189270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        return open(path, options, mode);
3199270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    }
3209270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
3219270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
3229270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode )
3239270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
3249270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return open( pathname, options, mode );
3259270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
3269270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
327703ed152147d90a549a2fee7cda5771703e502a0Doug Zongkerstatic __inline__  int  adb_creat(const char*  path, int  mode)
328703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker{
329703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker    int  fd = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
330703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker
331703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker    if ( fd < 0 )
332703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker        return -1;
333703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker
334703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker    close_on_exec(fd);
335703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker    return fd;
336703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker}
337703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker#undef   creat
338703ed152147d90a549a2fee7cda5771703e502a0Doug Zongker#define  creat  ___xxx_creat
3399270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
3409270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  adb_open( const char*  pathname, int  options )
3419270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
3429270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    int  fd = open( pathname, options );
3439270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    if (fd < 0)
3449270a20a801403c9f60d6a701b39eae70d380403Doug Zongker        return -1;
3459270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    close_on_exec( fd );
3469270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return fd;
3479270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
3489270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef   open
3499270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  open    ___xxx_open
3509270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
3519270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  adb_shutdown(int fd)
3529270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
3539270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return shutdown(fd, SHUT_RDWR);
3549270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
3559270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#undef   shutdown
3569270a20a801403c9f60d6a701b39eae70d380403Doug Zongker#define  shutdown   ____xxx_shutdown
3579270a20a801403c9f60d6a701b39eae70d380403Doug Zongker
3589270a20a801403c9f60d6a701b39eae70d380403Doug Zongkerstatic __inline__ int  adb_close(int fd)
3599270a20a801403c9f60d6a701b39eae70d380403Doug Zongker{
3609270a20a801403c9f60d6a701b39eae70d380403Doug Zongker    return close(fd);
3619270a20a801403c9f60d6a701b39eae70d380403Doug Zongker}
362