diff --git a/sensor.gni b/sensor.gni index addc8d5221b745b3a961df3d23d9f203ebfe6ca5..ed130bf76227f559cbda20917c82230472dd7efc 100644 --- a/sensor.gni +++ b/sensor.gni @@ -42,6 +42,13 @@ if (!defined(global_parts_info) || sensor_memmgr_enable = false } +if (!defined(global_parts_info) || + defined(global_parts_info.security_access_token)) { + sensor_access_token_enable = true +} else { + sensor_access_token_enable = false +} + if (build_variant == "root") { sensor_default_defines += [ "BUILD_VARIANT_ENG" ] sensor_build_eng = true diff --git a/services/BUILD.gn b/services/BUILD.gn index e2bca24f62e243491ff1caa5b6fb9a8ac66c39ac..d3109445bf83bd3b1626a618b843ac3bcc72843e 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -71,6 +71,10 @@ ohos_shared_library("libsensor_service") { external_deps += [ "memmgr:memmgrclient" ] } + if (sensor_access_token_enable) { + defines += [ "ACCESS_TOKEN_ENABLE" ] + } + if (hdf_drivers_interface_sensor) { sources += [ "hdi_connection/adapter/src/hdi_connection.cpp", @@ -160,6 +164,10 @@ ohos_static_library("libsensor_service_static") { external_deps += [ "memmgr:memmgrclient" ] } + if (sensor_access_token_enable) { + defines += [ "ACCESS_TOKEN_ENABLE" ] + } + if (hdf_drivers_interface_sensor) { sources += [ "hdi_connection/adapter/src/hdi_connection.cpp", diff --git a/services/include/sensor_manager.h b/services/include/sensor_manager.h index c5e8d031f588d63960960dbc413ed69b7accc7a4..f1b1075da385fce9b3d71450798ccbce86df67f6 100644 --- a/services/include/sensor_manager.h +++ b/services/include/sensor_manager.h @@ -47,7 +47,7 @@ public: SensorBasicInfo GetSensorInfo(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); bool IsOtherClientUsingSensor(int32_t sensorId, int32_t clientPid); ErrCode AfterDisableSensor(int32_t sensorId); - void GetPackageName(AccessTokenID tokenId, std::string &packageName); + void GetPackageName(AccessTokenID tokenId, std::string &packageName, bool isAccessTokenServiceActive = false); private: #ifdef HDF_DRIVERS_INTERFACE_SENSOR @@ -60,6 +60,6 @@ private: std::unordered_map sensorMap_; std::mutex sensorMapMutex_; }; -} // namespace Sensors -} // namespace OHOS -#endif // SENSOR_MANAGER_H +} // namespace Sensors +} // namespace OHOS +#endif // SENSOR_MANAGER_H diff --git a/services/include/sensor_service.h b/services/include/sensor_service.h index d493aab8462c93af253b3f2f92b94a30812c06d3..014cf1e3c44193802a59f94cc2b7ea39ef60c50f 100644 --- a/services/include/sensor_service.h +++ b/services/include/sensor_service.h @@ -70,6 +70,7 @@ public: private: DISALLOW_COPY_AND_MOVE(SensorService); void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; bool CheckParameter(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); class PermStateChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize { @@ -116,6 +117,7 @@ private: std::shared_ptr permStateChangeCb_ = nullptr; ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); std::atomic_bool isReportActiveInfo_ = false; + static std::atomic_bool isAccessTokenServiceActive_; }; #define POWER_POLICY SensorPowerPolicy::GetInstance() diff --git a/services/src/sensor_manager.cpp b/services/src/sensor_manager.cpp index cf079eba4fd40a75a4bfd4d0fd3018fc1b4238f1..7118ebebc5e940023b940273680aaf73b149f7de 100644 --- a/services/src/sensor_manager.cpp +++ b/services/src/sensor_manager.cpp @@ -187,9 +187,13 @@ ErrCode SensorManager::AfterDisableSensor(int32_t sensorId) return ERR_OK; } -void SensorManager::GetPackageName(AccessTokenID tokenId, std::string &packageName) +void SensorManager::GetPackageName(AccessTokenID tokenId, std::string &packageName, bool isAccessTokenServiceActive) { CALL_LOG_ENTER; + if (!isAccessTokenServiceActive) { + SEN_HILOGE("Access token service is inactive"); + return; + } int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId); switch (tokenType) { case ATokenTypeEnum::TOKEN_HAP: { diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index 408dc84a1bc6960ed29e7fa0386b1322cfecfb6b..aa0b5e465fa1000828b49f70079b88ecc45c1f18 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -48,6 +48,8 @@ constexpr int64_t MAX_EVENT_COUNT = 1000; std::atomic_bool g_isRegister = false; } // namespace +std::atomic_bool SensorService::isAccessTokenServiceActive_ = false; + SensorService::SensorService() : SystemAbility(SENSOR_SERVICE_ABILITY_ID, true), state_(SensorServiceState::STATE_STOPPED) { @@ -70,6 +72,21 @@ void SensorService::OnAddSystemAbility(int32_t systemAbilityId, const std::strin PROCESS_TYPE_SA, PROCESS_STATUS_STARTED, SENSOR_SERVICE_ABILITY_ID); } #endif // MEMMGR_ENABLE +#ifdef ACCESS_TOKEN_ENABLE + if (systemAbilityId == ACCESS_TOKEN_MANAGER_SERVICE_ID) { + isAccessTokenServiceActive_ = true; + } +#endif // ACCESS_TOKEN_ENABLE +} + +void SensorService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + SEN_HILOGI("OnRemoveSystemAbility systemAbilityId:%{public}d", systemAbilityId); +#ifdef ACCESS_TOKEN_ENABLE + if (systemAbilityId == ACCESS_TOKEN_MANAGER_SERVICE_ID) { + isAccessTokenServiceActive_ = false; + } +#endif // ACCESS_TOKEN_ENABLE } void SensorService::OnStart() @@ -108,6 +125,9 @@ void SensorService::OnStart() #ifdef MEMMGR_ENABLE AddSystemAbilityListener(MEMORY_MANAGER_SA_ID); #endif // MEMMGR_ENABLE +#ifdef ACCESS_TOKEN_ENABLE + AddSystemAbilityListener(ACCESS_TOKEN_MANAGER_SERVICE_ID); +#endif // ACCESS_TOKEN_ENABLE } #ifdef HDF_DRIVERS_INTERFACE_SENSOR @@ -185,7 +205,7 @@ void SensorService::ReportSensorSysEvent(int32_t sensorId, bool enable, int32_t { std::string packageName(""); AccessTokenID tokenId = clientInfo_.GetTokenIdByPid(pid); - sensorManager_.GetPackageName(tokenId, packageName); + sensorManager_.GetPackageName(tokenId, packageName, isAccessTokenServiceActive_); const int logLevel = 4; int32_t uid = clientInfo_.GetUidByPid(pid); if (enable) {