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