diff --git a/device_info/BUILD.gn b/device_info/BUILD.gn index 5f19102d8dd1ed1681a57e9e4d30d0384dea7e19..49cffae2bec1be811b4effdc96547aa9b32692cb 100644 --- a/device_info/BUILD.gn +++ b/device_info/BUILD.gn @@ -31,6 +31,13 @@ ohos_shared_library("deviceinfoservice") { "device_info_stub.cpp", ] + defines = [ + "INIT_AGENT", + "INIT_FILE", + "_GNU_SOURCE", + "USE_MBEDTLS", + ] + include_dirs = [ ".", "//base/startup/init_lite/services/include/param", @@ -43,7 +50,7 @@ ohos_shared_library("deviceinfoservice") { deps = [ "//base/startup/init_lite/interfaces/innerkits:libbegetutil", "//third_party/bounds_checking_function:libsec_shared", - "//third_party/openssl:libcrypto_shared", + "//third_party/mbedtls:mbedtls_shared", ] external_deps = [ diff --git a/device_info/device_info_kits.cpp b/device_info/device_info_kits.cpp index dd688ef99f86cc0860ded23d06f7fc82ebdf0003..8ab4f081ca1f25fdf92274c1204299e017f177d6 100644 --- a/device_info/device_info_kits.cpp +++ b/device_info/device_info_kits.cpp @@ -16,6 +16,7 @@ #include "beget_ext.h" #include "device_info_proxy.h" +#include "device_info_load.h" #include "idevice_info.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" @@ -33,59 +34,66 @@ DeviceInfoKits &DeviceInfoKits::GetInstance() return DelayedRefSingleton::GetInstance(); } -void DeviceInfoKits::ResetService(const wptr &remote) +void DeviceInfoKits::LoadDeviceInfoSa() { - std::lock_guard lock(lock_); - if (deviceInfoService_ != nullptr) { - sptr object = deviceInfoService_->AsObject(); - if ((object != nullptr) && (remote == object)) { - object->RemoveDeathRecipient(deathRecipient_); - deviceInfoService_ = nullptr; - } + DINFO_LOGV("deviceInfoService_ is %d", deviceInfoService_ == nullptr); + + auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + DINFO_CHECK(sam != nullptr, return, "GetSystemAbilityManager return null"); + + sptr deviceInfoLoad = new (std::nothrow) DeviceInfoLoad(); + DINFO_CHECK(deviceInfoLoad != nullptr, return, "new deviceInfoLoad fail."); + + int32_t ret = sam->LoadSystemAbility(SYSPARAM_DEVICE_SERVICE_ID, deviceInfoLoad); + DINFO_CHECK(ret == ERR_OK, return, "LoadSystemAbility deviceinfo sa failed"); + + std::unique_lock lock(lock_); + // wait_for release lock and block until time out(60s) or match the condition with notice + auto waitStatus = deviceInfoLoadCon_.wait_for(lock, std::chrono::milliseconds(DEVICEINFO_LOAD_SA_TIMEOUT_MS), + [this]() { return deviceInfoService_ != nullptr; }); + if (!waitStatus) { + // time out or loadcallback fail + DINFO_LOGE("tokensync load sa timeout"); + return; } } sptr DeviceInfoKits::GetService() { - std::lock_guard lock(lock_); - if (deviceInfoService_ != nullptr) { - return deviceInfoService_; - } + LoadDeviceInfoSa(); + return deviceInfoService_; +} - sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - DINFO_CHECK(samgr != nullptr, return nullptr, "Get samgr failed"); - sptr object = samgr->GetSystemAbility(SYSPARAM_DEVICE_SERVICE_ID); - DINFO_CHECK(object != nullptr, return nullptr, "Get device service object from samgr failed"); - if (deathRecipient_ == nullptr) { - deathRecipient_ = new DeathRecipient(); - } +void DeviceInfoKits::FinishStartSASuccess(const sptr &remoteObject) +{ + DINFO_LOGI("get deviceinfo sa success."); + deviceInfoService_ = iface_cast(remoteObject); - if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) { - DINFO_LOGE("Failed to add death recipient"); - } - deviceInfoService_ = iface_cast(object); - if (deviceInfoService_ == nullptr) { - DINFO_LOGE("device service iface_cast failed"); - } - return deviceInfoService_; + // get lock which wait_for release and send a notice so that wait_for can out of block + std::unique_lock lock(lock_); + deviceInfoLoadCon_.notify_one(); } -void DeviceInfoKits::DeathRecipient::OnRemoteDied(const wptr &remote) +void DeviceInfoKits::FinishStartSAFailed() { - DelayedRefSingleton::GetInstance().ResetService(remote); + DINFO_LOGI("get tokensync sa failed."); + + // get lock which wait_for release and send a notice + std::unique_lock lock(lock_); + deviceInfoLoadCon_.notify_one(); } int32_t DeviceInfoKits::GetUdid(std::string& result) { auto deviceService = GetService(); - DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get watcher manager"); + DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager"); return deviceService->GetUdid(result); } int32_t DeviceInfoKits::GetSerialID(std::string& result) { auto deviceService = GetService(); - DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get watcher manager"); + DINFO_CHECK(deviceService != nullptr, return -1, "Failed to get deviceinfo manager"); return deviceService->GetSerialID(result); } } // namespace device_info diff --git a/device_info/device_info_kits.h b/device_info/device_info_kits.h index e9aace73eb3a3daa17521265efffd529371df514..940ab5010dd4e14eb9bc6d21eb9a3c2f70856cbf 100644 --- a/device_info/device_info_kits.h +++ b/device_info/device_info_kits.h @@ -29,26 +29,16 @@ public: static DeviceInfoKits &GetInstance(); int32_t GetUdid(std::string& result); int32_t GetSerialID(std::string& result); - void ResetService(const wptr &remote); - - // For death event procession - class DeathRecipient final : public IRemoteObject::DeathRecipient { - public: - DeathRecipient() = default; - ~DeathRecipient() final = default; - DISALLOW_COPY_AND_MOVE(DeathRecipient); - void OnRemoteDied(const wptr &remote) final; - }; - sptr GetDeathRecipient() - { - return deathRecipient_; - } + void FinishStartSASuccess(const sptr &remoteObject); + void FinishStartSAFailed(); private: + static const int DEVICEINFO_LOAD_SA_TIMEOUT_MS = 60000; + void LoadDeviceInfoSa(); sptr GetService(); std::mutex lock_; + std::condition_variable deviceInfoLoadCon_; sptr deviceInfoService_ {}; - sptr deathRecipient_ {}; }; } // namespace device_info } // namespace OHOS diff --git a/device_info/device_info_load.cpp b/device_info/device_info_load.cpp new file mode 100755 index 0000000000000000000000000000000000000000..4aa7fec53f775ae0195dacf5d085ffd7e4a1111c --- /dev/null +++ b/device_info/device_info_load.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_info_load.h" + +#include "device_info_kits.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace device_info { +DeviceInfoLoad::DeviceInfoLoad() +{ +} + +void DeviceInfoLoad::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + DINFO_CHECK(systemAbilityId == SYSPARAM_DEVICE_SERVICE_ID, return, + "start aystemabilityId is not deviceinfo! %d", systemAbilityId); + DINFO_CHECK(remoteObject != nullptr, return, "remoteObject is null."); + + DINFO_LOGI("OnLoadSystemAbilitySuccess start systemAbilityId: %d success!", systemAbilityId); + DeviceInfoKits::GetInstance().FinishStartSASuccess(remoteObject); +} + +void DeviceInfoLoad::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + DINFO_CHECK(systemAbilityId == SYSPARAM_DEVICE_SERVICE_ID, return, + "start aystemabilityId is not deviceinfo! %d", systemAbilityId); + + DINFO_LOGI("OnLoadSystemAbilityFail systemAbilityId: %d failed.", systemAbilityId); + + DeviceInfoKits::GetInstance().FinishStartSAFailed(); +} +} // namespace device_info +} // namespace OHOS diff --git a/device_info/device_info_load.h b/device_info/device_info_load.h new file mode 100755 index 0000000000000000000000000000000000000000..9cf74126f2188715e887902dc1531112aae5f176 --- /dev/null +++ b/device_info/device_info_load.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEVICE_INFO_LOAD_H +#define DEVICE_INFO_LOAD_H + +#include "system_ability_load_callback_stub.h" +#include "idevice_info.h" + +namespace OHOS { +namespace device_info { +class DeviceInfoLoad : public SystemAbilityLoadCallbackStub { +public: + explicit DeviceInfoLoad(); + + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject); + void OnLoadSystemAbilityFail(int32_t systemAbilityId); +private: +}; +} // namespace device_info +} // namespace OHOS +#endif // DEVICE_INFO_LOAD_H diff --git a/device_info/etc/deviceinfoservice.cfg b/device_info/etc/deviceinfoservice.cfg index a57c172a98bbeb5fe89b26a9c8a2de1fb21a3b19..7b0b2b782ac9ef7253eec3d4f5bea3de2b0de47d 100755 --- a/device_info/etc/deviceinfoservice.cfg +++ b/device_info/etc/deviceinfoservice.cfg @@ -15,6 +15,7 @@ "jobs" : { "on-start" : "services:deviceinfoservice" }, + "ondemand" : true, "secon" : "u:r:deviceinfoservice:s0" } ] diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index b156ce2a9641bf28006fc75a19e88501c9912a72..d63f5195d4117781c00ff61317e99b856f1032c2 100755 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -231,6 +231,7 @@ if (defined(ohos_lite)) { if (enable_ohos_startup_init_feature_deviceinfo) { sources += [ "//base/startup/init_lite/device_info/device_info_kits.cpp", + "//base/startup/init_lite/device_info/device_info_load.cpp", "//base/startup/init_lite/device_info/device_info_proxy.cpp", ] defines += [ "PARAM_FEATURE_DEVICEINFO" ] diff --git a/services/begetctl/BUILD.gn b/services/begetctl/BUILD.gn index 0ea66b741f9c399902a31285bb5689636bad7cbd..3af24cae07a9c9698dfd304b1c3167938364a0fe 100755 --- a/services/begetctl/BUILD.gn +++ b/services/begetctl/BUILD.gn @@ -189,7 +189,8 @@ if (defined(ohos_lite)) { deps = [ "//base/startup/init_lite/services/log:agent_log", "//base/startup/init_lite/services/loopevent:loopevent", - "//base/startup/init_lite/services/param:parameter", + "//base/startup/init_lite/services/param/base:parameterbase", + "//base/startup/init_lite/services/param/linux:param_client", "//base/startup/init_lite/services/utils:libinit_utils", "//third_party/bounds_checking_function:libsec_static", ] diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 8f267276ddd9ee8a1e91a24a363cbc2e0725c67c..3f3c947215d85efdebc113569c3fa0b5612ff935 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -263,6 +263,7 @@ ohos_unittest("init_unittest") { if (enable_ohos_startup_init_feature_deviceinfo) { sources += [ "//base/startup/init_lite/device_info/device_info_kits.cpp", + "//base/startup/init_lite/device_info/device_info_load.cpp", "//base/startup/init_lite/device_info/device_info_proxy.cpp", ] defines += [ "PARAM_FEATURE_DEVICEINFO" ] diff --git a/test/unittest/deviceinfo/DeviceInfoUnittest.cpp b/test/unittest/deviceinfo/DeviceInfoUnittest.cpp index ea91c078c79aae11c426c2b1601a8dd0e9195264..a30e5b773e31118cb7e2c1b2221e5dd11b790f02 100755 --- a/test/unittest/deviceinfo/DeviceInfoUnittest.cpp +++ b/test/unittest/deviceinfo/DeviceInfoUnittest.cpp @@ -67,11 +67,6 @@ HWTEST_F(DeviceInfoUnittest, GetDevUdidTest, TestSize.Level1) ASSERT_NE(nullptr, samgr); sptr object = samgr->GetSystemAbility(SYSPARAM_DEVICE_SERVICE_ID); ASSERT_NE(nullptr, samgr); - OHOS::device_info::DeviceInfoKits &instance = OHOS::device_info::DeviceInfoKits::GetInstance(); - auto deathRecipientPtr = instance.GetDeathRecipient(); - if (deathRecipientPtr) { - deathRecipientPtr->OnRemoteDied(object); - } } HWTEST_F(DeviceInfoUnittest, StubTest, TestSize.Level1) {