blob: d949dd118cf9f6b6cc2b1164a5929c65664bf12b [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
17#include "adb.h"
18#include "adb_io.h"
19#include "shell_service.h"
20
21#include "cmd.h"
22
23#include <sys/wait.h>
24
Josh Gao8e0af5f2019-01-10 14:29:29 -080025#include <android-base/cmsg.h>
26
Alex Buynytskyy640407d2018-12-12 10:48:50 -080027namespace {
28
29class AdbFdTextOutput : public android::TextOutput {
30 public:
31 explicit AdbFdTextOutput(int fd) : mFD(fd) {}
32
33 private:
34 android::status_t print(const char* txt, size_t len) override {
35 return WriteFdExactly(mFD, txt, len) ? android::OK : -errno;
36 }
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:
46 int mFD;
47};
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
70static int execCmd(std::string_view args, int in, int out, int err) {
71 AdbFdTextOutput oin(out);
72 AdbFdTextOutput oerr(err);
73 return cmdMain(parseCmdArgs(args), oin, oerr, in, out, err, RunMode::kLibrary);
74}
75
76int main(int argc, char* const argv[]) {
77 signal(SIGPIPE, SIG_IGN);
78
79 int fd = STDIN_FILENO;
80 std::string data;
81 while (true) {
82 std::string error;
83 if (!ReadProtocolString(fd, &data, &error)) {
84 PLOG(ERROR) << "Failed to read message: " << error;
85 break;
86 }
87
Josh Gao8e0af5f2019-01-10 14:29:29 -080088 unique_fd result = StartCommandInProcess(std::move(data), &execCmd);
89 if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) {
Alex Buynytskyy640407d2018-12-12 10:48:50 -080090 PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data;
91 break;
92 }
93 }
94}