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