1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef HIDUMPER_SERVICES_DUMP_MANAGER_SERVICE_H
16 #define HIDUMPER_SERVICES_DUMP_MANAGER_SERVICE_H
17 #include <map>
18 #include <vector>
19 #include <system_ability.h>
20 #include "event_runner.h"
21 #include "event_handler.h"
22 #include "delayed_sp_singleton.h"
23 #include "dump_common_utils.h"
24 #include "dump_broker_stub.h"
25 #include "system_ability_ondemand_reason.h"
26 namespace OHOS {
27 namespace HiviewDFX {
28 enum WpId {
29     FDLEAK_WP_MIN = 0,
30     FDLEAK_WP_EVENTFD = 0,
31     FDLEAK_WP_EVENTPOLL = 1,
32     FDLEAK_WP_DMABUF = 2,
33     FDLEAK_WP_SYNCFENCE = 3,
34     FDLEAK_WP_SOCKET = 4,
35     FDLEAK_WP_PIPE = 5,
36     FDLEAK_WP_ASHMEM = 6,
37 };
38 
39 class RawParam;
40 #ifdef DUMP_TEST_MODE // for mock test
41 using DumpManagerServiceTestMainFunc = std::function<void(int argc, char *argv[],
42     const std::shared_ptr<RawParam>& reqCtl)>;
43 #endif // for mock test
44 class DumpManagerService final : public SystemAbility, public DumpBrokerStub {
45     DECLARE_SYSTEM_ABILITY(DumpManagerService)
46 public:
47     DumpManagerService();
48     ~DumpManagerService();
49 public:
50     virtual void OnStart() override;
51     virtual void OnStop() override;
52 public:
53     // Used for dump request
54     int32_t Request(std::vector<std::u16string> &args, int outfd) override;
55 public:
56     // Used for scan pid list over limit
57     int32_t ScanPidOverLimit(std::string requestType, int32_t limitSize, std::vector<int32_t> &pidList) override;
58     // Used for count fd nums
59     int32_t CountFdNums(int32_t pid, uint32_t &fdNums, std::string &detailFdInfo, std::string &topLeakedType) override;
60 public:
61     int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override;
62     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
IsServiceStarted()63     bool IsServiceStarted() const
64     {
65         return started_;
66     }
67     void DelayUnloadTask() override;
68 #ifdef DUMP_TEST_MODE // for mock test
69     void SetTestMainFunc(DumpManagerServiceTestMainFunc testMainFunc);
70 #endif // for mock test
71 private:
72     friend DumpDelayedSpSingleton<DumpManagerService>;
73 private:
74     bool Init();
75     std::shared_ptr<RawParam> AddRequestRawParam(std::vector<std::u16string> &args, int outfd);
76     void EraseRequestRawParam(const std::shared_ptr<RawParam> rawParam);
77     void CancelAllRequest();
78     int GetRequestSum();
79     void GetIdleRequest();
80     uint32_t GetRequestId();
81     int32_t StartRequest(const std::shared_ptr<RawParam> rawParam);
82     void RequestMain(const std::shared_ptr<RawParam> rawParam);
83     bool HasDumpPermission() const;
84     uint32_t GetFileDescriptorNums(int32_t pid, std::string requestType) const;
85     void RecordDetailFdInfo(std::string &detailFdInfo, std::string &topLeakedType);
86     void RecordDirFdInfo(std::string &detailFdInfo);
87     std::string GetFdLinkNum(const std::string &linkPath) const;
88     void SetCpuSchedAffinity();
89 private:
90     std::mutex mutex_;
91     std::mutex linkCntMutex_;
92     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
93     std::shared_ptr<AppExecFwk::EventHandler> handler_;
94     bool started_ {false};
95     bool blockRequest_ {false};
96     uint32_t requestIndex_ {0};
97     std::map<uint32_t, std::shared_ptr<RawParam>> requestRawParamMap_;
98     std::vector<std::pair<std::string, int>> linkCnt_;
99 #ifdef DUMP_TEST_MODE // for mock test
100     DumpManagerServiceTestMainFunc testMainFunc_ {nullptr};
101 #endif // for mock test
102 };
103 } // namespace HiviewDFX
104 } // namespace OHOS
105 #endif // HIDUMPER_SERVICES_DUMP_MANAGER_SERVICE_H
106