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 #include "uds_session.h"
17 #include <sstream>
18 #include <fcntl.h>
19 #include <cinttypes>
20 #include <sys/types.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 constexpr int32_t INPUT_UI_TIMEOUT_TIME = 5 * 1000000;
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "UDSSession" };
29 } // namespace
30
UDSSession(const std::string & programName,const int32_t moduleType,const int32_t fd,const int32_t uid,const int32_t pid)31 UDSSession::UDSSession(const std::string& programName, const int32_t moduleType, const int32_t fd,
32 const int32_t uid, const int32_t pid)
33 : programName_(programName),
34 moduleType_(moduleType),
35 fd_(fd),
36 uid_(uid),
37 pid_(pid)
38 {
39 UpdateDescript();
40 }
41
~UDSSession()42 UDSSession::~UDSSession() {}
43
SendMsg(const char * buf,size_t size) const44 bool UDSSession::SendMsg(const char *buf, size_t size) const
45 {
46 CHKPF(buf);
47 if ((size == 0) || (size > MAX_PACKET_BUF_SIZE)) {
48 MMI_LOGE("buf size:%{public}zu", size);
49 return false;
50 }
51 if (fd_ < 0) {
52 MMI_LOGE("fd_ is less than 0");
53 return false;
54 }
55
56 int32_t idx = 0;
57 int32_t retryCount = 0;
58 const int32_t bufSize = static_cast<int32_t>(size);
59 int32_t remSize = bufSize;
60 while (remSize > 0 && retryCount < SEND_RETRY_LIMIT) {
61 retryCount += 1;
62 auto count = send(fd_, &buf[idx], remSize, MSG_DONTWAIT | MSG_NOSIGNAL);
63 if (count < 0) {
64 if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) {
65 MMI_LOGW("continue for errno EAGAIN|EINTR|EWOULDBLOCK, errno:%{public}d", errno);
66 usleep(SEND_RETRY_SLEEP_TIME);
67 continue;
68 }
69 MMI_LOGE("Send return failed,error:%{public}d fd:%{public}d", errno, fd_);
70 return false;
71 }
72 idx += count;
73 remSize -= count;
74 if (remSize > 0) {
75 usleep(SEND_RETRY_SLEEP_TIME);
76 }
77 }
78 if (retryCount >= SEND_RETRY_LIMIT || remSize != 0) {
79 MMI_LOGE("Send too many times:%{public}d/%{public}d,size:%{public}d/%{public}d fd:%{public}d",
80 retryCount, SEND_RETRY_LIMIT, idx, bufSize, fd_);
81 return false;
82 }
83 return true;
84 }
85
Close()86 void UDSSession::Close()
87 {
88 MMI_LOGD("enter fd_:%{public}d,bHasClosed_ = %d.", fd_, bHasClosed_);
89 if (!bHasClosed_ && fd_ != -1) {
90 close(fd_);
91 bHasClosed_ = true;
92 UpdateDescript();
93 }
94 }
95
UpdateDescript()96 void UDSSession::UpdateDescript()
97 {
98 std::ostringstream oss;
99 oss << "fd = " << fd_
100 << ", programName = " << programName_
101 << ", moduleType = " << moduleType_
102 << (bHasClosed_ ? ", closed" : ", opened")
103 << std::endl;
104 descript_ = oss.str().c_str();
105 }
106
SendMsg(NetPacket & pkt) const107 bool UDSSession::SendMsg(NetPacket& pkt) const
108 {
109 CHKF(!pkt.ChkRWError(), PACKET_WRITE_FAIL);
110 StreamBuffer buf;
111 pkt.MakeData(buf);
112 return SendMsg(buf.Data(), buf.Size());
113 }
114
AddEvent(int32_t id,int64_t time)115 void UDSSession::AddEvent(int32_t id, int64_t time)
116 {
117 MMI_LOGI("begin");
118 EventTime eventTime = {id, time};
119 events_.push_back(eventTime);
120 MMI_LOGI("end");
121 }
122
DelEvents(int32_t id)123 void UDSSession::DelEvents(int32_t id)
124 {
125 MMI_LOGI("begin");
126 int32_t count = 0;
127 for (auto &item : events_) {
128 ++count;
129 if (item.id == id) {
130 events_.erase(events_.begin(), events_.begin() + count);
131 MMI_LOGI("Delete events");
132 break;
133 }
134 }
135 auto currentTime = GetSysClockTime();
136 if (events_.empty() || (currentTime < (events_.begin()->eventTime + INPUT_UI_TIMEOUT_TIME))) {
137 isANRProcess_ = false;
138 }
139 MMI_LOGI("end");
140 }
141
GetFirstEventTime()142 int64_t UDSSession::GetFirstEventTime()
143 {
144 MMI_LOGI("begin");
145 if (events_.empty()) {
146 MMI_LOGI("events_ is empty");
147 return 0;
148 }
149 MMI_LOGI("end");
150 return events_.begin()->eventTime;
151 }
152
EventsIsEmpty()153 bool UDSSession::EventsIsEmpty()
154 {
155 if (events_.empty()) {
156 MMI_LOGI("events_ is empty");
157 return true;
158 }
159 return false;
160 }
161
AddPermission(bool hasPermission)162 void UDSSession::AddPermission(bool hasPermission)
163 {
164 hasPermission_ = hasPermission;
165 }
166
HasPermission()167 bool UDSSession::HasPermission()
168 {
169 return hasPermission_;
170 }
171 } // namespace MMI
172 } // namespace OHOS