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