blob: 87ee8aa9898a4c8138f01efe7bd3d67dc11acdc4 [file] [log] [blame]
Alex Buynytskyy640407d2018-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
Alex Buynytskyy640407d2018-12-12 10:48:50 -080017#include <sys/wait.h>
18
Josh Gao8e0af5f2019-01-10 14:29:29 -080019#include <android-base/cmsg.h>
Josh Gaoccc58452019-02-25 13:02:43 -080020#include <cmd.h>
21
22#include "adb.h"
23#include "adb_io.h"
24#include "adb_utils.h"
25#include "shell_service.h"
Josh Gao8e0af5f2019-01-10 14:29:29 -080026
Alex Buynytskyy640407d2018-12-12 10:48:50 -080027namespace {
28
29class AdbFdTextOutput : public android::TextOutput {
30 public:
Josh Gao93d63c02019-04-18 16:39:30 -070031 explicit AdbFdTextOutput(borrowed_fd fd) : fd_(fd) {}
Alex Buynytskyy640407d2018-12-12 10:48:50 -080032
33 private:
34 android::status_t print(const char* txt, size_t len) override {
Josh Gao93d63c02019-04-18 16:39:30 -070035 return WriteFdExactly(fd_, txt, len) ? android::OK : -errno;
Alex Buynytskyy640407d2018-12-12 10:48:50 -080036 }
37 void moveIndent(int delta) override { /*not implemented*/
38 }
39
40 void pushBundle() override { /*not implemented*/
41 }
42 void popBundle() override { /*not implemented*/
43 }
44
45 private:
Josh Gao93d63c02019-04-18 16:39:30 -070046 borrowed_fd fd_;
Alex Buynytskyy640407d2018-12-12 10:48:50 -080047};
48
49std::vector<std::string_view> parseCmdArgs(std::string_view args) {
50 std::vector<std::string_view> argv;
51
52 char delim = ABB_ARG_DELIMETER;
53 size_t size = args.size();
54 size_t base = 0;
55 while (base < size) {
56 size_t found;
57 for (found = base; found < size && args[found] && args[found] != delim; ++found)
58 ;
59 if (found > base) {
60 argv.emplace_back(args.substr(base, found - base));
61 }
62 base = found + 1;
63 }
64
65 return argv;
66}
67
68} // namespace
69
Josh Gao93d63c02019-04-18 16:39:30 -070070static int execCmd(std::string_view args, borrowed_fd in, borrowed_fd out, borrowed_fd err) {
Alex Buynytskyy640407d2018-12-12 10:48:50 -080071 AdbFdTextOutput oin(out);
72 AdbFdTextOutput oerr(err);
Josh Gao93d63c02019-04-18 16:39:30 -070073 return cmdMain(parseCmdArgs(args), oin, oerr, in.get(), out.get(), err.get(),
74 RunMode::kLibrary);
Alex Buynytskyy640407d2018-12-12 10:48:50 -080075}
76
77int main(int argc, char* const argv[]) {
78 signal(SIGPIPE, SIG_IGN);
79
80 int fd = STDIN_FILENO;
81 std::string data;
82 while (true) {
83 std::string error;
84 if (!ReadProtocolString(fd, &data, &error)) {
85 PLOG(ERROR) << "Failed to read message: " << error;
86 break;
87 }
88
Alex Buynytskyy05626c12019-02-21 14:22:51 -080089 std::string_view name = data;
90 auto protocol = SubprocessProtocol::kShell;
Josh Gaoccc58452019-02-25 13:02:43 -080091 if (ConsumePrefix(&name, "abb:")) {
Alex Buynytskyy05626c12019-02-21 14:22:51 -080092 protocol = SubprocessProtocol::kShell;
Josh Gaoccc58452019-02-25 13:02:43 -080093 } else if (ConsumePrefix(&name, "abb_exec:")) {
Alex Buynytskyy05626c12019-02-21 14:22:51 -080094 protocol = SubprocessProtocol::kNone;
95 } else {
96 LOG(FATAL) << "Unknown command prefix for abb: " << data;
97 }
98
99 unique_fd result = StartCommandInProcess(std::string(name), &execCmd, protocol);
Josh Gao8e0af5f2019-01-10 14:29:29 -0800100 if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) {
Alex Buynytskyy640407d2018-12-12 10:48:50 -0800101 PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data;
102 break;
103 }
104 }
105}