blob: 08fe02934ccdfb39ac20f5ca6282d33c4cec7cd0 [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>
55#include <sys/stat.h>
Spencer Lowcf4ff642015-05-11 01:08:48 -070056#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057#include <winsock2.h>
Stephen Hines2f431a82014-10-01 17:37:06 -070058#include <windows.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059#include <ws2tcpip.h>
Dan Albert630b9af2014-11-24 23:34:35 -080060
Spencer Low2122c7a2015-08-26 18:46:09 -070061#include <memory> // unique_ptr
Spencer Lowd21dc822015-11-12 15:20:15 -080062#include <string>
Alex Vallée47d67c92015-05-06 16:26:00 -040063
Dan Albert630b9af2014-11-24 23:34:35 -080064#include "fdevent.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Elliott Hughes5c742702015-07-30 17:42:01 -070066#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067#define OS_PATH_SEPARATOR '\\'
68#define OS_PATH_SEPARATOR_STR "\\"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -070069#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Josh Gao07db1192015-11-07 15:38:19 -080071static __inline__ bool adb_is_separator(char c) {
72 return c == '\\' || c == '/';
73}
74
Spencer Low50beee32018-09-03 16:03:22 -070075extern int adb_thread_setname(const std::string& name);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -070076
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static __inline__ void close_on_exec(int fd)
78{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020079 /* nothing really */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080}
81
Spencer Lowcf4ff642015-05-11 01:08:48 -070082extern int adb_unlink(const char* path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083#undef unlink
84#define unlink ___xxx_unlink
85
Spencer Lowcf4ff642015-05-11 01:08:48 -070086extern int adb_mkdir(const std::string& path, int mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087#undef mkdir
88#define mkdir ___xxx_mkdir
89
Spencer Low6ac5d7d2015-05-22 20:09:06 -070090// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao2e1e7892018-03-23 13:03:28 -070091extern int adb_open(const char* path, int options);
92extern int adb_creat(const char* path, int mode);
93extern int adb_read(int fd, void* buf, int len);
94extern int adb_write(int fd, const void* buf, int len);
95extern int adb_lseek(int fd, int pos, int where);
96extern int adb_shutdown(int fd, int direction = SHUT_RDWR);
97extern int adb_close(int fd);
98extern int adb_register_socket(SOCKET s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700100// See the comments for the !defined(_WIN32) version of unix_close().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101static __inline__ int unix_close(int fd)
102{
103 return close(fd);
104}
105#undef close
106#define close ____xxx_close
107
Spencer Low2e02dc62015-11-07 17:34:39 -0800108// Like unix_read(), but may return EINTR.
109extern int unix_read_interruptible(int fd, void* buf, size_t len);
110
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700111// See the comments for the !defined(_WIN32) version of unix_read().
Spencer Low2e02dc62015-11-07 17:34:39 -0800112static __inline__ int unix_read(int fd, void* buf, size_t len) {
113 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
114}
Spencer Low50184062015-03-01 15:06:21 -0800115
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116#undef read
117#define read ___xxx_read
118
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700119// See the comments for the !defined(_WIN32) version of unix_write().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120static __inline__ int unix_write(int fd, const void* buf, size_t len)
121{
122 return write(fd, buf, len);
123}
124#undef write
125#define write ___xxx_write
126
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700127// See the comments for the !defined(_WIN32) version of adb_open_mode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128static __inline__ int adb_open_mode(const char* path, int options, int mode)
129{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200130 return adb_open(path, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131}
132
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700133// See the comments for the !defined(_WIN32) version of unix_open().
Spencer Lowcf4ff642015-05-11 01:08:48 -0700134extern int unix_open(const char* path, int options, ...);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135#define open ___xxx_unix_open
136
David Pursellc5b8ad82015-10-28 14:29:51 -0700137// Checks if |fd| corresponds to a console.
138// Standard Windows isatty() returns 1 for both console FDs and character
139// devices like NUL. unix_isatty() performs some extra checking to only match
140// console FDs.
141// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
142// will work but adb_open() FDs will not. Additionally the OS handle associated
143// with |fd| must have GENERIC_READ access (which console FDs have by default).
144// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
145// calling this function is unreliable and should not be used.
146int unix_isatty(int fd);
147#define isatty ___xxx_isatty
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Spencer Low5200c662015-07-30 23:07:55 -0700149int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gaocfb21412016-08-24 18:38:44 -0700150
151inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
152 abort();
153}
154
155inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
156 abort();
157}
158
Spencer Low5200c662015-07-30 23:07:55 -0700159int network_connect(const std::string& host, int port, int type, int timeout,
160 std::string* error);
161
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen);
163
164#undef accept
165#define accept ___xxx_accept
166
David Purselleaae97e2016-04-07 11:25:48 -0700167// Returns the local port number of a bound socket, or -1 on failure.
168int adb_socket_get_local_port(int fd);
169
Spencer Lowf055c192015-01-25 14:40:16 -0800170extern int adb_setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen);
171
172#undef setsockopt
173#define setsockopt ___xxx_setsockopt
174
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175extern int adb_socketpair( int sv[2] );
176
Josh Gao3777d2e2016-02-16 17:34:53 -0800177struct adb_pollfd {
178 int fd;
179 short events;
180 short revents;
181};
182extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
183#define poll ___xxx_poll
184
Elliott Hughes5c742702015-07-30 17:42:01 -0700185static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
187}
188
Spencer Lowcf4ff642015-05-11 01:08:48 -0700189// UTF-8 versions of POSIX APIs.
190extern DIR* adb_opendir(const char* dirname);
191extern struct dirent* adb_readdir(DIR* dir);
192extern int adb_closedir(DIR* dir);
193
194extern int adb_utime(const char *, struct utimbuf *);
195extern int adb_chmod(const char *, int);
196
Elliott Hughesd8a4c602018-06-26 13:06:15 -0700197extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
198 __attribute__((__format__(__printf__, 2, 0)));
199extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
200extern int adb_fprintf(FILE* stream, const char* format, ...)
201 __attribute__((__format__(__printf__, 2, 3)));
202extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Lowcf4ff642015-05-11 01:08:48 -0700203
204extern int adb_fputs(const char* buf, FILE* stream);
205extern int adb_fputc(int ch, FILE* stream);
Spencer Lowa30b79a2015-11-15 16:29:36 -0800206extern int adb_putchar(int ch);
207extern int adb_puts(const char* buf);
Spencer Lowcf4ff642015-05-11 01:08:48 -0700208extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb,
209 FILE* stream);
210
211extern FILE* adb_fopen(const char* f, const char* m);
212
213extern char* adb_getenv(const char* name);
214
215extern char* adb_getcwd(char* buf, int size);
216
217// Remap calls to POSIX APIs to our UTF-8 versions.
218#define opendir adb_opendir
219#define readdir adb_readdir
220#define closedir adb_closedir
221#define rewinddir rewinddir_utf8_not_yet_implemented
222#define telldir telldir_utf8_not_yet_implemented
Spencer Low363af562015-11-07 18:51:54 -0800223// Some compiler's C++ headers have members named seekdir, so we can't do the
224// macro technique and instead cause a link error if seekdir is called.
225inline void seekdir(DIR*, long) {
226 extern int seekdir_utf8_not_yet_implemented;
227 seekdir_utf8_not_yet_implemented = 1;
228}
Spencer Lowcf4ff642015-05-11 01:08:48 -0700229
230#define utime adb_utime
231#define chmod adb_chmod
232
233#define vfprintf adb_vfprintf
Spencer Lowa30b79a2015-11-15 16:29:36 -0800234#define vprintf adb_vprintf
Spencer Lowcf4ff642015-05-11 01:08:48 -0700235#define fprintf adb_fprintf
236#define printf adb_printf
237#define fputs adb_fputs
238#define fputc adb_fputc
Spencer Lowa30b79a2015-11-15 16:29:36 -0800239// putc may be a macro, so if so, undefine it, so that we can redefine it.
240#undef putc
241#define putc(c, s) adb_fputc(c, s)
242#define putchar adb_putchar
243#define puts adb_puts
Spencer Lowcf4ff642015-05-11 01:08:48 -0700244#define fwrite adb_fwrite
245
246#define fopen adb_fopen
Spencer Lowa30b79a2015-11-15 16:29:36 -0800247#define freopen freopen_utf8_not_yet_implemented
Spencer Lowcf4ff642015-05-11 01:08:48 -0700248
249#define getenv adb_getenv
250#define putenv putenv_utf8_not_yet_implemented
251#define setenv setenv_utf8_not_yet_implemented
252#define unsetenv unsetenv_utf8_not_yet_implemented
253
254#define getcwd adb_getcwd
255
Spencer Lowcf4ff642015-05-11 01:08:48 -0700256// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
257// passed to main().
258class NarrowArgs {
259public:
260 NarrowArgs(int argc, wchar_t** argv);
261 ~NarrowArgs();
262
263 inline char** data() {
264 return narrow_args;
265 }
266
267private:
268 char** narrow_args;
269};
270
Spencer Low5c398d22015-08-08 15:07:07 -0700271// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
272// so they can fit in an int. To convert back, we just need to sign-extend.
273// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
274// Note that this does not make a HANDLE value work with APIs like open(), nor
275// does this make a value from open() passable to APIs taking a HANDLE. This
276// just lets you take a HANDLE, pass it around as an int, and then use it again
277// as a HANDLE.
278inline int cast_handle_to_int(const HANDLE h) {
279 // truncate
280 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
281}
282
283inline HANDLE cast_int_to_handle(const int fd) {
284 // sign-extend
285 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
286}
287
Spencer Low2122c7a2015-08-26 18:46:09 -0700288// Deleter for unique_handle. Adapted from many sources, including:
289// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
290// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
291class handle_deleter {
292public:
293 typedef HANDLE pointer;
294
295 void operator()(HANDLE h);
296};
297
298// Like std::unique_ptr, but for Windows HANDLE objects that should be
299// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
300// but does not check if the handle != INVALID_HANDLE_VALUE.
301typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
302
Spencer Lowa30b79a2015-11-15 16:29:36 -0800303namespace internal {
304
305size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
306
307}
308
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309#else /* !_WIN32 a.k.a. Unix */
310
Spencer Low5200c662015-07-30 23:07:55 -0700311#include <cutils/sockets.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312#include <fcntl.h>
Josh Gao3777d2e2016-02-16 17:34:53 -0800313#include <poll.h>
314#include <signal.h>
315#include <sys/stat.h>
316#include <sys/wait.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317
318#include <pthread.h>
319#include <unistd.h>
320#include <fcntl.h>
321#include <stdarg.h>
Spencer Low5200c662015-07-30 23:07:55 -0700322#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323#include <netinet/in.h>
324#include <netinet/tcp.h>
325#include <string.h>
Kenny Root73167412012-10-12 15:26:45 -0700326#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327
Alex Vallée47d67c92015-05-06 16:26:00 -0400328#include <string>
329
Elliott Hughes5c742702015-07-30 17:42:01 -0700330#define OS_PATH_SEPARATORS "/"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331#define OS_PATH_SEPARATOR '/'
332#define OS_PATH_SEPARATOR_STR "/"
Benoit Gobyd5fcafa2012-04-12 12:23:49 -0700333#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334
Josh Gao07db1192015-11-07 15:38:19 -0800335static __inline__ bool adb_is_separator(char c) {
336 return c == '/';
337}
338
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339static __inline__ void close_on_exec(int fd)
340{
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200341 fcntl( fd, F_SETFD, FD_CLOEXEC );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342}
343
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700344// Open a file and return a file descriptor that may be used with unix_read(),
345// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
346//
347// On Unix, this is based on open(), so the file descriptor is a real OS file
348// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
349// file descriptor that can only be used with C Runtime APIs (which are wrapped
350// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
351// configurable CR/LF translation which defaults to text mode, but is settable
352// with _setmode().
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353static __inline__ int unix_open(const char* path, int options,...)
354{
355 if ((options & O_CREAT) == 0)
356 {
Kenny Root73167412012-10-12 15:26:45 -0700357 return TEMP_FAILURE_RETRY( open(path, options) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 }
359 else
360 {
361 int mode;
362 va_list args;
363 va_start( args, options );
364 mode = va_arg( args, int );
365 va_end( args );
Kenny Root73167412012-10-12 15:26:45 -0700366 return TEMP_FAILURE_RETRY( open( path, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 }
368}
369
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700370// Similar to the two-argument adb_open(), but takes a mode parameter for file
371// creation. See adb_open() for more info.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372static __inline__ int adb_open_mode( const char* pathname, int options, int mode )
373{
Kenny Root73167412012-10-12 15:26:45 -0700374 return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375}
376
377
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700378// Open a file and return a file descriptor that may be used with adb_read(),
379// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
380//
381// On Unix, this is based on open(), but the Windows implementation (in
382// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
383// and its CR/LF translation. The returned file descriptor should be used with
384// adb_read(), adb_write(), adb_close(), etc.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385static __inline__ int adb_open( const char* pathname, int options )
386{
Kenny Root73167412012-10-12 15:26:45 -0700387 int fd = TEMP_FAILURE_RETRY( open( pathname, options ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 if (fd < 0)
389 return -1;
390 close_on_exec( fd );
391 return fd;
392}
393#undef open
394#define open ___xxx_open
395
Josh Gao2e1e7892018-03-23 13:03:28 -0700396static __inline__ int adb_shutdown(int fd, int direction = SHUT_RDWR) {
David Pursell1ed57f02015-10-06 15:30:03 -0700397 return shutdown(fd, direction);
398}
Josh Gao2e1e7892018-03-23 13:03:28 -0700399
Mike Lockwood8cf0d592009-10-11 23:04:18 -0400400#undef shutdown
401#define shutdown ____xxx_shutdown
402
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700403// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
404// not designed to take a file descriptor from unix_open(). See the comments
405// for adb_open() for more info.
Josh Gaof0d3b4f2016-03-04 15:15:56 -0800406__inline__ int adb_close(int fd) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 return close(fd);
408}
409#undef close
410#define close ____xxx_close
411
Casey Dahlin2fe9b602016-09-21 14:03:39 -0700412// On Windows, ADB has an indirection layer for file descriptors. If we get a
413// Win32 SOCKET object from an external library, we have to map it in to that
414// indirection layer, which this does.
415__inline__ int adb_register_socket(int s) {
416 return s;
417}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418
419static __inline__ int adb_read(int fd, void* buf, size_t len)
420{
Kenny Root73167412012-10-12 15:26:45 -0700421 return TEMP_FAILURE_RETRY( read( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422}
423
Spencer Low2e02dc62015-11-07 17:34:39 -0800424// Like unix_read(), but does not handle EINTR.
425static __inline__ int unix_read_interruptible(int fd, void* buf, size_t len) {
426 return read(fd, buf, len);
427}
428
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429#undef read
430#define read ___xxx_read
431
432static __inline__ int adb_write(int fd, const void* buf, size_t len)
433{
Kenny Root73167412012-10-12 15:26:45 -0700434 return TEMP_FAILURE_RETRY( write( fd, buf, len ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435}
436#undef write
437#define write ___xxx_write
438
439static __inline__ int adb_lseek(int fd, int pos, int where)
440{
441 return lseek(fd, pos, where);
442}
443#undef lseek
444#define lseek ___xxx_lseek
445
446static __inline__ int adb_unlink(const char* path)
447{
448 return unlink(path);
449}
450#undef unlink
451#define unlink ___xxx_unlink
452
453static __inline__ int adb_creat(const char* path, int mode)
454{
Kenny Root73167412012-10-12 15:26:45 -0700455 int fd = TEMP_FAILURE_RETRY( creat( path, mode ) );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200457 if ( fd < 0 )
458 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459
460 close_on_exec(fd);
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +0200461 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462}
463#undef creat
464#define creat ___xxx_creat
465
David Pursellc5b8ad82015-10-28 14:29:51 -0700466static __inline__ int unix_isatty(int fd) {
467 return isatty(fd);
468}
469#define isatty ___xxx_isatty
470
Spencer Low5200c662015-07-30 23:07:55 -0700471// Helper for network_* functions.
472inline int _fd_set_error_str(int fd, std::string* error) {
473 if (fd == -1) {
474 *error = strerror(errno);
475 }
476 return fd;
477}
478
Spencer Low5200c662015-07-30 23:07:55 -0700479inline int network_inaddr_any_server(int port, int type, std::string* error) {
480 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
481}
482
Josh Gaocfb21412016-08-24 18:38:44 -0700483inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
484 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
485}
486
487inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
488 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low5200c662015-07-30 23:07:55 -0700489}
490
Josh Gao45e3e952018-08-10 16:03:09 -0700491int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low5200c662015-07-30 23:07:55 -0700492
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen)
494{
Benoit Goby95ef8282011-02-01 18:57:41 -0800495 int fd;
496
Kenny Root73167412012-10-12 15:26:45 -0700497 fd = TEMP_FAILURE_RETRY( accept( serverfd, addr, addrlen ) );
Benoit Goby95ef8282011-02-01 18:57:41 -0800498 if (fd >= 0)
499 close_on_exec(fd);
500
501 return fd;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502}
503
504#undef accept
505#define accept ___xxx_accept
506
David Purselleaae97e2016-04-07 11:25:48 -0700507inline int adb_socket_get_local_port(int fd) {
508 return socket_get_local_port(fd);
509}
510
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700511// Operate on a file descriptor returned from unix_open() or a well-known file
512// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
513//
514// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
515// adb_write(), adb_close() (which all map to Unix system calls), but the
516// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
517// into the C Runtime and its configurable CR/LF translation (which is settable
518// via _setmode()).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519#define unix_read adb_read
520#define unix_write adb_write
521#define unix_close adb_close
522
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700523static __inline__ int adb_thread_setname(const std::string& name) {
524#ifdef __APPLE__
525 return pthread_setname_np(name.c_str());
526#else
Elliott Hughes7462f182017-08-02 13:22:38 -0700527 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
528 // glibc doesn't have strlcpy, so we have to fake it.
529 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
530 strncpy(buf, name.c_str(), sizeof(buf) - 1);
531 buf[sizeof(buf) - 1] = '\0';
532 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700533#endif
534}
535
Spencer Lowf055c192015-01-25 14:40:16 -0800536static __inline__ int adb_setsockopt( int fd, int level, int optname, const void* optval, socklen_t optlen )
537{
538 return setsockopt( fd, level, optname, optval, optlen );
539}
540
541#undef setsockopt
542#define setsockopt ___xxx_setsockopt
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543
544static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] )
545{
546 return socketpair( d, type, protocol, sv );
547}
548
549static __inline__ int adb_socketpair( int sv[2] )
550{
551 int rc;
552
553 rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv );
554 if (rc < 0)
555 return -1;
556
557 close_on_exec( sv[0] );
558 close_on_exec( sv[1] );
559 return 0;
560}
561
562#undef socketpair
563#define socketpair ___xxx_socketpair
564
Josh Gao3777d2e2016-02-16 17:34:53 -0800565typedef struct pollfd adb_pollfd;
566static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
567 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
568}
569
570#define poll ___xxx_poll
571
Alex Vallée47d67c92015-05-06 16:26:00 -0400572static __inline__ int adb_mkdir(const std::string& path, int mode)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573{
Alex Vallée47d67c92015-05-06 16:26:00 -0400574 return mkdir(path.c_str(), mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575}
Alex Vallée47d67c92015-05-06 16:26:00 -0400576
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577#undef mkdir
578#define mkdir ___xxx_mkdir
579
Alex Vallée47d67c92015-05-06 16:26:00 -0400580static __inline__ int adb_is_absolute_host_path(const char* path) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581 return path[0] == '/';
582}
583
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584#endif /* !_WIN32 */
585
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800586static inline void disable_tcp_nagle(int fd) {
587 int off = 1;
588 adb_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
589}
590
David Pursellbfd95032016-02-22 14:27:23 -0800591// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
592// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
593// configured to drop after 10 missed keepalives. Returns true on success.
594bool set_tcp_keepalive(int fd, int interval_sec);
595
Elliott Hughes0aeb5052016-06-29 17:42:01 -0700596#if defined(_WIN32)
597// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
598#undef ERROR
599#endif
600
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601#endif /* _ADB_SYSDEPS_H */