From 01cfaef1f3e79f1b6176357549e6a3454821b75a Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 27 Oct 2022 15:17:09 +0800 Subject: [PATCH 1/6] =?UTF-8?q?freeWork(work)=E6=8F=90=E5=89=8D=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I523c0543082b6b018506369ac303715391d2f176 --- interfaces/plugin/src/sensor_napi_utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 4cfecb68..184edfeb 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -492,6 +492,7 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) int32_t ret = uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { CHKPV(work); sptr asyncCallbackInfo(static_cast(work->data)); + freeWork(work); /** * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the @@ -531,7 +532,6 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) } napi_close_handle_scope(asyncCallbackInfo->env, scope); asyncCallbackInfo->work = nullptr; - freeWork(work); }); if (ret != 0) { SEN_HILOGE("uv_queue_work fail"); -- Gitee From 67cbab9d25b74e4fd6069baf91d5085d6ca10f54 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 27 Oct 2022 15:25:07 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0argv[i]=E5=88=A4=E7=A9=BA?= =?UTF-8?q?=EF=BC=8C=E5=8F=AA=E5=9C=A8=E9=9D=9E=E7=A9=BA=E6=97=B6delete[]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I5b8f50b8001b3324d8760dd17331b1beb4dd644e --- services/sensor/src/sensor_dump.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/sensor/src/sensor_dump.cpp b/services/sensor/src/sensor_dump.cpp index d1d298b9..5a1c8a0c 100644 --- a/services/sensor/src/sensor_dump.cpp +++ b/services/sensor/src/sensor_dump.cpp @@ -173,6 +173,9 @@ void SensorDump::ParseCommand(int32_t fd, const std::vector &args, } RELEASE_RES: for (size_t i = 0; i < args.size(); ++i) { + if (argv[i] == nullptr) { + continue; + } delete[] argv[i]; } delete[] argv; -- Gitee From 414fdeac17cc3236663972acf09f81c8fbe8bd16 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 27 Oct 2022 15:43:24 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=99=BA=E8=83=BD=E6=8C=87=E9=92=88?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=BAnullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I2af2fdbefee7000fffd5eac496c57820ce3bb225 --- services/sensor/include/sensor_service.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index a436cf89..62137d5b 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -78,11 +78,11 @@ private: ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); FlushInfoRecord &flushInfo_ = FlushInfoRecord::GetInstance(); - sptr sensorDataProcesser_; - sptr reportDataCallback_; + sptr sensorDataProcesser_ = nullptr; + sptr reportDataCallback_ = nullptr; std::mutex uidLock_; // death recipient of sensor client - sptr clientDeathObserver_; + sptr clientDeathObserver_ = nullptr; ErrCode SaveSubscriber(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); }; } // namespace Sensors -- Gitee From 89db21e7d477e21e8e9aad52d63c3269967fac46 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Thu, 27 Oct 2022 16:25:13 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=B0=86=E7=BB=93=E6=9E=84=E4=BD=93?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=8C=87=E9=92=88=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E4=B8=BAnullptr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I429bb16bd81acf8de557f91175494d2056fa0e93 --- interfaces/native/include/sensor_agent_type.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/native/include/sensor_agent_type.h b/interfaces/native/include/sensor_agent_type.h index 85531a58..78479e4e 100644 --- a/interfaces/native/include/sensor_agent_type.h +++ b/interfaces/native/include/sensor_agent_type.h @@ -126,7 +126,7 @@ typedef struct SensorEvent { int64_t timestamp; /**< Time when sensor data was reported */ uint32_t option; /**< Sensor data options, including the measurement range and accuracy */ int32_t mode; /**< Sensor data reporting mode (described in {@link SensorMode}) */ - uint8_t *data; /**< Sensor data */ + uint8_t *data = nullptr; /**< Sensor data */ uint32_t dataLen; /**< Sensor data length */ } SensorEvent; @@ -154,7 +154,7 @@ typedef struct UserData { typedef struct SensorUser { char name[NAME_MAX_LEN]; /**< Name of the sensor data subscriber */ RecordSensorCallback callback; /**< Callback for reporting sensor data */ - UserData *userData; /**< Reserved field for the sensor data subscriber */ + UserData *userData = nullptr; /**< Reserved field for the sensor data subscriber */ } SensorUser; /** -- Gitee From e3b0116ebfecdb2ca9899af6b00510f2d0b2b5c8 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Fri, 28 Oct 2022 10:21:30 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=9A=84delete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I62f85dfa4c903ab2da48dfc422b7968429e3fdd1 --- interfaces/plugin/include/async_callback_info.h | 4 ---- interfaces/plugin/src/sensor_napi_utils.cpp | 3 --- 2 files changed, 7 deletions(-) diff --git a/interfaces/plugin/include/async_callback_info.h b/interfaces/plugin/include/async_callback_info.h index 2d515f21..94b3512b 100644 --- a/interfaces/plugin/include/async_callback_info.h +++ b/interfaces/plugin/include/async_callback_info.h @@ -121,10 +121,6 @@ public: napi_delete_reference(env, callback[i]); } } - if (work != nullptr) { - delete work; - work = nullptr; - } } private: diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 184edfeb..f8d1cab8 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -486,7 +486,6 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) CHKPV(loop); uv_work_t *work = new(std::nothrow) uv_work_t; CHKPV(work); - asyncCallbackInfo->work = work; asyncCallbackInfo->IncStrongRef(nullptr); work->data = asyncCallbackInfo.GetRefPtr(); int32_t ret = uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { @@ -531,12 +530,10 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) return; } napi_close_handle_scope(asyncCallbackInfo->env, scope); - asyncCallbackInfo->work = nullptr; }); if (ret != 0) { SEN_HILOGE("uv_queue_work fail"); asyncCallbackInfo->DecStrongRef(nullptr); - asyncCallbackInfo->work = nullptr; freeWork(work); } } -- Gitee From 8809cc326a7ea1251cda39cb7408351bf1d3ac41 Mon Sep 17 00:00:00 2001 From: hui1975 Date: Sat, 29 Oct 2022 17:45:12 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B3=9Bsensor=E5=91=8A=E8=AD=A6=E5=8F=8A?= =?UTF-8?q?=E4=B8=93=E9=A1=B9=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hui1975 Change-Id: I22ea7f7e19c5f9e7ad90a84500b851e0831c275a --- utils/include/sensor_basic_data_channel.h | 3 +- utils/src/sensor_basic_data_channel.cpp | 53 ++++++++++++----------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/utils/include/sensor_basic_data_channel.h b/utils/include/sensor_basic_data_channel.h index 30147e6b..e982b929 100644 --- a/utils/include/sensor_basic_data_channel.h +++ b/utils/include/sensor_basic_data_channel.h @@ -42,13 +42,12 @@ public: virtual ~SensorBasicDataChannel(); int32_t CreateSensorBasicChannel(); int32_t CreateSensorBasicChannel(MessageParcel &data); - int32_t DestroySensorBasicChannel(); + void DestroySensorBasicChannel(); int32_t GetSendDataFd() const; int32_t GetReceiveDataFd() const; int32_t SendToBinder(MessageParcel &data); void CloseSendFd(); int32_t SendData(const void *vaddr, size_t size); - int32_t ReceiveData(void *vaddr, size_t size); bool GetSensorStatus() const; void SetSensorStatus(bool isActive); const std::unordered_map &GetDataCacheBuf() const; diff --git a/utils/src/sensor_basic_data_channel.cpp b/utils/src/sensor_basic_data_channel.cpp index 0e7ef077..8a951caf 100755 --- a/utils/src/sensor_basic_data_channel.cpp +++ b/utils/src/sensor_basic_data_channel.cpp @@ -31,6 +31,7 @@ constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorBasicChannel" constexpr int32_t SENSOR_READ_DATA_SIZE = sizeof(SensorEvent) * 100; constexpr int32_t DEFAULT_CHANNEL_SIZE = 2 * 1024; constexpr int32_t SOCKET_PAIR_SIZE = 2; +constexpr int32_t SEND_RETRY_LIMIT = 32; } // namespace SensorBasicDataChannel::SensorBasicDataChannel() : sendFd_(-1), receiveFd_(-1), isActive_(false) @@ -54,37 +55,48 @@ int32_t SensorBasicDataChannel::CreateSensorBasicChannel() receiveFd_ = -1; return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } + auto closeFdWhenErr = [] (int32_t &fd){ + close(fd); + fd = -1; + }; + // set socket attr - int32_t ret = setsockopt(socketPair[0], SOL_SOCKET, SO_SNDBUF, &SENSOR_READ_DATA_SIZE, - sizeof(SENSOR_READ_DATA_SIZE)); + int32_t = ret = setsockopt(socketPair[0], SOL_SOCKET, SO_SNDBUF, &SENSOR_READ_DATA_SIZE, + sizeof(SENSOR_READ_DATA_SIZE)); if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 0 failed"); + closeFdWhenErr(socketPair[0]); + SEN_HILOGE("setsockopt socketpair 0 failed, ret:%{public}d, errno:%{public}d", ret, errno); return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } ret = setsockopt(socketPair[1], SOL_SOCKET, SO_RCVBUF, &SENSOR_READ_DATA_SIZE, sizeof(SENSOR_READ_DATA_SIZE)); if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 1 failed"); + closeFdWhenErr(socketPair[1]); + SEN_HILOGE("setsockopt socketpair 1 failed, ret:%{public}d, errno:%{public}d", ret, errno); return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } int32_t bufferSize = DEFAULT_CHANNEL_SIZE; ret = setsockopt(socketPair[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize)); if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 0 failed"); + closeFdWhenErr(socketPair[0]); + SEN_HILOGE("setsockopt socketpair 0 failed, ret:%{public}d, errno:%{public}d", ret, errno); return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } ret = setsockopt(socketPair[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize)); if (ret != 0) { - SEN_HILOGE("setsockopt socketpair 1 failed"); + closeFdWhenErr(socketPair[1]); + SEN_HILOGE("setsockopt socketpair 1 failed, ret:%{public}d, errno:%{public}d", ret, errno); return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } ret = fcntl(socketPair[0], F_SETFL, O_NONBLOCK); if (ret != 0) { - SEN_HILOGE("fcntl socketpair 0 failed"); + closeFdWhenErr(socketPair[0]); + SEN_HILOGE("fcntl socketpair 0 failed, ret:%{public}d, errno:{public}d", ret, errno); return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } ret = fcntl(socketPair[1], F_SETFL, O_NONBLOCK); if (ret != 0) { - SEN_HILOGE("fcntl socketpair 1 failed"); + closeFdWhenErr(socketPair[1]); + SEN_HILOGE("fcntl socketpair 1 failed, ret:%{public}d, errno:{public}", ret, errno); return SENSOR_CHANNEL_SOCKET_CREATE_ERR; } sendFd_ = socketPair[0]; @@ -96,7 +108,7 @@ int32_t SensorBasicDataChannel::CreateSensorBasicChannel() int32_t SensorBasicDataChannel::CreateSensorBasicChannel(MessageParcel &data) { CALL_LOG_ENTER; - if ((sendFd_ != -1) || (receiveFd_ != -1)) { + if (sendFd_ != -1) { SEN_HILOGD("already create socketpair"); return ERR_OK; } @@ -149,13 +161,15 @@ int32_t SensorBasicDataChannel::SendData(const void *vaddr, size_t size) { CHKPR(vaddr, SENSOR_CHANNEL_SEND_ADDR_ERR); if (sendFd_ < 0) { - SEN_HILOGE("failed, param is invalid"); + SEN_HILOGE("failed, sendFd is invalid"); return SENSOR_CHANNEL_SEND_ADDR_ERR; } - ssize_t length; + ssize_t length = 0; + int32_t retryCount = 0; do { length = send(sendFd_, vaddr, size, MSG_DONTWAIT | MSG_NOSIGNAL); - } while (errno == EINTR); + retryNum++; + } while (retryNum <= SEND_RETRY_LIMIT && errno == EINTR); if (length < 0) { HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::SENSOR, "SENSOR_DATA_CHANNEL_EXCEPTION", HiSysEvent::EventType::FAULT, "PKG_NAME", "SendData", "ERROR_CODE", errno); @@ -165,18 +179,6 @@ int32_t SensorBasicDataChannel::SendData(const void *vaddr, size_t size) return ERR_OK; } -int32_t SensorBasicDataChannel::ReceiveData(void *vaddr, size_t size) -{ - CHKPR(vaddr, SENSOR_CHANNEL_SEND_ADDR_ERR); - SEN_HILOGE("failed, vaddr is null or receiveFd_ invalid"); - return SENSOR_CHANNEL_RECEIVE_ADDR_ERR; - ssize_t length; - do { - length = recv(receiveFd_, vaddr, size, MSG_DONTWAIT); - } while (errno == EINTR); - return length; -} - int32_t SensorBasicDataChannel::GetSendDataFd() const { return sendFd_; @@ -187,7 +189,7 @@ int32_t SensorBasicDataChannel::GetReceiveDataFd() const return receiveFd_; } -int32_t SensorBasicDataChannel::DestroySensorBasicChannel() +void SensorBasicDataChannel::DestroySensorBasicChannel() { if (sendFd_ >= 0) { close(sendFd_); @@ -199,7 +201,6 @@ int32_t SensorBasicDataChannel::DestroySensorBasicChannel() receiveFd_ = -1; SEN_HILOGD("close receiveFd_ success"); } - return ERR_OK; } const std::unordered_map &SensorBasicDataChannel::GetDataCacheBuf() const -- Gitee