• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HIDL HALs." << std::endl
63           << "   (for AIDL HALs, see `dumpsys`)" << std::endl << std::endl
64           << "commands:" << std::endl;
65 
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     });
86 }
87 
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 
convert(const std::vector<std::string> & v)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 
emitDebugInfo(const std::string & interfaceName,const std::string & instanceName,const std::vector<std::string> & options,ParentDebugInfoLevel parentDebugInfoLevel,std::ostream & out,NullableOStream<std::ostream> err) const101 Status Lshal::emitDebugInfo(
102         const std::string &interfaceName,
103         const std::string &instanceName,
104         const std::vector<std::string> &options,
105         ParentDebugInfoLevel parentDebugInfoLevel,
106         std::ostream &out,
107         NullableOStream<std::ostream> err) const {
108     using android::hidl::base::V1_0::IBase;
109     using android::hardware::details::getDescriptor;
110 
111     hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
112 
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;
128     }
129 
130     if (parentDebugInfoLevel != ParentDebugInfoLevel::FULL) {
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) {
138             if (parentDebugInfoLevel == ParentDebugInfoLevel::FQNAME_ONLY) {
139                 out << "[See " << descriptor << "/" << instanceName << "]";
140             }
141             return OK;
142         }
143     }
144 
145     auto relay = PipeRelay::create(out, err, interfaceName + "/" + instanceName);
146     if (!relay.ok()) {
147         err << "Unable to create PipeRelay: " << relay.error() << std::endl;
148         LOG(ERROR) << "Unable to create PipeRelay: " << relay.error();
149         return IO_ERROR;
150     }
151 
152     deleted_unique_ptr<native_handle_t> fdHandle(
153         native_handle_create(1 /* numFds */, 0 /* numInts */),
154         native_handle_delete);
155 
156     fdHandle->data[0] = relay.value()->fd().get();
157 
158     hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
159 
160     if (!ret.isOk()) {
161         std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
162                 + ret.description();
163         err << msg << std::endl;
164         LOG(ERROR) << msg;
165         return TRANSACTION_ERROR;
166     }
167     return OK;
168 }
169 
parseArgs(const Arg & arg)170 Status Lshal::parseArgs(const Arg &arg) {
171     optind = 1;
172     if (optind >= arg.argc) {
173         // no options at all.
174         return OK;
175     }
176     mCommand = arg.argv[optind];
177     if (selectCommand(mCommand) != nullptr) {
178         ++optind;
179         return OK; // mCommand is set correctly
180     }
181 
182     if (mCommand.size() > 0 && mCommand[0] == '-') {
183         // first argument is an option, set command to "" (which is recognized as "list")
184         mCommand.clear();
185         return OK;
186     }
187 
188     err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
189     return USAGE;
190 }
191 
signalHandler(int sig)192 void signalHandler(int sig) {
193     if (sig == SIGINT) {
194         int retVal;
195         pthread_exit(&retVal);
196     }
197 }
198 
selectCommand(const std::string & command) const199 Command* Lshal::selectCommand(const std::string& command) const {
200     if (command.empty()) {
201         return selectCommand(ListCommand::GetName());
202     }
203     for (const auto& e : mRegisteredCommands) {
204         if (e->getName() == command) {
205             return e.get();
206         }
207     }
208     return nullptr;
209 }
210 
main(const Arg & arg)211 Status Lshal::main(const Arg &arg) {
212     // Allow SIGINT to terminate all threads.
213     signal(SIGINT, signalHandler);
214 
215     Status status = parseArgs(arg);
216     if (status != OK) {
217         usage();
218         return status;
219     }
220     auto c = selectCommand(mCommand);
221     if (c == nullptr) {
222         // unknown command, print global usage
223         usage();
224         return USAGE;
225     }
226     status = c->main(arg);
227     if (status == USAGE) {
228         // bad options. Run `lshal help ${mCommand}` instead.
229         // For example, `lshal --unknown-option` becomes `lshal help` (prints global help)
230         // and `lshal list --unknown-option` becomes `lshal help list`
231         auto&& help = selectCommand(HelpCommand::GetName());
232         return static_cast<HelpCommand*>(help)->usageOfCommand(mCommand);
233     }
234 
235     return status;
236 }
237 
err() const238 NullableOStream<std::ostream> Lshal::err() const {
239     return mErr;
240 }
out() const241 NullableOStream<std::ostream> Lshal::out() const {
242     return mOut;
243 }
244 
serviceManager() const245 const sp<IServiceManager> &Lshal::serviceManager() const {
246     return mServiceManager;
247 }
248 
passthroughManager() const249 const sp<IServiceManager> &Lshal::passthroughManager() const {
250     return mPassthroughManager;
251 }
252 
253 }  // namespace lshal
254 }  // namespace android
255