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