blob: fe0ecd6ea6e8f94a522dd6c9dc35bbfb81445752 [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
Pirama Arumuga Nainara2df1ef2018-06-25 11:48:58 -070044// compile-time format string checking.
Spencer Lowcf4ff642015-05-11 01:08:48 -070045#define ADB_FORMAT_ARCHETYPE __printf__
Spencer Lowcf4ff642015-05-11 01:08:48 -070046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#ifdef _WIN32
48
Josh Gaoa166e712016-01-29 12:08:34 -080049// Clang-only nullability specifiers
50#define _Nonnull
51#define _Nullable
52
Dan Albert630b9af2014-11-24 23:34:35 -080053#include <ctype.h>
54#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070055#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080056#include <errno.h>
57#include <fcntl.h>
58#include <io.h>
59#include <process.h>
60#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070061#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070063#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080065
Spencer Low2122c7a2015-08-26 18:46:09 -070066#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080067#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040068
Dan Albert630b9af2014-11-24 23:34:35 -080069#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Elliott Hughes5c742702015-07-30 17:42:01 -070071#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072#define OS_PATH_SEPARATOR '\\'
73#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070074#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Josh Gao07db1192015-11-07 15:38:19 -080076static __inline__ bool adb_is_separator(char c) {
77 return c == '\\' || c == '/';
78}
79
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070080static __inline__ int adb_thread_setname(const std::string& name) {
81 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
82 // the thread name in Windows. Unfortunately, it only works during debugging, but
83 // our build process doesn't generate PDB files needed for debugging.
84 return 0;
85}
86
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087static __inline__ void close_on_exec(int fd)
88{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020089 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090}
91
Spencer Lowcf4ff642015-05-11 01:08:48 -070092extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093#undef unlink
94#define unlink ___xxx_unlink
95
Spencer Lowcf4ff642015-05-11 01:08:48 -070096extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097#undef mkdir
98#define mkdir ___xxx_mkdir
99
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700100// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -0700101extern int adb_open(const char* path, int options);
102extern int adb_creat(const char* path, int mode);
103extern int adb_read(int fd, void* buf, int len);
104extern int adb_write(int fd, const void* buf, int len);
105extern int adb_lseek(int fd, int pos, int where);
106extern int adb_shutdown(int fd, int direction = SHUT_RDWR);
107extern int adb_close(int fd);
108extern int adb_register_socket(SOCKET s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700110// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111static __inline__ int unix_close(int fd)
112{
113 return close(fd);
114}
115#undef close
116#define close ____xxx_close
117
Spencer Low2e02dc62015-11-07 17:34:39 -0800118// Like unix_read(), but may return EINTR.
119extern int unix_read_interruptible(int fd, void* buf, size_t len);
120
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700121// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800122static __inline__ int unix_read(int fd, void* buf, size_t len) {
123 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
124}
Spencer Low50184062015-03-01 15:06:21 -0800125
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126#undef read
127#define read ___xxx_read
128
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700129// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130static __inline__ int unix_write(int fd, const void* buf, size_t len)
131{
132 return write(fd, buf, len);
133}
134#undef write
135#define write ___xxx_write
136
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700137// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138static __inline__ int adb_open_mode(const char* path, int options, int mode)
139{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200140 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141}
142
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700143// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700144extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145#define open ___xxx_unix_open
146
David Pursellc5b8ad82015-10-28 14:29:51 -0700147// Checks if |fd| corresponds to a console.
148// Standard Windows isatty() returns 1 for both console FDs and character
149// devices like NUL. unix_isatty() performs some extra checking to only match
150// console FDs.
151// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
152// will work but adb_open() FDs will not. Additionally the OS handle associated
153// with |fd| must have GENERIC_READ access (which console FDs have by default).
154// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
155// calling this function is unreliable and should not be used.
156int unix_isatty(int fd);
157#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158
Spencer Low5200c662015-07-30 23:07:55 -0700159int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700160
161inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
162 abort();
163}
164
165inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
166 abort();
167}
168
Spencer Low5200c662015-07-30 23:07:55 -0700169int network_connect(const std::string& host, int port, int type, int timeout,
170 std::string* error);
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
173
174#undef accept
175#define accept ___xxx_accept
176
David Purselleaae97e2016-04-07 11:25:48 -0700177// Returns the local port number of a bound socket, or -1 on failure.
178int adb_socket_get_local_port(int fd);
179
Spencer Lowf055c192015-01-25 14:40:16 -0800180extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
181
182#undef setsockopt
183#define setsockopt ___xxx_setsockopt
184
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185extern int adb_socketpair( int sv[2] );
186
Josh Gao3777d2e2016-02-16 17:34:53 -0800187struct adb_pollfd {
188 int fd;
189 short events;
190 short revents;
191};
192extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
193#define poll ___xxx_poll
194
Elliott Hughes5c742702015-07-30 17:42:01 -0700195static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
197}
198
Spencer Lowcf4ff642015-05-11 01:08:48 -0700199// UTF-8 versions of POSIX APIs.
200extern DIR* adb_opendir(const char* dirname);
201extern struct dirent* adb_readdir(DIR* dir);
202extern int adb_closedir(DIR* dir);
203
204extern int adb_utime(const char *, struct utimbuf *);
205extern int adb_chmod(const char *, int);
206
207extern int adb_vfprintf(FILE *stream, const char *format, va_list ap)
208 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 0)));
Spencer Lowa30b79a2015-11-15 16:29:36 -0800209extern int adb_vprintf(const char *format, va_list ap)
210 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 0)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700211extern int adb_fprintf(FILE *stream, const char *format, ...)
212 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3)));
213extern int adb_printf(const char *format, ...)
214 __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 1, 2)));
215
216extern int adb_fputs(const char* buf, FILE* stream);
217extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800218extern int adb_putchar(int ch);
219extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700220extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
221 FILE* stream);
222
223extern FILE* adb_fopen(const char* f, const char* m);
224
225extern char* adb_getenv(const char* name);
226
227extern char* adb_getcwd(char* buf, int size);
228
229// Remap calls to POSIX APIs to our UTF-8 versions.
230#define opendir adb_opendir
231#define readdir adb_readdir
232#define closedir adb_closedir
233#define rewinddir rewinddir_utf8_not_yet_implemented
234#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800235// Some compiler's C++ headers have members named seekdir, so we can't do the
236// macro technique and instead cause a link error if seekdir is called.
237inline void seekdir(DIR*, long) {
238 extern int seekdir_utf8_not_yet_implemented;
239 seekdir_utf8_not_yet_implemented = 1;
240}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700241
242#define utime adb_utime
243#define chmod adb_chmod
244
245#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800246#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700247#define fprintf adb_fprintf
248#define printf adb_printf
249#define fputs adb_fputs
250#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800251// putc may be a macro, so if so, undefine it, so that we can redefine it.
252#undef putc
253#define putc(c, s) adb_fputc(c, s)
254#define putchar adb_putchar
255#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700256#define fwrite adb_fwrite
257
258#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800259#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700260
261#define getenv adb_getenv
262#define putenv putenv_utf8_not_yet_implemented
263#define setenv setenv_utf8_not_yet_implemented
264#define unsetenv unsetenv_utf8_not_yet_implemented
265
266#define getcwd adb_getcwd
267
Spencer Lowcf4ff642015-05-11 01:08:48 -0700268// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
269// passed to main().
270class NarrowArgs {
271public:
272 NarrowArgs(int argc, wchar_t** argv);
273 ~NarrowArgs();
274
275 inline char** data() {
276 return narrow_args;
277 }
278
279private:
280 char** narrow_args;
281};
282
Spencer Low5c398d22015-08-08 15:07:07 -0700283// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
284// so they can fit in an int. To convert back, we just need to sign-extend.
285// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
286// Note that this does not make a HANDLE value work with APIs like open(), nor
287// does this make a value from open() passable to APIs taking a HANDLE. This
288// just lets you take a HANDLE, pass it around as an int, and then use it again
289// as a HANDLE.
290inline int cast_handle_to_int(const HANDLE h) {
291 // truncate
292 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
293}
294
295inline HANDLE cast_int_to_handle(const int fd) {
296 // sign-extend
297 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
298}
299
Spencer Low2122c7a2015-08-26 18:46:09 -0700300// Deleter for unique_handle. Adapted from many sources, including:
301// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
302// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
303class handle_deleter {
304public:
305 typedef HANDLE pointer;
306
307 void operator()(HANDLE h);
308};
309
310// Like std::unique_ptr, but for Windows HANDLE objects that should be
311// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
312// but does not check if the handle != INVALID_HANDLE_VALUE.
313typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
314
Spencer Lowa30b79a2015-11-15 16:29:36 -0800315namespace internal {
316
317size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
318
319}
320
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321#else /* !_WIN32 a.k.a. Unix */
322
Spencer Low5200c662015-07-30 23:07:55 -0700323#include <cutils/sockets.h>
Spencer Low8d8126a2015-07-21 02:06:26 -0700324#include <cutils/threads.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325#include <fcntl.h>
Josh Gao3777d2e2016-02-16 17:34:53 -0800326#include <poll.h>
327#include <signal.h>
328#include <sys/stat.h>
329#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330
331#include <pthread.h>
332#include <unistd.h>
333#include <fcntl.h>
334#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700335#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336#include <netinet/in.h>
337#include <netinet/tcp.h>
338#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700339#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340
Alex Vallée47d67c92015-05-06 16:26:00 -0400341#include <string>
342
Elliott Hughes5c742702015-07-30 17:42:01 -0700343#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344#define OS_PATH_SEPARATOR '/'
345#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700346#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347
Josh Gao07db1192015-11-07 15:38:19 -0800348static __inline__ bool adb_is_separator(char c) {
349 return c == '/';
350}
351
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352static __inline__ void close_on_exec(int fd)
353{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200354 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355}
356
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700357// Open a file and return a file descriptor that may be used with unix_read(),
358// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
359//
360// On Unix, this is based on open(), so the file descriptor is a real OS file
361// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
362// file descriptor that can only be used with C Runtime APIs (which are wrapped
363// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
364// configurable CR/LF translation which defaults to text mode, but is settable
365// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366static __inline__ int unix_open(const char* path, int options,...)
367{
368 if ((options & O_CREAT) == 0)
369 {
Kenny Root73167412012-10-12 15:26:45 -0700370 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 }
372 else
373 {
374 int mode;
375 va_list args;
376 va_start( args, options );
377 mode = va_arg( args, int );
378 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700379 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 }
381}
382
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700383// Similar to the two-argument adb_open(), but takes a mode parameter for file
384// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
386{
Kenny Root73167412012-10-12 15:26:45 -0700387 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388}
389
390
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700391// Open a file and return a file descriptor that may be used with adb_read(),
392// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
393//
394// On Unix, this is based on open(), but the Windows implementation (in
395// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
396// and its CR/LF translation. The returned file descriptor should be used with
397// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398static __inline__ int adb_open( const char* pathname, int options )
399{
Kenny Root73167412012-10-12 15:26:45 -0700400 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 if (fd < 0)
402 return -1;
403 close_on_exec( fd );
404 return fd;
405}
406#undef open
407#define open ___xxx_open
408
Josh Gao2e1e7892018-03-23 13:03:28 -0700409static __inline__ int adb_shutdown(int fd, int direction = SHUT_RDWR) {
David Pursell1ed57f02015-10-06 15:30:03 -0700410 return shutdown(fd, direction);
411}
Josh Gao2e1e7892018-03-23 13:03:28 -0700412
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400413#undef shutdown
414#define shutdown ____xxx_shutdown
415
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700416// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
417// not designed to take a file descriptor from unix_open(). See the comments
418// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800419__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 return close(fd);
421}
422#undef close
423#define close ____xxx_close
424
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700425// On Windows, ADB has an indirection layer for file descriptors. If we get a
426// Win32 SOCKET object from an external library, we have to map it in to that
427// indirection layer, which this does.
428__inline__ int adb_register_socket(int s) {
429 return s;
430}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431
432static __inline__ int adb_read(int fd, void* buf, size_t len)
433{
Kenny Root73167412012-10-12 15:26:45 -0700434 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435}
436
Spencer Low2e02dc62015-11-07 17:34:39 -0800437// Like unix_read(), but does not handle EINTR.
438static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
439 return read(fd, buf, len);
440}
441
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442#undef read
443#define read ___xxx_read
444
445static __inline__ int adb_write(int fd, const void* buf, size_t len)
446{
Kenny Root73167412012-10-12 15:26:45 -0700447 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448}
449#undef write
450#define write ___xxx_write
451
452static __inline__ int adb_lseek(int fd, int pos, int where)
453{
454 return lseek(fd, pos, where);
455}
456#undef lseek
457#define lseek ___xxx_lseek
458
459static __inline__ int adb_unlink(const char* path)
460{
461 return unlink(path);
462}
463#undef unlink
464#define unlink ___xxx_unlink
465
466static __inline__ int adb_creat(const char* path, int mode)
467{
Kenny Root73167412012-10-12 15:26:45 -0700468 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200470 if ( fd < 0 )
471 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472
473 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200474 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475}
476#undef creat
477#define creat ___xxx_creat
478
David Pursellc5b8ad82015-10-28 14:29:51 -0700479static __inline__ int unix_isatty(int fd) {
480 return isatty(fd);
481}
482#define isatty ___xxx_isatty
483
Spencer Low5200c662015-07-30 23:07:55 -0700484// Helper for network_* functions.
485inline int _fd_set_error_str(int fd, std::string* error) {
486 if (fd == -1) {
487 *error = strerror(errno);
488 }
489 return fd;
490}
491
Spencer Low5200c662015-07-30 23:07:55 -0700492inline int network_inaddr_any_server(int port, int type, std::string* error) {
493 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
494}
495
Josh Gaocfb21412016-08-24 18:38:44 -0700496inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
497 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
498}
499
500inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
501 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700502}
503
504inline int network_connect(const std::string& host, int port, int type,
505 int timeout, std::string* error) {
506 int getaddrinfo_error = 0;
507 int fd = socket_network_client_timeout(host.c_str(), port, type, timeout,
508 &getaddrinfo_error);
509 if (fd != -1) {
510 return fd;
511 }
512 if (getaddrinfo_error != 0) {
513 *error = gai_strerror(getaddrinfo_error);
514 } else {
515 *error = strerror(errno);
516 }
517 return -1;
518}
519
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
521{
Benoit Goby95ef8282011-02-01 18:57:41 -0800522 int fd;
523
Kenny Root73167412012-10-12 15:26:45 -0700524 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800525 if (fd >= 0)
526 close_on_exec(fd);
527
528 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529}
530
531#undef accept
532#define accept ___xxx_accept
533
David Purselleaae97e2016-04-07 11:25:48 -0700534inline int adb_socket_get_local_port(int fd) {
535 return socket_get_local_port(fd);
536}
537
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700538// Operate on a file descriptor returned from unix_open() or a well-known file
539// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
540//
541// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
542// adb_write(), adb_close() (which all map to Unix system calls), but the
543// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
544// into the C Runtime and its configurable CR/LF translation (which is settable
545// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546#define unix_read adb_read
547#define unix_write adb_write
548#define unix_close adb_close
549
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700550static __inline__ int adb_thread_setname(const std::string& name) {
551#ifdef __APPLE__
552 return pthread_setname_np(name.c_str());
553#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700554 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
555 // glibc doesn't have strlcpy, so we have to fake it.
556 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
557 strncpy(buf, name.c_str(), sizeof(buf) - 1);
558 buf[sizeof(buf) - 1] = '\0';
559 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700560#endif
561}
562
Spencer Lowf055c192015-01-25 14:40:16 -0800563static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
564{
565 return setsockopt( fd, level, optname, optval, optlen );
566}
567
568#undef setsockopt
569#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800570
571static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
572{
573 return socketpair( d, type, protocol, sv );
574}
575
576static __inline__ int adb_socketpair( int sv[2] )
577{
578 int rc;
579
580 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
581 if (rc < 0)
582 return -1;
583
584 close_on_exec( sv[0] );
585 close_on_exec( sv[1] );
586 return 0;
587}
588
589#undef socketpair
590#define socketpair ___xxx_socketpair
591
Josh Gao3777d2e2016-02-16 17:34:53 -0800592typedef struct pollfd adb_pollfd;
593static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
594 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
595}
596
597#define poll ___xxx_poll
598
Alex Vallée47d67c92015-05-06 16:26:00 -0400599static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600{
Alex Vallée47d67c92015-05-06 16:26:00 -0400601 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602}
Alex Vallée47d67c92015-05-06 16:26:00 -0400603
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604#undef mkdir
605#define mkdir ___xxx_mkdir
606
Alex Vallée47d67c92015-05-06 16:26:00 -0400607static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 return path[0] == '/';
609}
610
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611#endif /* !_WIN32 */
612
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800613static inline void disable_tcp_nagle(int fd) {
614 int off = 1;
615 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
616}
617
David Pursellbfd95032016-02-22 14:27:23 -0800618// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
619// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
620// configured to drop after 10 missed keepalives. Returns true on success.
621bool set_tcp_keepalive(int fd, int interval_sec);
622
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700623#if defined(_WIN32)
624// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
625#undef ERROR
626#endif
627
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628#endif /* _ADB_SYSDEPS_H */