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