diff --git a/bundle.json b/bundle.json index 5b576220296ae3be45166517146e1e3938a8135e..dc4fa75f140c09443ccbc350a1f64e34f670e631 100644 --- a/bundle.json +++ b/bundle.json @@ -23,6 +23,7 @@ "components": [ "ability_runtime", "bundle_framework", + "dataclassification", "dsoftbus", "ipc", "hilog", diff --git a/services/distributedfiledaemon/BUILD.gn b/services/distributedfiledaemon/BUILD.gn index 798fc595c782e6906cd5631c3d8bcba3c2a60d4b..7a2313b34a755878fd72e9cc45e1c49bc2de7842 100755 --- a/services/distributedfiledaemon/BUILD.gn +++ b/services/distributedfiledaemon/BUILD.gn @@ -29,6 +29,7 @@ ohos_shared_library("libdistributedfiledaemon") { "src/mountpoint/mount_manager.cpp", "src/mountpoint/mount_point.cpp", "src/multiuser/os_account_observer.cpp", + "src/network/devsl_dispatcher.cpp", "src/network/kernel_talker.cpp", "src/network/network_agent_template.cpp", "src/network/session_pool.cpp", @@ -43,6 +44,7 @@ ohos_shared_library("libdistributedfiledaemon") { "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", ] external_deps = [ + "dataclassification:data_transit_mgr", "dsoftbus_standard:softbus_client", "ipc:ipc_core", "os_account_standard:os_account_innerkits", diff --git a/services/distributedfiledaemon/include/network/devsl_dispatcher.h b/services/distributedfiledaemon/include/network/devsl_dispatcher.h new file mode 100644 index 0000000000000000000000000000000000000000..a789517382396258bdf6165b10a2be8dafdabcab --- /dev/null +++ b/services/distributedfiledaemon/include/network/devsl_dispatcher.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 DEVSL_DISPATCHER_H +#define DEVSL_DISPATCHER_H + +#include +#include +#include +#include +#include +#include "dev_slinfo_mgr.h" +#include "device/device_manager_agent.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +class DevslDispatcher final { +public: + DevslDispatcher() = delete; + ~DevslDispatcher() = delete; + static int Start(); + static void Stop(); + static void DevslGottonCallback(DEVSLQueryParams *queryParams, int32_t result, uint32_t levelInfo); + static uint32_t DevslGetRegister(const std::string &cid, std::weak_ptr); +private: + static DEVSLQueryParams MakeDevslQueryParams(const std::string &cid); + static void DevslGottonCallbackAsync(const std::string udid, uint32_t devsl); + static std::map>> talkersMap_; + static std::map idMap_; + static std::mutex mutex; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // DSL_DISPATCHER_H \ No newline at end of file diff --git a/services/distributedfiledaemon/include/network/kernel_talker.h b/services/distributedfiledaemon/include/network/kernel_talker.h index 89f06f10aa72828e64ba8f05b621c08e6bc77ec1..d549605543863beb72144feea3d273ec9c6b5fba 100644 --- a/services/distributedfiledaemon/include/network/kernel_talker.h +++ b/services/distributedfiledaemon/include/network/kernel_talker.h @@ -43,7 +43,7 @@ struct NotifyParam { char remoteCid[CID_MAX_LEN]; } __attribute__((packed)); -class KernelTalker final : protected NoCopyable { +class KernelTalker final : protected NoCopyable, public std::enable_shared_from_this { public: explicit KernelTalker(std::weak_ptr mountPoint, std::function getSessionCallback, @@ -55,6 +55,7 @@ public: ~KernelTalker() = default; void SinkSessionTokernel(std::shared_ptr session); + void SinkDevslTokernel(const std::string &cid, uint32_t devsl); void SinkOfflineCmdToKernel(std::string cid); void CreatePollThread(); diff --git a/services/distributedfiledaemon/include/network/network_agent_template.h b/services/distributedfiledaemon/include/network/network_agent_template.h index b3a8b726245f60886bc8b7dadd22bb6007aa9f10..a8c886f0386567e5f0426efe329abdd52a14e282 100644 --- a/services/distributedfiledaemon/include/network/network_agent_template.h +++ b/services/distributedfiledaemon/include/network/network_agent_template.h @@ -37,10 +37,10 @@ public: explicit NetworkAgentTemplate(std::weak_ptr mountPoint) : Actor(this), mountPoint_(mountPoint), - kernerlTalker_( + kernerlTalker_(std::make_shared( mountPoint, [&](NotifyParam ¶m) { GetSessionProcess(param); }, - [&](const std::string &cid) { CloseSessionForOneDevice(cid); }), + [&](const std::string &cid) { CloseSessionForOneDevice(cid); })), sessionPool_(kernerlTalker_) { } @@ -77,7 +77,7 @@ private: std::mutex taskMut_; std::list tasks_; - KernelTalker kernerlTalker_; + std::shared_ptr kernerlTalker_; SessionPool sessionPool_; }; } // namespace DistributedFile diff --git a/services/distributedfiledaemon/include/network/session_pool.h b/services/distributedfiledaemon/include/network/session_pool.h index b8ff96a83a68f0b7f8e4af5d4a539f1e3b4fcc76..d7a9d6776d32ad941b2042f4987902ab818db6b3 100644 --- a/services/distributedfiledaemon/include/network/session_pool.h +++ b/services/distributedfiledaemon/include/network/session_pool.h @@ -29,7 +29,7 @@ namespace Storage { namespace DistributedFile { class SessionPool final : protected NoCopyable { public: - explicit SessionPool(KernelTalker &talker) : talker_(talker) {} + explicit SessionPool(std::shared_ptr &talker) : talker_(talker) {} ~SessionPool() = default; void HoldSession(std::shared_ptr session); void ReleaseSession(const int32_t fd); @@ -39,7 +39,7 @@ public: private: std::recursive_mutex sessionPoolLock_; std::list> usrSpaceSessionPool_; - KernelTalker &talker_; + std::shared_ptr &talker_; void AddSessionToPool(std::shared_ptr session); }; diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 090088e8d6be8e6ad61f8d4334027d2af86bf871..1413deef0883a438deb0a224ee0b523e4e83d63e 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -22,6 +22,7 @@ #include "device_auth.h" #include "ipc/i_daemon.h" #include "mountpoint/mount_manager.h" +#include "network/devsl_dispatcher.h" #include "network/softbus/softbus_agent.h" #include "softbus_bus_center.h" #include "utils_exception.h" @@ -58,6 +59,7 @@ void DeviceManagerAgent::StopInstance() void DeviceManagerAgent::Start() { + DevslDispatcher::Start(); RegisterToExternalDm(); InitLocalNodeInfo(); } @@ -65,6 +67,7 @@ void DeviceManagerAgent::Start() void DeviceManagerAgent::Stop() { UnregisterFromExternalDm(); + DevslDispatcher::Stop(); } void DeviceManagerAgent::JoinGroup(weak_ptr mp) diff --git a/services/distributedfiledaemon/src/network/devsl_dispatcher.cpp b/services/distributedfiledaemon/src/network/devsl_dispatcher.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2462f05a00ef82d74d7705119baee1401ea6007e --- /dev/null +++ b/services/distributedfiledaemon/src/network/devsl_dispatcher.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2021 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 "network/devsl_dispatcher.h" +#include "ipc/i_daemon.h" +#include "utils_log.h" + + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +std::map>> DevslDispatcher::talkersMap_; +std::map DevslDispatcher::idMap_; +std::mutex DevslDispatcher::mutex; + +int32_t DevslDispatcher::Start() +{ + int32_t status = DATASL_OnStart(); + if (status != 0) { + LOGE("devsl dispatcher start error %{public}d", status); + } + + return status; +} + +void DevslDispatcher::Stop() +{ + DATASL_OnStop(); +} + +DEVSLQueryParams DevslDispatcher::MakeDevslQueryParams(const std::string &udid) +{ + DEVSLQueryParams queryParams; + memcpy_s(queryParams.udid, udid.size(), udid.c_str(), udid.size()); + queryParams.udidLen = udid.size(); + + return queryParams; +} + +uint32_t DevslDispatcher::DevslGetRegister(const std::string &cid, std::weak_ptr talker) +{ + std::string udid; + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, cid, udid); + + LOGI("dsl dispatcher cid : %{public}s udid : %{public}s %{public}d", cid.c_str(), udid.c_str(), udid.size()); + std::lock_guard lock(mutex); + DEVSLQueryParams queryParams = MakeDevslQueryParams(udid); + int status = DATASL_GetHighestSecLevelAsync(&queryParams, &DevslDispatcher::DevslGottonCallback); + if (status != 0) { + LOGE("devsl dispatcher register callback error %{public}d", status); + return 0; + } + + idMap_[udid] = cid; + auto iter = talkersMap_.find(udid); + if (iter == talkersMap_.end()) { + std::vector> talkers; + talkers.push_back(talker); + talkersMap_.emplace(udid, talkers); + } else { + iter->second.push_back(talker); + } + + return 0; +} + +void DevslDispatcher::DevslGottonCallbackAsync(const std::string udid, uint32_t devsl) +{ + std::lock_guard lock(mutex); + + auto it = talkersMap_.find(udid); + if (it == talkersMap_.end()) { + LOGE("devsl dispatcher callback, there is no talker"); + return; + } + + for (auto talker : it->second) { + auto realTalker = talker.lock(); + if (!realTalker) { + continue; + } + + auto iter = idMap_.find(udid); + if (iter != idMap_.end()) { + realTalker->SinkDevslTokernel(iter->second, devsl); + } + } + + talkersMap_.erase(it); +} + +void DevslDispatcher::DevslGottonCallback(DEVSLQueryParams *queryParams, int32_t result, uint32_t levelInfo) +{ + LOGI("devsl dispatcher callback"); + if (result != 0) { + levelInfo = DATA_SEC_LEVEL1; + LOGE("devsl dispatcher dsl get callback result : %{public}d", result); + } + + std::string udid(reinterpret_cast(queryParams->udid), queryParams->udidLen); + std::thread callbackThread = std::thread(DevslDispatcher::DevslGottonCallbackAsync, udid, levelInfo); + callbackThread.detach(); +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS diff --git a/services/distributedfiledaemon/src/network/kernel_talker.cpp b/services/distributedfiledaemon/src/network/kernel_talker.cpp index 46f73f23f227b09691b846be071c302bf5ae6f23..87b7f877ff81bfce3b29b8cc67748cb93ad54888 100644 --- a/services/distributedfiledaemon/src/network/kernel_talker.cpp +++ b/services/distributedfiledaemon/src/network/kernel_talker.cpp @@ -15,6 +15,7 @@ #include "network/kernel_talker.h" #include "device/device_manager_agent.h" +#include "network/devsl_dispatcher.h" #include "securec.h" #include "utils_log.h" @@ -32,18 +33,26 @@ constexpr int TIME_OUT_EVENT = 0; struct UpdateSocketParam { int32_t cmd; int32_t newfd; + uint32_t devsl; uint8_t status; uint8_t masterKey[KEY_MAX_LEN]; - char cid[CID_MAX_LEN]; + uint8_t cid[CID_MAX_LEN]; +} __attribute__((packed)); + +struct UpdateDevslParam { + int32_t cmd; + uint32_t devsl; + uint8_t cid[CID_MAX_LEN]; } __attribute__((packed)); struct OfflineParam { int32_t cmd; - char remoteCid[CID_MAX_LEN]; + uint8_t remoteCid[CID_MAX_LEN]; } __attribute__((packed)); enum { CMD_UPDATE_SOCKET = 0, + CMD_UPDATE_DEVSL, CMD_OFF_LINE, CMD_OFF_LINE_ALL, CMD_CNT, @@ -74,6 +83,7 @@ void KernelTalker::SinkSessionTokernel(shared_ptr session) UpdateSocketParam cmd = { .cmd = CMD_UPDATE_SOCKET, .newfd = socketFd, + .devsl = 3, .status = status, }; if (memcpy_s(cmd.masterKey, KEY_MAX_LEN, masterkey.data(), KEY_MAX_LEN) != EOK) { @@ -86,6 +96,20 @@ void KernelTalker::SinkSessionTokernel(shared_ptr session) SetCmd(cmd); } +void KernelTalker::SinkDevslTokernel(const std::string &cid, uint32_t devsl) +{ + LOGD("sink dsl to kernel success, cid:%{public}s, devsl:%{public}d", cid.c_str(), devsl); + UpdateDevslParam cmd = { + .cmd = CMD_UPDATE_DEVSL, + .devsl = devsl, + }; + + if (memcpy_s(cmd.cid, CID_MAX_LEN, cid.c_str(), CID_MAX_LEN)) { + return; + } + SetCmd(cmd); +} + void KernelTalker::SinkOfflineCmdToKernel(string cid) { OfflineParam cmd = { diff --git a/services/distributedfiledaemon/src/network/network_agent_template.cpp b/services/distributedfiledaemon/src/network/network_agent_template.cpp index c244fcda8cc9588d8d6aba06de15267fea7c359d..fabad4140de23ba4d74f7aec73b9dc93d5b36aaf 100644 --- a/services/distributedfiledaemon/src/network/network_agent_template.cpp +++ b/services/distributedfiledaemon/src/network/network_agent_template.cpp @@ -30,7 +30,7 @@ constexpr int OPEN_SESSSION_DELAY_TIME = 100; void NetworkAgentTemplate::Start() { JoinDomain(); - kernerlTalker_.CreatePollThread(); + kernerlTalker_->CreatePollThread(); ConnectOnlineDevices(); } @@ -38,7 +38,7 @@ void NetworkAgentTemplate::Stop() { StopTopHalf(); StopBottomHalf(); - kernerlTalker_.WaitForPollThreadExited(); + kernerlTalker_->WaitForPollThreadExited(); } void NetworkAgentTemplate::ConnectDeviceAsync(const DeviceInfo info) diff --git a/services/distributedfiledaemon/src/network/session_pool.cpp b/services/distributedfiledaemon/src/network/session_pool.cpp index 5add432afe50053ef551cfe3167facbe3bce25b9..0bf259db4a61ef699b9835ac2972b496de915fce 100644 --- a/services/distributedfiledaemon/src/network/session_pool.cpp +++ b/services/distributedfiledaemon/src/network/session_pool.cpp @@ -23,7 +23,7 @@ using namespace std; void SessionPool::HoldSession(shared_ptr session) { lock_guard lock(sessionPoolLock_); - talker_.SinkSessionTokernel(session); + talker_->SinkSessionTokernel(session); AddSessionToPool(session); } @@ -42,7 +42,7 @@ void SessionPool::ReleaseSession(const int32_t fd) void SessionPool::ReleaseSession(const string &cid) { - talker_.SinkOfflineCmdToKernel(cid); + talker_->SinkOfflineCmdToKernel(cid); lock_guard lock(sessionPoolLock_); for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) { if ((*iter)->GetCid() == cid) { @@ -58,7 +58,7 @@ void SessionPool::ReleaseAllSession() { lock_guard lock(sessionPoolLock_); for (auto iter = usrSpaceSessionPool_.begin(); iter != usrSpaceSessionPool_.end();) { - talker_.SinkOfflineCmdToKernel((*iter)->GetCid()); + talker_->SinkOfflineCmdToKernel((*iter)->GetCid()); /* device offline, session release by softbus*/ iter = usrSpaceSessionPool_.erase(iter); } diff --git a/services/distributedfiledaemon/test/unittest/BUILD.gn b/services/distributedfiledaemon/test/unittest/BUILD.gn index eaa7641fda57269fc6c5c721800cbf5b8c253203..94b311b308c399185e57b81306b6be9b0d172609 100644 --- a/services/distributedfiledaemon/test/unittest/BUILD.gn +++ b/services/distributedfiledaemon/test/unittest/BUILD.gn @@ -46,6 +46,7 @@ ohos_unittest("DeviceManagerAgentTest") { ] external_deps = [ + "dataclassification:data_transit_mgr", "dsoftbus_standard:softbus_client", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index ca23fabc3935b713faf21fdb026a547dc0c46b0a..3c6b63319a2f5dc704ae0033ed6473d8c52b4fa8 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -40,6 +40,7 @@ ohos_moduletest("DistributedFileDaemonServiceTest") { "${services_path}/distributedfiledaemon/src/mountpoint/mount_manager.cpp", "${services_path}/distributedfiledaemon/src/mountpoint/mount_point.cpp", "${services_path}/distributedfiledaemon/src/multiuser/os_account_observer.cpp", + "${services_path}/distributedfiledaemon/src/network/devsl_dispatcher.cpp", "${services_path}/distributedfiledaemon/src/network/kernel_talker.cpp", "${services_path}/distributedfiledaemon/src/network/network_agent_template.cpp", "${services_path}/distributedfiledaemon/src/network/session_pool.cpp", @@ -67,6 +68,7 @@ ohos_moduletest("DistributedFileDaemonServiceTest") { ] external_deps = [ + "dataclassification:data_transit_mgr", "dsoftbus_standard:softbus_client", "ipc:ipc_core", "os_account_standard:os_account_innerkits",