185dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong/* 28388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong * Copyright (C) 2017 The Android Open Source Project 385dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * 485dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * Licensed under the Apache License, Version 2.0 (the "License"); 585dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * you may not use this file except in compliance with the License. 685dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * You may obtain a copy of the License at 785dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * 885dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * http://www.apache.org/licenses/LICENSE-2.0 985dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * 1085dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * Unless required by applicable law or agreed to in writing, software 1185dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * distributed under the License is distributed on an "AS IS" BASIS, 1285dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1385dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * See the License for the specific language governing permissions and 1485dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong * limitations under the License. 1585dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong */ 1685dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong 174d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong#define LOG_TAG "lshal" 184d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong#include <android-base/logging.h> 194d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong 20b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hong#include "Lshal.h" 2185dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong 228388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong#include <set> 238388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong#include <string> 2485dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong 254d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong#include <hidl/ServiceManagement.h> 264d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong 274d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong#include "DebugCommand.h" 288388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong#include "ListCommand.h" 2928d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber#include "PipeRelay.h" 30b24760fd49d6475885eb4edae7b2f42888d5e30fYifan Hong 31b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hongnamespace android { 32b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hongnamespace lshal { 33b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hong 348388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hongusing ::android::hidl::manager::V1_0::IServiceManager; 35a3b8709a5dfc57d6d6bcc0ee89bbeb4aed3e908cYifan Hong 363b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan HongLshal::Lshal() 373b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong : mOut(std::cout), mErr(std::cerr), 383b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong mServiceManager(::android::hardware::defaultServiceManager()), 393b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong mPassthroughManager(::android::hardware::getPassthroughServiceManager()) { 403b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong} 413b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong 423b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan HongLshal::Lshal(std::ostream &out, std::ostream &err, 433b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong sp<hidl::manager::V1_0::IServiceManager> serviceManager, 443b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong sp<hidl::manager::V1_0::IServiceManager> passthroughManager) 453b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong : mOut(out), mErr(err), 463b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong mServiceManager(serviceManager), 473b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong mPassthroughManager(passthroughManager) { 483b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong 498388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong} 508388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong 518388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hongvoid Lshal::usage(const std::string &command) const { 528388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong static const std::string helpSummary = 538388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "lshal: List and debug HALs.\n" 548388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "\n" 558388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "commands:\n" 568388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " help Print help message\n" 578388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " list list HALs\n" 588388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " debug debug a specified HAL\n" 598388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "\n" 608388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "If no command is specified, `list` is the default.\n"; 618388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong 628388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong static const std::string list = 638388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "list:\n" 648388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal\n" 658388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal list\n" 668388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " List all hals with default ordering and columns (`lshal list -ipc`)\n" 678388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal list [-h|--help]\n" 688388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -h, --help: Print help message for list (`lshal help list`)\n" 698388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal [list] [--interface|-i] [--transport|-t] [-r|--arch]\n" 708388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " [--pid|-p] [--address|-a] [--clients|-c] [--cmdline|-m]\n" 718388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " [--sort={interface|i|pid|p}] [--init-vintf[=<output file>]]\n" 728388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " [--debug|-d[=<output file>]]\n" 738388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -i, --interface: print the interface name column\n" 748388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -n, --instance: print the instance name column\n" 758388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -t, --transport: print the transport mode column\n" 768388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -r, --arch: print if the HAL is in 64-bit or 32-bit\n" 778388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -p, --pid: print the server PID, or server cmdline if -m is set\n" 788388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -a, --address: print the server object address column\n" 798388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -c, --clients: print the client PIDs, or client cmdlines if -m is set\n" 808388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -m, --cmdline: print cmdline instead of PIDs\n" 818388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " -d[=<output file>], --debug[=<output file>]: emit debug info from \n" 828388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " IBase::debug with empty options\n" 838388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " --sort=i, --sort=interface: sort by interface name\n" 848388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " --sort=p, --sort=pid: sort by server pid\n" 858388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " --init-vintf=<output file>: form a skeleton HAL manifest to specified\n" 868388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " file, or stdout if no file specified.\n"; 878388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong 888388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong static const std::string debug = 898388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "debug:\n" 908388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal debug <interface> [options [options [...]]] \n" 918388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " Print debug information of a specified interface.\n" 928388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " <inteface>: Format is `android.hardware.foo@1.0::IFoo/default`.\n" 938388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " If instance name is missing `default` is used.\n" 948388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " options: space separated options to IBase::debug.\n"; 958388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong 968388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong static const std::string help = 978388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong "help:\n" 988388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal -h\n" 998388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal --help\n" 1008388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal help\n" 1018388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " Print this help message\n" 1028388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal help list\n" 1038388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " Print help message for list\n" 1048388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " lshal help debug\n" 1058388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong " Print help message for debug\n"; 1068388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong 1078388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (command == "list") { 1088388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong mErr << list; 1098388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return; 110ae09a3ddfec51c82257e338346e39ee472470061Yifan Hong } 1118388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (command == "debug") { 1128388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong mErr << debug; 1138388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return; 114b44790219f43369883b3dd8eaf3971bdc5fe656bYifan Hong } 115b44790219f43369883b3dd8eaf3971bdc5fe656bYifan Hong 1168388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong mErr << helpSummary << "\n" << list << "\n" << debug << "\n" << help; 117b44790219f43369883b3dd8eaf3971bdc5fe656bYifan Hong} 118b44790219f43369883b3dd8eaf3971bdc5fe656bYifan Hong 11928d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber// A unique_ptr type using a custom deleter function. 12028d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Hubertemplate<typename T> 12128d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huberusing deleted_unique_ptr = std::unique_ptr<T, std::function<void(T *)> >; 12228d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 1238388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hongstatic hardware::hidl_vec<hardware::hidl_string> convert(const std::vector<std::string> &v) { 1248388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong hardware::hidl_vec<hardware::hidl_string> hv; 1258388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong hv.resize(v.size()); 1268388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong for (size_t i = 0; i < v.size(); ++i) { 1278388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong hv[i].setToExternal(v[i].c_str(), v[i].size()); 1288388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong } 1298388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return hv; 1308388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong} 1318388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong 1324d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan HongStatus Lshal::emitDebugInfo( 13328d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber const std::string &interfaceName, 1348388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong const std::string &instanceName, 1358388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong const std::vector<std::string> &options, 1364d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong std::ostream &out, 1374d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong NullableOStream<std::ostream> err) const { 13828d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber using android::hidl::base::V1_0::IBase; 13928d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 1403b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong hardware::Return<sp<IBase>> retBase = serviceManager()->get(interfaceName, instanceName); 1414d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong 1424d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong if (!retBase.isOk()) { 1434d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong std::string msg = "Cannot get " + interfaceName + "/" + instanceName + ": " 1444d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong + retBase.description(); 1454d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong err << msg << std::endl; 1464d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong LOG(ERROR) << msg; 1474d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong return TRANSACTION_ERROR; 1484d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong } 14928d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 1504d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong sp<IBase> base = retBase; 1514d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong if (base == nullptr) { 1524d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong std::string msg = interfaceName + "/" + instanceName + " does not exist, or " 1534d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong + "no permission to connect."; 1544d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong err << msg << std::endl; 1554d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong LOG(ERROR) << msg; 1564d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong return NO_INTERFACE; 15728d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber } 15828d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 1598388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong PipeRelay relay(out); 16028d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 16128d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber if (relay.initCheck() != OK) { 1624d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong std::string msg = "PipeRelay::initCheck() FAILED w/ " + std::to_string(relay.initCheck()); 1634d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong err << msg << std::endl; 1644d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong LOG(ERROR) << msg; 1654d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong return IO_ERROR; 16628d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber } 16728d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 16828d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber deleted_unique_ptr<native_handle_t> fdHandle( 16928d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber native_handle_create(1 /* numFds */, 0 /* numInts */), 17028d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber native_handle_delete); 17128d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 17228d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber fdHandle->data[0] = relay.fd(); 17328d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 1748388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong hardware::Return<void> ret = base->debug(fdHandle.get(), convert(options)); 17528d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 17628d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber if (!ret.isOk()) { 1774d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong std::string msg = "debug() FAILED on " + interfaceName + "/" + instanceName + ": " 1784d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong + ret.description(); 1794d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong err << msg << std::endl; 1804d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong LOG(ERROR) << msg; 1814d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong return TRANSACTION_ERROR; 18228d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber } 1834d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong return OK; 18428d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber} 18528d35913d5324dd520d80d9b6afdd0698afa8fd4Andreas Huber 1868388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan HongStatus Lshal::parseArgs(const Arg &arg) { 1878388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong static std::set<std::string> sAllCommands{"list", "debug", "help"}; 1888388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong optind = 1; 1898388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (optind >= arg.argc) { 1908388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong // no options at all. 1918388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return OK; 192a3b8709a5dfc57d6d6bcc0ee89bbeb4aed3e908cYifan Hong } 1938388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong mCommand = arg.argv[optind]; 1948388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (sAllCommands.find(mCommand) != sAllCommands.end()) { 1958388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong ++optind; 1968388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return OK; // mCommand is set correctly 197a3b8709a5dfc57d6d6bcc0ee89bbeb4aed3e908cYifan Hong } 198b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hong 1998388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (mCommand.size() > 0 && mCommand[0] == '-') { 2008388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong // first argument is an option, set command to "" (which is recognized as "list") 2018388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong mCommand = ""; 2028388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return OK; 203b7ddc9e5cba5a0a287993b5b3124a75d67d9557cYifan Hong } 204b7ddc9e5cba5a0a287993b5b3124a75d67d9557cYifan Hong 2058388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong mErr << arg.argv[0] << ": unrecognized option `" << arg.argv[optind] << "`" << std::endl; 2068388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong usage(); 2078388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return USAGE; 208b7ddc9e5cba5a0a287993b5b3124a75d67d9557cYifan Hong} 209b7ddc9e5cba5a0a287993b5b3124a75d67d9557cYifan Hong 2103b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hongvoid signalHandler(int sig) { 2113b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong if (sig == SIGINT) { 2123b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong int retVal; 2133b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong pthread_exit(&retVal); 2143b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong } 2153b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong} 2163b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong 2178388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan HongStatus Lshal::main(const Arg &arg) { 2183b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong // Allow SIGINT to terminate all threads. 2193b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong signal(SIGINT, signalHandler); 2203b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong 2218388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong Status status = parseArgs(arg); 2228388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (status != OK) { 2238388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return status; 2249b5c15d266b3fd5d26c992f8f8f4e6a3a03436dfSteven Moreland } 2258388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (mCommand == "help") { 2268388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong usage(optind < arg.argc ? arg.argv[optind] : ""); 2278388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return USAGE; 228b24760fd49d6475885eb4edae7b2f42888d5e30fYifan Hong } 2298388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong // Default command is list 2308388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (mCommand == "list" || mCommand == "") { 2318388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return ListCommand{*this}.main(mCommand, arg); 232b7ddc9e5cba5a0a287993b5b3124a75d67d9557cYifan Hong } 2338388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong if (mCommand == "debug") { 2344d71d2913702fd5a6b6f84bdfae02f6ef1a14b24Yifan Hong return DebugCommand{*this}.main(mCommand, arg); 23585dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong } 2368388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong usage(); 2378388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return USAGE; 23885dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong} 23985dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong 2408388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan HongNullableOStream<std::ostream> Lshal::err() const { 2418388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return mErr; 242b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hong} 2438388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan HongNullableOStream<std::ostream> Lshal::out() const { 2448388ba3e2ddaf703192946b0b7874cc2431f3dd7Yifan Hong return mOut; 245b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hong} 24685dd3ad89f26e8a28336ac3b2365fb896df7a0f6Yifan Hong 2473b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hongconst sp<IServiceManager> &Lshal::serviceManager() const { 2483b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong return mServiceManager; 2493b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong} 2503b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong 2513b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hongconst sp<IServiceManager> &Lshal::passthroughManager() const { 2523b3fbb7e23140e87f9f85d7ce82daeef82dde1e1Yifan Hong return mPassthroughManager; 253a57dffb151145529e71b22a8d7f985bc1de105f4Yifan Hong} 254a57dffb151145529e71b22a8d7f985bc1de105f4Yifan Hong 255b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hong} // namespace lshal 256b0dde932ad8989a393fad148c17817bcb2ff7a43Yifan Hong} // namespace android 257