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/stringprintf.h>
22
set_tcp_keepalive(borrowed_fd fd,int interval_sec)23 bool set_tcp_keepalive(borrowed_fd fd, int interval_sec) {
24 int enable = (interval_sec > 0);
25 if (adb_setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable))) {
26 return false;
27 }
28
29 if (!enable) {
30 return true;
31 }
32
33 // Idle time before sending the first keepalive is TCP_KEEPIDLE on Linux, TCP_KEEPALIVE on Mac.
34 #if defined(TCP_KEEPIDLE)
35 if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &interval_sec, sizeof(interval_sec))) {
36 return false;
37 }
38 #elif defined(TCP_KEEPALIVE)
39 if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &interval_sec, sizeof(interval_sec))) {
40 return false;
41 }
42 #endif
43
44 // TCP_KEEPINTVL and TCP_KEEPCNT are available on Linux 2.4+ and OS X 10.8+ (Mountain Lion).
45 #if defined(TCP_KEEPINTVL)
46 if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval_sec, sizeof(interval_sec))) {
47 return false;
48 }
49 #endif
50
51 #if defined(TCP_KEEPCNT)
52 // On Windows this value is hardcoded to 10. This is a reasonable value, so we do the same here
53 // to match behavior. See SO_KEEPALIVE documentation at
54 // https://msdn.microsoft.com/en-us/library/windows/desktop/ee470551(v=vs.85).aspx.
55 const int keepcnt = 10;
56 if (adb_setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(keepcnt))) {
57 return false;
58 }
59 #endif
60
61 return true;
62 }
63
disable_close_on_exec(borrowed_fd fd)64 static __inline__ void disable_close_on_exec(borrowed_fd fd) {
65 const auto oldFlags = fcntl(fd.get(), F_GETFD);
66 const auto newFlags = (oldFlags & ~FD_CLOEXEC);
67 if (newFlags != oldFlags) {
68 fcntl(fd.get(), F_SETFD, newFlags);
69 }
70 }
71
adb_launch_process(std::string_view executable,std::vector<std::string> args,std::initializer_list<int> fds_to_inherit)72 Process adb_launch_process(std::string_view executable, std::vector<std::string> args,
73 std::initializer_list<int> fds_to_inherit) {
74 const auto pid = fork();
75 if (pid != 0) {
76 // parent, includes the case when failed to fork()
77 return Process(pid);
78 }
79 // child
80 std::vector<std::string> copies;
81 copies.reserve(args.size() + 1);
82 copies.emplace_back(executable);
83 copies.insert(copies.end(), std::make_move_iterator(args.begin()),
84 std::make_move_iterator(args.end()));
85
86 std::vector<char*> rawArgs;
87 rawArgs.reserve(copies.size() + 1);
88 for (auto&& str : copies) {
89 rawArgs.push_back(str.data());
90 }
91 rawArgs.push_back(nullptr);
92 for (auto fd : fds_to_inherit) {
93 disable_close_on_exec(fd);
94 }
95 exit(execv(copies.front().data(), rawArgs.data()));
96 }
97
98 // For Unix variants (Linux, OSX), the underlying uname() system call
99 // is utilized to extract out a version string comprising of:
100 // 1.) "Linux" or "Darwin"
101 // 2.) OS system release (e.g. "5.19.11")
102 // 3.) machine (e.g. "x86_64")
103 // like: "Linux 5.19.11-1<snip>1-amd64 (x86_64)"
GetOSVersion(void)104 std::string GetOSVersion(void) {
105 utsname name;
106 uname(&name);
107
108 return android::base::StringPrintf("%s %s (%s)", name.sysname, name.release, name.machine);
109 }
110