blob: f69babe6c6b82e0395ded340581adf5b90a7d7b2 [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
25namespace {
26
27class AdbFdTextOutput : public android::TextOutput {
28 public:
29 explicit AdbFdTextOutput(int fd) : mFD(fd) {}
30
31 private:
32 android::status_t print(const char* txt, size_t len) override {
33 return WriteFdExactly(mFD, txt, len) ? android::OK : -errno;
34 }
35 void moveIndent(int delta) override { /*not implemented*/
36 }
37
38 void pushBundle() override { /*not implemented*/
39 }
40 void popBundle() override { /*not implemented*/
41 }
42
43 private:
44 int mFD;
45};
46
47std::vector<std::string_view> parseCmdArgs(std::string_view args) {
48 std::vector<std::string_view> argv;
49
50 char delim = ABB_ARG_DELIMETER;
51 size_t size = args.size();
52 size_t base = 0;
53 while (base < size) {
54 size_t found;
55 for (found = base; found < size && args[found] && args[found] != delim; ++found)
56 ;
57 if (found > base) {
58 argv.emplace_back(args.substr(base, found - base));
59 }
60 base = found + 1;
61 }
62
63 return argv;
64}
65
66} // namespace
67
68static int execCmd(std::string_view args, int in, int out, int err) {
69 AdbFdTextOutput oin(out);
70 AdbFdTextOutput oerr(err);
71 return cmdMain(parseCmdArgs(args), oin, oerr, in, out, err, RunMode::kLibrary);
72}
73
74int main(int argc, char* const argv[]) {
75 signal(SIGPIPE, SIG_IGN);
76
77 int fd = STDIN_FILENO;
78 std::string data;
79 while (true) {
80 std::string error;
81 if (!ReadProtocolString(fd, &data, &error)) {
82 PLOG(ERROR) << "Failed to read message: " << error;
83 break;
84 }
85
86 auto result = StartCommandInProcess(std::move(data), &execCmd);
87 if (!SendFileDescriptor(fd, result)) {
88 PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data;
89 break;
90 }
91 }
92}