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