| 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 |  | 
| Spencer Low | d21dc82 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 31 | // Include this before open/unlink are defined as macros below. | 
|  | 32 | #include <base/utf8.h> | 
|  | 33 |  | 
| Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 34 | /* | 
|  | 35 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of | 
|  | 36 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's | 
|  | 37 | * not already defined, then define it here. | 
|  | 38 | */ | 
|  | 39 | #ifndef TEMP_FAILURE_RETRY | 
|  | 40 | /* Used to retry syscalls that can return EINTR. */ | 
|  | 41 | #define TEMP_FAILURE_RETRY(exp) ({         \ | 
|  | 42 | typeof (exp) _rc;                      \ | 
|  | 43 | do {                                   \ | 
|  | 44 | _rc = (exp);                       \ | 
|  | 45 | } while (_rc == -1 && errno == EINTR); \ | 
|  | 46 | _rc; }) | 
|  | 47 | #endif | 
|  | 48 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 49 | // Some printf-like functions are implemented in terms of | 
|  | 50 | // android::base::StringAppendV, so they should use the same attribute for | 
|  | 51 | // compile-time format string checking. On Windows, if the mingw version of | 
|  | 52 | // vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd | 
|  | 53 | // and PRIu64 (and related) to be recognized by the compile-time checking. | 
|  | 54 | #define ADB_FORMAT_ARCHETYPE __printf__ | 
|  | 55 | #ifdef __USE_MINGW_ANSI_STDIO | 
|  | 56 | #if __USE_MINGW_ANSI_STDIO | 
|  | 57 | #undef ADB_FORMAT_ARCHETYPE | 
|  | 58 | #define ADB_FORMAT_ARCHETYPE gnu_printf | 
|  | 59 | #endif | 
|  | 60 | #endif | 
|  | 61 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | #ifdef _WIN32 | 
|  | 63 |  | 
| Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 64 | #include <ctype.h> | 
|  | 65 | #include <direct.h> | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 66 | #include <dirent.h> | 
| Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 67 | #include <errno.h> | 
|  | 68 | #include <fcntl.h> | 
|  | 69 | #include <io.h> | 
|  | 70 | #include <process.h> | 
|  | 71 | #include <sys/stat.h> | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 72 | #include <utime.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | #include <winsock2.h> | 
| Stephen Hines | 2f431a8 | 2014-10-01 17:37:06 -0700 | [diff] [blame] | 74 | #include <windows.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | #include <ws2tcpip.h> | 
| Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 76 |  | 
| Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 77 | #include <memory>   // unique_ptr | 
| Spencer Low | d21dc82 | 2015-11-12 15:20:15 -0800 | [diff] [blame] | 78 | #include <string> | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 79 |  | 
| Dan Albert | 630b9af | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 80 | #include "fdevent.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 |  | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 82 | #define OS_PATH_SEPARATORS "\\/" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | #define OS_PATH_SEPARATOR '\\' | 
|  | 84 | #define OS_PATH_SEPARATOR_STR "\\" | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 85 | #define ENV_PATH_SEPARATOR_STR ";" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 |  | 
| Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 87 | static __inline__ bool adb_is_separator(char c) { | 
|  | 88 | return c == '\\' || c == '/'; | 
|  | 89 | } | 
|  | 90 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | typedef CRITICAL_SECTION          adb_mutex_t; | 
|  | 92 |  | 
|  | 93 | #define  ADB_MUTEX_DEFINE(x)     adb_mutex_t   x | 
|  | 94 |  | 
|  | 95 | /* declare all mutexes */ | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 96 | /* 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] | 97 | #define  ADB_MUTEX(x)   extern adb_mutex_t  x; | 
|  | 98 | #include "mutex_list.h" | 
|  | 99 |  | 
|  | 100 | extern void  adb_sysdeps_init(void); | 
|  | 101 |  | 
|  | 102 | static __inline__ void adb_mutex_lock( adb_mutex_t*  lock ) | 
|  | 103 | { | 
|  | 104 | EnterCriticalSection( lock ); | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | static __inline__ void  adb_mutex_unlock( adb_mutex_t*  lock ) | 
|  | 108 | { | 
|  | 109 | LeaveCriticalSection( lock ); | 
|  | 110 | } | 
|  | 111 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | typedef  void*  (*adb_thread_func_t)(void*  arg); | 
|  | 113 |  | 
|  | 114 | typedef  void (*win_thread_func_t)(void*  arg); | 
|  | 115 |  | 
| Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 116 | 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] | 117 | uintptr_t tid = _beginthread((win_thread_func_t)func, 0, arg); | 
|  | 118 | return (tid != static_cast<uintptr_t>(-1L)); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 121 | static __inline__ int adb_thread_setname(const std::string& name) { | 
|  | 122 | // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set | 
|  | 123 | // the thread name in Windows. Unfortunately, it only works during debugging, but | 
|  | 124 | // our build process doesn't generate PDB files needed for debugging. | 
|  | 125 | return 0; | 
|  | 126 | } | 
|  | 127 |  | 
| leozwang | cbf0267 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 128 | static __inline__  unsigned long adb_thread_id() | 
|  | 129 | { | 
|  | 130 | return GetCurrentThreadId(); | 
|  | 131 | } | 
|  | 132 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 133 | static __inline__ void  close_on_exec(int  fd) | 
|  | 134 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 135 | /* nothing really */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | #define  lstat    stat   /* no symlinks on Win32 */ | 
|  | 139 |  | 
|  | 140 | #define  S_ISLNK(m)   0   /* no symlinks on Win32 */ | 
|  | 141 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 142 | extern int  adb_unlink(const char*  path); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | #undef  unlink | 
|  | 144 | #define unlink  ___xxx_unlink | 
|  | 145 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 146 | extern int adb_mkdir(const std::string& path, int mode); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | #undef   mkdir | 
|  | 148 | #define  mkdir  ___xxx_mkdir | 
|  | 149 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 150 | // 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] | 151 | extern int  adb_open(const char*  path, int  options); | 
|  | 152 | extern int  adb_creat(const char*  path, int  mode); | 
|  | 153 | extern int  adb_read(int  fd, void* buf, int len); | 
|  | 154 | extern int  adb_write(int  fd, const void*  buf, int  len); | 
|  | 155 | extern int  adb_lseek(int  fd, int  pos, int  where); | 
| Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 156 | extern int  adb_shutdown(int  fd); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | extern int  adb_close(int  fd); | 
|  | 158 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 159 | // 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] | 160 | static __inline__ int  unix_close(int fd) | 
|  | 161 | { | 
|  | 162 | return close(fd); | 
|  | 163 | } | 
|  | 164 | #undef   close | 
|  | 165 | #define  close   ____xxx_close | 
|  | 166 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 167 | // See the comments for the !defined(_WIN32) version of unix_read(). | 
| Spencer Low | 5018406 | 2015-03-01 15:06:21 -0800 | [diff] [blame] | 168 | extern int  unix_read(int  fd, void*  buf, size_t  len); | 
|  | 169 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | #undef   read | 
|  | 171 | #define  read  ___xxx_read | 
|  | 172 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 173 | // 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] | 174 | static __inline__  int  unix_write(int  fd, const void*  buf, size_t  len) | 
|  | 175 | { | 
|  | 176 | return write(fd, buf, len); | 
|  | 177 | } | 
|  | 178 | #undef   write | 
|  | 179 | #define  write  ___xxx_write | 
|  | 180 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 181 | // 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] | 182 | static __inline__ int  adb_open_mode(const char* path, int options, int mode) | 
|  | 183 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 184 | return adb_open(path, options); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 187 | // See the comments for the !defined(_WIN32) version of unix_open(). | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 188 | extern int unix_open(const char* path, int options, ...); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | #define  open    ___xxx_unix_open | 
|  | 190 |  | 
| David Pursell | c5b8ad8 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 191 | // Checks if |fd| corresponds to a console. | 
|  | 192 | // Standard Windows isatty() returns 1 for both console FDs and character | 
|  | 193 | // devices like NUL. unix_isatty() performs some extra checking to only match | 
|  | 194 | // console FDs. | 
|  | 195 | // |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs | 
|  | 196 | // will work but adb_open() FDs will not. Additionally the OS handle associated | 
|  | 197 | // with |fd| must have GENERIC_READ access (which console FDs have by default). | 
|  | 198 | // Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after | 
|  | 199 | // calling this function is unreliable and should not be used. | 
|  | 200 | int unix_isatty(int fd); | 
|  | 201 | #define  isatty  ___xxx_isatty | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 |  | 
|  | 203 | /* normally provided by <cutils/misc.h> */ | 
|  | 204 | extern void*  load_file(const char*  pathname, unsigned*  psize); | 
|  | 205 |  | 
| David 'Digit' Turner | 414ff7d | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 206 | /* normally provided by "fdevent.h" */ | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 |  | 
|  | 208 | #define FDE_READ              0x0001 | 
|  | 209 | #define FDE_WRITE             0x0002 | 
|  | 210 | #define FDE_ERROR             0x0004 | 
|  | 211 | #define FDE_DONT_CLOSE        0x0080 | 
|  | 212 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); | 
|  | 214 |  | 
|  | 215 | fdevent *fdevent_create(int fd, fd_func func, void *arg); | 
|  | 216 | void     fdevent_destroy(fdevent *fde); | 
|  | 217 | void     fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); | 
|  | 218 | void     fdevent_remove(fdevent *item); | 
|  | 219 | void     fdevent_set(fdevent *fde, unsigned events); | 
|  | 220 | void     fdevent_add(fdevent *fde, unsigned events); | 
|  | 221 | void     fdevent_del(fdevent *fde, unsigned events); | 
|  | 222 | void     fdevent_loop(); | 
|  | 223 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | static __inline__ void  adb_sleep_ms( int  mseconds ) | 
|  | 225 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 226 | Sleep( mseconds ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 229 | int network_loopback_client(int port, int type, std::string* error); | 
|  | 230 | int network_loopback_server(int port, int type, std::string* error); | 
|  | 231 | int network_inaddr_any_server(int port, int type, std::string* error); | 
|  | 232 | int network_connect(const std::string& host, int port, int type, int timeout, | 
|  | 233 | std::string* error); | 
|  | 234 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | extern int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen); | 
|  | 236 |  | 
|  | 237 | #undef   accept | 
|  | 238 | #define  accept  ___xxx_accept | 
|  | 239 |  | 
| Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 240 | extern int  adb_setsockopt(int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen); | 
|  | 241 |  | 
|  | 242 | #undef   setsockopt | 
|  | 243 | #define  setsockopt  ___xxx_setsockopt | 
|  | 244 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 245 | static __inline__  int  adb_socket_setbufsize( int   fd, int  bufsize ) | 
|  | 246 | { | 
|  | 247 | int opt = bufsize; | 
| Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 248 | return adb_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const void*)&opt, sizeof(opt)); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | static __inline__ void  disable_tcp_nagle( int  fd ) | 
|  | 252 | { | 
|  | 253 | int  on = 1; | 
|  | 254 | 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] | 255 | } | 
|  | 256 |  | 
|  | 257 | extern int  adb_socketpair( int  sv[2] ); | 
|  | 258 |  | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 259 | 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] | 260 | return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; | 
|  | 261 | } | 
|  | 262 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 263 | // Like strerror(), but for Win32 error codes. | 
|  | 264 | std::string SystemErrorCodeToString(DWORD error_code); | 
|  | 265 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 266 | // We later define a macro mapping 'stat' to 'adb_stat'. This causes: | 
|  | 267 | //   struct stat s; | 
|  | 268 | //   stat(filename, &s); | 
|  | 269 | // To turn into the following: | 
|  | 270 | //   struct adb_stat s; | 
|  | 271 | //   adb_stat(filename, &s); | 
|  | 272 | // To get this to work, we need to make 'struct adb_stat' the same as | 
|  | 273 | // 'struct stat'. Note that this definition of 'struct adb_stat' uses the | 
|  | 274 | // *current* macro definition of stat, so it may actually be inheriting from | 
|  | 275 | // struct _stat32i64 (or some other remapping). | 
|  | 276 | struct adb_stat : public stat {}; | 
|  | 277 |  | 
|  | 278 | static_assert(sizeof(struct adb_stat) == sizeof(struct stat), | 
|  | 279 | "structures should be the same"); | 
|  | 280 |  | 
|  | 281 | extern int adb_stat(const char* f, struct adb_stat* s); | 
|  | 282 |  | 
|  | 283 | // stat is already a macro, undefine it so we can redefine it. | 
|  | 284 | #undef stat | 
|  | 285 | #define stat adb_stat | 
|  | 286 |  | 
|  | 287 | // UTF-8 versions of POSIX APIs. | 
|  | 288 | extern DIR* adb_opendir(const char* dirname); | 
|  | 289 | extern struct dirent* adb_readdir(DIR* dir); | 
|  | 290 | extern int adb_closedir(DIR* dir); | 
|  | 291 |  | 
|  | 292 | extern int adb_utime(const char *, struct utimbuf *); | 
|  | 293 | extern int adb_chmod(const char *, int); | 
|  | 294 |  | 
|  | 295 | extern int adb_vfprintf(FILE *stream, const char *format, va_list ap) | 
|  | 296 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0))); | 
|  | 297 | extern int adb_fprintf(FILE *stream, const char *format, ...) | 
|  | 298 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))); | 
|  | 299 | extern int adb_printf(const char *format, ...) | 
|  | 300 | __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2))); | 
|  | 301 |  | 
|  | 302 | extern int adb_fputs(const char* buf, FILE* stream); | 
|  | 303 | extern int adb_fputc(int ch, FILE* stream); | 
|  | 304 | extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, | 
|  | 305 | FILE* stream); | 
|  | 306 |  | 
|  | 307 | extern FILE* adb_fopen(const char* f, const char* m); | 
|  | 308 |  | 
|  | 309 | extern char* adb_getenv(const char* name); | 
|  | 310 |  | 
|  | 311 | extern char* adb_getcwd(char* buf, int size); | 
|  | 312 |  | 
|  | 313 | // Remap calls to POSIX APIs to our UTF-8 versions. | 
|  | 314 | #define opendir adb_opendir | 
|  | 315 | #define readdir adb_readdir | 
|  | 316 | #define closedir adb_closedir | 
|  | 317 | #define rewinddir rewinddir_utf8_not_yet_implemented | 
|  | 318 | #define telldir telldir_utf8_not_yet_implemented | 
| Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 319 | // Some compiler's C++ headers have members named seekdir, so we can't do the | 
|  | 320 | // macro technique and instead cause a link error if seekdir is called. | 
|  | 321 | inline void seekdir(DIR*, long) { | 
|  | 322 | extern int seekdir_utf8_not_yet_implemented; | 
|  | 323 | seekdir_utf8_not_yet_implemented = 1; | 
|  | 324 | } | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 325 |  | 
|  | 326 | #define utime adb_utime | 
|  | 327 | #define chmod adb_chmod | 
|  | 328 |  | 
|  | 329 | #define vfprintf adb_vfprintf | 
|  | 330 | #define fprintf adb_fprintf | 
|  | 331 | #define printf adb_printf | 
|  | 332 | #define fputs adb_fputs | 
|  | 333 | #define fputc adb_fputc | 
|  | 334 | #define fwrite adb_fwrite | 
|  | 335 |  | 
|  | 336 | #define fopen adb_fopen | 
|  | 337 |  | 
|  | 338 | #define getenv adb_getenv | 
|  | 339 | #define putenv putenv_utf8_not_yet_implemented | 
|  | 340 | #define setenv setenv_utf8_not_yet_implemented | 
|  | 341 | #define unsetenv unsetenv_utf8_not_yet_implemented | 
|  | 342 |  | 
|  | 343 | #define getcwd adb_getcwd | 
|  | 344 |  | 
| Spencer Low | 0a79600 | 2015-10-18 16:45:09 -0700 | [diff] [blame] | 345 | char* adb_strerror(int err); | 
|  | 346 | #define strerror adb_strerror | 
|  | 347 |  | 
| Spencer Low | cf4ff64 | 2015-05-11 01:08:48 -0700 | [diff] [blame] | 348 | // Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be | 
|  | 349 | // passed to main(). | 
|  | 350 | class NarrowArgs { | 
|  | 351 | public: | 
|  | 352 | NarrowArgs(int argc, wchar_t** argv); | 
|  | 353 | ~NarrowArgs(); | 
|  | 354 |  | 
|  | 355 | inline char** data() { | 
|  | 356 | return narrow_args; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | private: | 
|  | 360 | char** narrow_args; | 
|  | 361 | }; | 
|  | 362 |  | 
| Spencer Low | 5c398d2 | 2015-08-08 15:07:07 -0700 | [diff] [blame] | 363 | // Windows HANDLE values only use 32-bits of the type, even on 64-bit machines, | 
|  | 364 | // so they can fit in an int. To convert back, we just need to sign-extend. | 
|  | 365 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx | 
|  | 366 | // Note that this does not make a HANDLE value work with APIs like open(), nor | 
|  | 367 | // does this make a value from open() passable to APIs taking a HANDLE. This | 
|  | 368 | // just lets you take a HANDLE, pass it around as an int, and then use it again | 
|  | 369 | // as a HANDLE. | 
|  | 370 | inline int cast_handle_to_int(const HANDLE h) { | 
|  | 371 | // truncate | 
|  | 372 | return static_cast<int>(reinterpret_cast<INT_PTR>(h)); | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | inline HANDLE cast_int_to_handle(const int fd) { | 
|  | 376 | // sign-extend | 
|  | 377 | return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd)); | 
|  | 378 | } | 
|  | 379 |  | 
| Spencer Low | 2122c7a | 2015-08-26 18:46:09 -0700 | [diff] [blame] | 380 | // Deleter for unique_handle. Adapted from many sources, including: | 
|  | 381 | // http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api | 
|  | 382 | // https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx | 
|  | 383 | class handle_deleter { | 
|  | 384 | public: | 
|  | 385 | typedef HANDLE pointer; | 
|  | 386 |  | 
|  | 387 | void operator()(HANDLE h); | 
|  | 388 | }; | 
|  | 389 |  | 
|  | 390 | // Like std::unique_ptr, but for Windows HANDLE objects that should be | 
|  | 391 | // CloseHandle()'d. Operator bool() only checks if the handle != nullptr, | 
|  | 392 | // but does not check if the handle != INVALID_HANDLE_VALUE. | 
|  | 393 | typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle; | 
|  | 394 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 395 | #else /* !_WIN32 a.k.a. Unix */ | 
|  | 396 |  | 
| David 'Digit' Turner | 414ff7d | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 397 | #include "fdevent.h" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | #include <cutils/misc.h> | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 399 | #include <cutils/sockets.h> | 
| Spencer Low | 8d8126a | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 400 | #include <cutils/threads.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | #include <signal.h> | 
|  | 402 | #include <sys/wait.h> | 
|  | 403 | #include <sys/stat.h> | 
|  | 404 | #include <fcntl.h> | 
|  | 405 |  | 
|  | 406 | #include <pthread.h> | 
|  | 407 | #include <unistd.h> | 
|  | 408 | #include <fcntl.h> | 
|  | 409 | #include <stdarg.h> | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 410 | #include <netdb.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 411 | #include <netinet/in.h> | 
|  | 412 | #include <netinet/tcp.h> | 
|  | 413 | #include <string.h> | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 414 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 416 | #include <string> | 
|  | 417 |  | 
| Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 418 | #define OS_PATH_SEPARATORS "/" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 419 | #define OS_PATH_SEPARATOR '/' | 
|  | 420 | #define OS_PATH_SEPARATOR_STR "/" | 
| Benoit Goby | d5fcafa | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 421 | #define ENV_PATH_SEPARATOR_STR ":" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 |  | 
| Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 423 | static __inline__ bool adb_is_separator(char c) { | 
|  | 424 | return c == '/'; | 
|  | 425 | } | 
|  | 426 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | typedef  pthread_mutex_t          adb_mutex_t; | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 428 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | #define  ADB_MUTEX_INITIALIZER    PTHREAD_MUTEX_INITIALIZER | 
|  | 430 | #define  adb_mutex_init           pthread_mutex_init | 
|  | 431 | #define  adb_mutex_lock           pthread_mutex_lock | 
|  | 432 | #define  adb_mutex_unlock         pthread_mutex_unlock | 
|  | 433 | #define  adb_mutex_destroy        pthread_mutex_destroy | 
|  | 434 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 435 | #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] | 436 |  | 
|  | 437 | #define  adb_cond_t               pthread_cond_t | 
|  | 438 | #define  adb_cond_init            pthread_cond_init | 
|  | 439 | #define  adb_cond_wait            pthread_cond_wait | 
|  | 440 | #define  adb_cond_broadcast       pthread_cond_broadcast | 
|  | 441 | #define  adb_cond_signal          pthread_cond_signal | 
|  | 442 | #define  adb_cond_destroy         pthread_cond_destroy | 
|  | 443 |  | 
| JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 444 | /* declare all mutexes */ | 
|  | 445 | #define  ADB_MUTEX(x)   extern adb_mutex_t  x; | 
|  | 446 | #include "mutex_list.h" | 
|  | 447 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | static __inline__ void  close_on_exec(int  fd) | 
|  | 449 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 450 | fcntl( fd, F_SETFD, FD_CLOEXEC ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 451 | } | 
|  | 452 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 453 | // Open a file and return a file descriptor that may be used with unix_read(), | 
|  | 454 | // unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close(). | 
|  | 455 | // | 
|  | 456 | // On Unix, this is based on open(), so the file descriptor is a real OS file | 
|  | 457 | // descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a | 
|  | 458 | // file descriptor that can only be used with C Runtime APIs (which are wrapped | 
|  | 459 | // by unix_read(), unix_write(), unix_close()). Also, the C Runtime has | 
|  | 460 | // configurable CR/LF translation which defaults to text mode, but is settable | 
|  | 461 | // with _setmode(). | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 462 | static __inline__ int  unix_open(const char*  path, int options,...) | 
|  | 463 | { | 
|  | 464 | if ((options & O_CREAT) == 0) | 
|  | 465 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 466 | return  TEMP_FAILURE_RETRY( open(path, options) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 467 | } | 
|  | 468 | else | 
|  | 469 | { | 
|  | 470 | int      mode; | 
|  | 471 | va_list  args; | 
|  | 472 | va_start( args, options ); | 
|  | 473 | mode = va_arg( args, int ); | 
|  | 474 | va_end( args ); | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 475 | return TEMP_FAILURE_RETRY( open( path, options, mode ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 476 | } | 
|  | 477 | } | 
|  | 478 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 479 | // Similar to the two-argument adb_open(), but takes a mode parameter for file | 
|  | 480 | // creation. See adb_open() for more info. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 481 | static __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode ) | 
|  | 482 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 483 | return TEMP_FAILURE_RETRY( open( pathname, options, mode ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | } | 
|  | 485 |  | 
|  | 486 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 487 | // Open a file and return a file descriptor that may be used with adb_read(), | 
|  | 488 | // adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close(). | 
|  | 489 | // | 
|  | 490 | // On Unix, this is based on open(), but the Windows implementation (in | 
|  | 491 | // sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime | 
|  | 492 | // and its CR/LF translation. The returned file descriptor should be used with | 
|  | 493 | // adb_read(), adb_write(), adb_close(), etc. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 494 | static __inline__ int  adb_open( const char*  pathname, int  options ) | 
|  | 495 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 496 | int  fd = TEMP_FAILURE_RETRY( open( pathname, options ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 497 | if (fd < 0) | 
|  | 498 | return -1; | 
|  | 499 | close_on_exec( fd ); | 
|  | 500 | return fd; | 
|  | 501 | } | 
|  | 502 | #undef   open | 
|  | 503 | #define  open    ___xxx_open | 
|  | 504 |  | 
| Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 505 | static __inline__ int  adb_shutdown(int fd) | 
|  | 506 | { | 
|  | 507 | return shutdown(fd, SHUT_RDWR); | 
|  | 508 | } | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 509 | static __inline__ int  adb_shutdown(int fd, int direction) | 
|  | 510 | { | 
|  | 511 | return shutdown(fd, direction); | 
|  | 512 | } | 
| Mike Lockwood | 8cf0d59 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 513 | #undef   shutdown | 
|  | 514 | #define  shutdown   ____xxx_shutdown | 
|  | 515 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 516 | // Closes a file descriptor that came from adb_open() or adb_open_mode(), but | 
|  | 517 | // not designed to take a file descriptor from unix_open(). See the comments | 
|  | 518 | // for adb_open() for more info. | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | static __inline__ int  adb_close(int fd) | 
|  | 520 | { | 
|  | 521 | return close(fd); | 
|  | 522 | } | 
|  | 523 | #undef   close | 
|  | 524 | #define  close   ____xxx_close | 
|  | 525 |  | 
|  | 526 |  | 
|  | 527 | static __inline__  int  adb_read(int  fd, void*  buf, size_t  len) | 
|  | 528 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 529 | return TEMP_FAILURE_RETRY( read( fd, buf, len ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
|  | 532 | #undef   read | 
|  | 533 | #define  read  ___xxx_read | 
|  | 534 |  | 
|  | 535 | static __inline__  int  adb_write(int  fd, const void*  buf, size_t  len) | 
|  | 536 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 537 | return TEMP_FAILURE_RETRY( write( fd, buf, len ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 538 | } | 
|  | 539 | #undef   write | 
|  | 540 | #define  write  ___xxx_write | 
|  | 541 |  | 
|  | 542 | static __inline__ int   adb_lseek(int  fd, int  pos, int  where) | 
|  | 543 | { | 
|  | 544 | return lseek(fd, pos, where); | 
|  | 545 | } | 
|  | 546 | #undef   lseek | 
|  | 547 | #define  lseek   ___xxx_lseek | 
|  | 548 |  | 
|  | 549 | static __inline__  int    adb_unlink(const char*  path) | 
|  | 550 | { | 
|  | 551 | return  unlink(path); | 
|  | 552 | } | 
|  | 553 | #undef  unlink | 
|  | 554 | #define unlink  ___xxx_unlink | 
|  | 555 |  | 
|  | 556 | static __inline__  int  adb_creat(const char*  path, int  mode) | 
|  | 557 | { | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 558 | int  fd = TEMP_FAILURE_RETRY( creat( path, mode ) ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 |  | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 560 | if ( fd < 0 ) | 
|  | 561 | return -1; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 562 |  | 
|  | 563 | close_on_exec(fd); | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 564 | return fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 565 | } | 
|  | 566 | #undef   creat | 
|  | 567 | #define  creat  ___xxx_creat | 
|  | 568 |  | 
| David Pursell | c5b8ad8 | 2015-10-28 14:29:51 -0700 | [diff] [blame] | 569 | static __inline__ int unix_isatty(int fd) { | 
|  | 570 | return isatty(fd); | 
|  | 571 | } | 
|  | 572 | #define  isatty  ___xxx_isatty | 
|  | 573 |  | 
| Spencer Low | 5200c66 | 2015-07-30 23:07:55 -0700 | [diff] [blame] | 574 | // Helper for network_* functions. | 
|  | 575 | inline int _fd_set_error_str(int fd, std::string* error) { | 
|  | 576 | if (fd == -1) { | 
|  | 577 | *error = strerror(errno); | 
|  | 578 | } | 
|  | 579 | return fd; | 
|  | 580 | } | 
|  | 581 |  | 
|  | 582 | inline int network_loopback_client(int port, int type, std::string* error) { | 
|  | 583 | return _fd_set_error_str(socket_loopback_client(port, type), error); | 
|  | 584 | } | 
|  | 585 |  | 
|  | 586 | inline int network_loopback_server(int port, int type, std::string* error) { | 
|  | 587 | return _fd_set_error_str(socket_loopback_server(port, type), error); | 
|  | 588 | } | 
|  | 589 |  | 
|  | 590 | inline int network_inaddr_any_server(int port, int type, std::string* error) { | 
|  | 591 | return _fd_set_error_str(socket_inaddr_any_server(port, type), error); | 
|  | 592 | } | 
|  | 593 |  | 
|  | 594 | inline int network_local_server(const char *name, int namespace_id, int type, | 
|  | 595 | std::string* error) { | 
|  | 596 | return _fd_set_error_str(socket_local_server(name, namespace_id, type), | 
|  | 597 | error); | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | inline int network_connect(const std::string& host, int port, int type, | 
|  | 601 | int timeout, std::string* error) { | 
|  | 602 | int getaddrinfo_error = 0; | 
|  | 603 | int fd = socket_network_client_timeout(host.c_str(), port, type, timeout, | 
|  | 604 | &getaddrinfo_error); | 
|  | 605 | if (fd != -1) { | 
|  | 606 | return fd; | 
|  | 607 | } | 
|  | 608 | if (getaddrinfo_error != 0) { | 
|  | 609 | *error = gai_strerror(getaddrinfo_error); | 
|  | 610 | } else { | 
|  | 611 | *error = strerror(errno); | 
|  | 612 | } | 
|  | 613 | return -1; | 
|  | 614 | } | 
|  | 615 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 616 | static __inline__ int  adb_socket_accept(int  serverfd, struct sockaddr*  addr, socklen_t  *addrlen) | 
|  | 617 | { | 
| Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 618 | int fd; | 
|  | 619 |  | 
| Kenny Root | 7316741 | 2012-10-12 15:26:45 -0700 | [diff] [blame] | 620 | fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) ); | 
| Benoit Goby | 95ef828 | 2011-02-01 18:57:41 -0800 | [diff] [blame] | 621 | if (fd >= 0) | 
|  | 622 | close_on_exec(fd); | 
|  | 623 |  | 
|  | 624 | return fd; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 625 | } | 
|  | 626 |  | 
|  | 627 | #undef   accept | 
|  | 628 | #define  accept  ___xxx_accept | 
|  | 629 |  | 
| Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 630 | // Operate on a file descriptor returned from unix_open() or a well-known file | 
|  | 631 | // descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO. | 
|  | 632 | // | 
|  | 633 | // On Unix, unix_read(), unix_write(), unix_close() map to adb_read(), | 
|  | 634 | // adb_write(), adb_close() (which all map to Unix system calls), but the | 
|  | 635 | // Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call | 
|  | 636 | // into the C Runtime and its configurable CR/LF translation (which is settable | 
|  | 637 | // via _setmode()). | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 638 | #define  unix_read   adb_read | 
|  | 639 | #define  unix_write  adb_write | 
|  | 640 | #define  unix_close  adb_close | 
|  | 641 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 642 | typedef void*  (*adb_thread_func_t)( void*  arg ); | 
|  | 643 |  | 
| Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 644 | static __inline__ bool adb_thread_create(adb_thread_func_t start, void* arg) { | 
|  | 645 | pthread_attr_t attr; | 
|  | 646 | pthread_attr_init(&attr); | 
|  | 647 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 648 |  | 
| Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 649 | pthread_t thread; | 
|  | 650 | errno = pthread_create(&thread, &attr, start, arg); | 
|  | 651 | return (errno == 0); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 652 | } | 
|  | 653 |  | 
| Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 654 | static __inline__ int adb_thread_setname(const std::string& name) { | 
|  | 655 | #ifdef __APPLE__ | 
|  | 656 | return pthread_setname_np(name.c_str()); | 
|  | 657 | #else | 
|  | 658 | const char *s = name.c_str(); | 
|  | 659 |  | 
|  | 660 | // pthread_setname_np fails rather than truncating long strings. | 
|  | 661 | const int max_task_comm_len = 16; // including the null terminator | 
|  | 662 | if (name.length() > (max_task_comm_len - 1)) { | 
|  | 663 | char buf[max_task_comm_len]; | 
|  | 664 | strncpy(buf, name.c_str(), sizeof(buf) - 1); | 
|  | 665 | buf[sizeof(buf) - 1] = '\0'; | 
|  | 666 | s = buf; | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | return pthread_setname_np(pthread_self(), s) ; | 
|  | 670 | #endif | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 | static __inline__  int  adb_socket_setbufsize(int fd, int  bufsize ) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 674 | { | 
|  | 675 | int opt = bufsize; | 
|  | 676 | return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); | 
|  | 677 | } | 
|  | 678 |  | 
|  | 679 | static __inline__ void  disable_tcp_nagle(int fd) | 
|  | 680 | { | 
|  | 681 | int  on = 1; | 
|  | 682 | setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) ); | 
|  | 683 | } | 
|  | 684 |  | 
| Spencer Low | f055c19 | 2015-01-25 14:40:16 -0800 | [diff] [blame] | 685 | static __inline__ int  adb_setsockopt( int  fd, int  level, int  optname, const void*  optval, socklen_t  optlen ) | 
|  | 686 | { | 
|  | 687 | return setsockopt( fd, level, optname, optval, optlen ); | 
|  | 688 | } | 
|  | 689 |  | 
|  | 690 | #undef   setsockopt | 
|  | 691 | #define  setsockopt  ___xxx_setsockopt | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 692 |  | 
|  | 693 | static __inline__ int  unix_socketpair( int  d, int  type, int  protocol, int sv[2] ) | 
|  | 694 | { | 
|  | 695 | return socketpair( d, type, protocol, sv ); | 
|  | 696 | } | 
|  | 697 |  | 
|  | 698 | static __inline__ int  adb_socketpair( int  sv[2] ) | 
|  | 699 | { | 
|  | 700 | int  rc; | 
|  | 701 |  | 
|  | 702 | rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); | 
|  | 703 | if (rc < 0) | 
|  | 704 | return -1; | 
|  | 705 |  | 
|  | 706 | close_on_exec( sv[0] ); | 
|  | 707 | close_on_exec( sv[1] ); | 
|  | 708 | return 0; | 
|  | 709 | } | 
|  | 710 |  | 
|  | 711 | #undef   socketpair | 
|  | 712 | #define  socketpair   ___xxx_socketpair | 
|  | 713 |  | 
|  | 714 | static __inline__ void  adb_sleep_ms( int  mseconds ) | 
|  | 715 | { | 
| David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 716 | usleep( mseconds*1000 ); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 717 | } | 
|  | 718 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 719 | 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] | 720 | { | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 721 | return mkdir(path.c_str(), mode); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 722 | } | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 723 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 724 | #undef   mkdir | 
|  | 725 | #define  mkdir  ___xxx_mkdir | 
|  | 726 |  | 
|  | 727 | static __inline__ void  adb_sysdeps_init(void) | 
|  | 728 | { | 
|  | 729 | } | 
|  | 730 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 731 | 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] | 732 | return path[0] == '/'; | 
|  | 733 | } | 
|  | 734 |  | 
| leozwang | cbf0267 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 735 | static __inline__ unsigned long adb_thread_id() | 
|  | 736 | { | 
| Spencer Low | 8d8126a | 2015-07-21 02:06:26 -0700 | [diff] [blame] | 737 | return (unsigned long)gettid(); | 
| leozwang | cbf0267 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 738 | } | 
|  | 739 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 740 | #endif /* !_WIN32 */ | 
|  | 741 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 742 | #endif /* _ADB_SYSDEPS_H */ |