blob: 224a5176542a9a33578e6ac7ab27f1fbb8adfed4 [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
Dan Albertbbbbea62014-11-24 23:34:35 -080054#include <ctype.h>
55#include <direct.h>
Spencer Low6815c072015-05-11 01:08:48 -070056#include <dirent.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080057#include <errno.h>
58#include <fcntl.h>
59#include <io.h>
Joshua Duongf4ba8d72021-01-13 12:18:15 -080060#include <mswsock.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080061#include <process.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -070062#include <stdint.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080063#include <sys/stat.h>
Spencer Low6815c072015-05-11 01:08:48 -070064#include <utime.h>
Stephen Hinesb1170852014-10-01 17:37:06 -070065#include <windows.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -070066#include <winsock2.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067#include <ws2tcpip.h>
Dan Albertbbbbea62014-11-24 23:34:35 -080068
Spencer Low2bbb3a92015-08-26 18:46:09 -070069#include <memory> // unique_ptr
Spencer Low50f5bf12015-11-12 15:20:15 -080070#include <string>
Alex Vallée28d1f8d2015-05-06 16:26:00 -040071
Elliott Hughesd189cfb2015-07-30 17:42:01 -070072#define OS_PATH_SEPARATORS "\\/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073#define OS_PATH_SEPARATOR '\\'
74#define OS_PATH_SEPARATOR_STR "\\"
Benoit Goby2cc19e42012-04-12 12:23:49 -070075#define ENV_PATH_SEPARATOR_STR ";"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080076
Elliott Hughes915d67f2020-04-06 09:15:35 -070077static inline bool adb_is_separator(char c) {
Josh Gao8acf06c2015-11-07 15:38:19 -080078 return c == '\\' || c == '/';
79}
80
Spencer Lowae37a312018-09-03 16:03:22 -070081extern int adb_thread_setname(const std::string& name);
Siva Velusamy2669cf92015-08-28 16:37:29 -070082
Elliott Hughes915d67f2020-04-06 09:15:35 -070083static inline void close_on_exec(borrowed_fd fd) {
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020084 /* nothing really */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080085}
86
Josh Gao90228a62019-04-25 14:04:57 -070087extern int adb_unlink(const char* path);
88#undef unlink
89#define unlink ___xxx_unlink
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080090
Spencer Low6815c072015-05-11 01:08:48 -070091extern int adb_mkdir(const std::string& path, int mode);
Josh Gao90228a62019-04-25 14:04:57 -070092#undef mkdir
93#define mkdir ___xxx_mkdir
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080094
Joshua Duong290ccb52019-11-20 14:18:43 -080095extern int adb_rename(const char* oldpath, const char* newpath);
96
Spencer Low3a2421b2015-05-22 20:09:06 -070097// See the comments for the !defined(_WIN32) versions of adb_*().
Josh Gao96049b92018-03-23 13:03:28 -070098extern int adb_open(const char* path, int options);
99extern int adb_creat(const char* path, int mode);
Josh Gao90228a62019-04-25 14:04:57 -0700100extern int adb_read(borrowed_fd fd, void* buf, int len);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700101extern int adb_pread(borrowed_fd fd, void* buf, int len, off64_t offset);
Josh Gao90228a62019-04-25 14:04:57 -0700102extern int adb_write(borrowed_fd fd, const void* buf, int len);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700103extern int adb_pwrite(borrowed_fd fd, const void* buf, int len, off64_t offset);
Josh Gao90228a62019-04-25 14:04:57 -0700104extern int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where);
105extern int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR);
Josh Gao96049b92018-03-23 13:03:28 -0700106extern int adb_close(int fd);
107extern int adb_register_socket(SOCKET s);
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700108extern HANDLE adb_get_os_handle(borrowed_fd fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800109
Joshua Duong290ccb52019-11-20 14:18:43 -0800110extern int adb_gethostname(char* name, size_t len);
111extern int adb_getlogin_r(char* buf, size_t bufsize);
112
Spencer Low3a2421b2015-05-22 20:09:06 -0700113// See the comments for the !defined(_WIN32) version of unix_close().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700114static inline int unix_close(int fd) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800115 return close(fd);
116}
Josh Gao90228a62019-04-25 14:04:57 -0700117#undef close
118#define close ____xxx_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800119
Spencer Low55441402015-11-07 17:34:39 -0800120// Like unix_read(), but may return EINTR.
Josh Gao90228a62019-04-25 14:04:57 -0700121extern int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len);
Spencer Low55441402015-11-07 17:34:39 -0800122
Spencer Low3a2421b2015-05-22 20:09:06 -0700123// See the comments for the !defined(_WIN32) version of unix_read().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700124static inline int unix_read(borrowed_fd fd, void* buf, size_t len) {
Spencer Low55441402015-11-07 17:34:39 -0800125 return TEMP_FAILURE_RETRY(unix_read_interruptible(fd, buf, len));
126}
Spencer Lowbeb61982015-03-01 15:06:21 -0800127
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800128#undef read
129#define read ___xxx_read
130
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700131#undef pread
132#define pread ___xxx_pread
133
Spencer Low3a2421b2015-05-22 20:09:06 -0700134// See the comments for the !defined(_WIN32) version of unix_write().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700135static inline int unix_write(borrowed_fd fd, const void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700136 return write(fd.get(), buf, len);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800137}
138#undef write
139#define write ___xxx_write
140
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700141#undef pwrite
142#define pwrite ___xxx_pwrite
143
Spencer Low04b575d2018-09-02 19:19:39 -0700144// See the comments for the !defined(_WIN32) version of unix_lseek().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700145static inline int unix_lseek(borrowed_fd fd, int pos, int where) {
Josh Gao90228a62019-04-25 14:04:57 -0700146 return lseek(fd.get(), pos, where);
Spencer Low04b575d2018-09-02 19:19:39 -0700147}
148#undef lseek
149#define lseek ___xxx_lseek
150
Spencer Low3a2421b2015-05-22 20:09:06 -0700151// See the comments for the !defined(_WIN32) version of adb_open_mode().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700152static inline int adb_open_mode(const char* path, int options, int mode) {
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200153 return adb_open(path, options);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800154}
155
Spencer Low3a2421b2015-05-22 20:09:06 -0700156// See the comments for the !defined(_WIN32) version of unix_open().
Josh Gaof0c44032018-12-12 16:12:28 -0800157extern int unix_open(std::string_view path, int options, ...);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800158#define open ___xxx_unix_open
159
David Pursell58805362015-10-28 14:29:51 -0700160// Checks if |fd| corresponds to a console.
161// Standard Windows isatty() returns 1 for both console FDs and character
162// devices like NUL. unix_isatty() performs some extra checking to only match
163// console FDs.
164// |fd| must be a real file descriptor, meaning STDxx_FILENO or unix_open() FDs
165// will work but adb_open() FDs will not. Additionally the OS handle associated
166// with |fd| must have GENERIC_READ access (which console FDs have by default).
167// Returns 1 if |fd| is a console FD, 0 otherwise. The value of errno after
168// calling this function is unreliable and should not be used.
Josh Gao90228a62019-04-25 14:04:57 -0700169int unix_isatty(borrowed_fd fd);
David Pursell58805362015-10-28 14:29:51 -0700170#define isatty ___xxx_isatty
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800171
Spencer Low753d4852015-07-30 23:07:55 -0700172int network_inaddr_any_server(int port, int type, std::string* error);
Josh Gao4a5a95d2016-08-24 18:38:44 -0700173
174inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
175 abort();
176}
177
178inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
179 abort();
180}
181
Spencer Low753d4852015-07-30 23:07:55 -0700182int network_connect(const std::string& host, int port, int type, int timeout,
183 std::string* error);
184
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800185std::optional<ssize_t> network_peek(borrowed_fd fd);
186
Josh Gao90228a62019-04-25 14:04:57 -0700187extern int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr, socklen_t* addrlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188
189#undef accept
190#define accept ___xxx_accept
191
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800192int adb_getsockname(borrowed_fd fd, struct sockaddr* sockaddr, socklen_t* addrlen);
193
David Pursell19d0c232016-04-07 11:25:48 -0700194// Returns the local port number of a bound socket, or -1 on failure.
Josh Gao90228a62019-04-25 14:04:57 -0700195int adb_socket_get_local_port(borrowed_fd fd);
David Pursell19d0c232016-04-07 11:25:48 -0700196
Josh Gao90228a62019-04-25 14:04:57 -0700197extern int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
198 socklen_t optlen);
Spencer Low31aafa62015-01-25 14:40:16 -0800199
200#undef setsockopt
201#define setsockopt ___xxx_setsockopt
202
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800203// Wrapper around socket() call. On Windows, ADB has an indirection layer for file descriptors.
204extern int adb_socket(int domain, int type, int protocol);
205
206// Wrapper around bind() call, as Windows has indirection layer.
207extern int adb_bind(borrowed_fd fd, const sockaddr* addr, int namelen);
208
Josh Gao90228a62019-04-25 14:04:57 -0700209extern int adb_socketpair(int sv[2]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800210
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800211// Posix compatibility layer for msghdr
212struct adb_msghdr {
213 void* msg_name;
214 socklen_t msg_namelen;
215 struct adb_iovec* msg_iov;
216 size_t msg_iovlen;
217 void* msg_control;
218 size_t msg_controllen;
219 int msg_flags;
220};
221
222ssize_t adb_sendmsg(borrowed_fd fd, const adb_msghdr* msg, int flags);
223ssize_t adb_recvmsg(borrowed_fd fd, adb_msghdr* msg, int flags);
224
225using adb_cmsghdr = WSACMSGHDR;
226
227extern adb_cmsghdr* adb_CMSG_FIRSTHDR(adb_msghdr* msgh);
228extern adb_cmsghdr* adb_CMSG_NXTHDR(adb_msghdr* msgh, adb_cmsghdr* cmsg);
229extern unsigned char* adb_CMSG_DATA(adb_cmsghdr* cmsg);
230
Josh Gaoe7388122016-02-16 17:34:53 -0800231struct adb_pollfd {
232 int fd;
233 short events;
234 short revents;
235};
236extern int adb_poll(adb_pollfd* fds, size_t nfds, int timeout);
237#define poll ___xxx_poll
238
Elliott Hughes915d67f2020-04-06 09:15:35 -0700239static inline int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800240 return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
241}
242
Spencer Low6815c072015-05-11 01:08:48 -0700243// UTF-8 versions of POSIX APIs.
244extern DIR* adb_opendir(const char* dirname);
245extern struct dirent* adb_readdir(DIR* dir);
246extern int adb_closedir(DIR* dir);
247
248extern int adb_utime(const char *, struct utimbuf *);
249extern int adb_chmod(const char *, int);
250
Elliott Hughes874c9412018-06-26 13:06:15 -0700251extern int adb_vfprintf(FILE* stream, const char* format, va_list ap)
252 __attribute__((__format__(__printf__, 2, 0)));
253extern int adb_vprintf(const char* format, va_list ap) __attribute__((__format__(__printf__, 1, 0)));
254extern int adb_fprintf(FILE* stream, const char* format, ...)
255 __attribute__((__format__(__printf__, 2, 3)));
256extern int adb_printf(const char* format, ...) __attribute__((__format__(__printf__, 1, 2)));
Spencer Low6815c072015-05-11 01:08:48 -0700257
258extern int adb_fputs(const char* buf, FILE* stream);
259extern int adb_fputc(int ch, FILE* stream);
Spencer Lowf373c352015-11-15 16:29:36 -0800260extern int adb_putchar(int ch);
261extern int adb_puts(const char* buf);
Josh Gao90228a62019-04-25 14:04:57 -0700262extern size_t adb_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
Spencer Low6815c072015-05-11 01:08:48 -0700263
264extern FILE* adb_fopen(const char* f, const char* m);
265
266extern char* adb_getenv(const char* name);
267
268extern char* adb_getcwd(char* buf, int size);
269
270// Remap calls to POSIX APIs to our UTF-8 versions.
271#define opendir adb_opendir
272#define readdir adb_readdir
273#define closedir adb_closedir
274#define rewinddir rewinddir_utf8_not_yet_implemented
275#define telldir telldir_utf8_not_yet_implemented
Spencer Low28bc2cb2015-11-07 18:51:54 -0800276// Some compiler's C++ headers have members named seekdir, so we can't do the
277// macro technique and instead cause a link error if seekdir is called.
278inline void seekdir(DIR*, long) {
279 extern int seekdir_utf8_not_yet_implemented;
280 seekdir_utf8_not_yet_implemented = 1;
281}
Spencer Low6815c072015-05-11 01:08:48 -0700282
283#define utime adb_utime
284#define chmod adb_chmod
285
286#define vfprintf adb_vfprintf
Spencer Lowf373c352015-11-15 16:29:36 -0800287#define vprintf adb_vprintf
Spencer Low6815c072015-05-11 01:08:48 -0700288#define fprintf adb_fprintf
289#define printf adb_printf
290#define fputs adb_fputs
291#define fputc adb_fputc
Spencer Lowf373c352015-11-15 16:29:36 -0800292// putc may be a macro, so if so, undefine it, so that we can redefine it.
293#undef putc
294#define putc(c, s) adb_fputc(c, s)
295#define putchar adb_putchar
296#define puts adb_puts
Spencer Low6815c072015-05-11 01:08:48 -0700297#define fwrite adb_fwrite
298
299#define fopen adb_fopen
Spencer Lowf373c352015-11-15 16:29:36 -0800300#define freopen freopen_utf8_not_yet_implemented
Spencer Low6815c072015-05-11 01:08:48 -0700301
302#define getenv adb_getenv
303#define putenv putenv_utf8_not_yet_implemented
304#define setenv setenv_utf8_not_yet_implemented
305#define unsetenv unsetenv_utf8_not_yet_implemented
306
307#define getcwd adb_getcwd
308
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800309// A very simple wrapper over a launched child process
310class Process {
311 public:
312 constexpr explicit Process(HANDLE h = nullptr) : h_(h) {}
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700313 constexpr Process(Process&& other) : h_(std::exchange(other.h_, nullptr)) {}
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800314 ~Process() { close(); }
315 constexpr explicit operator bool() const { return h_ != nullptr; }
316
317 void wait() {
318 if (*this) {
319 ::WaitForSingleObject(h_, INFINITE);
320 close();
321 }
322 }
323 void kill() {
324 if (*this) {
325 ::TerminateProcess(h_, -1);
326 }
327 }
328
329 private:
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700330 DISALLOW_COPY_AND_ASSIGN(Process);
331
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800332 void close() {
333 if (*this) {
334 ::CloseHandle(h_);
335 h_ = nullptr;
336 }
337 }
338
339 HANDLE h_;
340};
341
342Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
343 std::initializer_list<int> fds_to_inherit = {});
344
Spencer Low6815c072015-05-11 01:08:48 -0700345// Helper class to convert UTF-16 argv from wmain() to UTF-8 args that can be
346// passed to main().
347class NarrowArgs {
348public:
349 NarrowArgs(int argc, wchar_t** argv);
350 ~NarrowArgs();
351
352 inline char** data() {
353 return narrow_args;
354 }
355
356private:
357 char** narrow_args;
358};
359
Spencer Lowb28812f2015-08-08 15:07:07 -0700360// Windows HANDLE values only use 32-bits of the type, even on 64-bit machines,
361// so they can fit in an int. To convert back, we just need to sign-extend.
362// https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203%28v=vs.85%29.aspx
363// Note that this does not make a HANDLE value work with APIs like open(), nor
364// does this make a value from open() passable to APIs taking a HANDLE. This
365// just lets you take a HANDLE, pass it around as an int, and then use it again
366// as a HANDLE.
367inline int cast_handle_to_int(const HANDLE h) {
368 // truncate
369 return static_cast<int>(reinterpret_cast<INT_PTR>(h));
370}
371
372inline HANDLE cast_int_to_handle(const int fd) {
373 // sign-extend
374 return reinterpret_cast<HANDLE>(static_cast<INT_PTR>(fd));
375}
376
Spencer Low2bbb3a92015-08-26 18:46:09 -0700377// Deleter for unique_handle. Adapted from many sources, including:
378// http://stackoverflow.com/questions/14841396/stdunique-ptr-deleters-and-the-win32-api
379// https://visualstudiomagazine.com/articles/2013/09/01/get-a-handle-on-the-windows-api.aspx
380class handle_deleter {
381public:
382 typedef HANDLE pointer;
383
384 void operator()(HANDLE h);
385};
386
387// Like std::unique_ptr, but for Windows HANDLE objects that should be
388// CloseHandle()'d. Operator bool() only checks if the handle != nullptr,
389// but does not check if the handle != INVALID_HANDLE_VALUE.
390typedef std::unique_ptr<HANDLE, handle_deleter> unique_handle;
391
Spencer Lowf373c352015-11-15 16:29:36 -0800392namespace internal {
393
394size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>* remaining_bytes);
395
396}
397
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398#else /* !_WIN32 a.k.a. Unix */
399
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800400#include <fcntl.h>
Spencer Low753d4852015-07-30 23:07:55 -0700401#include <netdb.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402#include <netinet/in.h>
403#include <netinet/tcp.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700404#include <poll.h>
405#include <pthread.h>
406#include <signal.h>
407#include <stdarg.h>
408#include <stdint.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800409#include <string.h>
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700410#include <sys/stat.h>
411#include <sys/wait.h>
Kenny Rooteac025c2012-10-12 15:26:45 -0700412#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800413
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400414#include <string>
415
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700416#include <cutils/sockets.h>
417
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700418#define OS_PATH_SEPARATORS "/"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800419#define OS_PATH_SEPARATOR '/'
420#define OS_PATH_SEPARATOR_STR "/"
Benoit Goby2cc19e42012-04-12 12:23:49 -0700421#define ENV_PATH_SEPARATOR_STR ":"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800422
Elliott Hughes915d67f2020-04-06 09:15:35 -0700423static inline bool adb_is_separator(char c) {
Josh Gao8acf06c2015-11-07 15:38:19 -0800424 return c == '/';
425}
426
Elliott Hughes915d67f2020-04-06 09:15:35 -0700427static inline int get_fd_flags(borrowed_fd fd) {
Daniel Colascione3e124692019-11-13 17:49:37 -0800428 return fcntl(fd.get(), F_GETFD);
429}
430
Elliott Hughes915d67f2020-04-06 09:15:35 -0700431static inline void close_on_exec(borrowed_fd fd) {
Daniel Colascione3e124692019-11-13 17:49:37 -0800432 int flags = get_fd_flags(fd);
433 if (flags >= 0 && (flags & FD_CLOEXEC) == 0) {
434 fcntl(fd.get(), F_SETFD, flags | FD_CLOEXEC);
435 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800436}
437
Spencer Low3a2421b2015-05-22 20:09:06 -0700438// Open a file and return a file descriptor that may be used with unix_read(),
439// unix_write(), unix_close(), but not adb_read(), adb_write(), adb_close().
440//
441// On Unix, this is based on open(), so the file descriptor is a real OS file
442// descriptor, but the Windows implementation (in sysdeps_win32.cpp) returns a
443// file descriptor that can only be used with C Runtime APIs (which are wrapped
444// by unix_read(), unix_write(), unix_close()). Also, the C Runtime has
445// configurable CR/LF translation which defaults to text mode, but is settable
446// with _setmode().
Elliott Hughes915d67f2020-04-06 09:15:35 -0700447static inline int unix_open(std::string_view path, int options, ...) {
Josh Gaof0c44032018-12-12 16:12:28 -0800448 std::string zero_terminated(path.begin(), path.end());
449 if ((options & O_CREAT) == 0) {
450 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options));
451 } else {
452 int mode;
453 va_list args;
454 va_start(args, options);
455 mode = va_arg(args, int);
456 va_end(args);
457 return TEMP_FAILURE_RETRY(open(zero_terminated.c_str(), options, mode));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458 }
459}
460
Spencer Low3a2421b2015-05-22 20:09:06 -0700461// Similar to the two-argument adb_open(), but takes a mode parameter for file
462// creation. See adb_open() for more info.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700463static inline int adb_open_mode(const char* pathname, int options, int mode) {
Josh Gao90228a62019-04-25 14:04:57 -0700464 return TEMP_FAILURE_RETRY(open(pathname, options, mode));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800465}
466
Spencer Low3a2421b2015-05-22 20:09:06 -0700467// Open a file and return a file descriptor that may be used with adb_read(),
468// adb_write(), adb_close(), but not unix_read(), unix_write(), unix_close().
469//
470// On Unix, this is based on open(), but the Windows implementation (in
471// sysdeps_win32.cpp) uses Windows native file I/O and bypasses the C Runtime
472// and its CR/LF translation. The returned file descriptor should be used with
473// adb_read(), adb_write(), adb_close(), etc.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700474static inline int adb_open(const char* pathname, int options) {
Josh Gao90228a62019-04-25 14:04:57 -0700475 int fd = TEMP_FAILURE_RETRY(open(pathname, options));
476 if (fd < 0) return -1;
477 close_on_exec(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800478 return fd;
479}
Josh Gao90228a62019-04-25 14:04:57 -0700480#undef open
481#define open ___xxx_open
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482
Elliott Hughes915d67f2020-04-06 09:15:35 -0700483static inline int adb_shutdown(borrowed_fd fd, int direction = SHUT_RDWR) {
Josh Gao90228a62019-04-25 14:04:57 -0700484 return shutdown(fd.get(), direction);
David Pursell3fe11f62015-10-06 15:30:03 -0700485}
Josh Gao96049b92018-03-23 13:03:28 -0700486
Josh Gao90228a62019-04-25 14:04:57 -0700487#undef shutdown
488#define shutdown ____xxx_shutdown
Mike Lockwood81ffe172009-10-11 23:04:18 -0400489
Spencer Low3a2421b2015-05-22 20:09:06 -0700490// Closes a file descriptor that came from adb_open() or adb_open_mode(), but
491// not designed to take a file descriptor from unix_open(). See the comments
492// for adb_open() for more info.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700493inline int adb_close(int fd) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800494 return close(fd);
495}
Josh Gao90228a62019-04-25 14:04:57 -0700496#undef close
497#define close ____xxx_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800498
Casey Dahlin20238f22016-09-21 14:03:39 -0700499// On Windows, ADB has an indirection layer for file descriptors. If we get a
500// Win32 SOCKET object from an external library, we have to map it in to that
501// indirection layer, which this does.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700502inline int adb_register_socket(int s) {
Casey Dahlin20238f22016-09-21 14:03:39 -0700503 return s;
504}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800505
Elliott Hughes915d67f2020-04-06 09:15:35 -0700506static inline int adb_gethostname(char* name, size_t len) {
Joshua Duong290ccb52019-11-20 14:18:43 -0800507 return gethostname(name, len);
508}
509
Elliott Hughes915d67f2020-04-06 09:15:35 -0700510static inline int adb_getlogin_r(char* buf, size_t bufsize) {
Joshua Duong290ccb52019-11-20 14:18:43 -0800511 return getlogin_r(buf, bufsize);
512}
513
Elliott Hughes915d67f2020-04-06 09:15:35 -0700514static inline int adb_read(borrowed_fd fd, void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700515 return TEMP_FAILURE_RETRY(read(fd.get(), buf, len));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800516}
517
Elliott Hughes915d67f2020-04-06 09:15:35 -0700518static inline int adb_pread(borrowed_fd fd, void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700519#if defined(__APPLE__)
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800520 return TEMP_FAILURE_RETRY(pread(fd.get(), buf, len, offset));
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700521#else
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800522 return TEMP_FAILURE_RETRY(pread64(fd.get(), buf, len, offset));
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700523#endif
524}
525
Spencer Low55441402015-11-07 17:34:39 -0800526// Like unix_read(), but does not handle EINTR.
Elliott Hughes915d67f2020-04-06 09:15:35 -0700527static inline int unix_read_interruptible(borrowed_fd fd, void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700528 return read(fd.get(), buf, len);
Spencer Low55441402015-11-07 17:34:39 -0800529}
530
Josh Gao90228a62019-04-25 14:04:57 -0700531#undef read
532#define read ___xxx_read
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700533#undef pread
534#define pread ___xxx_pread
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800535
Elliott Hughes915d67f2020-04-06 09:15:35 -0700536static inline int adb_write(borrowed_fd fd, const void* buf, size_t len) {
Josh Gao90228a62019-04-25 14:04:57 -0700537 return TEMP_FAILURE_RETRY(write(fd.get(), buf, len));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800538}
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700539
Elliott Hughes915d67f2020-04-06 09:15:35 -0700540static inline int adb_pwrite(int fd, const void* buf, size_t len, off64_t offset) {
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700541#if defined(__APPLE__)
542 return TEMP_FAILURE_RETRY(pwrite(fd, buf, len, offset));
543#else
544 return TEMP_FAILURE_RETRY(pwrite64(fd, buf, len, offset));
545#endif
546}
547
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800548#undef write
549#define write ___xxx_write
Yurii Zubrytskyi243d2482019-07-10 17:59:34 -0700550#undef pwrite
551#define pwrite ___xxx_pwrite
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800552
Elliott Hughes915d67f2020-04-06 09:15:35 -0700553static inline int64_t adb_lseek(borrowed_fd fd, int64_t pos, int where) {
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700554#if defined(__APPLE__)
Josh Gao90228a62019-04-25 14:04:57 -0700555 return lseek(fd.get(), pos, where);
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700556#else
Josh Gao90228a62019-04-25 14:04:57 -0700557 return lseek64(fd.get(), pos, where);
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700558#endif
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800559}
Elliott Hughes9dcbc212018-09-20 13:59:49 -0700560#undef lseek
561#define lseek ___xxx_lseek
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800562
Elliott Hughes915d67f2020-04-06 09:15:35 -0700563static inline int adb_unlink(const char* path) {
Josh Gao90228a62019-04-25 14:04:57 -0700564 return unlink(path);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800565}
Josh Gao90228a62019-04-25 14:04:57 -0700566#undef unlink
567#define unlink ___xxx_unlink
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800568
Elliott Hughes915d67f2020-04-06 09:15:35 -0700569static inline int adb_creat(const char* path, int mode) {
Josh Gao90228a62019-04-25 14:04:57 -0700570 int fd = TEMP_FAILURE_RETRY(creat(path, mode));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800571
Josh Gao90228a62019-04-25 14:04:57 -0700572 if (fd < 0) return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800573
574 close_on_exec(fd);
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +0200575 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800576}
Josh Gao90228a62019-04-25 14:04:57 -0700577#undef creat
578#define creat ___xxx_creat
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800579
Elliott Hughes915d67f2020-04-06 09:15:35 -0700580static inline int unix_isatty(borrowed_fd fd) {
Josh Gao90228a62019-04-25 14:04:57 -0700581 return isatty(fd.get());
David Pursell58805362015-10-28 14:29:51 -0700582}
Josh Gao90228a62019-04-25 14:04:57 -0700583#define isatty ___xxx_isatty
David Pursell58805362015-10-28 14:29:51 -0700584
Spencer Low753d4852015-07-30 23:07:55 -0700585// Helper for network_* functions.
586inline int _fd_set_error_str(int fd, std::string* error) {
Josh Gao90228a62019-04-25 14:04:57 -0700587 if (fd == -1) {
588 *error = strerror(errno);
589 }
590 return fd;
Spencer Low753d4852015-07-30 23:07:55 -0700591}
592
Spencer Low753d4852015-07-30 23:07:55 -0700593inline int network_inaddr_any_server(int port, int type, std::string* error) {
Josh Gao90228a62019-04-25 14:04:57 -0700594 return _fd_set_error_str(socket_inaddr_any_server(port, type), error);
Spencer Low753d4852015-07-30 23:07:55 -0700595}
596
Josh Gao4a5a95d2016-08-24 18:38:44 -0700597inline int network_local_client(const char* name, int namespace_id, int type, std::string* error) {
598 return _fd_set_error_str(socket_local_client(name, namespace_id, type), error);
599}
600
601inline int network_local_server(const char* name, int namespace_id, int type, std::string* error) {
602 return _fd_set_error_str(socket_local_server(name, namespace_id, type), error);
Spencer Low753d4852015-07-30 23:07:55 -0700603}
604
Josh Gao8d7080c2018-08-10 16:03:09 -0700605int network_connect(const std::string& host, int port, int type, int timeout, std::string* error);
Spencer Low753d4852015-07-30 23:07:55 -0700606
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800607inline std::optional<ssize_t> network_peek(borrowed_fd fd) {
608 ssize_t ret = recv(fd.get(), nullptr, 0, MSG_PEEK | MSG_TRUNC);
609 return ret == -1 ? std::nullopt : std::make_optional(ret);
610}
611
Elliott Hughes915d67f2020-04-06 09:15:35 -0700612static inline int adb_socket_accept(borrowed_fd serverfd, struct sockaddr* addr,
613 socklen_t* addrlen) {
Benoit Gobyf4ded742011-02-01 18:57:41 -0800614 int fd;
615
Josh Gao90228a62019-04-25 14:04:57 -0700616 fd = TEMP_FAILURE_RETRY(accept(serverfd.get(), addr, addrlen));
617 if (fd >= 0) close_on_exec(fd);
Benoit Gobyf4ded742011-02-01 18:57:41 -0800618
619 return fd;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800620}
621
Josh Gao90228a62019-04-25 14:04:57 -0700622#undef accept
623#define accept ___xxx_accept
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800624
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800625inline int adb_getsockname(borrowed_fd fd, struct sockaddr* sockaddr, socklen_t* addrlen) {
626 return getsockname(fd.get(), sockaddr, addrlen);
627}
628
Josh Gao90228a62019-04-25 14:04:57 -0700629inline int adb_socket_get_local_port(borrowed_fd fd) {
630 return socket_get_local_port(fd.get());
David Pursell19d0c232016-04-07 11:25:48 -0700631}
632
Spencer Low3a2421b2015-05-22 20:09:06 -0700633// Operate on a file descriptor returned from unix_open() or a well-known file
634// descriptor such as STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO.
635//
636// On Unix, unix_read(), unix_write(), unix_close() map to adb_read(),
637// adb_write(), adb_close() (which all map to Unix system calls), but the
638// Windows implementations (in the ifdef above and in sysdeps_win32.cpp) call
639// into the C Runtime and its configurable CR/LF translation (which is settable
640// via _setmode()).
Josh Gao90228a62019-04-25 14:04:57 -0700641#define unix_read adb_read
642#define unix_write adb_write
Spencer Low04b575d2018-09-02 19:19:39 -0700643#define unix_lseek adb_lseek
Josh Gao90228a62019-04-25 14:04:57 -0700644#define unix_close adb_close
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800645
Elliott Hughes915d67f2020-04-06 09:15:35 -0700646static inline int adb_thread_setname(const std::string& name) {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700647#ifdef __APPLE__
648 return pthread_setname_np(name.c_str());
649#else
Elliott Hughes3bb1b572017-08-02 13:22:38 -0700650 // Both bionic and glibc's pthread_setname_np fails rather than truncating long strings.
651 // glibc doesn't have strlcpy, so we have to fake it.
652 char buf[16]; // MAX_TASK_COMM_LEN, but that's not exported by the kernel headers.
653 strncpy(buf, name.c_str(), sizeof(buf) - 1);
654 buf[sizeof(buf) - 1] = '\0';
655 return pthread_setname_np(pthread_self(), buf);
Siva Velusamy2669cf92015-08-28 16:37:29 -0700656#endif
657}
658
Elliott Hughes915d67f2020-04-06 09:15:35 -0700659static inline int adb_setsockopt(borrowed_fd fd, int level, int optname, const void* optval,
660 socklen_t optlen) {
Josh Gao90228a62019-04-25 14:04:57 -0700661 return setsockopt(fd.get(), level, optname, optval, optlen);
Spencer Low31aafa62015-01-25 14:40:16 -0800662}
663
Josh Gao90228a62019-04-25 14:04:57 -0700664#undef setsockopt
665#define setsockopt ___xxx_setsockopt
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800666
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800667static inline int adb_socket(int domain, int type, int protocol) {
668 return socket(domain, type, protocol);
669}
670
671static inline int adb_bind(borrowed_fd fd, const sockaddr* addr, int namelen) {
672 return bind(fd.get(), addr, namelen);
673}
674
Elliott Hughes915d67f2020-04-06 09:15:35 -0700675static inline int unix_socketpair(int d, int type, int protocol, int sv[2]) {
Josh Gao90228a62019-04-25 14:04:57 -0700676 return socketpair(d, type, protocol, sv);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800677}
678
Elliott Hughes915d67f2020-04-06 09:15:35 -0700679static inline int adb_socketpair(int sv[2]) {
Josh Gao90228a62019-04-25 14:04:57 -0700680 int rc;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800681
Josh Gao90228a62019-04-25 14:04:57 -0700682 rc = unix_socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
683 if (rc < 0) return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800684
Josh Gao90228a62019-04-25 14:04:57 -0700685 close_on_exec(sv[0]);
686 close_on_exec(sv[1]);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800687 return 0;
688}
689
Josh Gao90228a62019-04-25 14:04:57 -0700690#undef socketpair
691#define socketpair ___xxx_socketpair
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800692
Joshua Duongf4ba8d72021-01-13 12:18:15 -0800693typedef struct msghdr adb_msghdr;
694inline ssize_t adb_sendmsg(borrowed_fd fd, const adb_msghdr* msg, int flags) {
695 return sendmsg(fd.get(), msg, flags);
696}
697
698inline ssize_t adb_recvmsg(borrowed_fd fd, adb_msghdr* msg, int flags) {
699 return recvmsg(fd.get(), msg, flags);
700}
701
702using adb_cmsghdr = cmsghdr;
703
704inline adb_cmsghdr* adb_CMSG_FIRSTHDR(adb_msghdr* msgh) {
705 return CMSG_FIRSTHDR(msgh);
706}
707
708inline adb_cmsghdr* adb_CMSG_NXTHDR(adb_msghdr* msgh, adb_cmsghdr* cmsg) {
709 return CMSG_NXTHDR(msgh, cmsg);
710}
711
712inline unsigned char* adb_CMSG_DATA(adb_cmsghdr* cmsg) {
713 return CMSG_DATA(cmsg);
714}
715
Josh Gaoe7388122016-02-16 17:34:53 -0800716typedef struct pollfd adb_pollfd;
Elliott Hughes915d67f2020-04-06 09:15:35 -0700717static inline int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) {
Josh Gaoe7388122016-02-16 17:34:53 -0800718 return TEMP_FAILURE_RETRY(poll(fds, nfds, timeout));
719}
720
721#define poll ___xxx_poll
722
Elliott Hughes915d67f2020-04-06 09:15:35 -0700723static inline int adb_mkdir(const std::string& path, int mode) {
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400724 return mkdir(path.c_str(), mode);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800725}
Alex Vallée28d1f8d2015-05-06 16:26:00 -0400726
Josh Gao90228a62019-04-25 14:04:57 -0700727#undef mkdir
728#define mkdir ___xxx_mkdir
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800729
Elliott Hughes915d67f2020-04-06 09:15:35 -0700730static inline int adb_rename(const char* oldpath, const char* newpath) {
Joshua Duong290ccb52019-11-20 14:18:43 -0800731 return rename(oldpath, newpath);
732}
733
Elliott Hughes915d67f2020-04-06 09:15:35 -0700734static inline int adb_is_absolute_host_path(const char* path) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800735 return path[0] == '/';
736}
737
Elliott Hughes915d67f2020-04-06 09:15:35 -0700738static inline int adb_get_os_handle(borrowed_fd fd) {
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700739 return fd.get();
740}
741
Elliott Hughes915d67f2020-04-06 09:15:35 -0700742static inline int cast_handle_to_int(int fd) {
Yurii Zubrytskyi3c0574f2020-03-26 18:16:36 -0700743 return fd;
744}
745
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800746// A very simple wrapper over a launched child process
747class Process {
748 public:
749 constexpr explicit Process(pid_t pid) : pid_(pid) {}
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700750 constexpr Process(Process&& other) : pid_(std::exchange(other.pid_, -1)) {}
751
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800752 constexpr explicit operator bool() const { return pid_ >= 0; }
753
754 void wait() {
755 if (*this) {
756 int status;
757 ::waitpid(pid_, &status, 0);
758 pid_ = -1;
759 }
760 }
761 void kill() {
762 if (*this) {
763 ::kill(pid_, SIGTERM);
764 }
765 }
766
767 private:
Yurii Zubrytskyi745a8182020-03-18 15:49:45 -0700768 DISALLOW_COPY_AND_ASSIGN(Process);
769
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800770 pid_t pid_;
771};
772
773Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
774 std::initializer_list<int> fds_to_inherit = {});
775
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800776#endif /* !_WIN32 */
777
Josh Gao90228a62019-04-25 14:04:57 -0700778static inline void disable_tcp_nagle(borrowed_fd fd) {
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800779 int off = 1;
Josh Gao90228a62019-04-25 14:04:57 -0700780 adb_setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &off, sizeof(off));
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800781}
782
David Pursellc25a34e2016-02-22 14:27:23 -0800783// Sets TCP socket |fd| to send a keepalive TCP message every |interval_sec| seconds. Set
784// |interval_sec| to 0 to disable keepalives. If keepalives are enabled, the connection will be
785// configured to drop after 10 missed keepalives. Returns true on success.
Josh Gao90228a62019-04-25 14:04:57 -0700786bool set_tcp_keepalive(borrowed_fd fd, int interval_sec);
David Pursellc25a34e2016-02-22 14:27:23 -0800787
shajue1f98522023-03-20 10:09:25 -0700788// Returns a human-readable OS version string.
789extern std::string GetOSVersion(void);
790
Elliott Hughes801066a2016-06-29 17:42:01 -0700791#if defined(_WIN32)
792// Win32 defines ERROR, which we don't need, but which conflicts with google3 logging.
793#undef ERROR
794#endif