blob: 9725f5cee5a659431dd8ca34fdfaae64d71d53f7 [file] [log] [blame]
Alex Buynytskyyef34f012018-12-12 10:48:50 -08001/*
2 * Copyright (C) 2018 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 "adb.h"
18#include "adb_io.h"
19#include "adb_unique_fd.h"
20#include "adb_utils.h"
21#include "shell_service.h"
22
Josh Gao2d83b542019-01-10 14:29:29 -080023#include <android-base/cmsg.h>
24
Alex Buynytskyyef34f012018-12-12 10:48:50 -080025namespace {
26
Alex Buynytskyyef34f012018-12-12 10:48:50 -080027struct AbbProcess {
28 unique_fd sendCommand(std::string_view command);
29
30 private:
31 static unique_fd startAbbProcess(unique_fd* error_fd);
32
33 static constexpr auto kRetries = 2;
34 static constexpr auto kErrorProtocol = SubprocessProtocol::kShell;
35
36 std::mutex locker_;
37 unique_fd socket_fd_;
38};
39
40unique_fd AbbProcess::sendCommand(std::string_view command) {
41 std::unique_lock lock{locker_};
42
43 for (int i = 0; i < kRetries; ++i) {
44 unique_fd error_fd;
45 if (socket_fd_ == -1) {
46 socket_fd_ = startAbbProcess(&error_fd);
47 }
48 if (socket_fd_ == -1) {
49 LOG(ERROR) << "failed to start abb process";
50 return error_fd;
51 }
52
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070053 if (!SendProtocolString(socket_fd_, command)) {
Alex Buynytskyyef34f012018-12-12 10:48:50 -080054 PLOG(ERROR) << "failed to send command to abb";
55 socket_fd_.reset();
56 continue;
57 }
58
59 unique_fd fd;
Josh Gao2d83b542019-01-10 14:29:29 -080060 char buf;
61 if (android::base::ReceiveFileDescriptors(socket_fd_, &buf, 1, &fd) != 1) {
62 PLOG(ERROR) << "failed to receive FD from abb";
Alex Buynytskyyef34f012018-12-12 10:48:50 -080063 socket_fd_.reset();
64 continue;
65 }
66
67 return fd;
68 }
69
70 LOG(ERROR) << "abb is unavailable";
71 socket_fd_.reset();
72 return ReportError(kErrorProtocol, "abb is unavailable");
73}
74
75unique_fd AbbProcess::startAbbProcess(unique_fd* error_fd) {
76 constexpr auto abb_process_type = SubprocessType::kRaw;
77 constexpr auto abb_protocol = SubprocessProtocol::kNone;
78 constexpr auto make_pty_raw = false;
79 return StartSubprocess("abb", "dumb", abb_process_type, abb_protocol, make_pty_raw,
80 kErrorProtocol, error_fd);
81}
82
Ryan Prichardecff26f2024-01-05 13:54:36 -080083static auto& abbp = *new AbbProcess;
84
Alex Buynytskyyef34f012018-12-12 10:48:50 -080085} // namespace
86
Alex Buynytskyy4f3fa052019-02-21 14:22:51 -080087unique_fd execute_abb_command(std::string_view command) {
Ryan Prichardecff26f2024-01-05 13:54:36 -080088 return abbp.sendCommand(command);
Alex Buynytskyyef34f012018-12-12 10:48:50 -080089}