diff --git a/services/sensor/BUILD.gn b/services/sensor/BUILD.gn index 96e9a20a75edd22545c60ad85785a052123699b0..f17756ae0f5ef523df082974972b2d326c7a89d3 100644 --- a/services/sensor/BUILD.gn +++ b/services/sensor/BUILD.gn @@ -46,10 +46,15 @@ ohos_shared_library("libsensor_service") { "hdi_connection/hardware/include", ] - deps = [ "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils" ] + deps = [ + "$SUBSYSTEM_DIR/sensor/utils:libsensor_utils", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + ] external_deps = [ "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", "hisysevent_native:libhisysevent", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/services/sensor/include/client_info.h b/services/sensor/include/client_info.h index 6566048a055bc7ffd13da6dea455a9fd86e23ebd..f2d2e0dbd31f50e56fc003b7096a378a893c334a 100644 --- a/services/sensor/include/client_info.h +++ b/services/sensor/include/client_info.h @@ -73,10 +73,10 @@ public: void UpdateDataQueue(int32_t sensorId, struct SensorEvent &event); std::unordered_map> GetDumpQueue(); void ClearDataQueue(int32_t sensorId); + int32_t GetUidByPid(int32_t pid); private: DISALLOW_COPY_AND_MOVE(ClientInfo); - int32_t GetUidByPid(int32_t pid); std::vector GetCmdList(uint32_t sensorId, int32_t uid); std::mutex clientMutex_; std::mutex channelMutex_; diff --git a/services/sensor/include/sensor_service.h b/services/sensor/include/sensor_service.h index b84b37eec4171fcd53a7302bbe11054c0384b1ad..2f84a54d43ef006ee0db9bec3dccfd2b24ac9c28 100644 --- a/services/sensor/include/sensor_service.h +++ b/services/sensor/include/sensor_service.h @@ -79,7 +79,8 @@ private: bool InitSensorList(); bool InitSensorPolicy(); void ReportOnChangeData(uint32_t sensorId); - void ReportSensorSysEvent(uint32_t sensorId, bool enable); + void ReportSensorSysEvent(uint32_t sensorId, bool enable, int32_t uid); + ErrCode DisableSensor(uint32_t sensorId, int32_t pid); SensorServiceState state_; std::mutex serviceLock_; std::mutex sensorsMutex_; diff --git a/services/sensor/src/sensor_manager.cpp b/services/sensor/src/sensor_manager.cpp index 777834a03bddfce65eb97b8f51d335c9d6094095..ee1bb1bdadf154ac74a48bf4add6c84c4e619f02 100644 --- a/services/sensor/src/sensor_manager.cpp +++ b/services/sensor/src/sensor_manager.cpp @@ -15,9 +15,14 @@ #include "sensor_manager.h" +#include "bundle_mgr_interface.h" +#include "bundle_mgr_client.h" +#include "bundle_mgr_proxy.h" +#include "iservice_registry.h" #include "sensor.h" #include "sensors_errors.h" #include "sensors_log_domain.h" +#include "system_ability_definition.h" namespace OHOS { namespace Sensors { @@ -181,6 +186,16 @@ ErrCode SensorManager::AfterDisableSensor(uint32_t sensorId) void SensorManager::GetPackageNameFromUid(int32_t uid, std::string &packageName) { CALL_LOG_ENTER; + sptr systemMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + CHKPV(systemMgr); + sptr remoteObject = systemMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + CHKPV(remoteObject); + sptr bundleMgrProxy = iface_cast(remoteObject); + CHKPV(bundleMgrProxy); + if (!bundleMgrProxy->GetNameForUid(uid, packageName)) { + SEN_HILOGE("Get bundle name failed"); + return; + } } } // namespace Sensors } // namespace OHOS diff --git a/services/sensor/src/sensor_service.cpp b/services/sensor/src/sensor_service.cpp index 2d5c0f16915b7b11795660fe52b7503a46f53027..5b4501bfd64a5166388f48f4899a31311ae1db83 100644 --- a/services/sensor/src/sensor_service.cpp +++ b/services/sensor/src/sensor_service.cpp @@ -152,10 +152,9 @@ void SensorService::OnStop() } } -void SensorService::ReportSensorSysEvent(uint32_t sensorId, bool enable) +void SensorService::ReportSensorSysEvent(uint32_t sensorId, bool enable, int32_t uid) { char uidChar[REPORT_STATUS_LEN] = {0}; - int32_t uid = this->GetCallingUid(); std::string packageName(""); sensorManager_.GetPackageNameFromUid(uid, packageName); int32_t ret = sprintf_s(uidChar, sizeof(uidChar) - 1, "%d", uid); @@ -169,13 +168,13 @@ void SensorService::ReportSensorSysEvent(uint32_t sensorId, bool enable) if (enable) { // define in LogPower.java, 500 stand for enable sensor message.append("uid : ").append(std::to_string(uid)).append(" pkgName : ").append(packageName) - .append(" type : "); + .append(" type : ").append(std::to_string(sensorId)); HiSysEvent::Write(HiSysEvent::Domain::SENSORS, "EnableSensor", HiSysEvent::EventType::FAULT, "LEVEL", logLevel, "TAG", "DUBAI_TAG_HSENSOR_ENABLE", "MESSAGE", message); } else { // define in LogPower.java, 501 stand for disable sensor message.append("uid : ").append(std::to_string(uid)).append(" pkgName : ").append(packageName) - .append(" type : "); + .append(" type : ").append(std::to_string(sensorId)); HiSysEvent::Write(HiSysEvent::Domain::SENSORS, "DisableSensor", HiSysEvent::EventType::FAULT, "LEVEL", logLevel, "TAG", "DUBAI_TAG_HSENSOR_DISABLE", "MESSAGE", message); } @@ -235,7 +234,8 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, SEN_HILOGE("sensorId is 0 or maxReportDelayNs exceeded the maximum value"); return ERR_NO_INIT; } - ReportSensorSysEvent(sensorId, true); + int32_t pid = this->GetCallingPid(); + int32_t uid = clientInfo_.GetUidByPid(pid); std::lock_guard serviceLock(serviceLock_); if (clientInfo_.GetSensorState(sensorId)) { SEN_HILOGW("sensor has been enabled already"); @@ -244,8 +244,8 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, SEN_HILOGE("SaveSubscriber failed"); return ret; } + ReportSensorSysEvent(sensorId, true, uid); uint32_t flag = sensorManager_.GetSensorFlag(sensorId); - int32_t pid = this->GetCallingPid(); ret = flushInfo_.FlushProcess(sensorId, flag, pid, true); if (ret != ERR_OK) { SEN_HILOGE("ret : %{public}d", ret); @@ -266,29 +266,25 @@ ErrCode SensorService::EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, clientInfo_.RemoveSubscriber(sensorId, this->GetCallingPid()); return ENABLE_SENSOR_ERR; } - + ReportSensorSysEvent(sensorId, true, uid); return ret; } -ErrCode SensorService::DisableSensor(uint32_t sensorId) +ErrCode SensorService::DisableSensor(uint32_t sensorId, int32_t pid) { CALL_LOG_ENTER; if (sensorId == INVALID_SENSOR_ID) { SEN_HILOGE("sensorId is invalid"); return ERR_NO_INIT; } - ReportSensorSysEvent(sensorId, false); - std::lock_guard serviceLock(serviceLock_); - const int32_t clientPid = this->GetCallingPid(); - if (clientPid < 0) { - SEN_HILOGE("clientPid is invalid, clientPid : %{public}d", clientPid); + if (pid < 0) { + SEN_HILOGE("pid is invalid, pid : %{public}d", pid); return CLIENT_PID_INVALID_ERR; } - if (!clientInfo_.GetSensorState(sensorId)) { - SEN_HILOGE("sensor should be enabled first"); - return DISABLE_SENSOR_ERR; - } - if (sensorManager_.IsOtherClientUsingSensor(sensorId, clientPid)) { + int32_t uid = clientInfo_.GetUidByPid(pid); + ReportSensorSysEvent(sensorId, false, uid); + std::lock_guard serviceLock(serviceLock_); + if (sensorManager_.IsOtherClientUsingSensor(sensorId, pid)) { SEN_HILOGW("other client is using this sensor now, cannot disable"); return ERR_OK; } @@ -296,11 +292,17 @@ ErrCode SensorService::DisableSensor(uint32_t sensorId) SEN_HILOGE("DisableSensor is failed"); return DISABLE_SENSOR_ERR; } - clientInfo_.DestroyCmd(this->GetCallingUid()); + clientInfo_.DestroyCmd(uid); clientInfo_.ClearDataQueue(sensorId); return sensorManager_.AfterDisableSensor(sensorId); } +ErrCode SensorService::DisableSensor(uint32_t sensorId) +{ + CALL_LOG_ENTER; + return DisableSensor(sensorId, this->GetCallingPid()); +} + int32_t SensorService::GetSensorState(uint32_t sensorId) { if (sensorId == INVALID_SENSOR_ID) { @@ -403,10 +405,17 @@ void SensorService::ProcessDeathObserver(const wptr &object) SEN_HILOGE("pid is -1"); return; } - SEN_HILOGI("pid is %d", pid); + SEN_HILOGI("pid is %{pubilc}d", pid); + std::vector activeSensors = clientInfo_.GetSensorIdByPid(pid); + for (size_t i = 0; i < activeSensors.size(); ++i) { + int32_t ret = DisableSensor(activeSensors[i], pid); + if (ret != ERR_OK) { + SEN_HILOGE("disablesensor failed, ret : %{pubilc}d", ret); + } + } clientInfo_.DestroySensorChannel(pid); clientInfo_.DestroyClientPid(client); - clientInfo_.DestroyCmd(this->GetCallingUid()); + clientInfo_.DestroyCmd(clientInfo_.GetUidByPid(pid)); } void SensorService::RegisterClientDeathRecipient(sptr sensorClient, int32_t pid)