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