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