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" |
Yurii Zubrytskyi | 6eca0e6 | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 27 | #include "sysdeps.h" |
Josh Gao | 8e0af5f | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 28 | |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | class AdbFdTextOutput : public android::TextOutput { |
| 32 | public: |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 33 | explicit AdbFdTextOutput(borrowed_fd fd) : fd_(fd) {} |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 34 | |
| 35 | private: |
| 36 | android::status_t print(const char* txt, size_t len) override { |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 37 | return WriteFdExactly(fd_, txt, len) ? android::OK : -errno; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 38 | } |
| 39 | void moveIndent(int delta) override { /*not implemented*/ |
| 40 | } |
| 41 | |
| 42 | void pushBundle() override { /*not implemented*/ |
| 43 | } |
| 44 | void popBundle() override { /*not implemented*/ |
| 45 | } |
| 46 | |
| 47 | private: |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 48 | borrowed_fd fd_; |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | std::vector<std::string_view> parseCmdArgs(std::string_view args) { |
| 52 | std::vector<std::string_view> argv; |
| 53 | |
| 54 | char delim = ABB_ARG_DELIMETER; |
| 55 | size_t size = args.size(); |
| 56 | size_t base = 0; |
| 57 | while (base < size) { |
| 58 | size_t found; |
| 59 | for (found = base; found < size && args[found] && args[found] != delim; ++found) |
| 60 | ; |
| 61 | if (found > base) { |
| 62 | argv.emplace_back(args.substr(base, found - base)); |
| 63 | } |
| 64 | base = found + 1; |
| 65 | } |
| 66 | |
| 67 | return argv; |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 72 | static int execCmd(std::string_view args, borrowed_fd in, borrowed_fd out, borrowed_fd err) { |
Yurii Zubrytskyi | 6eca0e6 | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 73 | int max_buf = LINUX_MAX_SOCKET_SIZE; |
| 74 | adb_setsockopt(in, SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
| 75 | adb_setsockopt(out, SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
| 76 | adb_setsockopt(err, SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
| 77 | |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 78 | AdbFdTextOutput oin(out); |
| 79 | AdbFdTextOutput oerr(err); |
Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 80 | return cmdMain(parseCmdArgs(args), oin, oerr, in.get(), out.get(), err.get(), |
| 81 | RunMode::kLibrary); |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | int main(int argc, char* const argv[]) { |
| 85 | signal(SIGPIPE, SIG_IGN); |
| 86 | |
| 87 | int fd = STDIN_FILENO; |
| 88 | std::string data; |
| 89 | while (true) { |
| 90 | std::string error; |
| 91 | if (!ReadProtocolString(fd, &data, &error)) { |
| 92 | PLOG(ERROR) << "Failed to read message: " << error; |
| 93 | break; |
| 94 | } |
| 95 | |
Alex Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 96 | std::string_view name = data; |
| 97 | auto protocol = SubprocessProtocol::kShell; |
Elliott Hughes | b4dc7be | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 98 | if (android::base::ConsumePrefix(&name, "abb:")) { |
Alex Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 99 | protocol = SubprocessProtocol::kShell; |
Elliott Hughes | b4dc7be | 2019-05-03 09:02:45 -0700 | [diff] [blame] | 100 | } else if (android::base::ConsumePrefix(&name, "abb_exec:")) { |
Alex Buynytskyy | 05626c1 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 101 | protocol = SubprocessProtocol::kNone; |
| 102 | } else { |
| 103 | LOG(FATAL) << "Unknown command prefix for abb: " << data; |
| 104 | } |
| 105 | |
| 106 | unique_fd result = StartCommandInProcess(std::string(name), &execCmd, protocol); |
Yurii Zubrytskyi | 6eca0e6 | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 107 | int max_buf = LINUX_MAX_SOCKET_SIZE; |
| 108 | adb_setsockopt(result, SOL_SOCKET, SO_SNDBUF, &max_buf, sizeof(max_buf)); |
Josh Gao | 8e0af5f | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 109 | if (android::base::SendFileDescriptors(fd, "", 1, result.get()) != 1) { |
Alex Buynytskyy | 640407d | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 110 | PLOG(ERROR) << "Failed to send an inprocess fd for command: " << data; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |