blob: 8c83457d3b271bf385c819d1fbe5149b569a3ac2 [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"
Yifan Hong443df792017-05-09 18:49:45 -070029#include "ListCommand.h"
Andreas Huber28d35912017-03-24 13:14:11 -070030#include "PipeRelay.h"
Yifan Hongb0dde932017-02-10 17:49:58 -080031
32namespace android {
33namespace lshal {
34
Yifan Hong443df792017-05-09 18:49:45 -070035using ::android::hidl::manager::V1_0::IServiceManager;
Yifan Hongb0dde932017-02-10 17:49:58 -080036
Yifan Hong9881df92017-05-10 14:33:05 -070037Lshal::Lshal()
Yifan Hong795b6ec2017-09-13 11:25:28 -070038 : Lshal(std::cout, std::cerr, ::android::hardware::defaultServiceManager(),
39 ::android::hardware::getPassthroughServiceManager()) {
Yifan Hong9881df92017-05-10 14:33:05 -070040}
41
42Lshal::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 Hong795b6ec2017-09-13 11:25:28 -070049 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
54void Lshal::forEachCommand(const std::function<void(const Command* c)>& f) const {
55 for (const auto& e : mRegisteredCommands) f(e.get());
Yifan Hong443df792017-05-09 18:49:45 -070056}
57
Yifan Honga8bedc62017-09-08 18:00:31 -070058void Lshal::usage() {
Yifan Hong795b6ec2017-09-13 11:25:28 -070059 err() << "lshal: List and debug HALs." << std::endl << std::endl
60 << "commands:" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -070061
Yifan Hong795b6ec2017-09-13 11:25:28 -070062 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 Hongb4479022017-03-02 16:54:11 -080082}
83
Andreas Huber28d35912017-03-24 13:14:11 -070084// A unique_ptr type using a custom deleter function.
85template<typename T>
86using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
87
Yifan Hong443df792017-05-09 18:49:45 -070088static 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 Hong48dc9f82017-05-09 19:33:08 -070097Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -070098 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -070099 const std::string &instanceName,
100 const std::vector<std::string> &options,
Steven Moreland5f328892018-01-18 14:38:07 -0800101 bool excludesParentInstances,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700102 std::ostream &out,
103 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700104 using android::hidl::base::V1_0::IBase;
Steven Moreland5f328892018-01-18 14:38:07 -0800105 using android::hardware::details::getDescriptor;
Andreas Huber28d35912017-03-24 13:14:11 -0700106
Yifan Hong9881df92017-05-10 14:33:05 -0700107 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700108
Yifan Hong48dc9f82017-05-09 19:33:08 -0700109 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 Huber28d35912017-03-24 13:14:11 -0700124 }
125
Steven Moreland5f328892018-01-18 14:38:07 -0800126 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 Hong443df792017-05-09 18:49:45 -0700138 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700139
140 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700141 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 Huber28d35912017-03-24 13:14:11 -0700145 }
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 Hong443df792017-05-09 18:49:45 -0700153 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700154
155 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700156 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 Huber28d35912017-03-24 13:14:11 -0700161 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700162 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700163}
164
Yifan Hong443df792017-05-09 18:49:45 -0700165Status Lshal::parseArgs(const Arg &arg) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800166 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700167 if (optind >= arg.argc) {
168 // no options at all.
169 return OK;
170 }
171 mCommand = arg.argv[optind];
Yifan Hong795b6ec2017-09-13 11:25:28 -0700172 if (selectCommand(mCommand) != nullptr) {
Yifan Hong443df792017-05-09 18:49:45 -0700173 ++optind;
174 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800175 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800176
Yifan Hong443df792017-05-09 18:49:45 -0700177 if (mCommand.size() > 0 && mCommand[0] == '-') {
178 // first argument is an option, set command to "" (which is recognized as "list")
Yifan Hong795b6ec2017-09-13 11:25:28 -0700179 mCommand.clear();
Yifan Hong443df792017-05-09 18:49:45 -0700180 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800181 }
Yifan Hong443df792017-05-09 18:49:45 -0700182
Yifan Honga8bedc62017-09-08 18:00:31 -0700183 err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700184 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800185}
186
Yifan Hong9881df92017-05-10 14:33:05 -0700187void signalHandler(int sig) {
188 if (sig == SIGINT) {
189 int retVal;
190 pthread_exit(&retVal);
191 }
192}
193
Yifan Hong795b6ec2017-09-13 11:25:28 -0700194Command* Lshal::selectCommand(const std::string& command) const {
195 if (command.empty()) {
196 return selectCommand(ListCommand::GetName());
Yifan Hongded398e2017-09-07 13:54:28 -0700197 }
Yifan Hong795b6ec2017-09-13 11:25:28 -0700198 for (const auto& e : mRegisteredCommands) {
199 if (e->getName() == command) {
200 return e.get();
201 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700202 }
Yifan Hongded398e2017-09-07 13:54:28 -0700203 return nullptr;
204}
205
Yifan Hong443df792017-05-09 18:49:45 -0700206Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700207 // Allow SIGINT to terminate all threads.
208 signal(SIGINT, signalHandler);
209
Yifan Hong443df792017-05-09 18:49:45 -0700210 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800211 if (status != OK) {
Yifan Honga8bedc62017-09-08 18:00:31 -0700212 usage();
Yifan Hongb0dde932017-02-10 17:49:58 -0800213 return status;
214 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700215 auto c = selectCommand(mCommand);
216 if (c == nullptr) {
217 // unknown command, print global usage
218 usage();
Yifan Hong443df792017-05-09 18:49:45 -0700219 return USAGE;
220 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700221 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 Hong795b6ec2017-09-13 11:25:28 -0700226 auto&& help = selectCommand(HelpCommand::GetName());
227 return static_cast<HelpCommand*>(help)->usageOfCommand(mCommand);
Yifan Hong443df792017-05-09 18:49:45 -0700228 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700229
230 return status;
Yifan Hong443df792017-05-09 18:49:45 -0700231}
232
233NullableOStream<std::ostream> Lshal::err() const {
234 return mErr;
235}
236NullableOStream<std::ostream> Lshal::out() const {
237 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800238}
239
Yifan Hong9881df92017-05-10 14:33:05 -0700240const sp<IServiceManager> &Lshal::serviceManager() const {
241 return mServiceManager;
242}
243
244const sp<IServiceManager> &Lshal::passthroughManager() const {
245 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800246}
247
Yifan Hongb0dde932017-02-10 17:49:58 -0800248} // namespace lshal
249} // namespace android