blob: 5ba85b4d8d6c9059a2e2352d9c8047cc92b61a56 [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
Elliott Hughes915d67f2020-04-06 09:15:35 -070017#pragma once
18
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080019/* this file contains system-dependent definitions used by ADB
20 * they're related to threads, sockets and file descriptors
21 */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022
23#ifdef __CYGWIN__
24# undef _WIN32
25#endif
26
Elliott Hughes43df1092015-07-23 17:12:58 -070027#include <errno.h>
28
Joshua Duongf4ba8d72021-01-13 12:18:15 -080029#include <optional>
Spencer Low753d4852015-07-30 23:07:55 -070030#include <string>
Josh Gaof0c44032018-12-12 16:12:28 -080031#include <string_view>
Spencer Lowf373c352015-11-15 16:29:36 -080032#include <vector>
Spencer Low753d4852015-07-30 23:07:55 -070033
Josh Gaod91139c2016-05-13 14:52:06 -070034// Include this before open/close/unlink are defined as macros below.
Josh Gaoa4f5e032016-02-09 14:59:09 -080035#include <android-base/errors.h>
Elliott Hughes8bddf242017-11-28 18:05:27 -080036#include <android-base/macros.h>
Adrian Roosdbfe01d2019-08-19 14:37:19 +020037#include <android-base/off64_t.h>
Josh Gaod91139c2016-05-13 14:52:06 -070038#include <android-base/unique_fd.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080039#include <android-base/utf8.h>
Spencer Low50f5bf12015-11-12 15:20:15 -080040
Josh Gao90228a62019-04-25 14:04:57 -070041#include "adb_unique_fd.h"
Josh Gao75e96bb2016-12-05 13:24:48 -080042#include "sysdeps/errno.h"
Josh Gao1f6a57a2017-04-12 13:57:06 -070043#include "sysdeps/network.h"
Josh Gaoa4f9c172016-07-28 18:09:48 -070044#include "sysdeps/stat.h"
45
Josh Gao659ec672020-04-03 10:12:44 -070046#if defined(__APPLE__)
Elliott Hughes915d67f2020-04-06 09:15:35 -070047static inline void* mempcpy(void* dst, const void* src, size_t n) {
Josh Gao659ec672020-04-03 10:12:44 -070048 return static_cast<char*>(memcpy(dst, src, n)) + n;
49}
50#endif
51
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080052#ifdef _WIN32
53
Josh Gao23f6f2b2016-01-29 12:08:34 -080054// Clang-only nullability specifiers
55#define _Nonnull
56#define _Nullable
57
Dan Albertbbbbea62014-11-24 23:34:35 -080058#include <ctype.h>
59#include <direct.h>
Spencer Low6815c072015-05-11 01:08:48 -070060#include <dirent.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080061#include <errno.h>
62#include <fcntl.h>
63#include <io.h>
Joshua Duongf4ba8d72021-01-13 12:18:15 -080064#include <mswsock.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080065#include <process.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -070066#include <stdint.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080067#include <sys/stat.h>
Spencer Low6815c072015-05-11 01:08:48 -070068#include <utime.h>
Stephen Hinesb1170852014-10-01 17:37:06 -070069#include <windows.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -070070#include <winsock2.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080071#include <ws2tcpip.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080072
Spencer Low2bbb3a92015-08-26 18:46:09 -070073#include <memory> // unique_ptr
Spencer Low50f5bf12015-11-12 15:20:15 -080074#include <string>
Alex Vallée28d1f8d2015-05-06 16:26:00 -040075
Elliott Hughesd189cfb2015-07-30 17:42:01 -070076#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080077#define OS_PATH_SEPARATOR '\\'
78#define OS_PATH_SEPARATOR_STR "\\"
Benoit Goby2cc19e42012-04-12 12:23:49 -070079#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080080
Elliott Hughes915d67f2020-04-06 09:15:35 -070081static inline bool adb_is_separator(char c) {
Josh Gao8acf06c2015-11-07 15:38:19 -080082 return c == '\\' || c == '/';
83}
84
Spencer Lowae37a312018-09-03 16:03:22 -070085extern int adb_thread_setname(const std::string& name);
Siva Velusamy2669cf92015-08-28 16:37:29 -070086
Elliott Hughes915d67f2020-04-06 09:15:35 -070087static inline void close_on_exec(borrowed_fd fd) {
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020088 /* nothing really */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080089}
90
Josh Gao90228a62019-04-25 14:04:57 -070091extern int adb_unlink(const char* path);
92#undef unlink
93#define unlink ___xxx_unlink
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080094
Spencer Low6815c072015-05-11 01:08:48 -070095extern int adb_mkdir(const std::string& path, int mode);
Josh Gao90228a62019-04-25 14:04:57 -070096#undef mkdir
97#define mkdir ___xxx_mkdir
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080098
Joshua Duong290ccb52019-11-20 14:18:43 -080099extern int adb_rename(const char* oldpath, const char* newpath);
100
Spencer Low3a2421b2015-05-22 20:09:06 -0700101// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao96049b92018-03-23 13:03:28 -0700102extern int adb_open(const char* path, int options);
103extern int adb_creat(const char* path, int mode);
Josh Gao90228a62019-04-25 14:04:57 -0700104extern int adb_read(borrowed_fd fd, void* buf, int len);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700105extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
Josh Gao90228a62019-04-25 14:04:57 -0700106extern int adb_write(borrowed_fd fd, const void* buf, int len);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700107extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
Josh Gao90228a62019-04-25 14:04:57 -0700108extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
109extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
Josh Gao96049b92018-03-23 13:03:28 -0700110extern int adb_close(int fd);
111extern int adb_register_socket(SOCKET s);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700112extern HANDLE adb_get_os_handle(borrowed_fd fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800113
Joshua Duong290ccb52019-11-20 14:18:43 -0800114extern int adb_gethostname(char* name, size_t len);
115extern int adb_getlogin_r(char* buf, size_t bufsize);
116
Spencer Low3a2421b2015-05-22 20:09:06 -0700117// See the comments for the !defined(_WIN32) version of unix_close().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700118static inline int unix_close(int fd) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800119 return close(fd);
120}
Josh Gao90228a62019-04-25 14:04:57 -0700121#undef close
122#define close ____xxx_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800123
Spencer Low55441402015-11-07 17:34:39 -0800124// Like unix_read(), but may return EINTR.
Josh Gao90228a62019-04-25 14:04:57 -0700125extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
Spencer Low55441402015-11-07 17:34:39 -0800126
Spencer Low3a2421b2015-05-22 20:09:06 -0700127// See the comments for the !defined(_WIN32) version of unix_read().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700128static inline int unix_read(borrowed_fd fd, void* buf, size_t len) {
Spencer Low55441402015-11-07 17:34:39 -0800129 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
130}
Spencer Lowbeb61982015-03-01 15:06:21 -0800131
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800132#undef read
133#define read ___xxx_read
134
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700135#undef pread
136#define pread ___xxx_pread
137
Spencer Low3a2421b2015-05-22 20:09:06 -0700138// See the comments for the !defined(_WIN32) version of unix_write().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700139static inline int unix_write(borrowed_fd fd, const void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700140 return write(fd.get(), buf, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800141}
142#undef write
143#define write ___xxx_write
144
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700145#undef pwrite
146#define pwrite ___xxx_pwrite
147
Spencer Low04b575d2018-09-02 19:19:39 -0700148// See the comments for the !defined(_WIN32) version of unix_lseek().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700149static inline int unix_lseek(borrowed_fd fd, int pos, int where) {
Josh Gao90228a62019-04-25 14:04:57 -0700150 return lseek(fd.get(), pos, where);
Spencer Low04b575d2018-09-02 19:19:39 -0700151}
152#undef lseek
153#define lseek ___xxx_lseek
154
Spencer Low3a2421b2015-05-22 20:09:06 -0700155// See the comments for the !defined(_WIN32) version of adb_open_mode().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700156static inline int adb_open_mode(const char* path, int options, int mode) {
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200157 return adb_open(path, options);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800158}
159
Spencer Low3a2421b2015-05-22 20:09:06 -0700160// See the comments for the !defined(_WIN32) version of unix_open().
Josh Gaof0c44032018-12-12 16:12:28 -0800161extern int unix_open(std::string_view path, int options, ...);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800162#define open ___xxx_unix_open
163
David Pursell58805362015-10-28 14:29:51 -0700164// Checks if |fd| corresponds to a console.
165// Standard Windows isatty() returns 1 for both console FDs and character
166// devices like NUL. unix_isatty() performs some extra checking to only match
167// console FDs.
168// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
169// will work but adb_open() FDs will not. Additionally the OS handle associated
170// with |fd| must have GENERIC_READ access (which console FDs have by default).
171// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
172// calling this function is unreliable and should not be used.
Josh Gao90228a62019-04-25 14:04:57 -0700173int unix_isatty(borrowed_fd fd);
David Pursell58805362015-10-28 14:29:51 -0700174#define isatty ___xxx_isatty
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800175
Spencer Low753d4852015-07-30 23:07:55 -0700176int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700177
178inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
179 abort();
180}
181
182inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
183 abort();
184}
185
Spencer Low753d4852015-07-30 23:07:55 -0700186int network_connect(const std::string& host, int port, int type, int timeout,
187 std::string* error);
188
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800189std::optional<ssize_t> network_peek(borrowed_fd fd);
190
Josh Gao90228a62019-04-25 14:04:57 -0700191extern int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192
193#undef accept
194#define accept ___xxx_accept
195
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800196int adb_getsockname(borrowed_fd fd, struct sockaddr* sockaddr, socklen_t* addrlen);
197
David Pursell19d0c232016-04-07 11:25:48 -0700198// Returns the local port number of a bound socket, or -1 on failure.
Josh Gao90228a62019-04-25 14:04:57 -0700199int adb_socket_get_local_port(borrowed_fd fd);
David Pursell19d0c232016-04-07 11:25:48 -0700200
Josh Gao90228a62019-04-25 14:04:57 -0700201extern int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
202 socklen_t optlen);
Spencer Low31aafa62015-01-25 14:40:16 -0800203
204#undef setsockopt
205#define setsockopt ___xxx_setsockopt
206
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800207// Wrapper around socket() call. On Windows, ADB has an indirection layer for file descriptors.
208extern int adb_socket(int domain, int type, int protocol);
209
210// Wrapper around bind() call, as Windows has indirection layer.
211extern int adb_bind(borrowed_fd fd, const sockaddr* addr, int namelen);
212
Josh Gao90228a62019-04-25 14:04:57 -0700213extern int adb_socketpair(int sv[2]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800214
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800215// Posix compatibility layer for msghdr
216struct adb_msghdr {
217 void* msg_name;
218 socklen_t msg_namelen;
219 struct adb_iovec* msg_iov;
220 size_t msg_iovlen;
221 void* msg_control;
222 size_t msg_controllen;
223 int msg_flags;
224};
225
226ssize_t adb_sendmsg(borrowed_fd fd, const adb_msghdr* msg, int flags);
227ssize_t adb_recvmsg(borrowed_fd fd, adb_msghdr* msg, int flags);
228
229using adb_cmsghdr = WSACMSGHDR;
230
231extern adb_cmsghdr* adb_CMSG_FIRSTHDR(adb_msghdr* msgh);
232extern adb_cmsghdr* adb_CMSG_NXTHDR(adb_msghdr* msgh, adb_cmsghdr* cmsg);
233extern unsigned char* adb_CMSG_DATA(adb_cmsghdr* cmsg);
234
Josh Gaoe7388122016-02-16 17:34:53 -0800235struct adb_pollfd {
236 int fd;
237 short events;
238 short revents;
239};
240extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
241#define poll ___xxx_poll
242
Elliott Hughes915d67f2020-04-06 09:15:35 -0700243static inline int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800244 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
245}
246
Spencer Low6815c072015-05-11 01:08:48 -0700247// UTF-8 versions of POSIX APIs.
248extern DIR* adb_opendir(const char* dirname);
249extern struct dirent* adb_readdir(DIR* dir);
250extern int adb_closedir(DIR* dir);
251
252extern int adb_utime(const char *, struct utimbuf *);
253extern int adb_chmod(const char *, int);
254
Elliott Hughes874c9412018-06-26 13:06:15 -0700255extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
256 __attribute__((__format__(__printf__, 2, 0)));
257extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
258extern int adb_fprintf(FILE* stream, const char* format, ...)
259 __attribute__((__format__(__printf__, 2, 3)));
260extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Low6815c072015-05-11 01:08:48 -0700261
262extern int adb_fputs(const char* buf, FILE* stream);
263extern int adb_fputc(int ch, FILE* stream);
Spencer Lowf373c352015-11-15 16:29:36 -0800264extern int adb_putchar(int ch);
265extern int adb_puts(const char* buf);
Josh Gao90228a62019-04-25 14:04:57 -0700266extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
Spencer Low6815c072015-05-11 01:08:48 -0700267
268extern FILE* adb_fopen(const char* f, const char* m);
269
270extern char* adb_getenv(const char* name);
271
272extern char* adb_getcwd(char* buf, int size);
273
274// Remap calls to POSIX APIs to our UTF-8 versions.
275#define opendir adb_opendir
276#define readdir adb_readdir
277#define closedir adb_closedir
278#define rewinddir rewinddir_utf8_not_yet_implemented
279#define telldir telldir_utf8_not_yet_implemented
Spencer Low28bc2cb2015-11-07 18:51:54 -0800280// Some compiler's C++ headers have members named seekdir, so we can't do the
281// macro technique and instead cause a link error if seekdir is called.
282inline void seekdir(DIR*, long) {
283 extern int seekdir_utf8_not_yet_implemented;
284 seekdir_utf8_not_yet_implemented = 1;
285}
Spencer Low6815c072015-05-11 01:08:48 -0700286
287#define utime adb_utime
288#define chmod adb_chmod
289
290#define vfprintf adb_vfprintf
Spencer Lowf373c352015-11-15 16:29:36 -0800291#define vprintf adb_vprintf
Spencer Low6815c072015-05-11 01:08:48 -0700292#define fprintf adb_fprintf
293#define printf adb_printf
294#define fputs adb_fputs
295#define fputc adb_fputc
Spencer Lowf373c352015-11-15 16:29:36 -0800296// putc may be a macro, so if so, undefine it, so that we can redefine it.
297#undef putc
298#define putc(c, s) adb_fputc(c, s)
299#define putchar adb_putchar
300#define puts adb_puts
Spencer Low6815c072015-05-11 01:08:48 -0700301#define fwrite adb_fwrite
302
303#define fopen adb_fopen
Spencer Lowf373c352015-11-15 16:29:36 -0800304#define freopen freopen_utf8_not_yet_implemented
Spencer Low6815c072015-05-11 01:08:48 -0700305
306#define getenv adb_getenv
307#define putenv putenv_utf8_not_yet_implemented
308#define setenv setenv_utf8_not_yet_implemented
309#define unsetenv unsetenv_utf8_not_yet_implemented
310
311#define getcwd adb_getcwd
312
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800313// A very simple wrapper over a launched child process
314class Process {
315 public:
316 constexpr explicit Process(HANDLE h = nullptr) : h_(h) {}
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700317 constexpr Process(Process&& other) : h_(std::exchange(other.h_, nullptr)) {}
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800318 ~Process() { close(); }
319 constexpr explicit operator bool() const { return h_ != nullptr; }
320
321 void wait() {
322 if (*this) {
323 ::WaitForSingleObject(h_, INFINITE);
324 close();
325 }
326 }
327 void kill() {
328 if (*this) {
329 ::TerminateProcess(h_, -1);
330 }
331 }
332
333 private:
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700334 DISALLOW_COPY_AND_ASSIGN(Process);
335
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800336 void close() {
337 if (*this) {
338 ::CloseHandle(h_);
339 h_ = nullptr;
340 }
341 }
342
343 HANDLE h_;
344};
345
346Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
347 std::initializer_list<int> fds_to_inherit = {});
348
Spencer Low6815c072015-05-11 01:08:48 -0700349// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
350// passed to main().
351class NarrowArgs {
352public:
353 NarrowArgs(int argc, wchar_t** argv);
354 ~NarrowArgs();
355
356 inline char** data() {
357 return narrow_args;
358 }
359
360private:
361 char** narrow_args;
362};
363
Spencer Lowb28812f2015-08-08 15:07:07 -0700364// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
365// so they can fit in an int. To convert back, we just need to sign-extend.
366// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
367// Note that this does not make a HANDLE value work with APIs like open(), nor
368// does this make a value from open() passable to APIs taking a HANDLE. This
369// just lets you take a HANDLE, pass it around as an int, and then use it again
370// as a HANDLE.
371inline int cast_handle_to_int(const HANDLE h) {
372 // truncate
373 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
374}
375
376inline HANDLE cast_int_to_handle(const int fd) {
377 // sign-extend
378 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
379}
380
Spencer Low2bbb3a92015-08-26 18:46:09 -0700381// Deleter for unique_handle. Adapted from many sources, including:
382// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
383// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
384class handle_deleter {
385public:
386 typedef HANDLE pointer;
387
388 void operator()(HANDLE h);
389};
390
391// Like std::unique_ptr, but for Windows HANDLE objects that should be
392// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
393// but does not check if the handle != INVALID_HANDLE_VALUE.
394typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
395
Spencer Lowf373c352015-11-15 16:29:36 -0800396namespace internal {
397
398size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
399
400}
401
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402#else /* !_WIN32 a.k.a. Unix */
403
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404#include <fcntl.h>
Spencer Low753d4852015-07-30 23:07:55 -0700405#include <netdb.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800406#include <netinet/in.h>
407#include <netinet/tcp.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700408#include <poll.h>
409#include <pthread.h>
410#include <signal.h>
411#include <stdarg.h>
412#include <stdint.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800413#include <string.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700414#include <sys/stat.h>
415#include <sys/wait.h>
Kenny Rooteac025c2012-10-12 15:26:45 -0700416#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800417
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400418#include <string>
419
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700420#include <cutils/sockets.h>
421
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700422#define OS_PATH_SEPARATORS "/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423#define OS_PATH_SEPARATOR '/'
424#define OS_PATH_SEPARATOR_STR "/"
Benoit Goby2cc19e42012-04-12 12:23:49 -0700425#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800426
Elliott Hughes915d67f2020-04-06 09:15:35 -0700427static inline bool adb_is_separator(char c) {
Josh Gao8acf06c2015-11-07 15:38:19 -0800428 return c == '/';
429}
430
Elliott Hughes915d67f2020-04-06 09:15:35 -0700431static inline int get_fd_flags(borrowed_fd fd) {
Daniel Colascione3e124692019-11-13 17:49:37 -0800432 return fcntl(fd.get(), F_GETFD);
433}
434
Elliott Hughes915d67f2020-04-06 09:15:35 -0700435static inline void close_on_exec(borrowed_fd fd) {
Daniel Colascione3e124692019-11-13 17:49:37 -0800436 int flags = get_fd_flags(fd);
437 if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
438 fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
439 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800440}
441
Spencer Low3a2421b2015-05-22 20:09:06 -0700442// Open a file and return a file descriptor that may be used with unix_read(),
443// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
444//
445// On Unix, this is based on open(), so the file descriptor is a real OS file
446// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
447// file descriptor that can only be used with C Runtime APIs (which are wrapped
448// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
449// configurable CR/LF translation which defaults to text mode, but is settable
450// with _setmode().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700451static inline int unix_open(std::string_view path, int options, ...) {
Josh Gaof0c44032018-12-12 16:12:28 -0800452 std::string zero_terminated(path.begin(), path.end());
453 if ((options & O_CREAT) == 0) {
454 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
455 } else {
456 int mode;
457 va_list args;
458 va_start(args, options);
459 mode = va_arg(args, int);
460 va_end(args);
461 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800462 }
463}
464
Spencer Low3a2421b2015-05-22 20:09:06 -0700465// Similar to the two-argument adb_open(), but takes a mode parameter for file
466// creation. See adb_open() for more info.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700467static inline int adb_open_mode(const char* pathname, int options, int mode) {
Josh Gao90228a62019-04-25 14:04:57 -0700468 return TEMP_FAILURE_RETRY(open(pathname, options, mode));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800469}
470
Spencer Low3a2421b2015-05-22 20:09:06 -0700471// Open a file and return a file descriptor that may be used with adb_read(),
472// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
473//
474// On Unix, this is based on open(), but the Windows implementation (in
475// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
476// and its CR/LF translation. The returned file descriptor should be used with
477// adb_read(), adb_write(), adb_close(), etc.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700478static inline int adb_open(const char* pathname, int options) {
Josh Gao90228a62019-04-25 14:04:57 -0700479 int fd = TEMP_FAILURE_RETRY(open(pathname, options));
480 if (fd < 0) return -1;
481 close_on_exec(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482 return fd;
483}
Josh Gao90228a62019-04-25 14:04:57 -0700484#undef open
485#define open ___xxx_open
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800486
Elliott Hughes915d67f2020-04-06 09:15:35 -0700487static inline int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
Josh Gao90228a62019-04-25 14:04:57 -0700488 return shutdown(fd.get(), direction);
David Pursell3fe11f62015-10-06 15:30:03 -0700489}
Josh Gao96049b92018-03-23 13:03:28 -0700490
Josh Gao90228a62019-04-25 14:04:57 -0700491#undef shutdown
492#define shutdown ____xxx_shutdown
Mike Lockwood81ffe172009-10-11 23:04:18 -0400493
Spencer Low3a2421b2015-05-22 20:09:06 -0700494// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
495// not designed to take a file descriptor from unix_open(). See the comments
496// for adb_open() for more info.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700497inline int adb_close(int fd) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800498 return close(fd);
499}
Josh Gao90228a62019-04-25 14:04:57 -0700500#undef close
501#define close ____xxx_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800502
Casey Dahlin20238f22016-09-21 14:03:39 -0700503// On Windows, ADB has an indirection layer for file descriptors. If we get a
504// Win32 SOCKET object from an external library, we have to map it in to that
505// indirection layer, which this does.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700506inline int adb_register_socket(int s) {
Casey Dahlin20238f22016-09-21 14:03:39 -0700507 return s;
508}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800509
Elliott Hughes915d67f2020-04-06 09:15:35 -0700510static inline int adb_gethostname(char* name, size_t len) {
Joshua Duong290ccb52019-11-20 14:18:43 -0800511 return gethostname(name, len);
512}
513
Elliott Hughes915d67f2020-04-06 09:15:35 -0700514static inline int adb_getlogin_r(char* buf, size_t bufsize) {
Joshua Duong290ccb52019-11-20 14:18:43 -0800515 return getlogin_r(buf, bufsize);
516}
517
Elliott Hughes915d67f2020-04-06 09:15:35 -0700518static inline int adb_read(borrowed_fd fd, void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700519 return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800520}
521
Elliott Hughes915d67f2020-04-06 09:15:35 -0700522static inline int adb_pread(borrowed_fd fd, void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700523#if defined(__APPLE__)
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800524 return TEMP_FAILURE_RETRY(pread(fd.get(), buf, len, offset));
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700525#else
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800526 return TEMP_FAILURE_RETRY(pread64(fd.get(), buf, len, offset));
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700527#endif
528}
529
Spencer Low55441402015-11-07 17:34:39 -0800530// Like unix_read(), but does not handle EINTR.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700531static inline int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700532 return read(fd.get(), buf, len);
Spencer Low55441402015-11-07 17:34:39 -0800533}
534
Josh Gao90228a62019-04-25 14:04:57 -0700535#undef read
536#define read ___xxx_read
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700537#undef pread
538#define pread ___xxx_pread
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800539
Elliott Hughes915d67f2020-04-06 09:15:35 -0700540static inline int adb_write(borrowed_fd fd, const void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700541 return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800542}
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700543
Elliott Hughes915d67f2020-04-06 09:15:35 -0700544static inline int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700545#if defined(__APPLE__)
546 return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
547#else
548 return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
549#endif
550}
551
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800552#undef write
553#define write ___xxx_write
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700554#undef pwrite
555#define pwrite ___xxx_pwrite
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800556
Elliott Hughes915d67f2020-04-06 09:15:35 -0700557static inline int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700558#if defined(__APPLE__)
Josh Gao90228a62019-04-25 14:04:57 -0700559 return lseek(fd.get(), pos, where);
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700560#else
Josh Gao90228a62019-04-25 14:04:57 -0700561 return lseek64(fd.get(), pos, where);
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700562#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800563}
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700564#undef lseek
565#define lseek ___xxx_lseek
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800566
Elliott Hughes915d67f2020-04-06 09:15:35 -0700567static inline int adb_unlink(const char* path) {
Josh Gao90228a62019-04-25 14:04:57 -0700568 return unlink(path);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800569}
Josh Gao90228a62019-04-25 14:04:57 -0700570#undef unlink
571#define unlink ___xxx_unlink
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800572
Elliott Hughes915d67f2020-04-06 09:15:35 -0700573static inline int adb_creat(const char* path, int mode) {
Josh Gao90228a62019-04-25 14:04:57 -0700574 int fd = TEMP_FAILURE_RETRY(creat(path, mode));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800575
Josh Gao90228a62019-04-25 14:04:57 -0700576 if (fd < 0) return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800577
578 close_on_exec(fd);
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200579 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800580}
Josh Gao90228a62019-04-25 14:04:57 -0700581#undef creat
582#define creat ___xxx_creat
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800583
Elliott Hughes915d67f2020-04-06 09:15:35 -0700584static inline int unix_isatty(borrowed_fd fd) {
Josh Gao90228a62019-04-25 14:04:57 -0700585 return isatty(fd.get());
David Pursell58805362015-10-28 14:29:51 -0700586}
Josh Gao90228a62019-04-25 14:04:57 -0700587#define isatty ___xxx_isatty
David Pursell58805362015-10-28 14:29:51 -0700588
Spencer Low753d4852015-07-30 23:07:55 -0700589// Helper for network_* functions.
590inline int _fd_set_error_str(int fd, std::string* error) {
Josh Gao90228a62019-04-25 14:04:57 -0700591 if (fd == -1) {
592 *error = strerror(errno);
593 }
594 return fd;
Spencer Low753d4852015-07-30 23:07:55 -0700595}
596
Spencer Low753d4852015-07-30 23:07:55 -0700597inline int network_inaddr_any_server(int port, int type, std::string* error) {
Josh Gao90228a62019-04-25 14:04:57 -0700598 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
Spencer Low753d4852015-07-30 23:07:55 -0700599}
600
Josh Gao4a5a95d2016-08-24 18:38:44 -0700601inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
602 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
603}
604
605inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
606 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low753d4852015-07-30 23:07:55 -0700607}
608
Josh Gao8d7080c2018-08-10 16:03:09 -0700609int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low753d4852015-07-30 23:07:55 -0700610
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800611inline std::optional<ssize_t> network_peek(borrowed_fd fd) {
612 ssize_t ret = recv(fd.get(), nullptr, 0, MSG_PEEK | MSG_TRUNC);
613 return ret == -1 ? std::nullopt : std::make_optional(ret);
614}
615
Elliott Hughes915d67f2020-04-06 09:15:35 -0700616static inline int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
617 socklen_t* addrlen) {
Benoit Gobyf4ded742011-02-01 18:57:41 -0800618 int fd;
619
Josh Gao90228a62019-04-25 14:04:57 -0700620 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
621 if (fd >= 0) close_on_exec(fd);
Benoit Gobyf4ded742011-02-01 18:57:41 -0800622
623 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800624}
625
Josh Gao90228a62019-04-25 14:04:57 -0700626#undef accept
627#define accept ___xxx_accept
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800628
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800629inline int adb_getsockname(borrowed_fd fd, struct sockaddr* sockaddr, socklen_t* addrlen) {
630 return getsockname(fd.get(), sockaddr, addrlen);
631}
632
Josh Gao90228a62019-04-25 14:04:57 -0700633inline int adb_socket_get_local_port(borrowed_fd fd) {
634 return socket_get_local_port(fd.get());
David Pursell19d0c232016-04-07 11:25:48 -0700635}
636
Spencer Low3a2421b2015-05-22 20:09:06 -0700637// Operate on a file descriptor returned from unix_open() or a well-known file
638// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
639//
640// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
641// adb_write(), adb_close() (which all map to Unix system calls), but the
642// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
643// into the C Runtime and its configurable CR/LF translation (which is settable
644// via _setmode()).
Josh Gao90228a62019-04-25 14:04:57 -0700645#define unix_read adb_read
646#define unix_write adb_write
Spencer Low04b575d2018-09-02 19:19:39 -0700647#define unix_lseek adb_lseek
Josh Gao90228a62019-04-25 14:04:57 -0700648#define unix_close adb_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800649
Elliott Hughes915d67f2020-04-06 09:15:35 -0700650static inline int adb_thread_setname(const std::string& name) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700651#ifdef __APPLE__
652 return pthread_setname_np(name.c_str());
653#else
Elliott Hughes3bb1b572017-08-02 13:22:38 -0700654 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
655 // glibc doesn't have strlcpy, so we have to fake it.
656 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
657 strncpy(buf, name.c_str(), sizeof(buf) - 1);
658 buf[sizeof(buf) - 1] = '\0';
659 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy2669cf92015-08-28 16:37:29 -0700660#endif
661}
662
Elliott Hughes915d67f2020-04-06 09:15:35 -0700663static inline int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
664 socklen_t optlen) {
Josh Gao90228a62019-04-25 14:04:57 -0700665 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Low31aafa62015-01-25 14:40:16 -0800666}
667
Josh Gao90228a62019-04-25 14:04:57 -0700668#undef setsockopt
669#define setsockopt ___xxx_setsockopt
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800670
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800671static inline int adb_socket(int domain, int type, int protocol) {
672 return socket(domain, type, protocol);
673}
674
675static inline int adb_bind(borrowed_fd fd, const sockaddr* addr, int namelen) {
676 return bind(fd.get(), addr, namelen);
677}
678
Elliott Hughes915d67f2020-04-06 09:15:35 -0700679static inline int unix_socketpair(int d, int type, int protocol, int sv[2]) {
Josh Gao90228a62019-04-25 14:04:57 -0700680 return socketpair(d, type, protocol, sv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800681}
682
Elliott Hughes915d67f2020-04-06 09:15:35 -0700683static inline int adb_socketpair(int sv[2]) {
Josh Gao90228a62019-04-25 14:04:57 -0700684 int rc;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800685
Josh Gao90228a62019-04-25 14:04:57 -0700686 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
687 if (rc < 0) return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800688
Josh Gao90228a62019-04-25 14:04:57 -0700689 close_on_exec(sv[0]);
690 close_on_exec(sv[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800691 return 0;
692}
693
Josh Gao90228a62019-04-25 14:04:57 -0700694#undef socketpair
695#define socketpair ___xxx_socketpair
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800696
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800697typedef struct msghdr adb_msghdr;
698inline ssize_t adb_sendmsg(borrowed_fd fd, const adb_msghdr* msg, int flags) {
699 return sendmsg(fd.get(), msg, flags);
700}
701
702inline ssize_t adb_recvmsg(borrowed_fd fd, adb_msghdr* msg, int flags) {
703 return recvmsg(fd.get(), msg, flags);
704}
705
706using adb_cmsghdr = cmsghdr;
707
708inline adb_cmsghdr* adb_CMSG_FIRSTHDR(adb_msghdr* msgh) {
709 return CMSG_FIRSTHDR(msgh);
710}
711
712inline adb_cmsghdr* adb_CMSG_NXTHDR(adb_msghdr* msgh, adb_cmsghdr* cmsg) {
713 return CMSG_NXTHDR(msgh, cmsg);
714}
715
716inline unsigned char* adb_CMSG_DATA(adb_cmsghdr* cmsg) {
717 return CMSG_DATA(cmsg);
718}
719
Josh Gaoe7388122016-02-16 17:34:53 -0800720typedef struct pollfd adb_pollfd;
Elliott Hughes915d67f2020-04-06 09:15:35 -0700721static inline int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
Josh Gaoe7388122016-02-16 17:34:53 -0800722 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
723}
724
725#define poll ___xxx_poll
726
Elliott Hughes915d67f2020-04-06 09:15:35 -0700727static inline int adb_mkdir(const std::string& path, int mode) {
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400728 return mkdir(path.c_str(), mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800729}
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400730
Josh Gao90228a62019-04-25 14:04:57 -0700731#undef mkdir
732#define mkdir ___xxx_mkdir
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800733
Elliott Hughes915d67f2020-04-06 09:15:35 -0700734static inline int adb_rename(const char* oldpath, const char* newpath) {
Joshua Duong290ccb52019-11-20 14:18:43 -0800735 return rename(oldpath, newpath);
736}
737
Elliott Hughes915d67f2020-04-06 09:15:35 -0700738static inline int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800739 return path[0] == '/';
740}
741
Elliott Hughes915d67f2020-04-06 09:15:35 -0700742static inline int adb_get_os_handle(borrowed_fd fd) {
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700743 return fd.get();
744}
745
Elliott Hughes915d67f2020-04-06 09:15:35 -0700746static inline int cast_handle_to_int(int fd) {
Yurii Zubrytskyi3c0574f2020-03-26 18:16:36 -0700747 return fd;
748}
749
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800750// A very simple wrapper over a launched child process
751class Process {
752 public:
753 constexpr explicit Process(pid_t pid) : pid_(pid) {}
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700754 constexpr Process(Process&& other) : pid_(std::exchange(other.pid_, -1)) {}
755
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800756 constexpr explicit operator bool() const { return pid_ >= 0; }
757
758 void wait() {
759 if (*this) {
760 int status;
761 ::waitpid(pid_, &status, 0);
762 pid_ = -1;
763 }
764 }
765 void kill() {
766 if (*this) {
767 ::kill(pid_, SIGTERM);
768 }
769 }
770
771 private:
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700772 DISALLOW_COPY_AND_ASSIGN(Process);
773
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800774 pid_t pid_;
775};
776
777Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
778 std::initializer_list<int> fds_to_inherit = {});
779
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800780#endif /* !_WIN32 */
781
Josh Gao90228a62019-04-25 14:04:57 -0700782static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800783 int off = 1;
Josh Gao90228a62019-04-25 14:04:57 -0700784 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800785}
786
David Pursellc25a34e2016-02-22 14:27:23 -0800787// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
788// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
789// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao90228a62019-04-25 14:04:57 -0700790bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellc25a34e2016-02-22 14:27:23 -0800791
Elliott Hughes801066a2016-06-29 17:42:01 -0700792#if defined(_WIN32)
793// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
794#undef ERROR
795#endif