blob: c6f28ac5021ff33608e93f6bc2b74ab0388dbb1b [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>
26
27#include "DebugCommand.h"
Yifan Hong443df792017-05-09 18:49:45 -070028#include "ListCommand.h"
Andreas Huber28d35912017-03-24 13:14:11 -070029#include "PipeRelay.h"
Yifan Hongb0dde932017-02-10 17:49:58 -080030
31namespace android {
32namespace lshal {
33
Yifan Hong443df792017-05-09 18:49:45 -070034using ::android::hidl::manager::V1_0::IServiceManager;
Yifan Hongb0dde932017-02-10 17:49:58 -080035
Yifan Hong9881df92017-05-10 14:33:05 -070036Lshal::Lshal()
Yifan Hong795b6ec2017-09-13 11:25:28 -070037 : Lshal(std::cout, std::cerr, ::android::hardware::defaultServiceManager(),
38 ::android::hardware::getPassthroughServiceManager()) {
Yifan Hong9881df92017-05-10 14:33:05 -070039}
40
41Lshal::Lshal(std::ostream &out, std::ostream &err,
42 sp<hidl::manager::V1_0::IServiceManager> serviceManager,
43 sp<hidl::manager::V1_0::IServiceManager> passthroughManager)
44 : mOut(out), mErr(err),
45 mServiceManager(serviceManager),
46 mPassthroughManager(passthroughManager) {
47
Yifan Hong795b6ec2017-09-13 11:25:28 -070048 mRegisteredCommands.push_back({std::make_unique<ListCommand>(*this)});
49 mRegisteredCommands.push_back({std::make_unique<DebugCommand>(*this)});
50 mRegisteredCommands.push_back({std::make_unique<HelpCommand>(*this)});
51}
52
53void Lshal::forEachCommand(const std::function<void(const Command* c)>& f) const {
54 for (const auto& e : mRegisteredCommands) f(e.get());
Yifan Hong443df792017-05-09 18:49:45 -070055}
56
Yifan Honga8bedc62017-09-08 18:00:31 -070057void Lshal::usage() {
Yifan Hong795b6ec2017-09-13 11:25:28 -070058 err() << "lshal: List and debug HALs." << std::endl << std::endl
59 << "commands:" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -070060
Yifan Hong795b6ec2017-09-13 11:25:28 -070061 size_t nameMaxLength = 0;
62 forEachCommand([&](const Command* e) {
63 nameMaxLength = std::max(nameMaxLength, e->getName().length());
64 });
65 bool first = true;
66 forEachCommand([&](const Command* e) {
67 if (!first) err() << std::endl;
68 first = false;
69 err() << " " << std::left << std::setw(nameMaxLength + 8) << e->getName()
70 << e->getSimpleDescription();
71 });
72 err() << std::endl << "If no command is specified, `" << ListCommand::GetName()
73 << "` is the default." << std::endl << std::endl;
74
75 first = true;
76 forEachCommand([&](const Command* e) {
77 if (!first) err() << std::endl;
78 first = false;
79 e->usage();
80 });
Yifan Hongb4479022017-03-02 16:54:11 -080081}
82
Andreas Huber28d35912017-03-24 13:14:11 -070083// A unique_ptr type using a custom deleter function.
84template<typename T>
85using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
86
Yifan Hong443df792017-05-09 18:49:45 -070087static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
88 hardware::hidl_vec<hardware::hidl_string> hv;
89 hv.resize(v.size());
90 for (size_t i = 0; i < v.size(); ++i) {
91 hv[i].setToExternal(v[i].c_str(), v[i].size());
92 }
93 return hv;
94}
95
Yifan Hong48dc9f82017-05-09 19:33:08 -070096Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -070097 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -070098 const std::string &instanceName,
99 const std::vector<std::string> &options,
Yifan Hong48dc9f82017-05-09 19:33:08 -0700100 std::ostream &out,
101 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -0700102 using android::hidl::base::V1_0::IBase;
103
Yifan Hong9881df92017-05-10 14:33:05 -0700104 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -0700105
Yifan Hong48dc9f82017-05-09 19:33:08 -0700106 if (!retBase.isOk()) {
107 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
108 + retBase.description();
109 err << msg << std::endl;
110 LOG(ERROR) << msg;
111 return TRANSACTION_ERROR;
112 }
113
114 sp<IBase> base = retBase;
115 if (base == nullptr) {
116 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
117 + "no permission to connect.";
118 err << msg << std::endl;
119 LOG(ERROR) << msg;
120 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700121 }
122
Yifan Hong443df792017-05-09 18:49:45 -0700123 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700124
125 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700126 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
127 err << msg << std::endl;
128 LOG(ERROR) << msg;
129 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700130 }
131
132 deleted_unique_ptr<native_handle_t> fdHandle(
133 native_handle_create(1 /* numFds */, 0 /* numInts */),
134 native_handle_delete);
135
136 fdHandle->data[0] = relay.fd();
137
Yifan Hong443df792017-05-09 18:49:45 -0700138 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700139
140 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700141 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
142 + ret.description();
143 err << msg << std::endl;
144 LOG(ERROR) << msg;
145 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700146 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700147 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700148}
149
Yifan Hong443df792017-05-09 18:49:45 -0700150Status Lshal::parseArgs(const Arg &arg) {
Yifan Hongb0dde932017-02-10 17:49:58 -0800151 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700152 if (optind >= arg.argc) {
153 // no options at all.
154 return OK;
155 }
156 mCommand = arg.argv[optind];
Yifan Hong795b6ec2017-09-13 11:25:28 -0700157 if (selectCommand(mCommand) != nullptr) {
Yifan Hong443df792017-05-09 18:49:45 -0700158 ++optind;
159 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800160 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800161
Yifan Hong443df792017-05-09 18:49:45 -0700162 if (mCommand.size() > 0 && mCommand[0] == '-') {
163 // first argument is an option, set command to "" (which is recognized as "list")
Yifan Hong795b6ec2017-09-13 11:25:28 -0700164 mCommand.clear();
Yifan Hong443df792017-05-09 18:49:45 -0700165 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800166 }
Yifan Hong443df792017-05-09 18:49:45 -0700167
Yifan Honga8bedc62017-09-08 18:00:31 -0700168 err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700169 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800170}
171
Yifan Hong9881df92017-05-10 14:33:05 -0700172void signalHandler(int sig) {
173 if (sig == SIGINT) {
174 int retVal;
175 pthread_exit(&retVal);
176 }
177}
178
Yifan Hong795b6ec2017-09-13 11:25:28 -0700179Command* Lshal::selectCommand(const std::string& command) const {
180 if (command.empty()) {
181 return selectCommand(ListCommand::GetName());
Yifan Hongded398e2017-09-07 13:54:28 -0700182 }
Yifan Hong795b6ec2017-09-13 11:25:28 -0700183 for (const auto& e : mRegisteredCommands) {
184 if (e->getName() == command) {
185 return e.get();
186 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700187 }
Yifan Hongded398e2017-09-07 13:54:28 -0700188 return nullptr;
189}
190
Yifan Hong443df792017-05-09 18:49:45 -0700191Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700192 // Allow SIGINT to terminate all threads.
193 signal(SIGINT, signalHandler);
194
Yifan Hong443df792017-05-09 18:49:45 -0700195 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800196 if (status != OK) {
Yifan Honga8bedc62017-09-08 18:00:31 -0700197 usage();
Yifan Hongb0dde932017-02-10 17:49:58 -0800198 return status;
199 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700200 auto c = selectCommand(mCommand);
201 if (c == nullptr) {
202 // unknown command, print global usage
203 usage();
Yifan Hong443df792017-05-09 18:49:45 -0700204 return USAGE;
205 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700206 status = c->main(arg);
207 if (status == USAGE) {
208 // bad options. Run `lshal help ${mCommand}` instead.
209 // For example, `lshal --unknown-option` becomes `lshal help` (prints global help)
210 // and `lshal list --unknown-option` becomes `lshal help list`
Yifan Hong795b6ec2017-09-13 11:25:28 -0700211 auto&& help = selectCommand(HelpCommand::GetName());
212 return static_cast<HelpCommand*>(help)->usageOfCommand(mCommand);
Yifan Hong443df792017-05-09 18:49:45 -0700213 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700214
215 return status;
Yifan Hong443df792017-05-09 18:49:45 -0700216}
217
218NullableOStream<std::ostream> Lshal::err() const {
219 return mErr;
220}
221NullableOStream<std::ostream> Lshal::out() const {
222 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800223}
224
Yifan Hong9881df92017-05-10 14:33:05 -0700225const sp<IServiceManager> &Lshal::serviceManager() const {
226 return mServiceManager;
227}
228
229const sp<IServiceManager> &Lshal::passthroughManager() const {
230 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800231}
232
Yifan Hongb0dde932017-02-10 17:49:58 -0800233} // namespace lshal
234} // namespace android