blob: b8d7e06b056651394ececc42887d59a93cebd5dd [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#ifdef _WIN32
43
Josh Gaoa166e712016-01-29 12:08:34 -080044// Clang-only nullability specifiers
45#define _Nonnull
46#define _Nullable
47
Dan Albert630b9af2014-11-24 23:34:35 -080048#include <ctype.h>
49#include <direct.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070050#include <dirent.h>
Dan Albert630b9af2014-11-24 23:34:35 -080051#include <errno.h>
52#include <fcntl.h>
53#include <io.h>
54#include <process.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070055#include <stdint.h>
Dan Albert630b9af2014-11-24 23:34:35 -080056#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070057#include <utime.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070058#include <windows.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -070059#include <winsock2.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080061
Spencer Low2122c7a2015-08-26 18:46:09 -070062#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080063#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040064
Dan Albert630b9af2014-11-24 23:34:35 -080065#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
Elliott Hughes5c742702015-07-30 17:42:01 -070067#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068#define OS_PATH_SEPARATOR '\\'
69#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070070#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Josh Gao07db1192015-11-07 15:38:19 -080072static __inline__ bool adb_is_separator(char c) {
73 return c == '\\' || c == '/';
74}
75
Spencer Low50beee32018-09-03 16:03:22 -070076extern int adb_thread_setname(const std::string& name);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070077
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078static __inline__ void close_on_exec(int fd)
79{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020080 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081}
82
Spencer Lowcf4ff642015-05-11 01:08:48 -070083extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084#undef unlink
85#define unlink ___xxx_unlink
86
Spencer Lowcf4ff642015-05-11 01:08:48 -070087extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088#undef mkdir
89#define mkdir ___xxx_mkdir
90
Spencer Low6ac5d7d2015-05-22 20:09:06 -070091// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -070092extern int adb_open(const char* path, int options);
93extern int adb_creat(const char* path, int mode);
94extern int adb_read(int fd, void* buf, int len);
95extern int adb_write(int fd, const void* buf, int len);
Elliott Hughescabfc3d2018-09-20 13:59:49 -070096extern int64_t adb_lseek(int fd, int64_t pos, int where);
Josh Gao2e1e7892018-03-23 13:03:28 -070097extern int adb_shutdown(int fd, int direction = SHUT_RDWR);
98extern int adb_close(int fd);
99extern int adb_register_socket(SOCKET s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700101// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102static __inline__ int unix_close(int fd)
103{
104 return close(fd);
105}
106#undef close
107#define close ____xxx_close
108
Spencer Low2e02dc62015-11-07 17:34:39 -0800109// Like unix_read(), but may return EINTR.
110extern int unix_read_interruptible(int fd, void* buf, size_t len);
111
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700112// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800113static __inline__ int unix_read(int fd, void* buf, size_t len) {
114 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
115}
Spencer Low50184062015-03-01 15:06:21 -0800116
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117#undef read
118#define read ___xxx_read
119
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700120// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121static __inline__ int unix_write(int fd, const void* buf, size_t len)
122{
123 return write(fd, buf, len);
124}
125#undef write
126#define write ___xxx_write
127
Spencer Low40babf02018-09-02 19:19:39 -0700128// See the comments for the !defined(_WIN32) version of unix_lseek().
129static __inline__ int unix_lseek(int fd, int pos, int where) {
130 return lseek(fd, pos, where);
131}
132#undef lseek
133#define lseek ___xxx_lseek
134
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700135// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136static __inline__ int adb_open_mode(const char* path, int options, int mode)
137{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200138 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139}
140
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700141// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700142extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143#define open ___xxx_unix_open
144
David Pursellc5b8ad82015-10-28 14:29:51 -0700145// Checks if |fd| corresponds to a console.
146// Standard Windows isatty() returns 1 for both console FDs and character
147// devices like NUL. unix_isatty() performs some extra checking to only match
148// console FDs.
149// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
150// will work but adb_open() FDs will not. Additionally the OS handle associated
151// with |fd| must have GENERIC_READ access (which console FDs have by default).
152// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
153// calling this function is unreliable and should not be used.
154int unix_isatty(int fd);
155#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
Spencer Low5200c662015-07-30 23:07:55 -0700157int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700158
159inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
160 abort();
161}
162
163inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
164 abort();
165}
166
Spencer Low5200c662015-07-30 23:07:55 -0700167int network_connect(const std::string& host, int port, int type, int timeout,
168 std::string* error);
169
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
171
172#undef accept
173#define accept ___xxx_accept
174
David Purselleaae97e2016-04-07 11:25:48 -0700175// Returns the local port number of a bound socket, or -1 on failure.
176int adb_socket_get_local_port(int fd);
177
Spencer Lowf055c192015-01-25 14:40:16 -0800178extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
179
180#undef setsockopt
181#define setsockopt ___xxx_setsockopt
182
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183extern int adb_socketpair( int sv[2] );
184
Josh Gao3777d2e2016-02-16 17:34:53 -0800185struct adb_pollfd {
186 int fd;
187 short events;
188 short revents;
189};
190extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
191#define poll ___xxx_poll
192
Elliott Hughes5c742702015-07-30 17:42:01 -0700193static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
195}
196
Spencer Lowcf4ff642015-05-11 01:08:48 -0700197// UTF-8 versions of POSIX APIs.
198extern DIR* adb_opendir(const char* dirname);
199extern struct dirent* adb_readdir(DIR* dir);
200extern int adb_closedir(DIR* dir);
201
202extern int adb_utime(const char *, struct utimbuf *);
203extern int adb_chmod(const char *, int);
204
Elliott Hughesd8a4c602018-06-26 13:06:15 -0700205extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
206 __attribute__((__format__(__printf__, 2, 0)));
207extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
208extern int adb_fprintf(FILE* stream, const char* format, ...)
209 __attribute__((__format__(__printf__, 2, 3)));
210extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700211
212extern int adb_fputs(const char* buf, FILE* stream);
213extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800214extern int adb_putchar(int ch);
215extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700216extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
217 FILE* stream);
218
219extern FILE* adb_fopen(const char* f, const char* m);
220
221extern char* adb_getenv(const char* name);
222
223extern char* adb_getcwd(char* buf, int size);
224
225// Remap calls to POSIX APIs to our UTF-8 versions.
226#define opendir adb_opendir
227#define readdir adb_readdir
228#define closedir adb_closedir
229#define rewinddir rewinddir_utf8_not_yet_implemented
230#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800231// Some compiler's C++ headers have members named seekdir, so we can't do the
232// macro technique and instead cause a link error if seekdir is called.
233inline void seekdir(DIR*, long) {
234 extern int seekdir_utf8_not_yet_implemented;
235 seekdir_utf8_not_yet_implemented = 1;
236}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700237
238#define utime adb_utime
239#define chmod adb_chmod
240
241#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800242#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700243#define fprintf adb_fprintf
244#define printf adb_printf
245#define fputs adb_fputs
246#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800247// putc may be a macro, so if so, undefine it, so that we can redefine it.
248#undef putc
249#define putc(c, s) adb_fputc(c, s)
250#define putchar adb_putchar
251#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700252#define fwrite adb_fwrite
253
254#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800255#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700256
257#define getenv adb_getenv
258#define putenv putenv_utf8_not_yet_implemented
259#define setenv setenv_utf8_not_yet_implemented
260#define unsetenv unsetenv_utf8_not_yet_implemented
261
262#define getcwd adb_getcwd
263
Spencer Lowcf4ff642015-05-11 01:08:48 -0700264// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
265// passed to main().
266class NarrowArgs {
267public:
268 NarrowArgs(int argc, wchar_t** argv);
269 ~NarrowArgs();
270
271 inline char** data() {
272 return narrow_args;
273 }
274
275private:
276 char** narrow_args;
277};
278
Spencer Low5c398d22015-08-08 15:07:07 -0700279// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
280// so they can fit in an int. To convert back, we just need to sign-extend.
281// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
282// Note that this does not make a HANDLE value work with APIs like open(), nor
283// does this make a value from open() passable to APIs taking a HANDLE. This
284// just lets you take a HANDLE, pass it around as an int, and then use it again
285// as a HANDLE.
286inline int cast_handle_to_int(const HANDLE h) {
287 // truncate
288 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
289}
290
291inline HANDLE cast_int_to_handle(const int fd) {
292 // sign-extend
293 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
294}
295
Spencer Low2122c7a2015-08-26 18:46:09 -0700296// Deleter for unique_handle. Adapted from many sources, including:
297// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
298// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
299class handle_deleter {
300public:
301 typedef HANDLE pointer;
302
303 void operator()(HANDLE h);
304};
305
306// Like std::unique_ptr, but for Windows HANDLE objects that should be
307// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
308// but does not check if the handle != INVALID_HANDLE_VALUE.
309typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
310
Spencer Lowa30b79a2015-11-15 16:29:36 -0800311namespace internal {
312
313size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
314
315}
316
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317#else /* !_WIN32 a.k.a. Unix */
318
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319#include <fcntl.h>
Spencer Low5200c662015-07-30 23:07:55 -0700320#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321#include <netinet/in.h>
322#include <netinet/tcp.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700323#include <poll.h>
324#include <pthread.h>
325#include <signal.h>
326#include <stdarg.h>
327#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328#include <string.h>
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700329#include <sys/stat.h>
330#include <sys/wait.h>
Kenny Root73167412012-10-12 15:26:45 -0700331#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332
Alex Vallée47d67c92015-05-06 16:26:00 -0400333#include <string>
334
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700335#include <cutils/sockets.h>
336
Elliott Hughes5c742702015-07-30 17:42:01 -0700337#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338#define OS_PATH_SEPARATOR '/'
339#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700340#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341
Josh Gao07db1192015-11-07 15:38:19 -0800342static __inline__ bool adb_is_separator(char c) {
343 return c == '/';
344}
345
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346static __inline__ void close_on_exec(int fd)
347{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200348 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349}
350
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700351// Open a file and return a file descriptor that may be used with unix_read(),
352// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
353//
354// On Unix, this is based on open(), so the file descriptor is a real OS file
355// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
356// file descriptor that can only be used with C Runtime APIs (which are wrapped
357// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
358// configurable CR/LF translation which defaults to text mode, but is settable
359// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360static __inline__ int unix_open(const char* path, int options,...)
361{
362 if ((options & O_CREAT) == 0)
363 {
Kenny Root73167412012-10-12 15:26:45 -0700364 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 }
366 else
367 {
368 int mode;
369 va_list args;
370 va_start( args, options );
371 mode = va_arg( args, int );
372 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700373 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 }
375}
376
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700377// Similar to the two-argument adb_open(), but takes a mode parameter for file
378// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
380{
Kenny Root73167412012-10-12 15:26:45 -0700381 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382}
383
384
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700385// Open a file and return a file descriptor that may be used with adb_read(),
386// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
387//
388// On Unix, this is based on open(), but the Windows implementation (in
389// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
390// and its CR/LF translation. The returned file descriptor should be used with
391// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392static __inline__ int adb_open( const char* pathname, int options )
393{
Kenny Root73167412012-10-12 15:26:45 -0700394 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 if (fd < 0)
396 return -1;
397 close_on_exec( fd );
398 return fd;
399}
400#undef open
401#define open ___xxx_open
402
Josh Gao2e1e7892018-03-23 13:03:28 -0700403static __inline__ int adb_shutdown(int fd, int direction = SHUT_RDWR) {
David Pursell1ed57f02015-10-06 15:30:03 -0700404 return shutdown(fd, direction);
405}
Josh Gao2e1e7892018-03-23 13:03:28 -0700406
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400407#undef shutdown
408#define shutdown ____xxx_shutdown
409
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700410// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
411// not designed to take a file descriptor from unix_open(). See the comments
412// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800413__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 return close(fd);
415}
416#undef close
417#define close ____xxx_close
418
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700419// On Windows, ADB has an indirection layer for file descriptors. If we get a
420// Win32 SOCKET object from an external library, we have to map it in to that
421// indirection layer, which this does.
422__inline__ int adb_register_socket(int s) {
423 return s;
424}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425
426static __inline__ int adb_read(int fd, void* buf, size_t len)
427{
Kenny Root73167412012-10-12 15:26:45 -0700428 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429}
430
Spencer Low2e02dc62015-11-07 17:34:39 -0800431// Like unix_read(), but does not handle EINTR.
432static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
433 return read(fd, buf, len);
434}
435
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436#undef read
437#define read ___xxx_read
438
439static __inline__ int adb_write(int fd, const void* buf, size_t len)
440{
Kenny Root73167412012-10-12 15:26:45 -0700441 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442}
443#undef write
444#define write ___xxx_write
445
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700446static __inline__ int64_t adb_lseek(int fd, int64_t pos, int where) {
447#if defined(__APPLE__)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 return lseek(fd, pos, where);
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700449#else
450 return lseek64(fd, pos, where);
451#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452}
Elliott Hughescabfc3d2018-09-20 13:59:49 -0700453#undef lseek
454#define lseek ___xxx_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455
456static __inline__ int adb_unlink(const char* path)
457{
458 return unlink(path);
459}
460#undef unlink
461#define unlink ___xxx_unlink
462
463static __inline__ int adb_creat(const char* path, int mode)
464{
Kenny Root73167412012-10-12 15:26:45 -0700465 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200467 if ( fd < 0 )
468 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
470 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200471 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472}
473#undef creat
474#define creat ___xxx_creat
475
David Pursellc5b8ad82015-10-28 14:29:51 -0700476static __inline__ int unix_isatty(int fd) {
477 return isatty(fd);
478}
479#define isatty ___xxx_isatty
480
Spencer Low5200c662015-07-30 23:07:55 -0700481// Helper for network_* functions.
482inline int _fd_set_error_str(int fd, std::string* error) {
483 if (fd == -1) {
484 *error = strerror(errno);
485 }
486 return fd;
487}
488
Spencer Low5200c662015-07-30 23:07:55 -0700489inline int network_inaddr_any_server(int port, int type, std::string* error) {
490 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
491}
492
Josh Gaocfb21412016-08-24 18:38:44 -0700493inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
494 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
495}
496
497inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
498 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700499}
500
Josh Gao45e3e952018-08-10 16:03:09 -0700501int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700502
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
504{
Benoit Goby95ef8282011-02-01 18:57:41 -0800505 int fd;
506
Kenny Root73167412012-10-12 15:26:45 -0700507 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800508 if (fd >= 0)
509 close_on_exec(fd);
510
511 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512}
513
514#undef accept
515#define accept ___xxx_accept
516
David Purselleaae97e2016-04-07 11:25:48 -0700517inline int adb_socket_get_local_port(int fd) {
518 return socket_get_local_port(fd);
519}
520
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700521// Operate on a file descriptor returned from unix_open() or a well-known file
522// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
523//
524// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
525// adb_write(), adb_close() (which all map to Unix system calls), but the
526// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
527// into the C Runtime and its configurable CR/LF translation (which is settable
528// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529#define unix_read adb_read
530#define unix_write adb_write
Spencer Low40babf02018-09-02 19:19:39 -0700531#define unix_lseek adb_lseek
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532#define unix_close adb_close
533
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700534static __inline__ int adb_thread_setname(const std::string& name) {
535#ifdef __APPLE__
536 return pthread_setname_np(name.c_str());
537#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700538 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
539 // glibc doesn't have strlcpy, so we have to fake it.
540 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
541 strncpy(buf, name.c_str(), sizeof(buf) - 1);
542 buf[sizeof(buf) - 1] = '\0';
543 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700544#endif
545}
546
Spencer Lowf055c192015-01-25 14:40:16 -0800547static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
548{
549 return setsockopt( fd, level, optname, optval, optlen );
550}
551
552#undef setsockopt
553#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554
555static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
556{
557 return socketpair( d, type, protocol, sv );
558}
559
560static __inline__ int adb_socketpair( int sv[2] )
561{
562 int rc;
563
564 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
565 if (rc < 0)
566 return -1;
567
568 close_on_exec( sv[0] );
569 close_on_exec( sv[1] );
570 return 0;
571}
572
573#undef socketpair
574#define socketpair ___xxx_socketpair
575
Josh Gao3777d2e2016-02-16 17:34:53 -0800576typedef struct pollfd adb_pollfd;
577static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
578 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
579}
580
581#define poll ___xxx_poll
582
Alex Vallée47d67c92015-05-06 16:26:00 -0400583static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584{
Alex Vallée47d67c92015-05-06 16:26:00 -0400585 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586}
Alex Vallée47d67c92015-05-06 16:26:00 -0400587
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588#undef mkdir
589#define mkdir ___xxx_mkdir
590
Alex Vallée47d67c92015-05-06 16:26:00 -0400591static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592 return path[0] == '/';
593}
594
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595#endif /* !_WIN32 */
596
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800597static inline void disable_tcp_nagle(int fd) {
598 int off = 1;
599 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
600}
601
David Pursellbfd95032016-02-22 14:27:23 -0800602// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
603// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
604// configured to drop after 10 missed keepalives. Returns true on success.
605bool set_tcp_keepalive(int fd, int interval_sec);
606
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700607#if defined(_WIN32)
608// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
609#undef ERROR
610#endif
611
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612#endif /* _ADB_SYSDEPS_H */