Lshal.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #define LOG_TAG "lshal"
  17. #include <android-base/logging.h>
  18. #include "Lshal.h"
  19. #include <set>
  20. #include <string>
  21. #include <hidl/ServiceManagement.h>
  22. #include <hidl/HidlTransportUtils.h>
  23. #include "DebugCommand.h"
  24. #include "ListCommand.h"
  25. #include "PipeRelay.h"
  26. namespace android {
  27. namespace lshal {
  28. using ::android::hidl::manager::V1_0::IServiceManager;
  29. Lshal::Lshal()
  30. : Lshal(std::cout, std::cerr, ::android::hardware::defaultServiceManager(),
  31. ::android::hardware::getPassthroughServiceManager()) {
  32. }
  33. Lshal::Lshal(std::ostream &out, std::ostream &err,
  34. sp<hidl::manager::V1_0::IServiceManager> serviceManager,
  35. sp<hidl::manager::V1_0::IServiceManager> passthroughManager)
  36. : mOut(out), mErr(err),
  37. mServiceManager(serviceManager),
  38. mPassthroughManager(passthroughManager) {
  39. mRegisteredCommands.push_back({std::make_unique<ListCommand>(*this)});
  40. mRegisteredCommands.push_back({std::make_unique<DebugCommand>(*this)});
  41. mRegisteredCommands.push_back({std::make_unique<HelpCommand>(*this)});
  42. }
  43. void Lshal::forEachCommand(const std::function<void(const Command* c)>& f) const {
  44. for (const auto& e : mRegisteredCommands) f(e.get());
  45. }
  46. void Lshal::usage() {
  47. err() << "lshal: List and debug HALs." << std::endl << std::endl
  48. << "commands:" << std::endl;
  49. size_t nameMaxLength = 0;
  50. forEachCommand([&](const Command* e) {
  51. nameMaxLength = std::max(nameMaxLength, e->getName().length());
  52. });
  53. bool first = true;
  54. forEachCommand([&](const Command* e) {
  55. if (!first) err() << std::endl;
  56. first = false;
  57. err() << " " << std::left << std::setw(nameMaxLength + 8) << e->getName()
  58. << e->getSimpleDescription();
  59. });
  60. err() << std::endl << "If no command is specified, `" << ListCommand::GetName()
  61. << "` is the default." << std::endl << std::endl;
  62. first = true;
  63. forEachCommand([&](const Command* e) {
  64. if (!first) err() << std::endl;
  65. first = false;
  66. e->usage();
  67. });
  68. }
  69. // A unique_ptr type using a custom deleter function.
  70. template<typename T>
  71. using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >;
  72. static hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) {
  73. hardware::hidl_vec<hardware::hidl_string> hv;
  74. hv.resize(v.size());
  75. for (size_t i = 0; i < v.size(); ++i) {
  76. hv[i].setToExternal(v[i].c_str(), v[i].size());
  77. }
  78. return hv;
  79. }
  80. Status Lshal::emitDebugInfo(
  81. const std::string &interfaceName,
  82. const std::string &instanceName,
  83. const std::vector<std::string> &options,
  84. bool excludesParentInstances,
  85. std::ostream &out,
  86. NullableOStream<std::ostream> err) const {
  87. using android::hidl::base::V1_0::IBase;
  88. using android::hardware::details::getDescriptor;
  89. hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName);
  90. if (!retBase.isOk()) {
  91. std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": "
  92. + retBase.description();
  93. err << msg << std::endl;
  94. LOG(ERROR) << msg;
  95. return TRANSACTION_ERROR;
  96. }
  97. sp<IBase> base = retBase;
  98. if (base == nullptr) {
  99. std::string msg = interfaceName + "/" + instanceName + " does not exist, or "
  100. + "no permission to connect.";
  101. err << msg << std::endl;
  102. LOG(ERROR) << msg;
  103. return NO_INTERFACE;
  104. }
  105. if (excludesParentInstances) {
  106. const std::string descriptor = getDescriptor(base.get());
  107. if (descriptor.empty()) {
  108. std::string msg = interfaceName + "/" + instanceName + " getDescriptor failed";
  109. err << msg << std::endl;
  110. LOG(ERROR) << msg;
  111. }
  112. if (descriptor != interfaceName) {
  113. return OK;
  114. }
  115. }
  116. PipeRelay relay(out);
  117. if (relay.initCheck() != OK) {
  118. std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck());
  119. err << msg << std::endl;
  120. LOG(ERROR) << msg;
  121. return IO_ERROR;
  122. }
  123. deleted_unique_ptr<native_handle_t> fdHandle(
  124. native_handle_create(1 /* numFds */, 0 /* numInts */),
  125. native_handle_delete);
  126. fdHandle->data[0] = relay.fd();
  127. hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options));
  128. if (!ret.isOk()) {
  129. std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": "
  130. + ret.description();
  131. err << msg << std::endl;
  132. LOG(ERROR) << msg;
  133. return TRANSACTION_ERROR;
  134. }
  135. return OK;
  136. }
  137. Status Lshal::parseArgs(const Arg &arg) {
  138. optind = 1;
  139. if (optind >= arg.argc) {
  140. // no options at all.
  141. return OK;
  142. }
  143. mCommand = arg.argv[optind];
  144. if (selectCommand(mCommand) != nullptr) {
  145. ++optind;
  146. return OK; // mCommand is set correctly
  147. }
  148. if (mCommand.size() > 0 && mCommand[0] == '-') {
  149. // first argument is an option, set command to "" (which is recognized as "list")
  150. mCommand.clear();
  151. return OK;
  152. }
  153. err() << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "'" << std::endl;
  154. return USAGE;
  155. }
  156. void signalHandler(int sig) {
  157. if (sig == SIGINT) {
  158. int retVal;
  159. pthread_exit(&retVal);
  160. }
  161. }
  162. Command* Lshal::selectCommand(const std::string& command) const {
  163. if (command.empty()) {
  164. return selectCommand(ListCommand::GetName());
  165. }
  166. for (const auto& e : mRegisteredCommands) {
  167. if (e->getName() == command) {
  168. return e.get();
  169. }
  170. }
  171. return nullptr;
  172. }
  173. Status Lshal::main(const Arg &arg) {
  174. // Allow SIGINT to terminate all threads.
  175. signal(SIGINT, signalHandler);
  176. Status status = parseArgs(arg);
  177. if (status != OK) {
  178. usage();
  179. return status;
  180. }
  181. auto c = selectCommand(mCommand);
  182. if (c == nullptr) {
  183. // unknown command, print global usage
  184. usage();
  185. return USAGE;
  186. }
  187. status = c->main(arg);
  188. if (status == USAGE) {
  189. // bad options. Run `lshal help ${mCommand}` instead.
  190. // For example, `lshal --unknown-option` becomes `lshal help` (prints global help)
  191. // and `lshal list --unknown-option` becomes `lshal help list`
  192. auto&& help = selectCommand(HelpCommand::GetName());
  193. return static_cast<HelpCommand*>(help)->usageOfCommand(mCommand);
  194. }
  195. return status;
  196. }
  197. NullableOStream<std::ostream> Lshal::err() const {
  198. return mErr;
  199. }
  200. NullableOStream<std::ostream> Lshal::out() const {
  201. return mOut;
  202. }
  203. const sp<IServiceManager> &Lshal::serviceManager() const {
  204. return mServiceManager;
  205. }
  206. const sp<IServiceManager> &Lshal::passthroughManager() const {
  207. return mPassthroughManager;
  208. }
  209. } // namespace lshal
  210. } // namespace android