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