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