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 <atomic> 18 #include <map> 19 #include <vector> 20 #include <system_ability.h> 21 #include "event_runner.h" 22 #include "event_handler.h" 23 #include "delayed_sp_singleton.h" 24 #include "dump_common_utils.h" 25 #include "dump_broker_stub.h" 26 #include "system_ability_ondemand_reason.h" 27 namespace OHOS { 28 namespace HiviewDFX { 29 enum WpId { 30 FDLEAK_WP_MIN = 0, 31 FDLEAK_WP_EVENTFD = 0, 32 FDLEAK_WP_EVENTPOLL = 1, 33 FDLEAK_WP_DMABUF = 2, 34 FDLEAK_WP_SYNCFENCE = 3, 35 FDLEAK_WP_SOCKET = 4, 36 FDLEAK_WP_PIPE = 5, 37 FDLEAK_WP_ASHMEM = 6, 38 }; 39 40 class RawParam; 41 #ifdef DUMP_TEST_MODE // for mock test 42 using DumpManagerServiceTestMainFunc = std::function<void(int argc, char *argv[], 43 const std::shared_ptr<RawParam>& reqCtl)>; 44 #endif // for mock test 45 class DumpManagerService final : public SystemAbility, public DumpBrokerStub { 46 DECLARE_SYSTEM_ABILITY(DumpManagerService) 47 public: 48 DumpManagerService(); 49 ~DumpManagerService(); 50 public: 51 virtual void OnStart() override; 52 virtual void OnStop() override; 53 public: 54 // Used for dump request 55 int32_t Request(std::vector<std::u16string> &args, int outfd) override; 56 public: 57 // Used for scan pid list over limit 58 int32_t ScanPidOverLimit(std::string requestType, int32_t limitSize, std::vector<int32_t> &pidList) override; 59 // Used for count fd nums 60 int32_t CountFdNums(int32_t pid, uint32_t &fdNums, std::string &detailFdInfo, std::string &topLeakedType) override; 61 public: 62 int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override; 63 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; IsServiceStarted()64 bool IsServiceStarted() const 65 { 66 return started_; 67 } 68 void DelayUnloadTask() override; 69 #ifdef DUMP_TEST_MODE // for mock test 70 void SetTestMainFunc(DumpManagerServiceTestMainFunc testMainFunc); 71 #endif // for mock test 72 private: 73 friend DumpDelayedSpSingleton<DumpManagerService>; 74 private: 75 bool Init(); 76 std::shared_ptr<RawParam> AddRequestRawParam(std::vector<std::u16string> &args, int outfd); 77 void EraseRequestRawParam(const std::shared_ptr<RawParam> rawParam); 78 void CancelAllRequest(); 79 int GetRequestSum(); 80 void GetIdleRequest(); 81 uint32_t GetRequestId(); 82 int32_t StartRequest(const std::shared_ptr<RawParam> rawParam); 83 void RequestMain(const std::shared_ptr<RawParam> rawParam); 84 bool HasDumpPermission() const; 85 uint32_t GetFileDescriptorNums(int32_t pid, std::string requestType) const; 86 void RecordDetailFdInfo(std::string &detailFdInfo, std::string &topLeakedType); 87 void RecordDirFdInfo(std::string &detailFdInfo); 88 std::string GetFdLinkNum(const std::string &linkPath) const; 89 void SetCpuSchedAffinity(); 90 void HandleRequestError(std::vector<std::u16string> &args, int outfd, 91 const int32_t& errorCode, const std::string& errorMsg); 92 private: 93 std::mutex mutex_; 94 std::mutex linkCntMutex_; 95 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 96 std::shared_ptr<AppExecFwk::EventHandler> handler_; 97 std::atomic<bool> started_ = false; 98 std::atomic<bool> blockRequest_ = false; 99 uint32_t requestIndex_ {0}; 100 std::map<uint32_t, std::shared_ptr<RawParam>> requestRawParamMap_; 101 std::vector<std::pair<std::string, int>> linkCnt_; 102 #ifdef DUMP_TEST_MODE // for mock test 103 DumpManagerServiceTestMainFunc testMainFunc_ {nullptr}; 104 #endif // for mock test 105 }; 106 } // namespace HiviewDFX 107 } // namespace OHOS 108 #endif // HIDUMPER_SERVICES_DUMP_MANAGER_SERVICE_H 109