• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 /* This files contains faultlog secure module. */
17 
18 #include "fault_logger_pipe.h"
19 
20 #include <algorithm>
21 #include <cerrno>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cstring>
25 #include <ctime>
26 #include <fcntl.h>
27 #include <mutex>
28 #include <securec.h>
29 #include <string>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/syscall.h>
33 #include <sys/types.h>
34 #include <sys/un.h>
35 #include <unistd.h>
36 #include <vector>
37 #include "dfx_define.h"
38 #include "dfx_log.h"
39 
40 
41 namespace OHOS {
42 namespace HiviewDFX {
43 namespace {
44 static const std::string FAULTLOGGER_PIPE_TAG = "FaultLoggerPipe";
45 static const int PIPE_READ = 0;
46 static const int PIPE_WRITE = 1;
47 static std::mutex g_pipeMapsMutex;
48 }
49 
FaultLoggerPipe()50 FaultLoggerPipe::FaultLoggerPipe()
51 {
52     bInit_ = false;
53     Init();
54 }
55 
~FaultLoggerPipe()56 FaultLoggerPipe::~FaultLoggerPipe()
57 {
58     Destroy();
59 }
60 
GetReadFd()61 int FaultLoggerPipe::GetReadFd()
62 {
63     DfxLogDebug("%s :: pipe read fd: %d", __func__, pfds_[PIPE_READ]);
64     return pfds_[PIPE_READ];
65 }
66 
GetWriteFd()67 int FaultLoggerPipe::GetWriteFd()
68 {
69     DfxLogDebug("%s :: pipe write fd: %d", __func__, pfds_[PIPE_WRITE]);
70     return pfds_[PIPE_WRITE];
71 }
72 
Init()73 bool FaultLoggerPipe::Init()
74 {
75     if (!bInit_) {
76         if (pipe(pfds_) != 0) {
77             DfxLogError("%s :: Failed to create pipe.", __func__);
78             return false;
79         }
80         DfxLogDebug("%s :: create pipe.", __func__);
81     }
82     bInit_ = true;
83     return true;
84 }
85 
Destroy()86 void FaultLoggerPipe::Destroy()
87 {
88     if (bInit_) {
89         DfxLogDebug("%s :: close pipe.", __func__);
90         Close(pfds_[PIPE_READ]);
91         Close(pfds_[PIPE_WRITE]);
92     }
93     bInit_ = false;
94 }
95 
Close(int fd) const96 void FaultLoggerPipe::Close(int fd) const
97 {
98     syscall(SYS_close, fd);
99 }
100 
FaultLoggerPipe2(std::unique_ptr<FaultLoggerPipe> pipeBuf,std::unique_ptr<FaultLoggerPipe> pipeRes)101 FaultLoggerPipe2::FaultLoggerPipe2(std::unique_ptr<FaultLoggerPipe> pipeBuf, std::unique_ptr<FaultLoggerPipe> pipeRes)
102     : faultLoggerPipeBuf_(std::move(pipeBuf)), faultLoggerPipeRes_(std::move(pipeRes))
103 {
104 }
105 
FaultLoggerPipe2()106 FaultLoggerPipe2::FaultLoggerPipe2(): FaultLoggerPipe2(std::unique_ptr<FaultLoggerPipe>(new FaultLoggerPipe()),
107     std::unique_ptr<FaultLoggerPipe>(new FaultLoggerPipe()))
108 {
109 }
110 
~FaultLoggerPipe2()111 FaultLoggerPipe2::~FaultLoggerPipe2()
112 {
113     faultLoggerPipeBuf_.reset();
114     faultLoggerPipeRes_.reset();
115 }
116 
FaultLoggerPipeMap()117 FaultLoggerPipeMap::FaultLoggerPipeMap()
118 {
119     std::lock_guard<std::mutex> lck(g_pipeMapsMutex);
120     faultLoggerPipes_.clear();
121 }
122 
~FaultLoggerPipeMap()123 FaultLoggerPipeMap::~FaultLoggerPipeMap()
124 {
125     std::lock_guard<std::mutex> lck(g_pipeMapsMutex);
126     std::map<int, std::unique_ptr<FaultLoggerPipe2> >::iterator iter = faultLoggerPipes_.begin();
127     while (iter != faultLoggerPipes_.end()) {
128         faultLoggerPipes_.erase(iter++);
129     }
130 }
131 
Set(int pid)132 void FaultLoggerPipeMap::Set(int pid)
133 {
134     std::lock_guard<std::mutex> lck(g_pipeMapsMutex);
135     if (!Find(pid)) {
136         std::unique_ptr<FaultLoggerPipe2> ptr = std::unique_ptr<FaultLoggerPipe2>(new FaultLoggerPipe2());
137         faultLoggerPipes_.insert(make_pair(pid, std::move(ptr)));
138     }
139 }
140 
Get(int pid)141 FaultLoggerPipe2* FaultLoggerPipeMap::Get(int pid)
142 {
143     std::lock_guard<std::mutex> lck(g_pipeMapsMutex);
144     if (!Find(pid)) {
145         return nullptr;
146     }
147     return faultLoggerPipes_[pid].get();
148 }
149 
Del(int pid)150 void FaultLoggerPipeMap::Del(int pid)
151 {
152     std::lock_guard<std::mutex> lck(g_pipeMapsMutex);
153     std::map<int, std::unique_ptr<FaultLoggerPipe2> >::const_iterator iter = faultLoggerPipes_.find(pid);
154     if (iter != faultLoggerPipes_.end()) {
155         faultLoggerPipes_.erase(iter);
156     }
157 }
158 
Find(int pid) const159 bool FaultLoggerPipeMap::Find(int pid) const
160 {
161     std::map<int, std::unique_ptr<FaultLoggerPipe2> >::const_iterator iter = faultLoggerPipes_.find(pid);
162     if (iter != faultLoggerPipes_.end()) {
163         return true;
164     }
165     return false;
166 }
167 } // namespace HiviewDfx
168 } // namespace OHOS
169