blob: 2c3efe524e138fff89d1edb4f9de8c0fa1263a12 [file] [log] [blame]
Yifan Hongb0dde932017-02-10 17:49:58 -08001/*
Yifan Hong443df792017-05-09 18:49:45 -07002 * Copyright (C) 2017 The Android Open Source Project
Yifan Hongb0dde932017-02-10 17:49:58 -08003 *
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 Hong48dc9f82017-05-09 19:33:08 -070017#define LOG_TAG "lshal"
18#include <android-base/logging.h>
19
Yifan Hongb0dde932017-02-10 17:49:58 -080020#include "Lshal.h"
21
Yifan Hong443df792017-05-09 18:49:45 -070022#include <set>
23#include <string>
Yifan Hongb0dde932017-02-10 17:49:58 -080024
Yifan Hong48dc9f82017-05-09 19:33:08 -070025#include <hidl/ServiceManagement.h>
Steven Moreland5f328892018-01-18 14:38:07 -080026#include <hidl/HidlTransportUtils.h>
Yifan Hong48dc9f82017-05-09 19:33:08 -070027
28#include "DebugCommand.h"
Steven Moreland552e4072019-06-25 09:33:32 -070029#include "HelpCommand.h"
Yifan Hong443df792017-05-09 18:49:45 -070030#include "ListCommand.h"
Steven Moreland552e4072019-06-25 09:33:32 -070031#include "WaitCommand.h"
Andreas Huber28d35912017-03-24 13:14:11 -070032#include "PipeRelay.h"
Yifan Hongb0dde932017-02-10 17:49:58 -080033
34namespace android {
35namespace lshal {
36
Yifan Hong443df792017-05-09 18:49:45 -070037using ::android::hidl::manager::V1_0::IServiceManager;
Yifan Hongb0dde932017-02-10 17:49:58 -080038
Yifan Hong9881df92017-05-10 14:33:05 -070039Lshal::Lshal()
Yifan Hong795b6ec2017-09-13 11:25:28 -070040 : Lshal(std::cout, std::cerr, ::android::hardware::defaultServiceManager(),
41 ::android::hardware::getPassthroughServiceManager()) {
Yifan Hong9881df92017-05-10 14:33:05 -070042}
43
44Lshal::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 Hong795b6ec2017-09-13 11:25:28 -070051 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 Moreland552e4072019-06-25 09:33:32 -070054 mRegisteredCommands.push_back({std::make_unique<WaitCommand>(*this)});
Yifan Hong795b6ec2017-09-13 11:25:28 -070055}
56
57void Lshal::forEachCommand(const std::function<void(const Command* c)>& f) const {
58 for (const auto& e : mRegisteredCommands) f(e.get());
Yifan Hong443df792017-05-09 18:49:45 -070059}
60
Yifan Honga8bedc62017-09-08 18:00:31 -070061void Lshal::usage() {
Yifan Hong795b6ec2017-09-13 11:25:28 -070062 err() << "lshal: List and debug HALs." << std::endl << std::endl
63 << "commands:" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -070064
Yifan Hong795b6ec2017-09-13 11:25:28 -070065 size_t nameMaxLength = 0;
66 forEachCommand([&](const Command* e) {
67 nameMaxLength = std::max(nameMaxLength, e->getName().length());
68 });
69 bool first = true;
70 forEachCommand([&](const Command* e) {
71 if (!first) err() << std::endl;
72 first = false;
73 err() << " " << std::left << std::setw(nameMaxLength + 8) << e->getName()
74 << e->getSimpleDescription();
75 });
76 err() << std::endl << "If no command is specified, `" << ListCommand::GetName()
77 << "` is the default." << std::endl << std::endl;
78
79 first = true;
80 forEachCommand([&](const Command* e) {
81 if (!first) err() << std::endl;
82 first = false;
83 e->usage();
84 });
Yifan Hongb4479022017-03-02 16:54:11 -080085}
86
Andreas Huber28d35912017-03-24 13:14:11 -070087// A unique_ptr type using a custom deleter function.
88template<typename T>
89using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
90
Yifan Hong443df792017-05-09 18:49:45 -070091static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
92 hardware::hidl_vec<hardware::hidl_string> hv;
93 hv.resize(v.size());
94 for (size_t i = 0; i < v.size(); ++i) {
95 hv[i].setToExternal(v[i].c_str(), v[i].size());
96 }
97 return hv;
98}
99
Yifan Hong48dc9f82017-05-09 19:33:08 -0700100Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -0700101 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -0700102 const std::string &instanceName,
103 const std::vector<std::string> &options,
Yifan Hong6884b872020-07-09 16:38:18 -0700104 ParentDebugInfoLevel parentDebugInfoLevel,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700105 std::ostream &out,
106 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700107 using android::hidl::base::V1_0::IBase;
Steven Moreland5f328892018-01-18 14:38:07 -0800108 using android::hardware::details::getDescriptor;
Andreas Huber28d35912017-03-24 13:14:11 -0700109
Yifan Hong9881df92017-05-10 14:33:05 -0700110 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700111
Yifan Hong48dc9f82017-05-09 19:33:08 -0700112 if (!retBase.isOk()) {
113 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
114 + retBase.description();
115 err << msg << std::endl;
116 LOG(ERROR) << msg;
117 return TRANSACTION_ERROR;
118 }
119
120 sp<IBase> base = retBase;
121 if (base == nullptr) {
122 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
123 + "no permission to connect.";
124 err << msg << std::endl;
125 LOG(ERROR) << msg;
126 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700127 }
128
Yifan Hong6884b872020-07-09 16:38:18 -0700129 if (parentDebugInfoLevel != ParentDebugInfoLevel::FULL) {
Steven Moreland5f328892018-01-18 14:38:07 -0800130 const std::string descriptor = getDescriptor(base.get());
131 if (descriptor.empty()) {
132 std::string msg = interfaceName + "/" + instanceName + " getDescriptor failed";
133 err << msg << std::endl;
134 LOG(ERROR) << msg;
135 }
136 if (descriptor != interfaceName) {
Yifan Hong6884b872020-07-09 16:38:18 -0700137 if (parentDebugInfoLevel == ParentDebugInfoLevel::FQNAME_ONLY) {
138 out << "[See " << descriptor << "/" << instanceName << "]";
139 }
Steven Moreland5f328892018-01-18 14:38:07 -0800140 return OK;
141 }
142 }
143
Yifan Hong443df792017-05-09 18:49:45 -0700144 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700145
146 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700147 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
148 err << msg << std::endl;
149 LOG(ERROR) << msg;
150 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700151 }
152
153 deleted_unique_ptr<native_handle_t> fdHandle(
154 native_handle_create(1 /* numFds */, 0 /* numInts */),
155 native_handle_delete);
156
157 fdHandle->data[0] = relay.fd();
158
Yifan Hong443df792017-05-09 18:49:45 -0700159 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700160
161 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700162 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
163 + ret.description();
164 err << msg << std::endl;
165 LOG(ERROR) << msg;
166 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700167 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700168 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700169}
170
Yifan Hong443df792017-05-09 18:49:45 -0700171Status Lshal::parseArgs(const Arg &arg) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800172 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700173 if (optind >= arg.argc) {
174 // no options at all.
175 return OK;
176 }
177 mCommand = arg.argv[optind];
Yifan Hong795b6ec2017-09-13 11:25:28 -0700178 if (selectCommand(mCommand) != nullptr) {
Yifan Hong443df792017-05-09 18:49:45 -0700179 ++optind;
180 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800181 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800182
Yifan Hong443df792017-05-09 18:49:45 -0700183 if (mCommand.size() > 0 && mCommand[0] == '-') {
184 // first argument is an option, set command to "" (which is recognized as "list")
Yifan Hong795b6ec2017-09-13 11:25:28 -0700185 mCommand.clear();
Yifan Hong443df792017-05-09 18:49:45 -0700186 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800187 }
Yifan Hong443df792017-05-09 18:49:45 -0700188
Yifan Honga8bedc62017-09-08 18:00:31 -0700189 err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700190 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800191}
192
Yifan Hong9881df92017-05-10 14:33:05 -0700193void signalHandler(int sig) {
194 if (sig == SIGINT) {
195 int retVal;
196 pthread_exit(&retVal);
197 }
198}
199
Yifan Hong795b6ec2017-09-13 11:25:28 -0700200Command* Lshal::selectCommand(const std::string& command) const {
201 if (command.empty()) {
202 return selectCommand(ListCommand::GetName());
Yifan Hongded398e2017-09-07 13:54:28 -0700203 }
Yifan Hong795b6ec2017-09-13 11:25:28 -0700204 for (const auto& e : mRegisteredCommands) {
205 if (e->getName() == command) {
206 return e.get();
207 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700208 }
Yifan Hongded398e2017-09-07 13:54:28 -0700209 return nullptr;
210}
211
Yifan Hong443df792017-05-09 18:49:45 -0700212Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700213 // Allow SIGINT to terminate all threads.
214 signal(SIGINT, signalHandler);
215
Yifan Hong443df792017-05-09 18:49:45 -0700216 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800217 if (status != OK) {
Yifan Honga8bedc62017-09-08 18:00:31 -0700218 usage();
Yifan Hongb0dde932017-02-10 17:49:58 -0800219 return status;
220 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700221 auto c = selectCommand(mCommand);
222 if (c == nullptr) {
223 // unknown command, print global usage
224 usage();
Yifan Hong443df792017-05-09 18:49:45 -0700225 return USAGE;
226 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700227 status = c->main(arg);
228 if (status == USAGE) {
229 // bad options. Run `lshal help ${mCommand}` instead.
230 // For example, `lshal --unknown-option` becomes `lshal help` (prints global help)
231 // and `lshal list --unknown-option` becomes `lshal help list`
Yifan Hong795b6ec2017-09-13 11:25:28 -0700232 auto&& help = selectCommand(HelpCommand::GetName());
233 return static_cast<HelpCommand*>(help)->usageOfCommand(mCommand);
Yifan Hong443df792017-05-09 18:49:45 -0700234 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700235
236 return status;
Yifan Hong443df792017-05-09 18:49:45 -0700237}
238
239NullableOStream<std::ostream> Lshal::err() const {
240 return mErr;
241}
242NullableOStream<std::ostream> Lshal::out() const {
243 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800244}
245
Yifan Hong9881df92017-05-10 14:33:05 -0700246const sp<IServiceManager> &Lshal::serviceManager() const {
247 return mServiceManager;
248}
249
250const sp<IServiceManager> &Lshal::passthroughManager() const {
251 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800252}
253
Yifan Hongb0dde932017-02-10 17:49:58 -0800254} // namespace lshal
255} // namespace android