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