blob: a08a02ce1a9fbfcbfddc298a903c1039f005a425 [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()
37 : mOut(std::cout), mErr(std::cerr),
38 mServiceManager(::android::hardware::defaultServiceManager()),
39 mPassthroughManager(::android::hardware::getPassthroughServiceManager()) {
40}
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 Hong443df792017-05-09 18:49:45 -070049}
50
Yifan Honga8bedc62017-09-08 18:00:31 -070051void Lshal::usage() {
Yifan Hong443df792017-05-09 18:49:45 -070052 static const std::string helpSummary =
53 "lshal: List and debug HALs.\n"
54 "\n"
55 "commands:\n"
56 " help Print help message\n"
57 " list list HALs\n"
58 " debug debug a specified HAL\n"
59 "\n"
60 "If no command is specified, `list` is the default.\n";
61
Yifan Honga8bedc62017-09-08 18:00:31 -070062 err() << helpSummary << "\n";
63 selectCommand("list")->usage();
64 err() << "\n";
65 selectCommand("debug")->usage();
66 err() << "\n";
67 selectCommand("help")->usage();
Yifan Hongb4479022017-03-02 16:54:11 -080068}
69
Andreas Huber28d35912017-03-24 13:14:11 -070070// A unique_ptr type using a custom deleter function.
71template<typename T>
72using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
73
Yifan Hong443df792017-05-09 18:49:45 -070074static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
75 hardware::hidl_vec<hardware::hidl_string> hv;
76 hv.resize(v.size());
77 for (size_t i = 0; i < v.size(); ++i) {
78 hv[i].setToExternal(v[i].c_str(), v[i].size());
79 }
80 return hv;
81}
82
Yifan Hong48dc9f82017-05-09 19:33:08 -070083Status Lshal::emitDebugInfo(
Andreas Huber28d35912017-03-24 13:14:11 -070084 const std::string &interfaceName,
Yifan Hong443df792017-05-09 18:49:45 -070085 const std::string &instanceName,
86 const std::vector<std::string> &options,
Yifan Hong48dc9f82017-05-09 19:33:08 -070087 std::ostream &out,
88 NullableOStream<std::ostream> err) const {
Andreas Huber28d35912017-03-24 13:14:11 -070089 using android::hidl::base::V1_0::IBase;
90
Yifan Hong9881df92017-05-10 14:33:05 -070091 hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
Andreas Huber28d35912017-03-24 13:14:11 -070092
Yifan Hong48dc9f82017-05-09 19:33:08 -070093 if (!retBase.isOk()) {
94 std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
95 + retBase.description();
96 err << msg << std::endl;
97 LOG(ERROR) << msg;
98 return TRANSACTION_ERROR;
99 }
100
101 sp<IBase> base = retBase;
102 if (base == nullptr) {
103 std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
104 + "no permission to connect.";
105 err << msg << std::endl;
106 LOG(ERROR) << msg;
107 return NO_INTERFACE;
Andreas Huber28d35912017-03-24 13:14:11 -0700108 }
109
Yifan Hong443df792017-05-09 18:49:45 -0700110 PipeRelay relay(out);
Andreas Huber28d35912017-03-24 13:14:11 -0700111
112 if (relay.initCheck() != OK) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700113 std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
114 err << msg << std::endl;
115 LOG(ERROR) << msg;
116 return IO_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700117 }
118
119 deleted_unique_ptr<native_handle_t> fdHandle(
120 native_handle_create(1 /* numFds */, 0 /* numInts */),
121 native_handle_delete);
122
123 fdHandle->data[0] = relay.fd();
124
Yifan Hong443df792017-05-09 18:49:45 -0700125 hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
Andreas Huber28d35912017-03-24 13:14:11 -0700126
127 if (!ret.isOk()) {
Yifan Hong48dc9f82017-05-09 19:33:08 -0700128 std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
129 + ret.description();
130 err << msg << std::endl;
131 LOG(ERROR) << msg;
132 return TRANSACTION_ERROR;
Andreas Huber28d35912017-03-24 13:14:11 -0700133 }
Yifan Hong48dc9f82017-05-09 19:33:08 -0700134 return OK;
Andreas Huber28d35912017-03-24 13:14:11 -0700135}
136
Yifan Hong443df792017-05-09 18:49:45 -0700137Status Lshal::parseArgs(const Arg &arg) {
138 static std::set<std::string> sAllCommands{"list", "debug", "help"};
Yifan Hongb0dde932017-02-10 17:49:58 -0800139 optind = 1;
Yifan Hong443df792017-05-09 18:49:45 -0700140 if (optind >= arg.argc) {
141 // no options at all.
142 return OK;
143 }
144 mCommand = arg.argv[optind];
145 if (sAllCommands.find(mCommand) != sAllCommands.end()) {
146 ++optind;
147 return OK; // mCommand is set correctly
Yifan Hongb0dde932017-02-10 17:49:58 -0800148 }
Yifan Hong38d53e02017-02-13 17:51:59 -0800149
Yifan Hong443df792017-05-09 18:49:45 -0700150 if (mCommand.size() > 0 && mCommand[0] == '-') {
151 // first argument is an option, set command to "" (which is recognized as "list")
152 mCommand = "";
153 return OK;
Yifan Hong38d53e02017-02-13 17:51:59 -0800154 }
Yifan Hong443df792017-05-09 18:49:45 -0700155
Yifan Honga8bedc62017-09-08 18:00:31 -0700156 err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
Yifan Hong443df792017-05-09 18:49:45 -0700157 return USAGE;
Yifan Hongb0dde932017-02-10 17:49:58 -0800158}
159
Yifan Hong9881df92017-05-10 14:33:05 -0700160void signalHandler(int sig) {
161 if (sig == SIGINT) {
162 int retVal;
163 pthread_exit(&retVal);
164 }
165}
166
Yifan Honga8bedc62017-09-08 18:00:31 -0700167std::unique_ptr<HelpCommand> Lshal::selectHelpCommand() {
168 return std::make_unique<HelpCommand>(*this);
169}
170
Yifan Hongded398e2017-09-07 13:54:28 -0700171std::unique_ptr<Command> Lshal::selectCommand(const std::string& command) {
172 // Default command is list
173 if (command == "list" || command == "") {
174 return std::make_unique<ListCommand>(*this);
175 }
176 if (command == "debug") {
177 return std::make_unique<DebugCommand>(*this);
178 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700179 if (command == "help") {
180 return selectHelpCommand();
181 }
Yifan Hongded398e2017-09-07 13:54:28 -0700182 return nullptr;
183}
184
Yifan Hong443df792017-05-09 18:49:45 -0700185Status Lshal::main(const Arg &arg) {
Yifan Hong9881df92017-05-10 14:33:05 -0700186 // Allow SIGINT to terminate all threads.
187 signal(SIGINT, signalHandler);
188
Yifan Hong443df792017-05-09 18:49:45 -0700189 Status status = parseArgs(arg);
Yifan Hongb0dde932017-02-10 17:49:58 -0800190 if (status != OK) {
Yifan Honga8bedc62017-09-08 18:00:31 -0700191 usage();
Yifan Hongb0dde932017-02-10 17:49:58 -0800192 return status;
193 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700194 auto c = selectCommand(mCommand);
195 if (c == nullptr) {
196 // unknown command, print global usage
197 usage();
Yifan Hong443df792017-05-09 18:49:45 -0700198 return USAGE;
199 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700200 status = c->main(arg);
201 if (status == USAGE) {
202 // bad options. Run `lshal help ${mCommand}` instead.
203 // For example, `lshal --unknown-option` becomes `lshal help` (prints global help)
204 // and `lshal list --unknown-option` becomes `lshal help list`
205 return selectHelpCommand()->usageOfCommand(mCommand);
Yifan Hong443df792017-05-09 18:49:45 -0700206 }
Yifan Honga8bedc62017-09-08 18:00:31 -0700207
208 return status;
Yifan Hong443df792017-05-09 18:49:45 -0700209}
210
211NullableOStream<std::ostream> Lshal::err() const {
212 return mErr;
213}
214NullableOStream<std::ostream> Lshal::out() const {
215 return mOut;
Yifan Hongb0dde932017-02-10 17:49:58 -0800216}
217
Yifan Hong9881df92017-05-10 14:33:05 -0700218const sp<IServiceManager> &Lshal::serviceManager() const {
219 return mServiceManager;
220}
221
222const sp<IServiceManager> &Lshal::passthroughManager() const {
223 return mPassthroughManager;
Yifan Honga57dffb2017-02-21 14:59:00 -0800224}
225
Yifan Hongb0dde932017-02-10 17:49:58 -0800226} // namespace lshal
227} // namespace android