1 /*
2  * Copyright (c) 2021-2023 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 #ifndef FAULT_LOGGER_PIPE_H
17 #define FAULT_LOGGER_PIPE_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 class FaultLoggerPipe {
27 public:
28     FaultLoggerPipe();
29     ~FaultLoggerPipe();
30 
31     int GetReadFd(void);
32     int GetWriteFd(void);
33 
34     void Close(int fd) const;
35 private:
36     bool Init(void);
37     bool SetSize(long sz);
38     void Destroy(void);
39 
40     int fds_[2] = {-1, -1};
41     bool init_;
42     bool write_;
43 };
44 
45 class FaultLoggerPipe2 {
46 public:
47     explicit FaultLoggerPipe2(uint64_t time, bool isJson = false);
48     ~FaultLoggerPipe2();
49     std::unique_ptr<FaultLoggerPipe> faultLoggerPipeBuf_;
50     std::unique_ptr<FaultLoggerPipe> faultLoggerPipeRes_;
51     std::unique_ptr<FaultLoggerPipe> faultLoggerJsonPipeBuf_;
52     std::unique_ptr<FaultLoggerPipe> faultLoggerJsonPipeRes_;
53     uint64_t time_;
54 };
55 
56 class FaultLoggerPipeMap {
57 public:
58     FaultLoggerPipeMap();
59     ~FaultLoggerPipeMap();
60 
61     bool Check(int pid, uint64_t time);
62     void Set(int pid, uint64_t time, bool isJson = false);
63     FaultLoggerPipe2* Get(int pid);
64     void Del(int pid);
65 
66 private:
67     bool Find(int pid) const;
68 
69 private:
70     std::map<int, std::unique_ptr<FaultLoggerPipe2> > faultLoggerPipes_;
71     std::mutex pipeMapsMutex_;
72 };
73 } // namespace HiviewDFX
74 } // namespace OHOS
75 #endif
76