The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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_close(int fd); |
| 117 | |
| 118 | static __inline__ int unix_close(int fd) |
| 119 | { |
| 120 | return close(fd); |
| 121 | } |
| 122 | #undef close |
| 123 | #define close ____xxx_close |
| 124 | |
| 125 | static __inline__ int unix_read(int fd, void* buf, size_t len) |
| 126 | { |
| 127 | return read(fd, buf, len); |
| 128 | } |
| 129 | #undef read |
| 130 | #define read ___xxx_read |
| 131 | |
| 132 | static __inline__ int unix_write(int fd, const void* buf, size_t len) |
| 133 | { |
| 134 | return write(fd, buf, len); |
| 135 | } |
| 136 | #undef write |
| 137 | #define write ___xxx_write |
| 138 | |
| 139 | static __inline__ int adb_open_mode(const char* path, int options, int mode) |
| 140 | { |
| 141 | return adb_open(path, options); |
| 142 | } |
| 143 | |
| 144 | static __inline__ int unix_open(const char* path, int options,...) |
| 145 | { |
| 146 | if ((options & O_CREAT) == 0) |
| 147 | { |
| 148 | return open(path, options); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | int mode; |
| 153 | va_list args; |
| 154 | va_start( args, options ); |
| 155 | mode = va_arg( args, int ); |
| 156 | va_end( args ); |
| 157 | return open(path, options, mode); |
| 158 | } |
| 159 | } |
| 160 | #define open ___xxx_unix_open |
| 161 | |
| 162 | |
| 163 | /* normally provided by <cutils/misc.h> */ |
| 164 | extern void* load_file(const char* pathname, unsigned* psize); |
| 165 | |
| 166 | /* normally provided by <cutils/sockets.h> */ |
| 167 | extern int socket_loopback_client(int port, int type); |
| 168 | extern int socket_network_client(const char *host, int port, int type); |
| 169 | extern int socket_loopback_server(int port, int type); |
| 170 | extern int socket_inaddr_any_server(int port, int type); |
| 171 | |
David 'Digit' Turner | 414ff7d | 2009-05-18 17:07:46 +0200 | [diff] [blame^] | 172 | /* normally provided by "fdevent.h" */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | |
| 174 | #define FDE_READ 0x0001 |
| 175 | #define FDE_WRITE 0x0002 |
| 176 | #define FDE_ERROR 0x0004 |
| 177 | #define FDE_DONT_CLOSE 0x0080 |
| 178 | |
| 179 | typedef struct fdevent fdevent; |
| 180 | |
| 181 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); |
| 182 | |
| 183 | fdevent *fdevent_create(int fd, fd_func func, void *arg); |
| 184 | void fdevent_destroy(fdevent *fde); |
| 185 | void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); |
| 186 | void fdevent_remove(fdevent *item); |
| 187 | void fdevent_set(fdevent *fde, unsigned events); |
| 188 | void fdevent_add(fdevent *fde, unsigned events); |
| 189 | void fdevent_del(fdevent *fde, unsigned events); |
| 190 | void fdevent_loop(); |
| 191 | |
| 192 | struct fdevent { |
| 193 | fdevent *next; |
| 194 | fdevent *prev; |
| 195 | |
| 196 | int fd; |
| 197 | unsigned short state; |
| 198 | unsigned short events; |
| 199 | |
| 200 | fd_func func; |
| 201 | void *arg; |
| 202 | }; |
| 203 | |
| 204 | static __inline__ void adb_sleep_ms( int mseconds ) |
| 205 | { |
| 206 | Sleep( mseconds ); |
| 207 | } |
| 208 | |
| 209 | extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen); |
| 210 | |
| 211 | #undef accept |
| 212 | #define accept ___xxx_accept |
| 213 | |
| 214 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) |
| 215 | { |
| 216 | int opt = bufsize; |
| 217 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt)); |
| 218 | } |
| 219 | |
| 220 | extern int adb_socketpair( int sv[2] ); |
| 221 | |
| 222 | static __inline__ char* adb_dirstart( const char* path ) |
| 223 | { |
| 224 | char* p = strchr(path, '/'); |
| 225 | char* p2 = strchr(path, '\\'); |
| 226 | |
| 227 | if ( !p ) |
| 228 | p = p2; |
| 229 | else if ( p2 && p2 > p ) |
| 230 | p = p2; |
| 231 | |
| 232 | return p; |
| 233 | } |
| 234 | |
| 235 | static __inline__ char* adb_dirstop( const char* path ) |
| 236 | { |
| 237 | char* p = strrchr(path, '/'); |
| 238 | char* p2 = strrchr(path, '\\'); |
| 239 | |
| 240 | if ( !p ) |
| 241 | p = p2; |
| 242 | else if ( p2 && p2 > p ) |
| 243 | p = p2; |
| 244 | |
| 245 | return p; |
| 246 | } |
| 247 | |
| 248 | static __inline__ int adb_is_absolute_host_path( const char* path ) |
| 249 | { |
| 250 | return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; |
| 251 | } |
| 252 | |
| 253 | #else /* !_WIN32 a.k.a. Unix */ |
| 254 | |
David 'Digit' Turner | 414ff7d | 2009-05-18 17:07:46 +0200 | [diff] [blame^] | 255 | #include "fdevent.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | #include <cutils/sockets.h> |
| 257 | #include <cutils/properties.h> |
| 258 | #include <cutils/misc.h> |
| 259 | #include <signal.h> |
| 260 | #include <sys/wait.h> |
| 261 | #include <sys/stat.h> |
| 262 | #include <fcntl.h> |
| 263 | |
| 264 | #include <pthread.h> |
| 265 | #include <unistd.h> |
| 266 | #include <fcntl.h> |
| 267 | #include <stdarg.h> |
| 268 | #include <netinet/in.h> |
| 269 | #include <netinet/tcp.h> |
| 270 | #include <string.h> |
| 271 | |
| 272 | #define OS_PATH_SEPARATOR '/' |
| 273 | #define OS_PATH_SEPARATOR_STR "/" |
| 274 | |
| 275 | typedef pthread_mutex_t adb_mutex_t; |
| 276 | #define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
| 277 | #define adb_mutex_init pthread_mutex_init |
| 278 | #define adb_mutex_lock pthread_mutex_lock |
| 279 | #define adb_mutex_unlock pthread_mutex_unlock |
| 280 | #define adb_mutex_destroy pthread_mutex_destroy |
| 281 | |
| 282 | #define ADB_MUTEX_DEFINE(m) static adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER |
| 283 | |
| 284 | #define adb_cond_t pthread_cond_t |
| 285 | #define adb_cond_init pthread_cond_init |
| 286 | #define adb_cond_wait pthread_cond_wait |
| 287 | #define adb_cond_broadcast pthread_cond_broadcast |
| 288 | #define adb_cond_signal pthread_cond_signal |
| 289 | #define adb_cond_destroy pthread_cond_destroy |
| 290 | |
| 291 | static __inline__ void close_on_exec(int fd) |
| 292 | { |
| 293 | fcntl( fd, F_SETFD, FD_CLOEXEC ); |
| 294 | } |
| 295 | |
| 296 | static __inline__ int unix_open(const char* path, int options,...) |
| 297 | { |
| 298 | if ((options & O_CREAT) == 0) |
| 299 | { |
| 300 | return open(path, options); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | int mode; |
| 305 | va_list args; |
| 306 | va_start( args, options ); |
| 307 | mode = va_arg( args, int ); |
| 308 | va_end( args ); |
| 309 | return open(path, options, mode); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static __inline__ int adb_open_mode( const char* pathname, int options, int mode ) |
| 314 | { |
| 315 | return open( pathname, options, mode ); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | static __inline__ int adb_open( const char* pathname, int options ) |
| 320 | { |
| 321 | int fd = open( pathname, options ); |
| 322 | if (fd < 0) |
| 323 | return -1; |
| 324 | close_on_exec( fd ); |
| 325 | return fd; |
| 326 | } |
| 327 | #undef open |
| 328 | #define open ___xxx_open |
| 329 | |
| 330 | static __inline__ int adb_close(int fd) |
| 331 | { |
| 332 | return close(fd); |
| 333 | } |
| 334 | #undef close |
| 335 | #define close ____xxx_close |
| 336 | |
| 337 | |
| 338 | static __inline__ int adb_read(int fd, void* buf, size_t len) |
| 339 | { |
| 340 | return read(fd, buf, len); |
| 341 | } |
| 342 | |
| 343 | #undef read |
| 344 | #define read ___xxx_read |
| 345 | |
| 346 | static __inline__ int adb_write(int fd, const void* buf, size_t len) |
| 347 | { |
| 348 | return write(fd, buf, len); |
| 349 | } |
| 350 | #undef write |
| 351 | #define write ___xxx_write |
| 352 | |
| 353 | static __inline__ int adb_lseek(int fd, int pos, int where) |
| 354 | { |
| 355 | return lseek(fd, pos, where); |
| 356 | } |
| 357 | #undef lseek |
| 358 | #define lseek ___xxx_lseek |
| 359 | |
| 360 | static __inline__ int adb_unlink(const char* path) |
| 361 | { |
| 362 | return unlink(path); |
| 363 | } |
| 364 | #undef unlink |
| 365 | #define unlink ___xxx_unlink |
| 366 | |
| 367 | static __inline__ int adb_creat(const char* path, int mode) |
| 368 | { |
| 369 | int fd = creat(path, mode); |
| 370 | |
| 371 | if ( fd < 0 ) |
| 372 | return -1; |
| 373 | |
| 374 | close_on_exec(fd); |
| 375 | return fd; |
| 376 | } |
| 377 | #undef creat |
| 378 | #define creat ___xxx_creat |
| 379 | |
| 380 | static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen) |
| 381 | { |
| 382 | return accept( serverfd, addr, addrlen ); |
| 383 | } |
| 384 | |
| 385 | #undef accept |
| 386 | #define accept ___xxx_accept |
| 387 | |
| 388 | #define unix_read adb_read |
| 389 | #define unix_write adb_write |
| 390 | #define unix_close adb_close |
| 391 | |
| 392 | typedef pthread_t adb_thread_t; |
| 393 | |
| 394 | typedef void* (*adb_thread_func_t)( void* arg ); |
| 395 | |
| 396 | static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg ) |
| 397 | { |
| 398 | pthread_attr_t attr; |
| 399 | |
| 400 | pthread_attr_init (&attr); |
| 401 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
| 402 | |
| 403 | return pthread_create( pthread, &attr, start, arg ); |
| 404 | } |
| 405 | |
| 406 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) |
| 407 | { |
| 408 | int opt = bufsize; |
| 409 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); |
| 410 | } |
| 411 | |
| 412 | static __inline__ void disable_tcp_nagle(int fd) |
| 413 | { |
| 414 | int on = 1; |
| 415 | setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) ); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] ) |
| 420 | { |
| 421 | return socketpair( d, type, protocol, sv ); |
| 422 | } |
| 423 | |
| 424 | static __inline__ int adb_socketpair( int sv[2] ) |
| 425 | { |
| 426 | int rc; |
| 427 | |
| 428 | rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); |
| 429 | if (rc < 0) |
| 430 | return -1; |
| 431 | |
| 432 | close_on_exec( sv[0] ); |
| 433 | close_on_exec( sv[1] ); |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | #undef socketpair |
| 438 | #define socketpair ___xxx_socketpair |
| 439 | |
| 440 | static __inline__ void adb_sleep_ms( int mseconds ) |
| 441 | { |
| 442 | usleep( mseconds*1000 ); |
| 443 | } |
| 444 | |
| 445 | static __inline__ int adb_mkdir(const char* path, int mode) |
| 446 | { |
| 447 | return mkdir(path, mode); |
| 448 | } |
| 449 | #undef mkdir |
| 450 | #define mkdir ___xxx_mkdir |
| 451 | |
| 452 | static __inline__ void adb_sysdeps_init(void) |
| 453 | { |
| 454 | } |
| 455 | |
| 456 | static __inline__ char* adb_dirstart(const char* path) |
| 457 | { |
| 458 | return strchr(path, '/'); |
| 459 | } |
| 460 | |
| 461 | static __inline__ char* adb_dirstop(const char* path) |
| 462 | { |
| 463 | return strrchr(path, '/'); |
| 464 | } |
| 465 | |
| 466 | static __inline__ int adb_is_absolute_host_path( const char* path ) |
| 467 | { |
| 468 | return path[0] == '/'; |
| 469 | } |
| 470 | |
| 471 | #endif /* !_WIN32 */ |
| 472 | |
| 473 | #endif /* _ADB_SYSDEPS_H */ |