1 /*
2  * Copyright (c) 2021-2022 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 UDS_SESSION_H
17 #define UDS_SESSION_H
18 
19 #include <list>
20 #include <memory>
21 
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 
25 #include "nocopyable.h"
26 
27 #include "net_packet.h"
28 #include "proto.h"
29 
30 namespace OHOS {
31 namespace MMI {
32 class UDSSession;
33 using SessionPtr = std::shared_ptr<UDSSession>;
34 class UDSSession : public std::enable_shared_from_this<UDSSession> {
35 public:
36     UDSSession(const std::string &programName, const int32_t moduleType, const int32_t fd, const int32_t uid,
37                const int32_t pid);
38     DISALLOW_COPY_AND_MOVE(UDSSession);
39     virtual ~UDSSession() = default;
40 
41     bool SendMsg(const char *buf, size_t size) const;
42     bool SendMsg(NetPacket &pkt) const;
43     void Close();
44 
GetUid()45     int32_t GetUid() const
46     {
47         return uid_;
48     }
49 
GetPid()50     int32_t GetPid() const
51     {
52         return pid_;
53     }
54 
GetModuleType()55     int32_t GetModuleType() const
56     {
57         return moduleType_;
58     }
59 
GetSharedPtr()60     SessionPtr GetSharedPtr()
61     {
62         return shared_from_this();
63     }
64 
GetFd()65     int32_t GetFd() const
66     {
67         return fd_;
68     }
69 
GetDescript()70     const std::string& GetDescript() const
71     {
72         return descript_;
73     }
74 
GetProgramName()75     const std::string GetProgramName() const
76     {
77         return programName_;
78     }
79 
SetAnrStatus(int32_t type,bool status)80     void SetAnrStatus(int32_t type, bool status)
81     {
82         isAnrProcess_[type] = status;
83     }
84 
CheckAnrStatus(int32_t type)85     bool CheckAnrStatus(int32_t type)
86     {
87         return isAnrProcess_[type];
88     }
89 
SetTokenType(int32_t type)90     void SetTokenType(int32_t type)
91     {
92         tokenType_ = type;
93     }
94 
GetTokenType()95     int32_t GetTokenType() const
96     {
97         return tokenType_;
98     }
99 
IsSocketValid()100     bool IsSocketValid()
101     {
102         return !invalidSocket_;
103     }
104     void UpdateDescript();
105     void SaveANREvent(int32_t type, int32_t id, int64_t time, int32_t timerId);
106     std::vector<int32_t> GetTimerIds(int32_t type);
107     std::list<int32_t> DelEvents(int32_t type, int32_t id);
108     int64_t GetEarliestEventTime(int32_t type = 0) const;
109     bool IsEventQueueEmpty(int32_t type = 0);
110     void ReportSocketBufferFull() const;
111 
112 protected:
113     struct EventTime {
114         int32_t id { 0 };
115         int64_t eventTime { 0 };
116         int32_t timerId { -1 };
117     };
118     std::map<int32_t, std::vector<EventTime>> events_;
119     std::map<int32_t, bool> isAnrProcess_;
120     std::string descript_;
121     const std::string programName_;
122     const int32_t moduleType_ { -1 };
123     int32_t fd_ { -1 };
124     const int32_t uid_ { -1 };
125     const int32_t pid_ { -1 };
126     int32_t tokenType_ { TokenType::TOKEN_INVALID };
127     mutable bool invalidSocket_ { false };
128 };
129 } // namespace MMI
130 } // namespace OHOS
131 #endif // UDS_SESSION_H