blob: 307be6d58489f483a6f2455cce19c83cf4de0715 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Hughes381cfa92015-07-23 17:12:58 -070027#include <errno.h>
28
Spencer Low5200c662015-07-30 23:07:55 -070029#include <string>
Spencer Lowa30b79a2015-11-15 16:29:36 -080030#include <vector>
Spencer Low5200c662015-07-30 23:07:55 -070031
Josh Gao13ea01d2016-05-13 14:52:06 -070032// Include this before open/close/unlink are defined as macros below.
Josh Gao3b3e10d2016-02-09 14:59:09 -080033#include <android-base/errors.h>
Elliott Hughes3df2fa62017-11-28 18:05:27 -080034#include <android-base/macros.h>
Josh Gao13ea01d2016-05-13 14:52:06 -070035#include <android-base/unique_fd.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080036#include <android-base/utf8.h>
Spencer Lowd21dc822015-11-12 15:20:15 -080037
Josh Gaoa3577e12016-12-05 13:24:48 -080038#include "sysdeps/errno.h"
Josh Gao46de1d72017-04-12 13:57:06 -070039#include "sysdeps/network.h"
Josh Gaof551ea02016-07-28 18:09:48 -070040#include "sysdeps/stat.h"
41
Spencer Lowcf4ff642015-05-11 01:08:48 -070042// Some printf-like functions are implemented in terms of
43// android::base::StringAppendV, so they should use the same attribute for
44// compile-time format string checking. On Windows, if the mingw version of
45// vsnprintf is used in StringAppendV, use `gnu_printf' which allows z in %zd
46// and PRIu64 (and related) to be recognized by the compile-time checking.
47#define ADB_FORMAT_ARCHETYPE __printf__
48#ifdef __USE_MINGW_ANSI_STDIO
49#if __USE_MINGW_ANSI_STDIO
50#undef ADB_FORMAT_ARCHETYPE
51#define ADB_FORMAT_ARCHETYPE gnu_printf
52#endif
53#endif
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055#ifdef _WIN32
56
Josh Gaoa166e712016-01-29 12:08:34 -080057// Clang-only nullability specifiers
58#define _Nonnull
59#define _Nullable
60
Dan Albert630b9af2014-11-24 23:34:35 -080061#include <ctype.h>
62#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070063#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080064#include <errno.h>
65#include <fcntl.h>
66#include <io.h>
67#include <process.h>
68#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070069#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070071#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080073
Spencer Low2122c7a2015-08-26 18:46:09 -070074#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080075#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040076
Dan Albert630b9af2014-11-24 23:34:35 -080077#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Elliott Hughes5c742702015-07-30 17:42:01 -070079#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080#define OS_PATH_SEPARATOR '\\'
81#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070082#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083
Josh Gao07db1192015-11-07 15:38:19 -080084static __inline__ bool adb_is_separator(char c) {
85 return c == '\\' || c == '/';
86}
87
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070088static __inline__ int adb_thread_setname(const std::string& name) {
89 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
90 // the thread name in Windows. Unfortunately, it only works during debugging, but
91 // our build process doesn't generate PDB files needed for debugging.
92 return 0;
93}
94
leozwangcbf02672014-08-15 09:51:27 -070095static __inline__ unsigned long adb_thread_id()
96{
97 return GetCurrentThreadId();
98}
99
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100static __inline__ void close_on_exec(int fd)
101{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200102 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103}
104
Spencer Lowcf4ff642015-05-11 01:08:48 -0700105extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106#undef unlink
107#define unlink ___xxx_unlink
108
Spencer Lowcf4ff642015-05-11 01:08:48 -0700109extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110#undef mkdir
111#define mkdir ___xxx_mkdir
112
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700113// See the comments for the !defined(_WIN32) versions of adb_*().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114extern int adb_open(const char* path, int options);
115extern int adb_creat(const char* path, int mode);
116extern int adb_read(int fd, void* buf, int len);
117extern int adb_write(int fd, const void* buf, int len);
118extern int adb_lseek(int fd, int pos, int where);
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400119extern int adb_shutdown(int fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120extern int adb_close(int fd);
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700121extern int adb_register_socket(SOCKET s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700123// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124static __inline__ int unix_close(int fd)
125{
126 return close(fd);
127}
128#undef close
129#define close ____xxx_close
130
Spencer Low2e02dc62015-11-07 17:34:39 -0800131// Like unix_read(), but may return EINTR.
132extern int unix_read_interruptible(int fd, void* buf, size_t len);
133
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700134// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800135static __inline__ int unix_read(int fd, void* buf, size_t len) {
136 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
137}
Spencer Low50184062015-03-01 15:06:21 -0800138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139#undef read
140#define read ___xxx_read
141
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700142// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143static __inline__ int unix_write(int fd, const void* buf, size_t len)
144{
145 return write(fd, buf, len);
146}
147#undef write
148#define write ___xxx_write
149
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700150// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151static __inline__ int adb_open_mode(const char* path, int options, int mode)
152{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200153 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
155
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700156// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700157extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158#define open ___xxx_unix_open
159
David Pursellc5b8ad82015-10-28 14:29:51 -0700160// Checks if |fd| corresponds to a console.
161// Standard Windows isatty() returns 1 for both console FDs and character
162// devices like NUL. unix_isatty() performs some extra checking to only match
163// console FDs.
164// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
165// will work but adb_open() FDs will not. Additionally the OS handle associated
166// with |fd| must have GENERIC_READ access (which console FDs have by default).
167// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
168// calling this function is unreliable and should not be used.
169int unix_isatty(int fd);
170#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Spencer Low5200c662015-07-30 23:07:55 -0700172int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700173
174inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
175 abort();
176}
177
178inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
179 abort();
180}
181
Spencer Low5200c662015-07-30 23:07:55 -0700182int network_connect(const std::string& host, int port, int type, int timeout,
183 std::string* error);
184
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
186
187#undef accept
188#define accept ___xxx_accept
189
David Purselleaae97e2016-04-07 11:25:48 -0700190// Returns the local port number of a bound socket, or -1 on failure.
191int adb_socket_get_local_port(int fd);
192
Spencer Lowf055c192015-01-25 14:40:16 -0800193extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
194
195#undef setsockopt
196#define setsockopt ___xxx_setsockopt
197
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198extern int adb_socketpair( int sv[2] );
199
Josh Gao3777d2e2016-02-16 17:34:53 -0800200struct adb_pollfd {
201 int fd;
202 short events;
203 short revents;
204};
205extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
206#define poll ___xxx_poll
207
Elliott Hughes5c742702015-07-30 17:42:01 -0700208static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
210}
211
Spencer Lowcf4ff642015-05-11 01:08:48 -0700212// UTF-8 versions of POSIX APIs.
213extern DIR* adb_opendir(const char* dirname);
214extern struct dirent* adb_readdir(DIR* dir);
215extern int adb_closedir(DIR* dir);
216
217extern int adb_utime(const char *, struct utimbuf *);
218extern int adb_chmod(const char *, int);
219
220extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
221 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
Spencer Lowa30b79a2015-11-15 16:29:36 -0800222extern int adb_vprintf(const char *format, va_list ap)
223 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700224extern int adb_fprintf(FILE *stream, const char *format, ...)
225 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
226extern int adb_printf(const char *format, ...)
227 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
228
229extern int adb_fputs(const char* buf, FILE* stream);
230extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800231extern int adb_putchar(int ch);
232extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700233extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
234 FILE* stream);
235
236extern FILE* adb_fopen(const char* f, const char* m);
237
238extern char* adb_getenv(const char* name);
239
240extern char* adb_getcwd(char* buf, int size);
241
242// Remap calls to POSIX APIs to our UTF-8 versions.
243#define opendir adb_opendir
244#define readdir adb_readdir
245#define closedir adb_closedir
246#define rewinddir rewinddir_utf8_not_yet_implemented
247#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800248// Some compiler's C++ headers have members named seekdir, so we can't do the
249// macro technique and instead cause a link error if seekdir is called.
250inline void seekdir(DIR*, long) {
251 extern int seekdir_utf8_not_yet_implemented;
252 seekdir_utf8_not_yet_implemented = 1;
253}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700254
255#define utime adb_utime
256#define chmod adb_chmod
257
258#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800259#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700260#define fprintf adb_fprintf
261#define printf adb_printf
262#define fputs adb_fputs
263#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800264// putc may be a macro, so if so, undefine it, so that we can redefine it.
265#undef putc
266#define putc(c, s) adb_fputc(c, s)
267#define putchar adb_putchar
268#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700269#define fwrite adb_fwrite
270
271#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800272#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700273
274#define getenv adb_getenv
275#define putenv putenv_utf8_not_yet_implemented
276#define setenv setenv_utf8_not_yet_implemented
277#define unsetenv unsetenv_utf8_not_yet_implemented
278
279#define getcwd adb_getcwd
280
Spencer Lowcf4ff642015-05-11 01:08:48 -0700281// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
282// passed to main().
283class NarrowArgs {
284public:
285 NarrowArgs(int argc, wchar_t** argv);
286 ~NarrowArgs();
287
288 inline char** data() {
289 return narrow_args;
290 }
291
292private:
293 char** narrow_args;
294};
295
Spencer Low5c398d22015-08-08 15:07:07 -0700296// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
297// so they can fit in an int. To convert back, we just need to sign-extend.
298// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
299// Note that this does not make a HANDLE value work with APIs like open(), nor
300// does this make a value from open() passable to APIs taking a HANDLE. This
301// just lets you take a HANDLE, pass it around as an int, and then use it again
302// as a HANDLE.
303inline int cast_handle_to_int(const HANDLE h) {
304 // truncate
305 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
306}
307
308inline HANDLE cast_int_to_handle(const int fd) {
309 // sign-extend
310 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
311}
312
Spencer Low2122c7a2015-08-26 18:46:09 -0700313// Deleter for unique_handle. Adapted from many sources, including:
314// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
315// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
316class handle_deleter {
317public:
318 typedef HANDLE pointer;
319
320 void operator()(HANDLE h);
321};
322
323// Like std::unique_ptr, but for Windows HANDLE objects that should be
324// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
325// but does not check if the handle != INVALID_HANDLE_VALUE.
326typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
327
Spencer Lowa30b79a2015-11-15 16:29:36 -0800328namespace internal {
329
330size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
331
332}
333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334#else /* !_WIN32 a.k.a. Unix */
335
Spencer Low5200c662015-07-30 23:07:55 -0700336#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700337#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338#include <fcntl.h>
Josh Gao3777d2e2016-02-16 17:34:53 -0800339#include <poll.h>
340#include <signal.h>
341#include <sys/stat.h>
342#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343
344#include <pthread.h>
345#include <unistd.h>
346#include <fcntl.h>
347#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700348#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349#include <netinet/in.h>
350#include <netinet/tcp.h>
351#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700352#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353
Alex Vallée47d67c92015-05-06 16:26:00 -0400354#include <string>
355
Elliott Hughes5c742702015-07-30 17:42:01 -0700356#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357#define OS_PATH_SEPARATOR '/'
358#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700359#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360
Josh Gao07db1192015-11-07 15:38:19 -0800361static __inline__ bool adb_is_separator(char c) {
362 return c == '/';
363}
364
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365static __inline__ void close_on_exec(int fd)
366{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200367 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368}
369
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700370// Open a file and return a file descriptor that may be used with unix_read(),
371// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
372//
373// On Unix, this is based on open(), so the file descriptor is a real OS file
374// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
375// file descriptor that can only be used with C Runtime APIs (which are wrapped
376// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
377// configurable CR/LF translation which defaults to text mode, but is settable
378// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379static __inline__ int unix_open(const char* path, int options,...)
380{
381 if ((options & O_CREAT) == 0)
382 {
Kenny Root73167412012-10-12 15:26:45 -0700383 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 }
385 else
386 {
387 int mode;
388 va_list args;
389 va_start( args, options );
390 mode = va_arg( args, int );
391 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700392 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 }
394}
395
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700396// Similar to the two-argument adb_open(), but takes a mode parameter for file
397// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
399{
Kenny Root73167412012-10-12 15:26:45 -0700400 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401}
402
403
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700404// Open a file and return a file descriptor that may be used with adb_read(),
405// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
406//
407// On Unix, this is based on open(), but the Windows implementation (in
408// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
409// and its CR/LF translation. The returned file descriptor should be used with
410// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411static __inline__ int adb_open( const char* pathname, int options )
412{
Kenny Root73167412012-10-12 15:26:45 -0700413 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 if (fd < 0)
415 return -1;
416 close_on_exec( fd );
417 return fd;
418}
419#undef open
420#define open ___xxx_open
421
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400422static __inline__ int adb_shutdown(int fd)
423{
424 return shutdown(fd, SHUT_RDWR);
425}
David Pursell1ed57f02015-10-06 15:30:03 -0700426static __inline__ int adb_shutdown(int fd, int direction)
427{
428 return shutdown(fd, direction);
429}
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400430#undef shutdown
431#define shutdown ____xxx_shutdown
432
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700433// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
434// not designed to take a file descriptor from unix_open(). See the comments
435// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800436__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 return close(fd);
438}
439#undef close
440#define close ____xxx_close
441
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700442// On Windows, ADB has an indirection layer for file descriptors. If we get a
443// Win32 SOCKET object from an external library, we have to map it in to that
444// indirection layer, which this does.
445__inline__ int adb_register_socket(int s) {
446 return s;
447}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
449static __inline__ int adb_read(int fd, void* buf, size_t len)
450{
Kenny Root73167412012-10-12 15:26:45 -0700451 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452}
453
Spencer Low2e02dc62015-11-07 17:34:39 -0800454// Like unix_read(), but does not handle EINTR.
455static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
456 return read(fd, buf, len);
457}
458
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459#undef read
460#define read ___xxx_read
461
462static __inline__ int adb_write(int fd, const void* buf, size_t len)
463{
Kenny Root73167412012-10-12 15:26:45 -0700464 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465}
466#undef write
467#define write ___xxx_write
468
469static __inline__ int adb_lseek(int fd, int pos, int where)
470{
471 return lseek(fd, pos, where);
472}
473#undef lseek
474#define lseek ___xxx_lseek
475
476static __inline__ int adb_unlink(const char* path)
477{
478 return unlink(path);
479}
480#undef unlink
481#define unlink ___xxx_unlink
482
483static __inline__ int adb_creat(const char* path, int mode)
484{
Kenny Root73167412012-10-12 15:26:45 -0700485 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200487 if ( fd < 0 )
488 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489
490 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200491 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492}
493#undef creat
494#define creat ___xxx_creat
495
David Pursellc5b8ad82015-10-28 14:29:51 -0700496static __inline__ int unix_isatty(int fd) {
497 return isatty(fd);
498}
499#define isatty ___xxx_isatty
500
Spencer Low5200c662015-07-30 23:07:55 -0700501// Helper for network_* functions.
502inline int _fd_set_error_str(int fd, std::string* error) {
503 if (fd == -1) {
504 *error = strerror(errno);
505 }
506 return fd;
507}
508
Spencer Low5200c662015-07-30 23:07:55 -0700509inline int network_inaddr_any_server(int port, int type, std::string* error) {
510 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
511}
512
Josh Gaocfb21412016-08-24 18:38:44 -0700513inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
514 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
515}
516
517inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
518 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700519}
520
521inline int network_connect(const std::string& host, int port, int type,
522 int timeout, std::string* error) {
523 int getaddrinfo_error = 0;
524 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
525 &getaddrinfo_error);
526 if (fd != -1) {
527 return fd;
528 }
529 if (getaddrinfo_error != 0) {
530 *error = gai_strerror(getaddrinfo_error);
531 } else {
532 *error = strerror(errno);
533 }
534 return -1;
535}
536
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
538{
Benoit Goby95ef8282011-02-01 18:57:41 -0800539 int fd;
540
Kenny Root73167412012-10-12 15:26:45 -0700541 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800542 if (fd >= 0)
543 close_on_exec(fd);
544
545 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546}
547
548#undef accept
549#define accept ___xxx_accept
550
David Purselleaae97e2016-04-07 11:25:48 -0700551inline int adb_socket_get_local_port(int fd) {
552 return socket_get_local_port(fd);
553}
554
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700555// Operate on a file descriptor returned from unix_open() or a well-known file
556// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
557//
558// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
559// adb_write(), adb_close() (which all map to Unix system calls), but the
560// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
561// into the C Runtime and its configurable CR/LF translation (which is settable
562// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563#define unix_read adb_read
564#define unix_write adb_write
565#define unix_close adb_close
566
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700567static __inline__ int adb_thread_setname(const std::string& name) {
568#ifdef __APPLE__
569 return pthread_setname_np(name.c_str());
570#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700571 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
572 // glibc doesn't have strlcpy, so we have to fake it.
573 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
574 strncpy(buf, name.c_str(), sizeof(buf) - 1);
575 buf[sizeof(buf) - 1] = '\0';
576 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700577#endif
578}
579
Spencer Lowf055c192015-01-25 14:40:16 -0800580static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
581{
582 return setsockopt( fd, level, optname, optval, optlen );
583}
584
585#undef setsockopt
586#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587
588static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
589{
590 return socketpair( d, type, protocol, sv );
591}
592
593static __inline__ int adb_socketpair( int sv[2] )
594{
595 int rc;
596
597 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
598 if (rc < 0)
599 return -1;
600
601 close_on_exec( sv[0] );
602 close_on_exec( sv[1] );
603 return 0;
604}
605
606#undef socketpair
607#define socketpair ___xxx_socketpair
608
Josh Gao3777d2e2016-02-16 17:34:53 -0800609typedef struct pollfd adb_pollfd;
610static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
611 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
612}
613
614#define poll ___xxx_poll
615
Alex Vallée47d67c92015-05-06 16:26:00 -0400616static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617{
Alex Vallée47d67c92015-05-06 16:26:00 -0400618 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619}
Alex Vallée47d67c92015-05-06 16:26:00 -0400620
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621#undef mkdir
622#define mkdir ___xxx_mkdir
623
Alex Vallée47d67c92015-05-06 16:26:00 -0400624static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800625 return path[0] == '/';
626}
627
leozwangcbf02672014-08-15 09:51:27 -0700628static __inline__ unsigned long adb_thread_id()
629{
Spencer Low8d8126a2015-07-21 02:06:26 -0700630 return (unsigned long)gettid();
leozwangcbf02672014-08-15 09:51:27 -0700631}
632
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633#endif /* !_WIN32 */
634
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800635static inline void disable_tcp_nagle(int fd) {
636 int off = 1;
637 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
638}
639
David Pursellbfd95032016-02-22 14:27:23 -0800640// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
641// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
642// configured to drop after 10 missed keepalives. Returns true on success.
643bool set_tcp_keepalive(int fd, int interval_sec);
644
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700645#if defined(_WIN32)
646// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
647#undef ERROR
648#endif
649
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650#endif /* _ADB_SYSDEPS_H */