blob: 99f6b74f7376b4e8c09c6caffc25f2d8a7d7536c [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
Ryan Prichard6e55ec12024-04-22 16:49:48 -070034// Include this before open/close/isatty/unlink are defined as macros below.
Josh Gaoa4f5e032016-02-09 14:59:09 -080035#include <android-base/errors.h>
Fabien Sanglard89bd8c62024-06-18 10:29:46 -070036#include <android-base/logging.h>
Elliott Hughes8bddf242017-11-28 18:05:27 -080037#include <android-base/macros.h>
Adrian Roosdbfe01d2019-08-19 14:37:19 +020038#include <android-base/off64_t.h>
Josh Gaod91139c2016-05-13 14:52:06 -070039#include <android-base/unique_fd.h>
Elliott Hughesf55ead92015-12-04 22:00:26 -080040#include <android-base/utf8.h>
Ryan Prichard6e55ec12024-04-22 16:49:48 -070041#if __has_include(<print>)
42#include <print>
43#endif
Spencer Low50f5bf12015-11-12 15:20:15 -080044
Josh Gao90228a62019-04-25 14:04:57 -070045#include "adb_unique_fd.h"
Josh Gao75e96bb2016-12-05 13:24:48 -080046#include "sysdeps/errno.h"
Josh Gao1f6a57a2017-04-12 13:57:06 -070047#include "sysdeps/network.h"
Josh Gaoa4f9c172016-07-28 18:09:48 -070048#include "sysdeps/stat.h"
49
Josh Gao659ec672020-04-03 10:12:44 -070050#if defined(__APPLE__)
Elliott Hughes915d67f2020-04-06 09:15:35 -070051static inline void* mempcpy(void* dst, const void* src, size_t n) {
Josh Gao659ec672020-04-03 10:12:44 -070052 return static_cast<char*>(memcpy(dst, src, n)) + n;
53}
54#endif
55
Fabien Sanglardc692c162024-06-17 17:16:53 -070056std::optional<ssize_t> network_peek(borrowed_fd fd);
57
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080058#ifdef _WIN32
59
Dan Albertbbbbea62014-11-24 23:34:35 -080060#include <ctype.h>
61#include <direct.h>
Spencer Low6815c072015-05-11 01:08:48 -070062#include <dirent.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080063#include <errno.h>
64#include <fcntl.h>
65#include <io.h>
Joshua Duongf4ba8d72021-01-13 12:18:15 -080066#include <mswsock.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080067#include <process.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -070068#include <stdint.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080069#include <sys/stat.h>
Spencer Low6815c072015-05-11 01:08:48 -070070#include <utime.h>
Stephen Hinesb1170852014-10-01 17:37:06 -070071#include <windows.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -070072#include <winsock2.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073#include <ws2tcpip.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080074
Spencer Low2bbb3a92015-08-26 18:46:09 -070075#include <memory> // unique_ptr
Spencer Low50f5bf12015-11-12 15:20:15 -080076#include <string>
Alex Vallée28d1f8d2015-05-06 16:26:00 -040077
Elliott Hughesd189cfb2015-07-30 17:42:01 -070078#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080079#define OS_PATH_SEPARATOR '\\'
80#define OS_PATH_SEPARATOR_STR "\\"
Benoit Goby2cc19e42012-04-12 12:23:49 -070081#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082
Elliott Hughes915d67f2020-04-06 09:15:35 -070083static inline bool adb_is_separator(char c) {
Josh Gao8acf06c2015-11-07 15:38:19 -080084 return c == '\\' || c == '/';
85}
86
Spencer Lowae37a312018-09-03 16:03:22 -070087extern int adb_thread_setname(const std::string& name);
Siva Velusamy2669cf92015-08-28 16:37:29 -070088
Elliott Hughes915d67f2020-04-06 09:15:35 -070089static inline void close_on_exec(borrowed_fd fd) {
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020090 /* nothing really */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080091}
92
Josh Gao90228a62019-04-25 14:04:57 -070093extern int adb_unlink(const char* path);
94#undef unlink
95#define unlink ___xxx_unlink
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080096
Spencer Low6815c072015-05-11 01:08:48 -070097extern int adb_mkdir(const std::string& path, int mode);
Josh Gao90228a62019-04-25 14:04:57 -070098#undef mkdir
99#define mkdir ___xxx_mkdir
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800100
Joshua Duong290ccb52019-11-20 14:18:43 -0800101extern int adb_rename(const char* oldpath, const char* newpath);
102
Spencer Low3a2421b2015-05-22 20:09:06 -0700103// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao96049b92018-03-23 13:03:28 -0700104extern int adb_open(const char* path, int options);
105extern int adb_creat(const char* path, int mode);
Josh Gao90228a62019-04-25 14:04:57 -0700106extern int adb_read(borrowed_fd fd, void* buf, int len);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700107extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
Josh Gao90228a62019-04-25 14:04:57 -0700108extern int adb_write(borrowed_fd fd, const void* buf, int len);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700109extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
Josh Gao90228a62019-04-25 14:04:57 -0700110extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
111extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
Josh Gao96049b92018-03-23 13:03:28 -0700112extern int adb_close(int fd);
113extern int adb_register_socket(SOCKET s);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700114extern HANDLE adb_get_os_handle(borrowed_fd fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800115
Joshua Duong290ccb52019-11-20 14:18:43 -0800116extern int adb_gethostname(char* name, size_t len);
117extern int adb_getlogin_r(char* buf, size_t bufsize);
118
Spencer Low3a2421b2015-05-22 20:09:06 -0700119// See the comments for the !defined(_WIN32) version of unix_close().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700120static inline int unix_close(int fd) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800121 return close(fd);
122}
Josh Gao90228a62019-04-25 14:04:57 -0700123#undef close
124#define close ____xxx_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800125
Spencer Low55441402015-11-07 17:34:39 -0800126// Like unix_read(), but may return EINTR.
Josh Gao90228a62019-04-25 14:04:57 -0700127extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
Spencer Low55441402015-11-07 17:34:39 -0800128
Spencer Low3a2421b2015-05-22 20:09:06 -0700129// See the comments for the !defined(_WIN32) version of unix_read().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700130static inline int unix_read(borrowed_fd fd, void* buf, size_t len) {
Spencer Low55441402015-11-07 17:34:39 -0800131 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
132}
Spencer Lowbeb61982015-03-01 15:06:21 -0800133
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800134#undef read
135#define read ___xxx_read
136
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700137#undef pread
138#define pread ___xxx_pread
139
Spencer Low3a2421b2015-05-22 20:09:06 -0700140// See the comments for the !defined(_WIN32) version of unix_write().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700141static inline int unix_write(borrowed_fd fd, const void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700142 return write(fd.get(), buf, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143}
144#undef write
145#define write ___xxx_write
146
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700147#undef pwrite
148#define pwrite ___xxx_pwrite
149
Spencer Low04b575d2018-09-02 19:19:39 -0700150// See the comments for the !defined(_WIN32) version of unix_lseek().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700151static inline int unix_lseek(borrowed_fd fd, int pos, int where) {
Josh Gao90228a62019-04-25 14:04:57 -0700152 return lseek(fd.get(), pos, where);
Spencer Low04b575d2018-09-02 19:19:39 -0700153}
154#undef lseek
155#define lseek ___xxx_lseek
156
Spencer Low3a2421b2015-05-22 20:09:06 -0700157// See the comments for the !defined(_WIN32) version of adb_open_mode().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700158static inline int adb_open_mode(const char* path, int options, int mode) {
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200159 return adb_open(path, options);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160}
161
Spencer Low3a2421b2015-05-22 20:09:06 -0700162// See the comments for the !defined(_WIN32) version of unix_open().
Josh Gaof0c44032018-12-12 16:12:28 -0800163extern int unix_open(std::string_view path, int options, ...);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800164#define open ___xxx_unix_open
165
David Pursell58805362015-10-28 14:29:51 -0700166// Checks if |fd| corresponds to a console.
167// Standard Windows isatty() returns 1 for both console FDs and character
168// devices like NUL. unix_isatty() performs some extra checking to only match
169// console FDs.
170// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
171// will work but adb_open() FDs will not. Additionally the OS handle associated
172// with |fd| must have GENERIC_READ access (which console FDs have by default).
173// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
174// calling this function is unreliable and should not be used.
Josh Gao90228a62019-04-25 14:04:57 -0700175int unix_isatty(borrowed_fd fd);
David Pursell58805362015-10-28 14:29:51 -0700176#define isatty ___xxx_isatty
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800177
Spencer Low753d4852015-07-30 23:07:55 -0700178int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700179
180inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
181 abort();
182}
183
184inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
185 abort();
186}
187
Spencer Low753d4852015-07-30 23:07:55 -0700188int network_connect(const std::string& host, int port, int type, int timeout,
189 std::string* error);
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
Elliott Hughes915d67f2020-04-06 09:15:35 -0700611static inline int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
612 socklen_t* addrlen) {
Benoit Gobyf4ded742011-02-01 18:57:41 -0800613 int fd;
614
Josh Gao90228a62019-04-25 14:04:57 -0700615 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
616 if (fd >= 0) close_on_exec(fd);
Benoit Gobyf4ded742011-02-01 18:57:41 -0800617
618 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800619}
620
Josh Gao90228a62019-04-25 14:04:57 -0700621#undef accept
622#define accept ___xxx_accept
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800623
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800624inline int adb_getsockname(borrowed_fd fd, struct sockaddr* sockaddr, socklen_t* addrlen) {
625 return getsockname(fd.get(), sockaddr, addrlen);
626}
627
Josh Gao90228a62019-04-25 14:04:57 -0700628inline int adb_socket_get_local_port(borrowed_fd fd) {
629 return socket_get_local_port(fd.get());
David Pursell19d0c232016-04-07 11:25:48 -0700630}
631
Spencer Low3a2421b2015-05-22 20:09:06 -0700632// Operate on a file descriptor returned from unix_open() or a well-known file
633// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
634//
635// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
636// adb_write(), adb_close() (which all map to Unix system calls), but the
637// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
638// into the C Runtime and its configurable CR/LF translation (which is settable
639// via _setmode()).
Josh Gao90228a62019-04-25 14:04:57 -0700640#define unix_read adb_read
641#define unix_write adb_write
Spencer Low04b575d2018-09-02 19:19:39 -0700642#define unix_lseek adb_lseek
Josh Gao90228a62019-04-25 14:04:57 -0700643#define unix_close adb_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800644
Elliott Hughes915d67f2020-04-06 09:15:35 -0700645static inline int adb_thread_setname(const std::string& name) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700646#ifdef __APPLE__
647 return pthread_setname_np(name.c_str());
648#else
Elliott Hughes3bb1b572017-08-02 13:22:38 -0700649 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
650 // glibc doesn't have strlcpy, so we have to fake it.
651 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
652 strncpy(buf, name.c_str(), sizeof(buf) - 1);
653 buf[sizeof(buf) - 1] = '\0';
654 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy2669cf92015-08-28 16:37:29 -0700655#endif
656}
657
Elliott Hughes915d67f2020-04-06 09:15:35 -0700658static inline int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
659 socklen_t optlen) {
Josh Gao90228a62019-04-25 14:04:57 -0700660 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Low31aafa62015-01-25 14:40:16 -0800661}
662
Josh Gao90228a62019-04-25 14:04:57 -0700663#undef setsockopt
664#define setsockopt ___xxx_setsockopt
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800665
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800666static inline int adb_socket(int domain, int type, int protocol) {
667 return socket(domain, type, protocol);
668}
669
670static inline int adb_bind(borrowed_fd fd, const sockaddr* addr, int namelen) {
671 return bind(fd.get(), addr, namelen);
672}
673
Elliott Hughes915d67f2020-04-06 09:15:35 -0700674static inline int unix_socketpair(int d, int type, int protocol, int sv[2]) {
Josh Gao90228a62019-04-25 14:04:57 -0700675 return socketpair(d, type, protocol, sv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800676}
677
Elliott Hughes915d67f2020-04-06 09:15:35 -0700678static inline int adb_socketpair(int sv[2]) {
Josh Gao90228a62019-04-25 14:04:57 -0700679 int rc;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800680
Josh Gao90228a62019-04-25 14:04:57 -0700681 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
682 if (rc < 0) return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800683
Josh Gao90228a62019-04-25 14:04:57 -0700684 close_on_exec(sv[0]);
685 close_on_exec(sv[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800686 return 0;
687}
688
Josh Gao90228a62019-04-25 14:04:57 -0700689#undef socketpair
690#define socketpair ___xxx_socketpair
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800691
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800692typedef struct msghdr adb_msghdr;
693inline ssize_t adb_sendmsg(borrowed_fd fd, const adb_msghdr* msg, int flags) {
694 return sendmsg(fd.get(), msg, flags);
695}
696
697inline ssize_t adb_recvmsg(borrowed_fd fd, adb_msghdr* msg, int flags) {
Fabien Sanglard89bd8c62024-06-18 10:29:46 -0700698 ssize_t ret = recvmsg(fd.get(), msg, flags);
699 if (ret == -1) {
700 PLOG(ERROR) << "adb_recmsg error";
701 }
702 return ret;
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800703}
704
705using adb_cmsghdr = cmsghdr;
706
707inline adb_cmsghdr* adb_CMSG_FIRSTHDR(adb_msghdr* msgh) {
708 return CMSG_FIRSTHDR(msgh);
709}
710
711inline adb_cmsghdr* adb_CMSG_NXTHDR(adb_msghdr* msgh, adb_cmsghdr* cmsg) {
712 return CMSG_NXTHDR(msgh, cmsg);
713}
714
715inline unsigned char* adb_CMSG_DATA(adb_cmsghdr* cmsg) {
716 return CMSG_DATA(cmsg);
717}
718
Josh Gaoe7388122016-02-16 17:34:53 -0800719typedef struct pollfd adb_pollfd;
Elliott Hughes915d67f2020-04-06 09:15:35 -0700720static inline int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
Josh Gaoe7388122016-02-16 17:34:53 -0800721 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
722}
723
724#define poll ___xxx_poll
725
Elliott Hughes915d67f2020-04-06 09:15:35 -0700726static inline int adb_mkdir(const std::string& path, int mode) {
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400727 return mkdir(path.c_str(), mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800728}
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400729
Josh Gao90228a62019-04-25 14:04:57 -0700730#undef mkdir
731#define mkdir ___xxx_mkdir
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800732
Elliott Hughes915d67f2020-04-06 09:15:35 -0700733static inline int adb_rename(const char* oldpath, const char* newpath) {
Joshua Duong290ccb52019-11-20 14:18:43 -0800734 return rename(oldpath, newpath);
735}
736
Elliott Hughes915d67f2020-04-06 09:15:35 -0700737static inline int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800738 return path[0] == '/';
739}
740
Elliott Hughes915d67f2020-04-06 09:15:35 -0700741static inline int adb_get_os_handle(borrowed_fd fd) {
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700742 return fd.get();
743}
744
Elliott Hughes915d67f2020-04-06 09:15:35 -0700745static inline int cast_handle_to_int(int fd) {
Yurii Zubrytskyi3c0574f2020-03-26 18:16:36 -0700746 return fd;
747}
748
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800749// A very simple wrapper over a launched child process
750class Process {
751 public:
752 constexpr explicit Process(pid_t pid) : pid_(pid) {}
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700753 constexpr Process(Process&& other) : pid_(std::exchange(other.pid_, -1)) {}
754
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800755 constexpr explicit operator bool() const { return pid_ >= 0; }
756
757 void wait() {
758 if (*this) {
759 int status;
760 ::waitpid(pid_, &status, 0);
761 pid_ = -1;
762 }
763 }
764 void kill() {
765 if (*this) {
766 ::kill(pid_, SIGTERM);
767 }
768 }
769
770 private:
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700771 DISALLOW_COPY_AND_ASSIGN(Process);
772
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800773 pid_t pid_;
774};
775
776Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
777 std::initializer_list<int> fds_to_inherit = {});
778
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800779#endif /* !_WIN32 */
780
Josh Gao90228a62019-04-25 14:04:57 -0700781static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800782 int off = 1;
Josh Gao90228a62019-04-25 14:04:57 -0700783 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800784}
785
David Pursellc25a34e2016-02-22 14:27:23 -0800786// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
787// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
788// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao90228a62019-04-25 14:04:57 -0700789bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellc25a34e2016-02-22 14:27:23 -0800790
shajue1f98522023-03-20 10:09:25 -0700791// Returns a human-readable OS version string.
Elliott Hughesa8ab5ce2024-06-28 12:10:09 +0000792extern std::string GetOSVersion();
shajue1f98522023-03-20 10:09:25 -0700793
Elliott Hughes801066a2016-06-29 17:42:01 -0700794#if defined(_WIN32)
795// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
796#undef ERROR
797#endif