Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 1 | /* |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 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 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 17 | #define LOG_TAG "lshal" |
| 18 | #include <android-base/logging.h> |
| 19 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 20 | #include "Lshal.h" |
| 21 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 22 | #include <set> |
| 23 | #include <string> |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 24 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 25 | #include <hidl/ServiceManagement.h> |
| 26 | |
| 27 | #include "DebugCommand.h" |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 28 | #include "ListCommand.h" |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 29 | #include "PipeRelay.h" |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace lshal { |
| 33 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 34 | using ::android::hidl::manager::V1_0::IServiceManager; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 35 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 36 | Lshal::Lshal() |
| 37 | : mOut(std::cout), mErr(std::cerr), |
| 38 | mServiceManager(::android::hardware::defaultServiceManager()), |
| 39 | mPassthroughManager(::android::hardware::getPassthroughServiceManager()) { |
| 40 | } |
| 41 | |
| 42 | Lshal::Lshal(std::ostream &out, std::ostream &err, |
| 43 | sp<hidl::manager::V1_0::IServiceManager> serviceManager, |
| 44 | sp<hidl::manager::V1_0::IServiceManager> passthroughManager) |
| 45 | : mOut(out), mErr(err), |
| 46 | mServiceManager(serviceManager), |
| 47 | mPassthroughManager(passthroughManager) { |
| 48 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 51 | void Lshal::usage() { |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 52 | static const std::string helpSummary = |
| 53 | "lshal: List and debug HALs.\n" |
| 54 | "\n" |
| 55 | "commands:\n" |
| 56 | " help Print help message\n" |
| 57 | " list list HALs\n" |
| 58 | " debug debug a specified HAL\n" |
| 59 | "\n" |
| 60 | "If no command is specified, `list` is the default.\n"; |
| 61 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 62 | err() << helpSummary << "\n"; |
| 63 | selectCommand("list")->usage(); |
| 64 | err() << "\n"; |
| 65 | selectCommand("debug")->usage(); |
| 66 | err() << "\n"; |
| 67 | selectCommand("help")->usage(); |
Yifan Hong | b447902 | 2017-03-02 16:54:11 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 70 | // A unique_ptr type using a custom deleter function. |
| 71 | template<typename T> |
| 72 | using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >; |
| 73 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 74 | static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) { |
| 75 | hardware::hidl_vec<hardware::hidl_string> hv; |
| 76 | hv.resize(v.size()); |
| 77 | for (size_t i = 0; i < v.size(); ++i) { |
| 78 | hv[i].setToExternal(v[i].c_str(), v[i].size()); |
| 79 | } |
| 80 | return hv; |
| 81 | } |
| 82 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 83 | Status Lshal::emitDebugInfo( |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 84 | const std::string &interfaceName, |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 85 | const std::string &instanceName, |
| 86 | const std::vector<std::string> &options, |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 87 | std::ostream &out, |
| 88 | NullableOStream<std::ostream> err) const { |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 89 | using android::hidl::base::V1_0::IBase; |
| 90 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 91 | hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 92 | |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 93 | if (!retBase.isOk()) { |
| 94 | std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": " |
| 95 | + retBase.description(); |
| 96 | err << msg << std::endl; |
| 97 | LOG(ERROR) << msg; |
| 98 | return TRANSACTION_ERROR; |
| 99 | } |
| 100 | |
| 101 | sp<IBase> base = retBase; |
| 102 | if (base == nullptr) { |
| 103 | std::string msg = interfaceName + "/" + instanceName + " does not exist, or " |
| 104 | + "no permission to connect."; |
| 105 | err << msg << std::endl; |
| 106 | LOG(ERROR) << msg; |
| 107 | return NO_INTERFACE; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 110 | PipeRelay relay(out); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 111 | |
| 112 | if (relay.initCheck() != OK) { |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 113 | std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck()); |
| 114 | err << msg << std::endl; |
| 115 | LOG(ERROR) << msg; |
| 116 | return IO_ERROR; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | deleted_unique_ptr<native_handle_t> fdHandle( |
| 120 | native_handle_create(1 /* numFds */, 0 /* numInts */), |
| 121 | native_handle_delete); |
| 122 | |
| 123 | fdHandle->data[0] = relay.fd(); |
| 124 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 125 | hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options)); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 126 | |
| 127 | if (!ret.isOk()) { |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 128 | std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": " |
| 129 | + ret.description(); |
| 130 | err << msg << std::endl; |
| 131 | LOG(ERROR) << msg; |
| 132 | return TRANSACTION_ERROR; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 133 | } |
Yifan Hong | 48dc9f8 | 2017-05-09 19:33:08 -0700 | [diff] [blame] | 134 | return OK; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 137 | Status Lshal::parseArgs(const Arg &arg) { |
| 138 | static std::set<std::string> sAllCommands{"list", "debug", "help"}; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 139 | optind = 1; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 140 | if (optind >= arg.argc) { |
| 141 | // no options at all. |
| 142 | return OK; |
| 143 | } |
| 144 | mCommand = arg.argv[optind]; |
| 145 | if (sAllCommands.find(mCommand) != sAllCommands.end()) { |
| 146 | ++optind; |
| 147 | return OK; // mCommand is set correctly |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 148 | } |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 149 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 150 | if (mCommand.size() > 0 && mCommand[0] == '-') { |
| 151 | // first argument is an option, set command to "" (which is recognized as "list") |
| 152 | mCommand = ""; |
| 153 | return OK; |
Yifan Hong | 38d53e0 | 2017-02-13 17:51:59 -0800 | [diff] [blame] | 154 | } |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 155 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 156 | err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 157 | return USAGE; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 160 | void signalHandler(int sig) { |
| 161 | if (sig == SIGINT) { |
| 162 | int retVal; |
| 163 | pthread_exit(&retVal); |
| 164 | } |
| 165 | } |
| 166 | |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 167 | std::unique_ptr<HelpCommand> Lshal::selectHelpCommand() { |
| 168 | return std::make_unique<HelpCommand>(*this); |
| 169 | } |
| 170 | |
Yifan Hong | ded398e | 2017-09-07 13:54:28 -0700 | [diff] [blame] | 171 | std::unique_ptr<Command> Lshal::selectCommand(const std::string& command) { |
| 172 | // Default command is list |
| 173 | if (command == "list" || command == "") { |
| 174 | return std::make_unique<ListCommand>(*this); |
| 175 | } |
| 176 | if (command == "debug") { |
| 177 | return std::make_unique<DebugCommand>(*this); |
| 178 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 179 | if (command == "help") { |
| 180 | return selectHelpCommand(); |
| 181 | } |
Yifan Hong | ded398e | 2017-09-07 13:54:28 -0700 | [diff] [blame] | 182 | return nullptr; |
| 183 | } |
| 184 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 185 | Status Lshal::main(const Arg &arg) { |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 186 | // Allow SIGINT to terminate all threads. |
| 187 | signal(SIGINT, signalHandler); |
| 188 | |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 189 | Status status = parseArgs(arg); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 190 | if (status != OK) { |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 191 | usage(); |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 192 | return status; |
| 193 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 194 | auto c = selectCommand(mCommand); |
| 195 | if (c == nullptr) { |
| 196 | // unknown command, print global usage |
| 197 | usage(); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 198 | return USAGE; |
| 199 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 200 | status = c->main(arg); |
| 201 | if (status == USAGE) { |
| 202 | // bad options. Run `lshal help ${mCommand}` instead. |
| 203 | // For example, `lshal --unknown-option` becomes `lshal help` (prints global help) |
| 204 | // and `lshal list --unknown-option` becomes `lshal help list` |
| 205 | return selectHelpCommand()->usageOfCommand(mCommand); |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 206 | } |
Yifan Hong | a8bedc6 | 2017-09-08 18:00:31 -0700 | [diff] [blame^] | 207 | |
| 208 | return status; |
Yifan Hong | 443df79 | 2017-05-09 18:49:45 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | NullableOStream<std::ostream> Lshal::err() const { |
| 212 | return mErr; |
| 213 | } |
| 214 | NullableOStream<std::ostream> Lshal::out() const { |
| 215 | return mOut; |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Yifan Hong | 9881df9 | 2017-05-10 14:33:05 -0700 | [diff] [blame] | 218 | const sp<IServiceManager> &Lshal::serviceManager() const { |
| 219 | return mServiceManager; |
| 220 | } |
| 221 | |
| 222 | const sp<IServiceManager> &Lshal::passthroughManager() const { |
| 223 | return mPassthroughManager; |
Yifan Hong | a57dffb | 2017-02-21 14:59:00 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Yifan Hong | b0dde93 | 2017-02-10 17:49:58 -0800 | [diff] [blame] | 226 | } // namespace lshal |
| 227 | } // namespace android |