The Android Open Source Project | 9ca14dc | 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 "\\" |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 41 | #define ENV_PATH_SEPARATOR_STR ";" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
| 43 | typedef CRITICAL_SECTION adb_mutex_t; |
| 44 | |
| 45 | #define ADB_MUTEX_DEFINE(x) adb_mutex_t x |
| 46 | |
| 47 | /* declare all mutexes */ |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 48 | /* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 72 | thread->tid = _beginthread( (win_thread_func_t)func, 0, arg ); |
| 73 | if (thread->tid == (unsigned)-1L) { |
| 74 | return -1; |
| 75 | } |
| 76 | return 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static __inline__ void close_on_exec(int fd) |
| 80 | { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 81 | /* nothing really */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | { |
JP Abgrall | 1f501f3 | 2011-02-23 18:44:39 -0800 | [diff] [blame] | 108 | return _mkdir(path); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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); |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 118 | extern int adb_shutdown(int fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 144 | return adb_open(path, options); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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); |
Elliott Hughes | 4fa7db0 | 2014-05-20 08:51:15 -0700 | [diff] [blame] | 172 | extern int socket_network_client_timeout(const char *host, int port, int type, |
| 173 | int timeout); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | extern int socket_loopback_server(int port, int type); |
| 175 | extern int socket_inaddr_any_server(int port, int type); |
| 176 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 177 | /* normally provided by "fdevent.h" */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | |
| 179 | #define FDE_READ 0x0001 |
| 180 | #define FDE_WRITE 0x0002 |
| 181 | #define FDE_ERROR 0x0004 |
| 182 | #define FDE_DONT_CLOSE 0x0080 |
| 183 | |
| 184 | typedef struct fdevent fdevent; |
| 185 | |
| 186 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); |
| 187 | |
| 188 | fdevent *fdevent_create(int fd, fd_func func, void *arg); |
| 189 | void fdevent_destroy(fdevent *fde); |
| 190 | void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); |
| 191 | void fdevent_remove(fdevent *item); |
| 192 | void fdevent_set(fdevent *fde, unsigned events); |
| 193 | void fdevent_add(fdevent *fde, unsigned events); |
| 194 | void fdevent_del(fdevent *fde, unsigned events); |
| 195 | void fdevent_loop(); |
| 196 | |
| 197 | struct fdevent { |
| 198 | fdevent *next; |
| 199 | fdevent *prev; |
| 200 | |
| 201 | int fd; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 202 | int force_eof; |
| 203 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | unsigned short state; |
| 205 | unsigned short events; |
| 206 | |
| 207 | fd_func func; |
| 208 | void *arg; |
| 209 | }; |
| 210 | |
| 211 | static __inline__ void adb_sleep_ms( int mseconds ) |
| 212 | { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 213 | Sleep( mseconds ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen); |
| 217 | |
| 218 | #undef accept |
| 219 | #define accept ___xxx_accept |
| 220 | |
| 221 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) |
| 222 | { |
| 223 | int opt = bufsize; |
| 224 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt)); |
| 225 | } |
| 226 | |
| 227 | extern int adb_socketpair( int sv[2] ); |
| 228 | |
| 229 | static __inline__ char* adb_dirstart( const char* path ) |
| 230 | { |
| 231 | char* p = strchr(path, '/'); |
| 232 | char* p2 = strchr(path, '\\'); |
| 233 | |
| 234 | if ( !p ) |
| 235 | p = p2; |
| 236 | else if ( p2 && p2 > p ) |
| 237 | p = p2; |
| 238 | |
| 239 | return p; |
| 240 | } |
| 241 | |
| 242 | static __inline__ char* adb_dirstop( const char* path ) |
| 243 | { |
| 244 | char* p = strrchr(path, '/'); |
| 245 | char* p2 = strrchr(path, '\\'); |
| 246 | |
| 247 | if ( !p ) |
| 248 | p = p2; |
| 249 | else if ( p2 && p2 > p ) |
| 250 | p = p2; |
| 251 | |
| 252 | return p; |
| 253 | } |
| 254 | |
| 255 | static __inline__ int adb_is_absolute_host_path( const char* path ) |
| 256 | { |
| 257 | return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; |
| 258 | } |
| 259 | |
Scott Anderson | 0fa5978 | 2012-06-05 17:54:27 -0700 | [diff] [blame] | 260 | extern char* adb_strtok_r(char *str, const char *delim, char **saveptr); |
| 261 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 262 | #else /* !_WIN32 a.k.a. Unix */ |
| 263 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 264 | #include "fdevent.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 265 | #include <cutils/sockets.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 | #include <cutils/misc.h> |
| 267 | #include <signal.h> |
| 268 | #include <sys/wait.h> |
| 269 | #include <sys/stat.h> |
| 270 | #include <fcntl.h> |
| 271 | |
| 272 | #include <pthread.h> |
| 273 | #include <unistd.h> |
| 274 | #include <fcntl.h> |
| 275 | #include <stdarg.h> |
| 276 | #include <netinet/in.h> |
| 277 | #include <netinet/tcp.h> |
| 278 | #include <string.h> |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 279 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | |
Kenny Root | 8ca7535 | 2012-10-13 11:59:01 -0700 | [diff] [blame] | 281 | /* |
| 282 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of |
| 283 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's |
| 284 | * not already defined, then define it here. |
| 285 | */ |
| 286 | #ifndef TEMP_FAILURE_RETRY |
| 287 | /* Used to retry syscalls that can return EINTR. */ |
| 288 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 289 | typeof (exp) _rc; \ |
| 290 | do { \ |
| 291 | _rc = (exp); \ |
| 292 | } while (_rc == -1 && errno == EINTR); \ |
| 293 | _rc; }) |
| 294 | #endif |
| 295 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 296 | #define OS_PATH_SEPARATOR '/' |
| 297 | #define OS_PATH_SEPARATOR_STR "/" |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 298 | #define ENV_PATH_SEPARATOR_STR ":" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 299 | |
| 300 | typedef pthread_mutex_t adb_mutex_t; |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 301 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 302 | #define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
| 303 | #define adb_mutex_init pthread_mutex_init |
| 304 | #define adb_mutex_lock pthread_mutex_lock |
| 305 | #define adb_mutex_unlock pthread_mutex_unlock |
| 306 | #define adb_mutex_destroy pthread_mutex_destroy |
| 307 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 308 | #define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | |
| 310 | #define adb_cond_t pthread_cond_t |
| 311 | #define adb_cond_init pthread_cond_init |
| 312 | #define adb_cond_wait pthread_cond_wait |
| 313 | #define adb_cond_broadcast pthread_cond_broadcast |
| 314 | #define adb_cond_signal pthread_cond_signal |
| 315 | #define adb_cond_destroy pthread_cond_destroy |
| 316 | |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 317 | /* declare all mutexes */ |
| 318 | #define ADB_MUTEX(x) extern adb_mutex_t x; |
| 319 | #include "mutex_list.h" |
| 320 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | static __inline__ void close_on_exec(int fd) |
| 322 | { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 323 | fcntl( fd, F_SETFD, FD_CLOEXEC ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static __inline__ int unix_open(const char* path, int options,...) |
| 327 | { |
| 328 | if ((options & O_CREAT) == 0) |
| 329 | { |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 330 | return TEMP_FAILURE_RETRY( open(path, options) ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | } |
| 332 | else |
| 333 | { |
| 334 | int mode; |
| 335 | va_list args; |
| 336 | va_start( args, options ); |
| 337 | mode = va_arg( args, int ); |
| 338 | va_end( args ); |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 339 | return TEMP_FAILURE_RETRY( open( path, options, mode ) ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | static __inline__ int adb_open_mode( const char* pathname, int options, int mode ) |
| 344 | { |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 345 | return TEMP_FAILURE_RETRY( open( pathname, options, mode ) ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
| 349 | static __inline__ int adb_open( const char* pathname, int options ) |
| 350 | { |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 351 | int fd = TEMP_FAILURE_RETRY( open( pathname, options ) ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 352 | if (fd < 0) |
| 353 | return -1; |
| 354 | close_on_exec( fd ); |
| 355 | return fd; |
| 356 | } |
| 357 | #undef open |
| 358 | #define open ___xxx_open |
| 359 | |
Mike Lockwood | 81ffe17 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 360 | static __inline__ int adb_shutdown(int fd) |
| 361 | { |
| 362 | return shutdown(fd, SHUT_RDWR); |
| 363 | } |
| 364 | #undef shutdown |
| 365 | #define shutdown ____xxx_shutdown |
| 366 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | static __inline__ int adb_close(int fd) |
| 368 | { |
| 369 | return close(fd); |
| 370 | } |
| 371 | #undef close |
| 372 | #define close ____xxx_close |
| 373 | |
| 374 | |
| 375 | static __inline__ int adb_read(int fd, void* buf, size_t len) |
| 376 | { |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 377 | return TEMP_FAILURE_RETRY( read( fd, buf, len ) ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | #undef read |
| 381 | #define read ___xxx_read |
| 382 | |
| 383 | static __inline__ int adb_write(int fd, const void* buf, size_t len) |
| 384 | { |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 385 | return TEMP_FAILURE_RETRY( write( fd, buf, len ) ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | } |
| 387 | #undef write |
| 388 | #define write ___xxx_write |
| 389 | |
| 390 | static __inline__ int adb_lseek(int fd, int pos, int where) |
| 391 | { |
| 392 | return lseek(fd, pos, where); |
| 393 | } |
| 394 | #undef lseek |
| 395 | #define lseek ___xxx_lseek |
| 396 | |
| 397 | static __inline__ int adb_unlink(const char* path) |
| 398 | { |
| 399 | return unlink(path); |
| 400 | } |
| 401 | #undef unlink |
| 402 | #define unlink ___xxx_unlink |
| 403 | |
| 404 | static __inline__ int adb_creat(const char* path, int mode) |
| 405 | { |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 406 | int fd = TEMP_FAILURE_RETRY( creat( path, mode ) ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 408 | if ( fd < 0 ) |
| 409 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 410 | |
| 411 | close_on_exec(fd); |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 412 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | } |
| 414 | #undef creat |
| 415 | #define creat ___xxx_creat |
| 416 | |
| 417 | static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen) |
| 418 | { |
Benoit Goby | f4ded74 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 419 | int fd; |
| 420 | |
Kenny Root | eac025c | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 421 | fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) ); |
Benoit Goby | f4ded74 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 422 | if (fd >= 0) |
| 423 | close_on_exec(fd); |
| 424 | |
| 425 | return fd; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | #undef accept |
| 429 | #define accept ___xxx_accept |
| 430 | |
| 431 | #define unix_read adb_read |
| 432 | #define unix_write adb_write |
| 433 | #define unix_close adb_close |
| 434 | |
| 435 | typedef pthread_t adb_thread_t; |
| 436 | |
| 437 | typedef void* (*adb_thread_func_t)( void* arg ); |
| 438 | |
| 439 | static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg ) |
| 440 | { |
| 441 | pthread_attr_t attr; |
| 442 | |
| 443 | pthread_attr_init (&attr); |
| 444 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
| 445 | |
| 446 | return pthread_create( pthread, &attr, start, arg ); |
| 447 | } |
| 448 | |
| 449 | static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) |
| 450 | { |
| 451 | int opt = bufsize; |
| 452 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); |
| 453 | } |
| 454 | |
| 455 | static __inline__ void disable_tcp_nagle(int fd) |
| 456 | { |
| 457 | int on = 1; |
| 458 | setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) ); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] ) |
| 463 | { |
| 464 | return socketpair( d, type, protocol, sv ); |
| 465 | } |
| 466 | |
| 467 | static __inline__ int adb_socketpair( int sv[2] ) |
| 468 | { |
| 469 | int rc; |
| 470 | |
| 471 | rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); |
| 472 | if (rc < 0) |
| 473 | return -1; |
| 474 | |
| 475 | close_on_exec( sv[0] ); |
| 476 | close_on_exec( sv[1] ); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | #undef socketpair |
| 481 | #define socketpair ___xxx_socketpair |
| 482 | |
| 483 | static __inline__ void adb_sleep_ms( int mseconds ) |
| 484 | { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 485 | usleep( mseconds*1000 ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static __inline__ int adb_mkdir(const char* path, int mode) |
| 489 | { |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 490 | return mkdir(path, mode); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 | } |
| 492 | #undef mkdir |
| 493 | #define mkdir ___xxx_mkdir |
| 494 | |
| 495 | static __inline__ void adb_sysdeps_init(void) |
| 496 | { |
| 497 | } |
| 498 | |
| 499 | static __inline__ char* adb_dirstart(const char* path) |
| 500 | { |
| 501 | return strchr(path, '/'); |
| 502 | } |
| 503 | |
| 504 | static __inline__ char* adb_dirstop(const char* path) |
| 505 | { |
| 506 | return strrchr(path, '/'); |
| 507 | } |
| 508 | |
| 509 | static __inline__ int adb_is_absolute_host_path( const char* path ) |
| 510 | { |
| 511 | return path[0] == '/'; |
| 512 | } |
| 513 | |
Scott Anderson | 0fa5978 | 2012-06-05 17:54:27 -0700 | [diff] [blame] | 514 | static __inline__ char* adb_strtok_r(char *str, const char *delim, char **saveptr) |
| 515 | { |
| 516 | return strtok_r(str, delim, saveptr); |
| 517 | } |
| 518 | #undef strtok_r |
| 519 | #define strtok_r ___xxx_strtok_r |
| 520 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 521 | #endif /* !_WIN32 */ |
| 522 | |
| 523 | #endif /* _ADB_SYSDEPS_H */ |