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#ifndef FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_
18#define FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_
19
20#include <stdint.h>
21
22#include <fstream>
23#include <string>
24#include <vector>
25
26#include <android-base/macros.h>
27#include <android/hidl/manager/1.0/IServiceManager.h>
28
29#include "NullableOStream.h"
30#include "TableEntry.h"
31#include "utils.h"
32
33namespace android {
34namespace lshal {
35
36class Lshal;
37
38class ListCommand {
39public:
40    ListCommand(Lshal &lshal);
41    Status main(const std::string &command, const Arg &arg);
42private:
43    Status parseArgs(const std::string &command, const Arg &arg);
44    Status fetch();
45    void postprocess();
46    void dump();
47    void putEntry(TableEntrySource source, TableEntry &&entry);
48    Status fetchPassthrough(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
49    Status fetchBinderized(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
50    Status fetchAllLibraries(const sp<::android::hidl::manager::V1_0::IServiceManager> &manager);
51    bool getReferencedPids(
52        pid_t serverPid, std::map<uint64_t, Pids> *objects) const;
53    void dumpTable();
54    void dumpVintf() const;
55    void printLine(
56            const std::string &interfaceName,
57            const std::string &transport,
58            const std::string &arch,
59            const std::string &server,
60            const std::string &serverCmdline,
61            const std::string &address, const std::string &clients,
62            const std::string &clientCmdlines) const ;
63    // Return /proc/{pid}/cmdline if it exists, else empty string.
64    const std::string &getCmdline(pid_t pid);
65    // Call getCmdline on all pid in pids. If it returns empty string, the process might
66    // have died, and the pid is removed from pids.
67    void removeDeadProcesses(Pids *pids);
68    void forEachTable(const std::function<void(Table &)> &f);
69    void forEachTable(const std::function<void(const Table &)> &f) const;
70
71    Lshal &mLshal;
72
73    Table mServicesTable{};
74    Table mPassthroughRefTable{};
75    Table mImplementationsTable{};
76
77    NullableOStream<std::ostream> mErr;
78    NullableOStream<std::ostream> mOut;
79    NullableOStream<std::ofstream> mFileOutput = nullptr;
80    TableEntryCompare mSortColumn = nullptr;
81    TableEntrySelect mSelectedColumns = 0;
82    // If true, cmdlines will be printed instead of pid.
83    bool mEnableCmdlines = false;
84
85    // If true, calls IBase::debug(...) on each service.
86    bool mEmitDebugInfo = false;
87
88    bool mVintf = false;
89    // If an entry does not exist, need to ask /proc/{pid}/cmdline to get it.
90    // If an entry exist but is an empty string, process might have died.
91    // If an entry exist and not empty, it contains the cached content of /proc/{pid}/cmdline.
92    std::map<pid_t, std::string> mCmdlines;
93
94    DISALLOW_COPY_AND_ASSIGN(ListCommand);
95};
96
97
98}  // namespace lshal
99}  // namespace android
100
101#endif  // FRAMEWORK_NATIVE_CMDS_LSHAL_LIST_COMMAND_H_
102