• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #include "fault_logger_daemon.h"
17 
18 #include <csignal>
19 #include <memory>
20 #include <thread>
21 
22 #include "dfx_log.h"
23 #include "dfx_util.h"
24 #include "epoll_manager.h"
25 #include "fault_logger_server.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 
30 namespace {
31 constexpr const char* const FAULTLOGGERD_DAEMON_TAG = "FAULT_LOGGER_DAEMON";
32 constexpr int32_t MAX_CONNECTION = 30;
33 constexpr int32_t MAX_EPOLL_EVENT = 1024;
34 }
35 
GetInstance()36 FaultLoggerDaemon& FaultLoggerDaemon::GetInstance()
37 {
38     static FaultLoggerDaemon faultLoggerDaemon;
39     return faultLoggerDaemon;
40 }
41 
FaultLoggerDaemon()42 FaultLoggerDaemon::FaultLoggerDaemon() : mainServer_(mainEpollManager_), tempFileManager_(secondaryEpollManager_)
43 {
44     if (!mainEpollManager_.Init(MAX_EPOLL_EVENT)) {
45         DFXLOGE("%{public}s :: Failed to init main epollManager", FAULTLOGGERD_DAEMON_TAG);
46         return;
47     }
48     if (!secondaryEpollManager_.Init(MAX_EPOLL_EVENT)) {
49         DFXLOGE("%{public}s :: Failed to init secondary epollManager", FAULTLOGGERD_DAEMON_TAG);
50         return;
51     }
52     if (!mainServer_.Init()) {
53         DFXLOGE("%{public}s :: Failed to init Faultloggerd Server", FAULTLOGGERD_DAEMON_TAG);
54         return;
55     }
56     if (!tempFileManager_.Init()) {
57         DFXLOGE("%{public}s :: Failed to init tempFileManager", FAULTLOGGERD_DAEMON_TAG);
58         return;
59     }
60     if (signal(SIGCHLD, SIG_IGN) == SIG_ERR || signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
61         DFXLOGE("%{public}s :: Failed to signal SIGCHLD or SIGPIPE, errno: %{public}d",
62             FAULTLOGGERD_DAEMON_TAG, errno);
63     }
64 }
65 
~FaultLoggerDaemon()66 FaultLoggerDaemon::~FaultLoggerDaemon()
67 {
68     mainEpollManager_.StopEpoll();
69     secondaryEpollManager_.StopEpoll();
70 }
71 
StartServer()72 int32_t FaultLoggerDaemon::StartServer()
73 {
74     std::thread([this] {
75         pthread_setname_np(pthread_self(), "HelperServer");
76         secondaryEpollManager_.StartEpoll(MAX_CONNECTION);
77     }).detach();
78 #ifdef FAULTLOGGERD_TEST
79     constexpr auto epollTimeoutInMilliseconds = 3 * 1000;
80 #else
81     constexpr auto epollTimeoutInMilliseconds = 20 * 1000;
82 #endif
83     mainEpollManager_.StartEpoll(MAX_CONNECTION, epollTimeoutInMilliseconds);
84     return 0;
85 }
86 
GetEpollManager(EpollManagerType type)87 EpollManager& FaultLoggerDaemon::GetEpollManager(EpollManagerType type)
88 {
89     if (type == EpollManagerType::MAIN_SERVER) {
90         return GetInstance().mainEpollManager_;
91     }
92     return GetInstance().secondaryEpollManager_;
93 }
94 }
95 }