Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 17 | #include <sys/wait.h> |
| 18 | |
Josh Gao | 8e0af5f | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 19 | #include <android-base/cmsg.h> |
Elliott Hughes | b4dc7be | 2019-05-03 09:02:45 -0700 | [diff] [blame^] | 20 | #include <android-base/strings.h> |
Josh Gao | ccc5845 | 2019-02-25 13:02:43 -0800 | [diff] [blame] | 21 | #include <cmd.h> |
| 22 | |
| 23 | #include "adb.h" |
| 24 | #include "adb_io.h" |
| 25 | #include "adb_utils.h" |
| 26 | #include "shell_service.h" |
Josh Gao | 8e0af5f | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 27 | |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | class AdbFdTextOutput : public android::TextOutput { |
| 31 | public: |
Ian Kasprzak | 2c58e19 | 2019-04-25 15:59:28 +0000 | [diff] [blame] | 32 | explicit AdbFdTextOutput(int fd) : mFD(fd) {} |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 33 | |
| 34 | private: |
| 35 | android::status_t print(const char* txt, size_t len) override { |
Ian Kasprzak | 2c58e19 | 2019-04-25 15:59:28 +0000 | [diff] [blame] | 36 | return WriteFdExactly(mFD, txt, len) ? android::OK : -errno; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 37 | } |
| 38 | void moveIndent(int delta) override { /*not implemented*/ |
| 39 | } |
| 40 | |
| 41 | void pushBundle() override { /*not implemented*/ |
| 42 | } |
| 43 | void popBundle() override { /*not implemented*/ |
| 44 | } |
| 45 | |
| 46 | private: |
Ian Kasprzak | 2c58e19 | 2019-04-25 15:59:28 +0000 | [diff] [blame] | 47 | int mFD; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | std::vector<std::string_view> parseCmdArgs(std::string_view args) { |
| 51 | std::vector<std::string_view> argv; |
| 52 | |
| 53 | char delim = ABB_ARG_DELIMETER; |
| 54 | size_t size = args.size(); |
| 55 | size_t base = 0; |
| 56 | while (base < size) { |
| 57 | size_t found; |
| 58 | for (found = base; found < size && args[found] && args[found] != delim; ++found) |
| 59 | ; |
| 60 | if (found > base) { |
| 61 | argv.emplace_back(args.substr(base, found - base)); |
| 62 | } |
| 63 | base = found + 1; |
| 64 | } |
| 65 | |
| 66 | return argv; |
| 67 | } |
| 68 | |
| 69 | } // namespace |
| 70 | |
Ian Kasprzak | 2c58e19 | 2019-04-25 15:59:28 +0000 | [diff] [blame] | 71 | static int execCmd(std::string_view args, int in, int out, int err) { |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 72 | AdbFdTextOutput oin(out); |
| 73 | AdbFdTextOutput oerr(err); |
Ian Kasprzak | 2c58e19 | 2019-04-25 15:59:28 +0000 | [diff] [blame] | 74 | return cmdMain(parseCmdArgs(args), oin, oerr, in, out, err, RunMode::kLibrary); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int 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 Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 89 | std::string_view name = data; |
| 90 | auto protocol = SubprocessProtocol::kShell; |
Elliott Hughes | b4dc7be | 2019-05-03 09:02:45 -0700 | [diff] [blame^] | 91 | if (android::base::ConsumePrefix(&name, "abb:")) { |
Alex Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 92 | protocol = SubprocessProtocol::kShell; |
Elliott Hughes | b4dc7be | 2019-05-03 09:02:45 -0700 | [diff] [blame^] | 93 | } else if (android::base::ConsumePrefix(&name, "abb_exec:")) { |
Alex Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 94 | 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 Gao | 8e0af5f | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 100 | if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) { |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 101 | PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |