1 /*
2 * Copyright (C) 2017 The Android Open Source Project
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
17 #define LOG_TAG "lshal"
18 #include <android-base/logging.h>
19
20 #include "Lshal.h"
21
22 #include <set>
23 #include <string>
24
25 #include <hidl/ServiceManagement.h>
26 #include <hidl/HidlTransportUtils.h>
27
28 #include "DebugCommand.h"
29 #include "HelpCommand.h"
30 #include "ListCommand.h"
31 #include "WaitCommand.h"
32 #include "PipeRelay.h"
33
34 namespace android {
35 namespace lshal {
36
37 using ::android::hidl::manager::V1_0::IServiceManager;
38
Lshal()39 Lshal::Lshal()
40 : Lshal(std::cout, std::cerr, ::android::hardware::defaultServiceManager(),
41 ::android::hardware::getPassthroughServiceManager()) {
42 }
43
Lshal(std::ostream & out,std::ostream & err,sp<hidl::manager::V1_0::IServiceManager> serviceManager,sp<hidl::manager::V1_0::IServiceManager> passthroughManager)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
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)});
54 mRegisteredCommands.push_back({std::make_unique<WaitCommand>(*this)});
55 }
56
forEachCommand(const std::function<void (const Command * c)> & f) const57 void Lshal::forEachCommand(const std::function<void(const Command* c)>& f) const {
58 for (const auto& e : mRegisteredCommands) f(e.get());
59 }
60
usage()61 void Lshal::usage() {
62 err() << "lshal: List and debug HALs." << std::endl << std::endl
63 << "commands:" << std::endl;
64
65 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 });
85 }
86
87 // A unique_ptr type using a custom deleter function.
88 template<typename T>
89 using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
90
convert(const std::vector<std::string> & v)91 static 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
emitDebugInfo(const std::string & interfaceName,const std::string & instanceName,const std::vector<std::string> & options,bool excludesParentInstances,std::ostream & out,NullableOStream<std::ostream> err) const100 Status Lshal::emitDebugInfo(
101 const std::string &interfaceName,
102 const std::string &instanceName,
103 const std::vector<std::string> &options,
104 bool excludesParentInstances,
105 std::ostream &out,
106 NullableOStream<std::ostream> err) const {
107 using android::hidl::base::V1_0::IBase;
108 using android::hardware::details::getDescriptor;
109
110 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
111
112 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;
127 }
128
129 if (excludesParentInstances) {
130 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) {
137 return OK;
138 }
139 }
140
141 PipeRelay relay(out);
142
143 if (relay.initCheck() != OK) {
144 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
145 err << msg << std::endl;
146 LOG(ERROR) << msg;
147 return IO_ERROR;
148 }
149
150 deleted_unique_ptr<native_handle_t> fdHandle(
151 native_handle_create(1 /* numFds */, 0 /* numInts */),
152 native_handle_delete);
153
154 fdHandle->data[0] = relay.fd();
155
156 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
157
158 if (!ret.isOk()) {
159 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
160 + ret.description();
161 err << msg << std::endl;
162 LOG(ERROR) << msg;
163 return TRANSACTION_ERROR;
164 }
165 return OK;
166 }
167
parseArgs(const Arg & arg)168 Status Lshal::parseArgs(const Arg &arg) {
169 optind = 1;
170 if (optind >= arg.argc) {
171 // no options at all.
172 return OK;
173 }
174 mCommand = arg.argv[optind];
175 if (selectCommand(mCommand) != nullptr) {
176 ++optind;
177 return OK; // mCommand is set correctly
178 }
179
180 if (mCommand.size() > 0 && mCommand[0] == '-') {
181 // first argument is an option, set command to "" (which is recognized as "list")
182 mCommand.clear();
183 return OK;
184 }
185
186 err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
187 return USAGE;
188 }
189
signalHandler(int sig)190 void signalHandler(int sig) {
191 if (sig == SIGINT) {
192 int retVal;
193 pthread_exit(&retVal);
194 }
195 }
196
selectCommand(const std::string & command) const197 Command* Lshal::selectCommand(const std::string& command) const {
198 if (command.empty()) {
199 return selectCommand(ListCommand::GetName());
200 }
201 for (const auto& e : mRegisteredCommands) {
202 if (e->getName() == command) {
203 return e.get();
204 }
205 }
206 return nullptr;
207 }
208
main(const Arg & arg)209 Status Lshal::main(const Arg &arg) {
210 // Allow SIGINT to terminate all threads.
211 signal(SIGINT, signalHandler);
212
213 Status status = parseArgs(arg);
214 if (status != OK) {
215 usage();
216 return status;
217 }
218 auto c = selectCommand(mCommand);
219 if (c == nullptr) {
220 // unknown command, print global usage
221 usage();
222 return USAGE;
223 }
224 status = c->main(arg);
225 if (status == USAGE) {
226 // bad options. Run `lshal help ${mCommand}` instead.
227 // For example, `lshal --unknown-option` becomes `lshal help` (prints global help)
228 // and `lshal list --unknown-option` becomes `lshal help list`
229 auto&& help = selectCommand(HelpCommand::GetName());
230 return static_cast<HelpCommand*>(help)->usageOfCommand(mCommand);
231 }
232
233 return status;
234 }
235
err() const236 NullableOStream<std::ostream> Lshal::err() const {
237 return mErr;
238 }
out() const239 NullableOStream<std::ostream> Lshal::out() const {
240 return mOut;
241 }
242
serviceManager() const243 const sp<IServiceManager> &Lshal::serviceManager() const {
244 return mServiceManager;
245 }
246
passthroughManager() const247 const sp<IServiceManager> &Lshal::passthroughManager() const {
248 return mPassthroughManager;
249 }
250
251 } // namespace lshal
252 } // namespace android
253