1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "sysdeps.h"
18 
19 #include <sys/utsname.h>
20 
21 #include <android-base/logging.h>
22 #include <android-base/stringprintf.h>
23 
set_tcp_keepalive(borrowed_fd fd,int interval_sec)24 bool set_tcp_keepalive(borrowed_fd fd, int interval_sec) {
25     int enable = (interval_sec > 0);
26     if (adb_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable))) {
27         return false;
28     }
29 
30     if (!enable) {
31         return true;
32     }
33 
34     // Idle time before sending the first keepalive is TCP_KEEPIDLE on Linux, TCP_KEEPALIVE on Mac.
35 #if defined(TCP_KEEPIDLE)
36     if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &interval_sec, sizeof(interval_sec))) {
37         return false;
38     }
39 #elif defined(TCP_KEEPALIVE)
40     if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &interval_sec, sizeof(interval_sec))) {
41         return false;
42     }
43 #endif
44 
45     // TCP_KEEPINTVL and TCP_KEEPCNT are available on Linux 2.4+ and OS X 10.8+ (Mountain Lion).
46 #if defined(TCP_KEEPINTVL)
47     if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval_sec, sizeof(interval_sec))) {
48         return false;
49     }
50 #endif
51 
52 #if defined(TCP_KEEPCNT)
53     // On Windows this value is hardcoded to 10. This is a reasonable value, so we do the same here
54     // to match behavior. See SO_KEEPALIVE documentation at
55     // https://msdn.microsoft.com/en-us/library/windows/desktop/ee470551(v=vs.85).aspx.
56     const int keepcnt = 10;
57     if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(keepcnt))) {
58         return false;
59     }
60 #endif
61 
62     return true;
63 }
64 
disable_close_on_exec(borrowed_fd fd)65 static __inline__ void disable_close_on_exec(borrowed_fd fd) {
66     const auto oldFlags = fcntl(fd.get(), F_GETFD);
67     const auto newFlags = (oldFlags & ~FD_CLOEXEC);
68     if (newFlags != oldFlags) {
69         fcntl(fd.get(), F_SETFD, newFlags);
70     }
71 }
72 
adb_launch_process(std::string_view executable,std::vector<std::string> args,std::initializer_list<int> fds_to_inherit)73 Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
74                            std::initializer_list<int> fds_to_inherit) {
75     const auto pid = fork();
76     if (pid != 0) {
77         // parent, includes the case when failed to fork()
78         return Process(pid);
79     }
80     // child
81     std::vector<std::string> copies;
82     copies.reserve(args.size() + 1);
83     copies.emplace_back(executable);
84     copies.insert(copies.end(), std::make_move_iterator(args.begin()),
85                   std::make_move_iterator(args.end()));
86 
87     std::vector<char*> rawArgs;
88     rawArgs.reserve(copies.size() + 1);
89     for (auto&& str : copies) {
90         rawArgs.push_back(str.data());
91     }
92     rawArgs.push_back(nullptr);
93     for (auto fd : fds_to_inherit) {
94         disable_close_on_exec(fd);
95     }
96     exit(execv(copies.front().data(), rawArgs.data()));
97 }
98 
99 // For Unix variants (Linux, OSX), the underlying uname() system call
100 // is utilized to extract out a version string comprising of:
101 // 1.) "Linux" or "Darwin"
102 // 2.) OS system release (e.g. "5.19.11")
103 // 3.) machine (e.g. "x86_64")
104 // like: "Linux 5.19.11-1<snip>1-amd64 (x86_64)"
GetOSVersion(void)105 std::string GetOSVersion(void) {
106     utsname name;
107     uname(&name);
108 
109     return android::base::StringPrintf("%s %s (%s)", name.sysname, name.release, name.machine);
110 }
111 
network_peek(borrowed_fd fd)112 std::optional<ssize_t> network_peek(borrowed_fd fd) {
113     ssize_t upper_bound_bytes;
114 #if defined(__APPLE__)
115     // Can't use recv(MSG_TRUNC) (not supported).
116     // Can't use ioctl(FIONREAD) (returns size in socket queue instead next message size).
117     socklen_t optlen = sizeof(upper_bound_bytes);
118     if (getsockopt(fd.get(), SOL_SOCKET, SO_NREAD, &upper_bound_bytes, &optlen) == -1) {
119         upper_bound_bytes = -1;
120     }
121 #else
122     upper_bound_bytes = recv(fd.get(), nullptr, 0, MSG_PEEK | MSG_TRUNC);
123 #endif
124     if (upper_bound_bytes == -1) {
125         PLOG(ERROR) << "network_peek error";
126     }
127     return upper_bound_bytes == -1 ? std::nullopt : std::make_optional(upper_bound_bytes);
128 }