Home | History | Annotate | Download | only in adb
      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 #ifdef _WIN32
     28 
     29 #include <winsock2.h>
     30 #include <windows.h>
     31 #include <ws2tcpip.h>
     32 #include <process.h>
     33 #include <fcntl.h>
     34 #include <io.h>
     35 #include <sys/stat.h>
     36 #include <errno.h>
     37 #include <ctype.h>
     38 #include <direct.h>
     39 
     40 #define OS_PATH_SEPARATOR '\\'
     41 #define OS_PATH_SEPARATOR_STR "\\"
     42 #define ENV_PATH_SEPARATOR_STR ";"
     43 
     44 typedef CRITICAL_SECTION          adb_mutex_t;
     45 
     46 #define  ADB_MUTEX_DEFINE(x)     adb_mutex_t   x
     47 
     48 /* declare all mutexes */
     49 /* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */
     50 #define  ADB_MUTEX(x)   extern adb_mutex_t  x;
     51 #include "mutex_list.h"
     52 
     53 extern void  adb_sysdeps_init(void);
     54 
     55 static __inline__ void adb_mutex_lock( adb_mutex_t*  lock )
     56 {
     57     EnterCriticalSection( lock );
     58 }
     59 
     60 static __inline__ void  adb_mutex_unlock( adb_mutex_t*  lock )
     61 {
     62     LeaveCriticalSection( lock );
     63 }
     64 
     65 typedef struct { unsigned  tid; }  adb_thread_t;
     66 
     67 typedef  void*  (*adb_thread_func_t)(void*  arg);
     68 
     69 typedef  void (*win_thread_func_t)(void*  arg);
     70 
     71 static __inline__ int  adb_thread_create( adb_thread_t  *thread, adb_thread_func_t  func, void*  arg)
     72 {
     73     thread->tid = _beginthread( (win_thread_func_t)func, 0, arg );
     74     if (thread->tid == (unsigned)-1L) {
     75         return -1;
     76     }
     77     return 0;
     78 }
     79 
     80 static __inline__ void  close_on_exec(int  fd)
     81 {
     82     /* nothing really */
     83 }
     84 
     85 extern void  disable_tcp_nagle(int  fd);
     86 
     87 #define  lstat    stat   /* no symlinks on Win32 */
     88 
     89 #define  S_ISLNK(m)   0   /* no symlinks on Win32 */
     90 
     91 static __inline__  int    adb_unlink(const char*  path)
     92 {
     93     int  rc = unlink(path);
     94 
     95     if (rc == -1 && errno == EACCES) {
     96         /* unlink returns EACCES when the file is read-only, so we first */
     97         /* try to make it writable, then unlink again...                  */
     98         rc = chmod(path, _S_IREAD|_S_IWRITE );
     99         if (rc == 0)
    100             rc = unlink(path);
    101     }
    102     return rc;
    103 }
    104 #undef  unlink
    105 #define unlink  ___xxx_unlink
    106 
    107 static __inline__ int  adb_mkdir(const char*  path, int mode)
    108 {
    109 	return _mkdir(path);
    110 }
    111 #undef   mkdir
    112 #define  mkdir  ___xxx_mkdir
    113 
    114 extern int  adb_open(const char*  path, int  options);
    115 extern int  adb_creat(const char*  path, int  mode);
    116 extern int  adb_read(int  fd, void* buf, int len);
    117 extern int  adb_write(int  fd, const void*  buf, int  len);
    118 extern int  adb_lseek(int  fd, int  pos, int  where);
    119 extern int  adb_shutdown(int  fd);
    120 extern int  adb_close(int  fd);
    121 
    122 static __inline__ int  unix_close(int fd)
    123 {
    124     return close(fd);
    125 }
    126 #undef   close
    127 #define  close   ____xxx_close
    128 
    129 static __inline__  int  unix_read(int  fd, void*  buf, size_t  len)
    130 {
    131     return read(fd, buf, len);
    132 }
    133 #undef   read
    134 #define  read  ___xxx_read
    135 
    136 static __inline__  int  unix_write(int  fd, const void*  buf, size_t  len)
    137 {
    138     return write(fd, buf, len);
    139 }
    140 #undef   write
    141 #define  write  ___xxx_write
    142 
    143 static __inline__ int  adb_open_mode(const char* path, int options, int mode)
    144 {
    145     return adb_open(path, options);
    146 }
    147 
    148 static __inline__ int  unix_open(const char*  path, int options,...)
    149 {
    150     if ((options & O_CREAT) == 0)
    151     {
    152         return  open(path, options);
    153     }
    154     else
    155     {
    156         int      mode;
    157         va_list  args;
    158         va_start( args, options );
    159         mode = va_arg( args, int );
    160         va_end( args );
    161         return open(path, options, mode);
    162     }
    163 }
    164 #define  open    ___xxx_unix_open
    165 
    166 
    167 /* normally provided by <cutils/misc.h> */
    168 extern void*  load_file(const char*  pathname, unsigned*  psize);
    169 
    170 /* normally provided by <cutils/sockets.h> */
    171 extern int socket_loopback_client(int port, int type);
    172 extern int socket_network_client(const char *host, int port, int type);
    173 extern int socket_network_client_timeout(const char *host, int port, int type,
    174                                          int timeout);
    175 extern int socket_loopback_server(int port, int type);
    176 extern int socket_inaddr_any_server(int port, int type);
    177 
    178 /* normally provided by "fdevent.h" */
    179 
    180 #define FDE_READ              0x0001
    181 #define FDE_WRITE             0x0002
    182 #define FDE_ERROR             0x0004
    183 #define FDE_DONT_CLOSE        0x0080
    184 
    185 typedef struct fdevent fdevent;
    186 
    187 typedef void (*fd_func)(int fd, unsigned events, void *userdata);
    188 
    189 fdevent *fdevent_create(int fd, fd_func func, void *arg);
    190 void     fdevent_destroy(fdevent *fde);
    191 void     fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
    192 void     fdevent_remove(fdevent *item);
    193 void     fdevent_set(fdevent *fde, unsigned events);
    194 void     fdevent_add(fdevent *fde, unsigned events);
    195 void     fdevent_del(fdevent *fde, unsigned events);
    196 void     fdevent_loop();
    197 
    198 struct fdevent {
    199     fdevent *next;
    200     fdevent *prev;
    201 
    202     int fd;
    203     int force_eof;
    204 
    205     unsigned short state;
    206     unsigned short events;
    207 
    208     fd_func func;
    209     void *arg;
    210 };
    211 
    212 static __inline__ void  adb_sleep_ms( int  mseconds )
    213 {
    214     Sleep( mseconds );
    215 }
    216 
    217 extern int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen);
    218 
    219 #undef   accept
    220 #define  accept  ___xxx_accept
    221 
    222 static __inline__  int  adb_socket_setbufsize( int   fd, int  bufsize )
    223 {
    224     int opt = bufsize;
    225     return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt));
    226 }
    227 
    228 extern int  adb_socketpair( int  sv[2] );
    229 
    230 static __inline__  char*  adb_dirstart( const char*  path )
    231 {
    232     char*  p  = strchr(path, '/');
    233     char*  p2 = strchr(path, '\\');
    234 
    235     if ( !p )
    236         p = p2;
    237     else if ( p2 && p2 > p )
    238         p = p2;
    239 
    240     return p;
    241 }
    242 
    243 static __inline__  char*  adb_dirstop( const char*  path )
    244 {
    245     char*  p  = strrchr(path, '/');
    246     char*  p2 = strrchr(path, '\\');
    247 
    248     if ( !p )
    249         p = p2;
    250     else if ( p2 && p2 > p )
    251         p = p2;
    252 
    253     return p;
    254 }
    255 
    256 static __inline__  int  adb_is_absolute_host_path( const char*  path )
    257 {
    258     return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
    259 }
    260 
    261 extern char*  adb_strtok_r(char *str, const char *delim, char **saveptr);
    262 
    263 #else /* !_WIN32 a.k.a. Unix */
    264 
    265 #include "fdevent.h"
    266 #include <cutils/sockets.h>
    267 #include <cutils/misc.h>
    268 #include <signal.h>
    269 #include <sys/wait.h>
    270 #include <sys/stat.h>
    271 #include <fcntl.h>
    272 
    273 #include <pthread.h>
    274 #include <unistd.h>
    275 #include <fcntl.h>
    276 #include <stdarg.h>
    277 #include <netinet/in.h>
    278 #include <netinet/tcp.h>
    279 #include <string.h>
    280 #include <unistd.h>
    281 
    282 /*
    283  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
    284  * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
    285  * not already defined, then define it here.
    286  */
    287 #ifndef TEMP_FAILURE_RETRY
    288 /* Used to retry syscalls that can return EINTR. */
    289 #define TEMP_FAILURE_RETRY(exp) ({         \
    290     typeof (exp) _rc;                      \
    291     do {                                   \
    292         _rc = (exp);                       \
    293     } while (_rc == -1 && errno == EINTR); \
    294     _rc; })
    295 #endif
    296 
    297 #define OS_PATH_SEPARATOR '/'
    298 #define OS_PATH_SEPARATOR_STR "/"
    299 #define ENV_PATH_SEPARATOR_STR ":"
    300 
    301 typedef  pthread_mutex_t          adb_mutex_t;
    302 
    303 #define  ADB_MUTEX_INITIALIZER    PTHREAD_MUTEX_INITIALIZER
    304 #define  adb_mutex_init           pthread_mutex_init
    305 #define  adb_mutex_lock           pthread_mutex_lock
    306 #define  adb_mutex_unlock         pthread_mutex_unlock
    307 #define  adb_mutex_destroy        pthread_mutex_destroy
    308 
    309 #define  ADB_MUTEX_DEFINE(m)      adb_mutex_t   m = PTHREAD_MUTEX_INITIALIZER
    310 
    311 #define  adb_cond_t               pthread_cond_t
    312 #define  adb_cond_init            pthread_cond_init
    313 #define  adb_cond_wait            pthread_cond_wait
    314 #define  adb_cond_broadcast       pthread_cond_broadcast
    315 #define  adb_cond_signal          pthread_cond_signal
    316 #define  adb_cond_destroy         pthread_cond_destroy
    317 
    318 /* declare all mutexes */
    319 #define  ADB_MUTEX(x)   extern adb_mutex_t  x;
    320 #include "mutex_list.h"
    321 
    322 static __inline__ void  close_on_exec(int  fd)
    323 {
    324     fcntl( fd, F_SETFD, FD_CLOEXEC );
    325 }
    326 
    327 static __inline__ int  unix_open(const char*  path, int options,...)
    328 {
    329     if ((options & O_CREAT) == 0)
    330     {
    331         return  TEMP_FAILURE_RETRY( open(path, options) );
    332     }
    333     else
    334     {
    335         int      mode;
    336         va_list  args;
    337         va_start( args, options );
    338         mode = va_arg( args, int );
    339         va_end( args );
    340         return TEMP_FAILURE_RETRY( open( path, options, mode ) );
    341     }
    342 }
    343 
    344 static __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode )
    345 {
    346     return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
    347 }
    348 
    349 
    350 static __inline__ int  adb_open( const char*  pathname, int  options )
    351 {
    352     int  fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
    353     if (fd < 0)
    354         return -1;
    355     close_on_exec( fd );
    356     return fd;
    357 }
    358 #undef   open
    359 #define  open    ___xxx_open
    360 
    361 static __inline__ int  adb_shutdown(int fd)
    362 {
    363     return shutdown(fd, SHUT_RDWR);
    364 }
    365 #undef   shutdown
    366 #define  shutdown   ____xxx_shutdown
    367 
    368 static __inline__ int  adb_close(int fd)
    369 {
    370     return close(fd);
    371 }
    372 #undef   close
    373 #define  close   ____xxx_close
    374 
    375 
    376 static __inline__  int  adb_read(int  fd, void*  buf, size_t  len)
    377 {
    378     return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
    379 }
    380 
    381 #undef   read
    382 #define  read  ___xxx_read
    383 
    384 static __inline__  int  adb_write(int  fd, const void*  buf, size_t  len)
    385 {
    386     return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
    387 }
    388 #undef   write
    389 #define  write  ___xxx_write
    390 
    391 static __inline__ int   adb_lseek(int  fd, int  pos, int  where)
    392 {
    393     return lseek(fd, pos, where);
    394 }
    395 #undef   lseek
    396 #define  lseek   ___xxx_lseek
    397 
    398 static __inline__  int    adb_unlink(const char*  path)
    399 {
    400     return  unlink(path);
    401 }
    402 #undef  unlink
    403 #define unlink  ___xxx_unlink
    404 
    405 static __inline__  int  adb_creat(const char*  path, int  mode)
    406 {
    407     int  fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
    408 
    409     if ( fd < 0 )
    410         return -1;
    411 
    412     close_on_exec(fd);
    413     return fd;
    414 }
    415 #undef   creat
    416 #define  creat  ___xxx_creat
    417 
    418 static __inline__ int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen)
    419 {
    420     int fd;
    421 
    422     fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
    423     if (fd >= 0)
    424         close_on_exec(fd);
    425 
    426     return fd;
    427 }
    428 
    429 #undef   accept
    430 #define  accept  ___xxx_accept
    431 
    432 #define  unix_read   adb_read
    433 #define  unix_write  adb_write
    434 #define  unix_close  adb_close
    435 
    436 typedef  pthread_t                 adb_thread_t;
    437 
    438 typedef void*  (*adb_thread_func_t)( void*  arg );
    439 
    440 static __inline__ int  adb_thread_create( adb_thread_t  *pthread, adb_thread_func_t  start, void*  arg )
    441 {
    442     pthread_attr_t   attr;
    443 
    444     pthread_attr_init (&attr);
    445     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
    446 
    447     return pthread_create( pthread, &attr, start, arg );
    448 }
    449 
    450 static __inline__  int  adb_socket_setbufsize( int   fd, int  bufsize )
    451 {
    452     int opt = bufsize;
    453     return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt));
    454 }
    455 
    456 static __inline__ void  disable_tcp_nagle(int fd)
    457 {
    458     int  on = 1;
    459     setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) );
    460 }
    461 
    462 
    463 static __inline__ int  unix_socketpair( int  d, int  type, int  protocol, int sv[2] )
    464 {
    465     return socketpair( d, type, protocol, sv );
    466 }
    467 
    468 static __inline__ int  adb_socketpair( int  sv[2] )
    469 {
    470     int  rc;
    471 
    472     rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
    473     if (rc < 0)
    474         return -1;
    475 
    476     close_on_exec( sv[0] );
    477     close_on_exec( sv[1] );
    478     return 0;
    479 }
    480 
    481 #undef   socketpair
    482 #define  socketpair   ___xxx_socketpair
    483 
    484 static __inline__ void  adb_sleep_ms( int  mseconds )
    485 {
    486     usleep( mseconds*1000 );
    487 }
    488 
    489 static __inline__ int  adb_mkdir(const char*  path, int mode)
    490 {
    491     return mkdir(path, mode);
    492 }
    493 #undef   mkdir
    494 #define  mkdir  ___xxx_mkdir
    495 
    496 static __inline__ void  adb_sysdeps_init(void)
    497 {
    498 }
    499 
    500 static __inline__ char*  adb_dirstart(const char*  path)
    501 {
    502     return strchr(path, '/');
    503 }
    504 
    505 static __inline__ char*  adb_dirstop(const char*  path)
    506 {
    507     return strrchr(path, '/');
    508 }
    509 
    510 static __inline__  int  adb_is_absolute_host_path( const char*  path )
    511 {
    512     return path[0] == '/';
    513 }
    514 
    515 static __inline__ char*  adb_strtok_r(char *str, const char *delim, char **saveptr)
    516 {
    517     return strtok_r(str, delim, saveptr);
    518 }
    519 #undef   strtok_r
    520 #define  strtok_r  ___xxx_strtok_r
    521 
    522 #endif /* !_WIN32 */
    523 
    524 #endif /* _ADB_SYSDEPS_H */
    525