Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef COMMANDLINE_H |
| 18 | #define COMMANDLINE_H |
| 19 | |
Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 20 | #include <android-base/strings.h> |
| 21 | |
Shaju Mathew | bf7da1c | 2021-10-30 13:47:11 -0700 | [diff] [blame] | 22 | #include <optional> |
| 23 | |
Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 24 | #include "adb.h" |
Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 25 | #include "adb_client.h" |
| 26 | #include "adb_unique_fd.h" |
Shaju Mathew | bf7da1c | 2021-10-30 13:47:11 -0700 | [diff] [blame] | 27 | #include "transport.h" |
Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 28 | |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 29 | // Callback used to handle the standard streams (stdout and stderr) sent by the |
| 30 | // device's upon receiving a command. |
Shaju Mathew | bf7da1c | 2021-10-30 13:47:11 -0700 | [diff] [blame] | 31 | |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 32 | // |
| 33 | class StandardStreamsCallbackInterface { |
| 34 | public: |
| 35 | StandardStreamsCallbackInterface() { |
| 36 | } |
| 37 | // Handles the stdout output from devices supporting the Shell protocol. |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 38 | // Returns true on success and false on failure. |
| 39 | virtual bool OnStdout(const char* buffer, size_t length) = 0; |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 40 | |
| 41 | // Handles the stderr output from devices supporting the Shell protocol. |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 42 | // Returns true on success and false on failure. |
| 43 | virtual bool OnStderr(const char* buffer, size_t length) = 0; |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 44 | |
| 45 | // Indicates the communication is finished and returns the appropriate error |
| 46 | // code. |
| 47 | // |
| 48 | // |status| has the status code returning by the underlying communication |
| 49 | // channels |
| 50 | virtual int Done(int status) = 0; |
| 51 | |
| 52 | protected: |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 53 | static bool OnStream(std::string* string, FILE* stream, const char* buffer, size_t length, |
| 54 | bool returnErrors) { |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 55 | if (string != nullptr) { |
| 56 | string->append(buffer, length); |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 57 | return true; |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 58 | } else { |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 59 | bool okay = (fwrite(buffer, 1, length, stream) == length); |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 60 | fflush(stream); |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 61 | return returnErrors ? okay : true; |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | DISALLOW_COPY_AND_ASSIGN(StandardStreamsCallbackInterface); |
| 67 | }; |
| 68 | |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 69 | // Default implementation that redirects the streams to the equivalent host |
| 70 | // stream or to a string passed to the constructor. |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 71 | class DefaultStandardStreamsCallback : public StandardStreamsCallbackInterface { |
| 72 | public: |
| 73 | // If |stdout_str| is non-null, OnStdout will append to it. |
| 74 | // If |stderr_str| is non-null, OnStderr will append to it. |
| 75 | DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str) |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 76 | : stdout_str_(stdout_str), stderr_str_(stderr_str), returnErrors_(false) { |
| 77 | } |
| 78 | DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str, |
| 79 | bool returnErrors) |
| 80 | : stdout_str_(stdout_str), stderr_str_(stderr_str), returnErrors_(returnErrors) { |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 83 | bool OnStdout(const char* buffer, size_t length) { |
| 84 | return OnStream(stdout_str_, stdout, buffer, length, returnErrors_); |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 87 | bool OnStderr(const char* buffer, size_t length) { |
| 88 | return OnStream(stderr_str_, stderr, buffer, length, returnErrors_); |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int Done(int status) { |
| 92 | return status; |
| 93 | } |
| 94 | |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 95 | void ReturnErrors(bool returnErrors) { |
| 96 | returnErrors_ = returnErrors; |
| 97 | } |
| 98 | |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 99 | private: |
| 100 | std::string* stdout_str_; |
| 101 | std::string* stderr_str_; |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 102 | bool returnErrors_; |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 103 | |
| 104 | DISALLOW_COPY_AND_ASSIGN(DefaultStandardStreamsCallback); |
| 105 | }; |
| 106 | |
Josh Gao | 37f11f5 | 2018-07-09 18:00:48 -0700 | [diff] [blame] | 107 | class SilentStandardStreamsCallbackInterface : public StandardStreamsCallbackInterface { |
| 108 | public: |
| 109 | SilentStandardStreamsCallbackInterface() = default; |
Lee Shombert | b796cd5 | 2022-03-22 12:19:06 -0700 | [diff] [blame] | 110 | bool OnStdout(const char*, size_t) override final { return true; } |
| 111 | bool OnStderr(const char*, size_t) override final { return true; } |
Josh Gao | 37f11f5 | 2018-07-09 18:00:48 -0700 | [diff] [blame] | 112 | int Done(int status) override final { return status; } |
| 113 | }; |
| 114 | |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 115 | // Singleton. |
| 116 | extern DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK; |
| 117 | |
Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 118 | int adb_commandline(int argc, const char** argv); |
Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 119 | |
Shaju Mathew | bf7da1c | 2021-10-30 13:47:11 -0700 | [diff] [blame] | 120 | // Helper retrieval function. |
| 121 | const std::optional<FeatureSet>& adb_get_feature_set_or_die(void); |
| 122 | |
Josh Gao | d7f1d0b | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 123 | bool copy_to_file(int inFd, int outFd); |
Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 124 | |
Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 125 | // Connects to the device "shell" service with |command| and prints the |
| 126 | // resulting output. |
Felipe Leme | cc6ebb5 | 2016-07-26 12:14:39 -0700 | [diff] [blame] | 127 | // if |callback| is non-null, stdout/stderr output will be handled by it. |
Josh Gao | b39e415 | 2017-08-16 16:57:01 -0700 | [diff] [blame] | 128 | int send_shell_command( |
Elliott Hughes | 8fa3351 | 2018-06-14 10:46:05 -0700 | [diff] [blame] | 129 | const std::string& command, bool disable_shell_protocol = false, |
| 130 | StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK); |
Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 131 | |
Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 132 | // Reads from |fd| and prints received data. If |use_shell_protocol| is true |
| 133 | // this expects that incoming data will use the shell protocol, in which case |
| 134 | // stdout/stderr are routed independently and the remote exit code will be |
| 135 | // returned. |
| 136 | // if |callback| is non-null, stdout/stderr output will be handled by it. |
| 137 | int read_and_dump(borrowed_fd fd, bool use_shell_protocol = false, |
| 138 | StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK); |
| 139 | |
Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 140 | // Connects to the device "abb" service with |command| and returns the fd. |
| 141 | template <typename ContainerT> |
| 142 | unique_fd send_abb_exec_command(const ContainerT& command_args, std::string* error) { |
| 143 | std::string service_string = "abb_exec:" + android::base::Join(command_args, ABB_ARG_DELIMETER); |
| 144 | |
| 145 | unique_fd fd(adb_connect(service_string, error)); |
| 146 | if (fd < 0) { |
| 147 | fprintf(stderr, "adb: failed to run abb_exec. Error: %s\n", error->c_str()); |
| 148 | return unique_fd{}; |
| 149 | } |
| 150 | return fd; |
| 151 | } |
| 152 | |
Felipe Leme | a9be4f3 | 2016-07-19 17:07:22 -0700 | [diff] [blame] | 153 | #endif // COMMANDLINE_H |