blob: bc189942395d2c2cb7c2932e6cbb724fd0c4c131 [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Hughes43df1092015-07-23 17:12:58 -070027#include <errno.h>
28
Spencer Low753d4852015-07-30 23:07:55 -070029#include <string>
Spencer Lowf373c352015-11-15 16:29:36 -080030#include <vector>
Spencer Low753d4852015-07-30 23:07:55 -070031
Josh Gaod91139c2016-05-13 14:52:06 -070032// Include this before open/close/unlink are defined as macros below.
Josh Gaoa4f5e032016-02-09 14:59:09 -080033#include <android-base/errors.h>
Elliott Hughes8bddf242017-11-28 18:05:27 -080034#include <android-base/macros.h>
Josh Gaod91139c2016-05-13 14:52:06 -070035#include <android-base/unique_fd.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080036#include <android-base/utf8.h>
Spencer Low50f5bf12015-11-12 15:20:15 -080037
Josh Gao75e96bb2016-12-05 13:24:48 -080038#include "sysdeps/errno.h"
Josh Gao1f6a57a2017-04-12 13:57:06 -070039#include "sysdeps/network.h"
Josh Gaoa4f9c172016-07-28 18:09:48 -070040#include "sysdeps/stat.h"
41
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#ifdef _WIN32
43
Josh Gao23f6f2b2016-01-29 12:08:34 -080044// Clang-only nullability specifiers
45#define _Nonnull
46#define _Nullable
47
Dan Albertbbbbea62014-11-24 23:34:35 -080048#include <ctype.h>
49#include <direct.h>
Spencer Low6815c072015-05-11 01:08:48 -070050#include <dirent.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080051#include <errno.h>
52#include <fcntl.h>
53#include <io.h>
54#include <process.h>
55#include <sys/stat.h>
Spencer Low6815c072015-05-11 01:08:48 -070056#include <utime.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057#include <winsock2.h>
Stephen Hinesb1170852014-10-01 17:37:06 -070058#include <windows.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059#include <ws2tcpip.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080060
Spencer Low2bbb3a92015-08-26 18:46:09 -070061#include <memory> // unique_ptr
Spencer Low50f5bf12015-11-12 15:20:15 -080062#include <string>
Alex Vallée28d1f8d2015-05-06 16:26:00 -040063
Dan Albertbbbbea62014-11-24 23:34:35 -080064#include "fdevent.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080065
Elliott Hughesd189cfb2015-07-30 17:42:01 -070066#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067#define OS_PATH_SEPARATOR '\\'
68#define OS_PATH_SEPARATOR_STR "\\"
Benoit Goby2cc19e42012-04-12 12:23:49 -070069#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070
Josh Gao8acf06c2015-11-07 15:38:19 -080071static __inline__ bool adb_is_separator(char c) {
72 return c == '\\' || c == '/';
73}
74
Siva Velusamy2669cf92015-08-28 16:37:29 -070075static __inline__ int adb_thread_setname(const std::string& name) {
76 // TODO: See https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx for how to set
77 // the thread name in Windows. Unfortunately, it only works during debugging, but
78 // our build process doesn't generate PDB files needed for debugging.
79 return 0;
80}
81
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082static __inline__ void close_on_exec(int fd)
83{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020084 /* nothing really */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080085}
86
Spencer Low6815c072015-05-11 01:08:48 -070087extern int adb_unlink(const char* path);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080088#undef unlink
89#define unlink ___xxx_unlink
90
Spencer Low6815c072015-05-11 01:08:48 -070091extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092#undef mkdir
93#define mkdir ___xxx_mkdir
94
Spencer Low3a2421b2015-05-22 20:09:06 -070095// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao96049b92018-03-23 13:03:28 -070096extern int adb_open(const char* path, int options);
97extern int adb_creat(const char* path, int mode);
98extern int adb_read(int fd, void* buf, int len);
99extern int adb_write(int fd, const void* buf, int len);
100extern int adb_lseek(int fd, int pos, int where);
101extern int adb_shutdown(int fd, int direction = SHUT_RDWR);
102extern int adb_close(int fd);
103extern int adb_register_socket(SOCKET s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800104
Spencer Low3a2421b2015-05-22 20:09:06 -0700105// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800106static __inline__ int unix_close(int fd)
107{
108 return close(fd);
109}
110#undef close
111#define close ____xxx_close
112
Spencer Low55441402015-11-07 17:34:39 -0800113// Like unix_read(), but may return EINTR.
114extern int unix_read_interruptible(int fd, void* buf, size_t len);
115
Spencer Low3a2421b2015-05-22 20:09:06 -0700116// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low55441402015-11-07 17:34:39 -0800117static __inline__ int unix_read(int fd, void* buf, size_t len) {
118 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
119}
Spencer Lowbeb61982015-03-01 15:06:21 -0800120
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800121#undef read
122#define read ___xxx_read
123
Spencer Low3a2421b2015-05-22 20:09:06 -0700124// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800125static __inline__ int unix_write(int fd, const void* buf, size_t len)
126{
127 return write(fd, buf, len);
128}
129#undef write
130#define write ___xxx_write
131
Spencer Low04b575d2018-09-02 19:19:39 -0700132// See the comments for the !defined(_WIN32) version of unix_lseek().
133static __inline__ int unix_lseek(int fd, int pos, int where) {
134 return lseek(fd, pos, where);
135}
136#undef lseek
137#define lseek ___xxx_lseek
138
Spencer Low3a2421b2015-05-22 20:09:06 -0700139// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140static __inline__ int adb_open_mode(const char* path, int options, int mode)
141{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200142 return adb_open(path, options);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143}
144
Spencer Low3a2421b2015-05-22 20:09:06 -0700145// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Low6815c072015-05-11 01:08:48 -0700146extern int unix_open(const char* path, int options, ...);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800147#define open ___xxx_unix_open
148
David Pursell58805362015-10-28 14:29:51 -0700149// Checks if |fd| corresponds to a console.
150// Standard Windows isatty() returns 1 for both console FDs and character
151// devices like NUL. unix_isatty() performs some extra checking to only match
152// console FDs.
153// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
154// will work but adb_open() FDs will not. Additionally the OS handle associated
155// with |fd| must have GENERIC_READ access (which console FDs have by default).
156// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
157// calling this function is unreliable and should not be used.
158int unix_isatty(int fd);
159#define isatty ___xxx_isatty
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160
Spencer Low753d4852015-07-30 23:07:55 -0700161int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700162
163inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
164 abort();
165}
166
167inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
168 abort();
169}
170
Spencer Low753d4852015-07-30 23:07:55 -0700171int network_connect(const std::string& host, int port, int type, int timeout,
172 std::string* error);
173
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800174extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
175
176#undef accept
177#define accept ___xxx_accept
178
David Pursell19d0c232016-04-07 11:25:48 -0700179// Returns the local port number of a bound socket, or -1 on failure.
180int adb_socket_get_local_port(int fd);
181
Spencer Low31aafa62015-01-25 14:40:16 -0800182extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
183
184#undef setsockopt
185#define setsockopt ___xxx_setsockopt
186
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800187extern int adb_socketpair( int sv[2] );
188
Josh Gaoe7388122016-02-16 17:34:53 -0800189struct adb_pollfd {
190 int fd;
191 short events;
192 short revents;
193};
194extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
195#define poll ___xxx_poll
196
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700197static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800198 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
199}
200
Spencer Low6815c072015-05-11 01:08:48 -0700201// UTF-8 versions of POSIX APIs.
202extern DIR* adb_opendir(const char* dirname);
203extern struct dirent* adb_readdir(DIR* dir);
204extern int adb_closedir(DIR* dir);
205
206extern int adb_utime(const char *, struct utimbuf *);
207extern int adb_chmod(const char *, int);
208
Elliott Hughes874c9412018-06-26 13:06:15 -0700209extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
210 __attribute__((__format__(__printf__, 2, 0)));
211extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
212extern int adb_fprintf(FILE* stream, const char* format, ...)
213 __attribute__((__format__(__printf__, 2, 3)));
214extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Low6815c072015-05-11 01:08:48 -0700215
216extern int adb_fputs(const char* buf, FILE* stream);
217extern int adb_fputc(int ch, FILE* stream);
Spencer Lowf373c352015-11-15 16:29:36 -0800218extern int adb_putchar(int ch);
219extern int adb_puts(const char* buf);
Spencer Low6815c072015-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 Low28bc2cb2015-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 Low6815c072015-05-11 01:08:48 -0700241
242#define utime adb_utime
243#define chmod adb_chmod
244
245#define vfprintf adb_vfprintf
Spencer Lowf373c352015-11-15 16:29:36 -0800246#define vprintf adb_vprintf
Spencer Low6815c072015-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 Lowf373c352015-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 Low6815c072015-05-11 01:08:48 -0700256#define fwrite adb_fwrite
257
258#define fopen adb_fopen
Spencer Lowf373c352015-11-15 16:29:36 -0800259#define freopen freopen_utf8_not_yet_implemented
Spencer Low6815c072015-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 Low6815c072015-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 Lowb28812f2015-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 Low2bbb3a92015-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 Lowf373c352015-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 Project9ca14dc2009-03-03 19:32:55 -0800321#else /* !_WIN32 a.k.a. Unix */
322
Spencer Low753d4852015-07-30 23:07:55 -0700323#include <cutils/sockets.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800324#include <fcntl.h>
Josh Gaoe7388122016-02-16 17:34:53 -0800325#include <poll.h>
326#include <signal.h>
327#include <sys/stat.h>
328#include <sys/wait.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800329
330#include <pthread.h>
331#include <unistd.h>
332#include <fcntl.h>
333#include <stdarg.h>
Spencer Low753d4852015-07-30 23:07:55 -0700334#include <netdb.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800335#include <netinet/in.h>
336#include <netinet/tcp.h>
337#include <string.h>
Kenny Rooteac025c2012-10-12 15:26:45 -0700338#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800339
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400340#include <string>
341
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700342#define OS_PATH_SEPARATORS "/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800343#define OS_PATH_SEPARATOR '/'
344#define OS_PATH_SEPARATOR_STR "/"
Benoit Goby2cc19e42012-04-12 12:23:49 -0700345#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346
Josh Gao8acf06c2015-11-07 15:38:19 -0800347static __inline__ bool adb_is_separator(char c) {
348 return c == '/';
349}
350
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800351static __inline__ void close_on_exec(int fd)
352{
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200353 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800354}
355
Spencer Low3a2421b2015-05-22 20:09:06 -0700356// Open a file and return a file descriptor that may be used with unix_read(),
357// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
358//
359// On Unix, this is based on open(), so the file descriptor is a real OS file
360// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
361// file descriptor that can only be used with C Runtime APIs (which are wrapped
362// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
363// configurable CR/LF translation which defaults to text mode, but is settable
364// with _setmode().
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800365static __inline__ int unix_open(const char* path, int options,...)
366{
367 if ((options & O_CREAT) == 0)
368 {
Kenny Rooteac025c2012-10-12 15:26:45 -0700369 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800370 }
371 else
372 {
373 int mode;
374 va_list args;
375 va_start( args, options );
376 mode = va_arg( args, int );
377 va_end( args );
Kenny Rooteac025c2012-10-12 15:26:45 -0700378 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 }
380}
381
Spencer Low3a2421b2015-05-22 20:09:06 -0700382// Similar to the two-argument adb_open(), but takes a mode parameter for file
383// creation. See adb_open() for more info.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800384static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
385{
Kenny Rooteac025c2012-10-12 15:26:45 -0700386 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800387}
388
389
Spencer Low3a2421b2015-05-22 20:09:06 -0700390// Open a file and return a file descriptor that may be used with adb_read(),
391// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
392//
393// On Unix, this is based on open(), but the Windows implementation (in
394// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
395// and its CR/LF translation. The returned file descriptor should be used with
396// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800397static __inline__ int adb_open( const char* pathname, int options )
398{
Kenny Rooteac025c2012-10-12 15:26:45 -0700399 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800400 if (fd < 0)
401 return -1;
402 close_on_exec( fd );
403 return fd;
404}
405#undef open
406#define open ___xxx_open
407
Josh Gao96049b92018-03-23 13:03:28 -0700408static __inline__ int adb_shutdown(int fd, int direction = SHUT_RDWR) {
David Pursell3fe11f62015-10-06 15:30:03 -0700409 return shutdown(fd, direction);
410}
Josh Gao96049b92018-03-23 13:03:28 -0700411
Mike Lockwood81ffe172009-10-11 23:04:18 -0400412#undef shutdown
413#define shutdown ____xxx_shutdown
414
Spencer Low3a2421b2015-05-22 20:09:06 -0700415// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
416// not designed to take a file descriptor from unix_open(). See the comments
417// for adb_open() for more info.
Josh Gao1b533c82016-03-04 15:15:56 -0800418__inline__ int adb_close(int fd) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800419 return close(fd);
420}
421#undef close
422#define close ____xxx_close
423
Casey Dahlin20238f22016-09-21 14:03:39 -0700424// On Windows, ADB has an indirection layer for file descriptors. If we get a
425// Win32 SOCKET object from an external library, we have to map it in to that
426// indirection layer, which this does.
427__inline__ int adb_register_socket(int s) {
428 return s;
429}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800430
431static __inline__ int adb_read(int fd, void* buf, size_t len)
432{
Kenny Rooteac025c2012-10-12 15:26:45 -0700433 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800434}
435
Spencer Low55441402015-11-07 17:34:39 -0800436// Like unix_read(), but does not handle EINTR.
437static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
438 return read(fd, buf, len);
439}
440
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441#undef read
442#define read ___xxx_read
443
444static __inline__ int adb_write(int fd, const void* buf, size_t len)
445{
Kenny Rooteac025c2012-10-12 15:26:45 -0700446 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800447}
448#undef write
449#define write ___xxx_write
450
451static __inline__ int adb_lseek(int fd, int pos, int where)
452{
453 return lseek(fd, pos, where);
454}
455#undef lseek
456#define lseek ___xxx_lseek
457
458static __inline__ int adb_unlink(const char* path)
459{
460 return unlink(path);
461}
462#undef unlink
463#define unlink ___xxx_unlink
464
465static __inline__ int adb_creat(const char* path, int mode)
466{
Kenny Rooteac025c2012-10-12 15:26:45 -0700467 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800468
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200469 if ( fd < 0 )
470 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800471
472 close_on_exec(fd);
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200473 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800474}
475#undef creat
476#define creat ___xxx_creat
477
David Pursell58805362015-10-28 14:29:51 -0700478static __inline__ int unix_isatty(int fd) {
479 return isatty(fd);
480}
481#define isatty ___xxx_isatty
482
Spencer Low753d4852015-07-30 23:07:55 -0700483// Helper for network_* functions.
484inline int _fd_set_error_str(int fd, std::string* error) {
485 if (fd == -1) {
486 *error = strerror(errno);
487 }
488 return fd;
489}
490
Spencer Low753d4852015-07-30 23:07:55 -0700491inline int network_inaddr_any_server(int port, int type, std::string* error) {
492 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
493}
494
Josh Gao4a5a95d2016-08-24 18:38:44 -0700495inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
496 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
497}
498
499inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
500 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low753d4852015-07-30 23:07:55 -0700501}
502
Josh Gao8d7080c2018-08-10 16:03:09 -0700503int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low753d4852015-07-30 23:07:55 -0700504
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800505static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
506{
Benoit Gobyf4ded742011-02-01 18:57:41 -0800507 int fd;
508
Kenny Rooteac025c2012-10-12 15:26:45 -0700509 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Gobyf4ded742011-02-01 18:57:41 -0800510 if (fd >= 0)
511 close_on_exec(fd);
512
513 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800514}
515
516#undef accept
517#define accept ___xxx_accept
518
David Pursell19d0c232016-04-07 11:25:48 -0700519inline int adb_socket_get_local_port(int fd) {
520 return socket_get_local_port(fd);
521}
522
Spencer Low3a2421b2015-05-22 20:09:06 -0700523// Operate on a file descriptor returned from unix_open() or a well-known file
524// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
525//
526// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
527// adb_write(), adb_close() (which all map to Unix system calls), but the
528// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
529// into the C Runtime and its configurable CR/LF translation (which is settable
530// via _setmode()).
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800531#define unix_read adb_read
532#define unix_write adb_write
Spencer Low04b575d2018-09-02 19:19:39 -0700533#define unix_lseek adb_lseek
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800534#define unix_close adb_close
535
Siva Velusamy2669cf92015-08-28 16:37:29 -0700536static __inline__ int adb_thread_setname(const std::string& name) {
537#ifdef __APPLE__
538 return pthread_setname_np(name.c_str());
539#else
Elliott Hughes3bb1b572017-08-02 13:22:38 -0700540 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
541 // glibc doesn't have strlcpy, so we have to fake it.
542 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
543 strncpy(buf, name.c_str(), sizeof(buf) - 1);
544 buf[sizeof(buf) - 1] = '\0';
545 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy2669cf92015-08-28 16:37:29 -0700546#endif
547}
548
Spencer Low31aafa62015-01-25 14:40:16 -0800549static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
550{
551 return setsockopt( fd, level, optname, optval, optlen );
552}
553
554#undef setsockopt
555#define setsockopt ___xxx_setsockopt
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800556
557static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
558{
559 return socketpair( d, type, protocol, sv );
560}
561
562static __inline__ int adb_socketpair( int sv[2] )
563{
564 int rc;
565
566 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
567 if (rc < 0)
568 return -1;
569
570 close_on_exec( sv[0] );
571 close_on_exec( sv[1] );
572 return 0;
573}
574
575#undef socketpair
576#define socketpair ___xxx_socketpair
577
Josh Gaoe7388122016-02-16 17:34:53 -0800578typedef struct pollfd adb_pollfd;
579static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
580 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
581}
582
583#define poll ___xxx_poll
584
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400585static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800586{
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400587 return mkdir(path.c_str(), mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800588}
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400589
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800590#undef mkdir
591#define mkdir ___xxx_mkdir
592
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400593static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800594 return path[0] == '/';
595}
596
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800597#endif /* !_WIN32 */
598
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800599static inline void disable_tcp_nagle(int fd) {
600 int off = 1;
601 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
602}
603
David Pursellc25a34e2016-02-22 14:27:23 -0800604// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
605// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
606// configured to drop after 10 missed keepalives. Returns true on success.
607bool set_tcp_keepalive(int fd, int interval_sec);
608
Elliott Hughes801066a2016-06-29 17:42:01 -0700609#if defined(_WIN32)
610// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
611#undef ERROR
612#endif
613
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800614#endif /* _ADB_SYSDEPS_H */