diff --git a/frameworks/native/light/src/light_client.cpp b/frameworks/native/light/src/light_client.cpp index de77025ef4605a9695247d1112917b376c2d7c81..44dd0d6b764041c467d998f242d9e1b4e2e5bcd3 100644 --- a/frameworks/native/light/src/light_client.cpp +++ b/frameworks/native/light/src/light_client.cpp @@ -147,6 +147,7 @@ int32_t LightClient::TurnOn(int32_t lightId, const LightColor &color, const Ligh MISC_HILOGE("animation is invalid"); return PARAMETER_ERROR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); LightAnimationIPC animationIPC; animationIPC.SetMode(animation.mode); @@ -162,6 +163,7 @@ int32_t LightClient::TurnOff(int32_t lightId) MISC_HILOGE("lightId is invalid, lightId:%{public}d", lightId); return LIGHT_ID_NOT_SUPPORT; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); return miscdeviceProxy_->TurnOff(lightId); } @@ -170,7 +172,10 @@ void LightClient::ProcessDeathObserver(wptr object) { CALL_LOG_ENTER; (void)object; - miscdeviceProxy_ = nullptr; + { + std::lock_guard clientLock(clientMutex_); + miscdeviceProxy_ = nullptr; + } auto ret = InitLightClient(); if (ret != ERR_OK) { MISC_HILOGE("InitLightClient failed, ret:%{public}d", ret); diff --git a/frameworks/native/vibrator/src/vibrator_service_client.cpp b/frameworks/native/vibrator/src/vibrator_service_client.cpp index dc0e8d7499563f08e7d256175932f303422c62a5..5694af53ea690a3eabf379e79e414d04c5a4066b 100644 --- a/frameworks/native/vibrator/src/vibrator_service_client.cpp +++ b/frameworks/native/vibrator/src/vibrator_service_client.cpp @@ -128,6 +128,7 @@ int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, int32_t timeOut, int3 MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "VibrateTime"); ret = miscdeviceProxy_->Vibrate(vibratorId, timeOut, usage, systemUsage); @@ -148,6 +149,7 @@ int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, const std::string &ef MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "VibrateEffect"); ret = miscdeviceProxy_->PlayVibratorEffect(vibratorId, effect, loopCount, usage, systemUsage); @@ -170,6 +172,7 @@ int32_t VibratorServiceClient::PlayVibratorCustom(int32_t vibratorId, const RawF MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "PlayVibratorCustom"); VibrateParameter vibateParameter = { @@ -193,6 +196,7 @@ int32_t VibratorServiceClient::StopVibrator(int32_t vibratorId, const std::strin MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "StopVibratorByMode"); ret = miscdeviceProxy_->StopVibrator(vibratorId, mode); @@ -211,6 +215,7 @@ int32_t VibratorServiceClient::StopVibrator(int32_t vibratorId) MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "StopVibratorAll"); ret = miscdeviceProxy_->StopVibrator(vibratorId); @@ -224,6 +229,11 @@ int32_t VibratorServiceClient::StopVibrator(int32_t vibratorId) bool VibratorServiceClient::IsHdHapticSupported() { CALL_LOG_ENTER; + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return MISC_NATIVE_GET_SERVICE_ERR; + } return capacity_.isSupportHdHaptic; } @@ -235,6 +245,7 @@ int32_t VibratorServiceClient::IsSupportEffect(const std::string &effect, bool & MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "VibrateEffect"); ret = miscdeviceProxy_->IsSupportEffect(effect, state); @@ -249,7 +260,10 @@ void VibratorServiceClient::ProcessDeathObserver(const wptr &obje { CALL_LOG_ENTER; (void)object; - miscdeviceProxy_ = nullptr; + { + std::lock_guard clientLock(clientMutex_); + miscdeviceProxy_ = nullptr; + } int32_t ret = InitServiceClient(); if (ret != ERR_OK) { MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); @@ -324,6 +338,7 @@ int32_t VibratorServiceClient::GetDelayTime(int32_t &delayTime) MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "GetDelayTime"); ret = miscdeviceProxy_->GetDelayTime(delayTime); @@ -343,7 +358,6 @@ int32_t VibratorServiceClient::PlayPattern(const VibratorPattern &pattern, int32 MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } - CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "PlayPattern"); VibratePattern vibratePattern = {}; vibratePattern.startTime = pattern.time; @@ -377,6 +391,8 @@ int32_t VibratorServiceClient::PlayPattern(const VibratorPattern &pattern, int32 .intensity = parameter.intensity, .frequency = parameter.frequency }; + std::lock_guard clientLock(clientMutex_); + CHKPR(miscdeviceProxy_, ERROR); ret = miscdeviceProxy_->PlayPattern(vibratePattern, usage, systemUsage, vibateParameter); FinishTrace(HITRACE_TAG_SENSORS); if (ret != ERR_OK) { @@ -477,6 +493,7 @@ int32_t VibratorServiceClient::PlayPrimitiveEffect(int32_t vibratorId, const std MISC_HILOGE("InitServiceClient failed, ret:%{public}d", ret); return MISC_NATIVE_GET_SERVICE_ERR; } + std::lock_guard clientLock(clientMutex_); CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "PlayPrimitiveEffect"); ret = miscdeviceProxy_->PlayPrimitiveEffect(vibratorId, effect, intensity, usage, systemUsage, count); diff --git a/utils/common/src/file_utils.cpp b/utils/common/src/file_utils.cpp index 929255aaebaf23f842cf01a48fe2238a5f895775..3bf532e0fb142e16e6f00e91e304cb2ebcf07d2d 100755 --- a/utils/common/src/file_utils.cpp +++ b/utils/common/src/file_utils.cpp @@ -108,13 +108,13 @@ int32_t GetFileName(const int32_t &fd, std::string &fileName) char buf[FILE_PATH_MAX] = {'\0'}; char filePath[FILE_PATH_MAX] = {'\0'}; - int ret = snprintf_s(buf, sizeof(buf), sizeof(buf), "/proc/self/fd/%d", fd); + int ret = snprintf_s(buf, sizeof(buf), (sizeof(buf) - 1), "/proc/self/fd/%d", fd); if (ret < 0) { MISC_HILOGE("snprintf failed with %{public}d", errno); return ERROR; } - ret = readlink(buf, filePath, FILE_PATH_MAX); + ret = readlink(buf, filePath, FILE_PATH_MAX - 1); if (ret < 0 || ret >= FILE_PATH_MAX) { MISC_HILOGE("readlink failed with %{public}d", errno); return ERROR;