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