blob: bebf4e6fdc7c9937b03d968f3afc876dc95e3961 [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>
Elliott Hughes4679a392018-10-19 13:59:44 -070028#include <error.h>
Elliott Hughes381cfa92015-07-23 17:12:58 -070029
Spencer Low5200c662015-07-30 23:07:55 -070030#include <string>
Spencer Lowa30b79a2015-11-15 16:29:36 -080031#include <vector>
Spencer Low5200c662015-07-30 23:07:55 -070032
Josh Gao13ea01d2016-05-13 14:52:06 -070033// Include this before open/close/unlink are defined as macros below.
Josh Gao3b3e10d2016-02-09 14:59:09 -080034#include <android-base/errors.h>
Elliott Hughes3df2fa62017-11-28 18:05:27 -080035#include <android-base/macros.h>
Josh Gao13ea01d2016-05-13 14:52:06 -070036#include <android-base/unique_fd.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/utf8.h>
Spencer Lowd21dc822015-11-12 15:20:15 -080038
Josh Gaoa3577e12016-12-05 13:24:48 -080039#include "sysdeps/errno.h"
Josh Gao46de1d72017-04-12 13:57:06 -070040#include "sysdeps/network.h"
Josh Gaof551ea02016-07-28 18:09:48 -070041#include "sysdeps/stat.h"
42
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#ifdef _WIN32
44
Josh Gaoa166e712016-01-29 12:08:34 -080045// Clang-only nullability specifiers
46#define _Nonnull
47#define _Nullable
48
Dan Albert630b9af2014-11-24 23:34:35 -080049#include <ctype.h>
50#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070051#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080052#include <errno.h>
53#include <fcntl.h>
54#include <io.h>
55#include <process.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070056#include <stdint.h>
Dan Albert630b9af2014-11-24 23:34:35 -080057#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070058#include <utime.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070059#include <windows.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070060#include <winsock2.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080062
Spencer Low2122c7a2015-08-26 18:46:09 -070063#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080064#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040065
Dan Albert630b9af2014-11-24 23:34:35 -080066#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Elliott Hughes5c742702015-07-30 17:42:01 -070068#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069#define OS_PATH_SEPARATOR '\\'
70#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070071#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Elliott Hughes4679a392018-10-19 13:59:44 -070073void error(int status, int error, const char* fmt, ...) __attribute__((__format__(printf, 3, 4)));
74
Josh Gao07db1192015-11-07 15:38:19 -080075static __inline__ bool adb_is_separator(char c) {
76 return c == '\\' || c == '/';
77}
78
Spencer Low50beee32018-09-03 16:03:22 -070079extern int adb_thread_setname(const std::string& name);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070080
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081static __inline__ void close_on_exec(int fd)
82{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020083 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084}
85
Spencer Lowcf4ff642015-05-11 01:08:48 -070086extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087#undef unlink
88#define unlink ___xxx_unlink
89
Spencer Lowcf4ff642015-05-11 01:08:48 -070090extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091#undef mkdir
92#define mkdir ___xxx_mkdir
93
Spencer Low6ac5d7d2015-05-22 20:09:06 -070094// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -070095extern int adb_open(const char* path, int options);
96extern int adb_creat(const char* path, int mode);
97extern int adb_read(int fd, void* buf, int len);
98extern int adb_write(int fd, const void* buf, int len);
Elliott Hughescabfc3d2018-09-20 13:59:49 -070099extern int64_t adb_lseek(int fd, int64_t pos, int where);
Josh Gao2e1e7892018-03-23 13:03:28 -0700100extern int adb_shutdown(int fd, int direction = SHUT_RDWR);
101extern int adb_close(int fd);
102extern int adb_register_socket(SOCKET s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700104// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105static __inline__ int unix_close(int fd)
106{
107 return close(fd);
108}
109#undef close
110#define close ____xxx_close
111
Spencer Low2e02dc62015-11-07 17:34:39 -0800112// Like unix_read(), but may return EINTR.
113extern int unix_read_interruptible(int fd, void* buf, size_t len);
114
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700115// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800116static __inline__ int unix_read(int fd, void* buf, size_t len) {
117 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
118}
Spencer Low50184062015-03-01 15:06:21 -0800119
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120#undef read
121#define read ___xxx_read
122
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700123// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124static __inline__ int unix_write(int fd, const void* buf, size_t len)
125{
126 return write(fd, buf, len);
127}
128#undef write
129#define write ___xxx_write
130
Spencer Low40babf02018-09-02 19:19:39 -0700131// See the comments for the !defined(_WIN32) version of unix_lseek().
132static __inline__ int unix_lseek(int fd, int pos, int where) {
133 return lseek(fd, pos, where);
134}
135#undef lseek
136#define lseek ___xxx_lseek
137
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700138// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139static __inline__ int adb_open_mode(const char* path, int options, int mode)
140{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200141 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142}
143
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700144// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700145extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146#define open ___xxx_unix_open
147
David Pursellc5b8ad82015-10-28 14:29:51 -0700148// Checks if |fd| corresponds to a console.
149// Standard Windows isatty() returns 1 for both console FDs and character
150// devices like NUL. unix_isatty() performs some extra checking to only match
151// console FDs.
152// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
153// will work but adb_open() FDs will not. Additionally the OS handle associated
154// with |fd| must have GENERIC_READ access (which console FDs have by default).
155// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
156// calling this function is unreliable and should not be used.
157int unix_isatty(int fd);
158#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
Spencer Low5200c662015-07-30 23:07:55 -0700160int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700161
162inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
163 abort();
164}
165
166inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
167 abort();
168}
169
Spencer Low5200c662015-07-30 23:07:55 -0700170int network_connect(const std::string& host, int port, int type, int timeout,
171 std::string* error);
172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
174
175#undef accept
176#define accept ___xxx_accept
177
David Purselleaae97e2016-04-07 11:25:48 -0700178// Returns the local port number of a bound socket, or -1 on failure.
179int adb_socket_get_local_port(int fd);
180
Spencer Lowf055c192015-01-25 14:40:16 -0800181extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
182
183#undef setsockopt
184#define setsockopt ___xxx_setsockopt
185
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186extern int adb_socketpair( int sv[2] );
187
Josh Gao3777d2e2016-02-16 17:34:53 -0800188struct adb_pollfd {
189 int fd;
190 short events;
191 short revents;
192};
193extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
194#define poll ___xxx_poll
195
Elliott Hughes5c742702015-07-30 17:42:01 -0700196static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
198}
199
Spencer Lowcf4ff642015-05-11 01:08:48 -0700200// UTF-8 versions of POSIX APIs.
201extern DIR* adb_opendir(const char* dirname);
202extern struct dirent* adb_readdir(DIR* dir);
203extern int adb_closedir(DIR* dir);
204
205extern int adb_utime(const char *, struct utimbuf *);
206extern int adb_chmod(const char *, int);
207
Elliott Hughesd8a4c602018-06-26 13:06:15 -0700208extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
209 __attribute__((__format__(__printf__, 2, 0)));
210extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
211extern int adb_fprintf(FILE* stream, const char* format, ...)
212 __attribute__((__format__(__printf__, 2, 3)));
213extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700214
215extern int adb_fputs(const char* buf, FILE* stream);
216extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800217extern int adb_putchar(int ch);
218extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700219extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
220 FILE* stream);
221
222extern FILE* adb_fopen(const char* f, const char* m);
223
224extern char* adb_getenv(const char* name);
225
226extern char* adb_getcwd(char* buf, int size);
227
228// Remap calls to POSIX APIs to our UTF-8 versions.
229#define opendir adb_opendir
230#define readdir adb_readdir
231#define closedir adb_closedir
232#define rewinddir rewinddir_utf8_not_yet_implemented
233#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800234// Some compiler's C++ headers have members named seekdir, so we can't do the
235// macro technique and instead cause a link error if seekdir is called.
236inline void seekdir(DIR*, long) {
237 extern int seekdir_utf8_not_yet_implemented;
238 seekdir_utf8_not_yet_implemented = 1;
239}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700240
241#define utime adb_utime
242#define chmod adb_chmod
243
244#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800245#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700246#define fprintf adb_fprintf
247#define printf adb_printf
248#define fputs adb_fputs
249#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800250// putc may be a macro, so if so, undefine it, so that we can redefine it.
251#undef putc
252#define putc(c, s) adb_fputc(c, s)
253#define putchar adb_putchar
254#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700255#define fwrite adb_fwrite
256
257#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800258#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700259
260#define getenv adb_getenv
261#define putenv putenv_utf8_not_yet_implemented
262#define setenv setenv_utf8_not_yet_implemented
263#define unsetenv unsetenv_utf8_not_yet_implemented
264
265#define getcwd adb_getcwd
266
Spencer Lowcf4ff642015-05-11 01:08:48 -0700267// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
268// passed to main().
269class NarrowArgs {
270public:
271 NarrowArgs(int argc, wchar_t** argv);
272 ~NarrowArgs();
273
274 inline char** data() {
275 return narrow_args;
276 }
277
278private:
279 char** narrow_args;
280};
281
Spencer Low5c398d22015-08-08 15:07:07 -0700282// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
283// so they can fit in an int. To convert back, we just need to sign-extend.
284// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
285// Note that this does not make a HANDLE value work with APIs like open(), nor
286// does this make a value from open() passable to APIs taking a HANDLE. This
287// just lets you take a HANDLE, pass it around as an int, and then use it again
288// as a HANDLE.
289inline int cast_handle_to_int(const HANDLE h) {
290 // truncate
291 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
292}
293
294inline HANDLE cast_int_to_handle(const int fd) {
295 // sign-extend
296 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
297}
298
Spencer Low2122c7a2015-08-26 18:46:09 -0700299// Deleter for unique_handle. Adapted from many sources, including:
300// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
301// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
302class handle_deleter {
303public:
304 typedef HANDLE pointer;
305
306 void operator()(HANDLE h);
307};
308
309// Like std::unique_ptr, but for Windows HANDLE objects that should be
310// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
311// but does not check if the handle != INVALID_HANDLE_VALUE.
312typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
313
Spencer Lowa30b79a2015-11-15 16:29:36 -0800314namespace internal {
315
316size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
317
318}
319
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320#else /* !_WIN32 a.k.a. Unix */
321
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322#include <fcntl.h>
Spencer Low5200c662015-07-30 23:07:55 -0700323#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324#include <netinet/in.h>
325#include <netinet/tcp.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700326#include <poll.h>
327#include <pthread.h>
328#include <signal.h>
329#include <stdarg.h>
330#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331#include <string.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700332#include <sys/stat.h>
333#include <sys/wait.h>
Kenny Root73167412012-10-12 15:26:45 -0700334#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335
Alex Vallée47d67c92015-05-06 16:26:00 -0400336#include <string>
337
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700338#include <cutils/sockets.h>
339
Elliott Hughes5c742702015-07-30 17:42:01 -0700340#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341#define OS_PATH_SEPARATOR '/'
342#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700343#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344
Josh Gao07db1192015-11-07 15:38:19 -0800345static __inline__ bool adb_is_separator(char c) {
346 return c == '/';
347}
348
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349static __inline__ void close_on_exec(int fd)
350{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200351 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352}
353
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700354// Open a file and return a file descriptor that may be used with unix_read(),
355// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
356//
357// On Unix, this is based on open(), so the file descriptor is a real OS file
358// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
359// file descriptor that can only be used with C Runtime APIs (which are wrapped
360// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
361// configurable CR/LF translation which defaults to text mode, but is settable
362// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363static __inline__ int unix_open(const char* path, int options,...)
364{
365 if ((options & O_CREAT) == 0)
366 {
Kenny Root73167412012-10-12 15:26:45 -0700367 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 }
369 else
370 {
371 int mode;
372 va_list args;
373 va_start( args, options );
374 mode = va_arg( args, int );
375 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700376 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 }
378}
379
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700380// Similar to the two-argument adb_open(), but takes a mode parameter for file
381// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
383{
Kenny Root73167412012-10-12 15:26:45 -0700384 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385}
386
387
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700388// Open a file and return a file descriptor that may be used with adb_read(),
389// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
390//
391// On Unix, this is based on open(), but the Windows implementation (in
392// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
393// and its CR/LF translation. The returned file descriptor should be used with
394// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395static __inline__ int adb_open( const char* pathname, int options )
396{
Kenny Root73167412012-10-12 15:26:45 -0700397 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 if (fd < 0)
399 return -1;
400 close_on_exec( fd );
401 return fd;
402}
403#undef open
404#define open ___xxx_open
405
Josh Gao2e1e7892018-03-23 13:03:28 -0700406static __inline__ int adb_shutdown(int fd, int direction = SHUT_RDWR) {
David Pursell1ed57f02015-10-06 15:30:03 -0700407 return shutdown(fd, direction);
408}
Josh Gao2e1e7892018-03-23 13:03:28 -0700409
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400410#undef shutdown
411#define shutdown ____xxx_shutdown
412
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700413// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
414// not designed to take a file descriptor from unix_open(). See the comments
415// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800416__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 return close(fd);
418}
419#undef close
420#define close ____xxx_close
421
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700422// On Windows, ADB has an indirection layer for file descriptors. If we get a
423// Win32 SOCKET object from an external library, we have to map it in to that
424// indirection layer, which this does.
425__inline__ int adb_register_socket(int s) {
426 return s;
427}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428
429static __inline__ int adb_read(int fd, void* buf, size_t len)
430{
Kenny Root73167412012-10-12 15:26:45 -0700431 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432}
433
Spencer Low2e02dc62015-11-07 17:34:39 -0800434// Like unix_read(), but does not handle EINTR.
435static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
436 return read(fd, buf, len);
437}
438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439#undef read
440#define read ___xxx_read
441
442static __inline__ int adb_write(int fd, const void* buf, size_t len)
443{
Kenny Root73167412012-10-12 15:26:45 -0700444 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445}
446#undef write
447#define write ___xxx_write
448
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700449static __inline__ int64_t adb_lseek(int fd, int64_t pos, int where) {
450#if defined(__APPLE__)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 return lseek(fd, pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700452#else
453 return lseek64(fd, pos, where);
454#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455}
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700456#undef lseek
457#define lseek ___xxx_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458
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
Josh Gao45e3e952018-08-10 16:03:09 -0700504int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700505
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
507{
Benoit Goby95ef8282011-02-01 18:57:41 -0800508 int fd;
509
Kenny Root73167412012-10-12 15:26:45 -0700510 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800511 if (fd >= 0)
512 close_on_exec(fd);
513
514 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515}
516
517#undef accept
518#define accept ___xxx_accept
519
David Purselleaae97e2016-04-07 11:25:48 -0700520inline int adb_socket_get_local_port(int fd) {
521 return socket_get_local_port(fd);
522}
523
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700524// Operate on a file descriptor returned from unix_open() or a well-known file
525// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
526//
527// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
528// adb_write(), adb_close() (which all map to Unix system calls), but the
529// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
530// into the C Runtime and its configurable CR/LF translation (which is settable
531// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532#define unix_read adb_read
533#define unix_write adb_write
Spencer Low40babf02018-09-02 19:19:39 -0700534#define unix_lseek adb_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535#define unix_close adb_close
536
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700537static __inline__ int adb_thread_setname(const std::string& name) {
538#ifdef __APPLE__
539 return pthread_setname_np(name.c_str());
540#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700541 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
542 // glibc doesn't have strlcpy, so we have to fake it.
543 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
544 strncpy(buf, name.c_str(), sizeof(buf) - 1);
545 buf[sizeof(buf) - 1] = '\0';
546 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700547#endif
548}
549
Spencer Lowf055c192015-01-25 14:40:16 -0800550static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
551{
552 return setsockopt( fd, level, optname, optval, optlen );
553}
554
555#undef setsockopt
556#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557
558static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
559{
560 return socketpair( d, type, protocol, sv );
561}
562
563static __inline__ int adb_socketpair( int sv[2] )
564{
565 int rc;
566
567 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
568 if (rc < 0)
569 return -1;
570
571 close_on_exec( sv[0] );
572 close_on_exec( sv[1] );
573 return 0;
574}
575
576#undef socketpair
577#define socketpair ___xxx_socketpair
578
Josh Gao3777d2e2016-02-16 17:34:53 -0800579typedef struct pollfd adb_pollfd;
580static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
581 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
582}
583
584#define poll ___xxx_poll
585
Alex Vallée47d67c92015-05-06 16:26:00 -0400586static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587{
Alex Vallée47d67c92015-05-06 16:26:00 -0400588 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589}
Alex Vallée47d67c92015-05-06 16:26:00 -0400590
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591#undef mkdir
592#define mkdir ___xxx_mkdir
593
Alex Vallée47d67c92015-05-06 16:26:00 -0400594static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595 return path[0] == '/';
596}
597
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800598#endif /* !_WIN32 */
599
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800600static inline void disable_tcp_nagle(int fd) {
601 int off = 1;
602 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
603}
604
David Pursellbfd95032016-02-22 14:27:23 -0800605// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
606// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
607// configured to drop after 10 missed keepalives. Returns true on success.
608bool set_tcp_keepalive(int fd, int interval_sec);
609
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700610#if defined(_WIN32)
611// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
612#undef ERROR
613#endif
614
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615#endif /* _ADB_SYSDEPS_H */