diff --git a/services/distributedfile.cfg b/services/distributedfile.cfg index e2e31e54058eaf37067d3b9c864fba421a40c028..442e2113f870f5feaaefc4d1255bc226af1290ca 100644 --- a/services/distributedfile.cfg +++ b/services/distributedfile.cfg @@ -20,8 +20,8 @@ },{ "name": "distributedfiledaemon", "path": ["/system/bin/sa_main", "/system/profile/distributedfiledaemon.xml"], - "uid": "root", - "gid": ["root"], + "uid": "system", + "gid": ["system"], "caps": ["SYS_PTRACE", "KILL"] }] } diff --git a/services/distributedfiledaemon/BUILD.gn b/services/distributedfiledaemon/BUILD.gn index af0d2e29484cc47e92c572714d7ec71e6234bc28..eb28773d252ad2e4b2b598a1ab07e6e8c75651c7 100755 --- a/services/distributedfiledaemon/BUILD.gn +++ b/services/distributedfiledaemon/BUILD.gn @@ -28,6 +28,7 @@ ohos_shared_library("libdistributedfiledaemon") { "src/ipc/daemon_stub.cpp", "src/mountpoint/mount_manager.cpp", "src/mountpoint/mount_point.cpp", + "src/multiuser/os_account_observer.cpp", "src/network/kernel_talker.cpp", "src/network/network_agent_template.cpp", "src/network/session_pool.cpp", @@ -44,6 +45,7 @@ ohos_shared_library("libdistributedfiledaemon") { external_deps = [ "dsoftbus_standard:softbus_client", "ipc:ipc_core", + "os_account_standard:os_account_innerkits", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index ef5fdc57a85daa12e758ec7a15c4461fcba7b61c..f777529be09c0e7148bf4fad25115377679aa223 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -69,7 +69,6 @@ public: void OfflineAllDevice(); void ReconnectOnlineDevices(); - void AuthGroupOnlineProc(const DeviceInfo info); void OnRemoteDied() override; DeviceInfo &GetLocalDeviceInfo(); @@ -83,17 +82,16 @@ private: void RegisterToExternalDm(); void UnregisterFromExternalDm(); - void AuthGroupOfflineProc(const DeviceInfo &info); - void QueryRelatedGroups(const std::string &udid, std::vector &groupList); + void QueryRelatedGroups(const std::string &udid, const std::string &networkId); bool CheckIsAuthGroup(const GroupInfo &group); - void AllAuthGroupsOfflineProc(); + std::shared_ptr FindNetworkBaseTrustRelation(bool isAccountless); // We use a mutex instead of a shared_mutex to serialize online/offline procedures std::mutex mpToNetworksMutex_; std::map> mpToNetworks_; DeviceInfo localDeviceInfo_; - // key:groupId val:groupIdMark, the set of networkId - std::unordered_map>> authGroupMap_; + // cid-->same_account/accoutless's network + std::unordered_map> cidNetTypeRecord_; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/include/ipc/daemon.h b/services/distributedfiledaemon/include/ipc/daemon.h index 32283511c3dc6dd3563da86d383cab362288819a..d5326ac4eb421d93cf53ee17f8a2b282e8bf1d06 100644 --- a/services/distributedfiledaemon/include/ipc/daemon.h +++ b/services/distributedfiledaemon/include/ipc/daemon.h @@ -52,7 +52,7 @@ private: bool registerToService_{false}; void PublishSA(); - void StartManagers(); + void RegisterOsAccount(); }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/include/mountpoint/mount_manager.h b/services/distributedfiledaemon/include/mountpoint/mount_manager.h index 05a4abca6fa36e289716efc5180bf9fbeb6a1ec0..7b04f10c3d17c515c6e97924f3bdcba23236209d 100644 --- a/services/distributedfiledaemon/include/mountpoint/mount_manager.h +++ b/services/distributedfiledaemon/include/mountpoint/mount_manager.h @@ -36,9 +36,6 @@ public: private: void StartInstance() override {} void StopInstance() override {} - - std::mutex serializer_; - std::vector> mountPoints_; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/include/mountpoint/mount_point.h b/services/distributedfiledaemon/include/mountpoint/mount_point.h index e6a9a84a2921be6e33e2ed5e38501fc1960e2afe..4c047f41cba4369f6e6b3fb805751b8ba566c6d2 100644 --- a/services/distributedfiledaemon/include/mountpoint/mount_point.h +++ b/services/distributedfiledaemon/include/mountpoint/mount_point.h @@ -35,23 +35,22 @@ public: return id_; }; - std::string GetAuthGroupId() const + bool isAccountLess() const { - return authGroupId_; - } + return mountArg_.accountless_; + }; std::string ToString() const; Utils::MountArgument GetMountArgument() const; bool operator==(const MountPoint &rop) const; private: - friend class MountManager; + friend class OsAccountObserver; Utils::MountArgument mountArg_; void Mount() const; void Umount() const; static std::atomic idGen_; uint32_t id_{0}; - std::string authGroupId_{""}; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/include/multiuser/os_account_observer.h b/services/distributedfiledaemon/include/multiuser/os_account_observer.h new file mode 100644 index 0000000000000000000000000000000000000000..a7cc9e024fd60895517aab313934560e1d0cc0a6 --- /dev/null +++ b/services/distributedfiledaemon/include/multiuser/os_account_observer.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 ACCOUNT_CHANGE_OBSERVER_H +#define ACCOUNT_CHANGE_OBSERVER_H + +#include +#include +#include + +#include "mountpoint/mount_point.h" +#include "os_account_manager.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +static constexpr int MOUNT_POINT_NUM = 2; +class OsAccountObserver final : public AccountSA::OsAccountSubscriber { +public: + OsAccountObserver() = default; + ~OsAccountObserver(); + explicit OsAccountObserver(const AccountSA::OsAccountSubscribeInfo &subscribeInfo); + + void OnAccountsChanged(const int &id) override; +private: + void RemoveMPInfo(const int id); + void AddMPInfo(const int id, const std::string &relativePath); + + std::mutex serializer_; + std::unordered_map>> mountPoints_; + int curUsrId { -1 }; +}; +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS +#endif // ACCOUNT_CHANGE_OBSERVER_H \ No newline at end of file diff --git a/services/distributedfiledaemon/include/network/network_agent_template.h b/services/distributedfiledaemon/include/network/network_agent_template.h index d878472cb1aae8a6742d4db7561f454ebc074aeb..b3a8b726245f60886bc8b7dadd22bb6007aa9f10 100644 --- a/services/distributedfiledaemon/include/network/network_agent_template.h +++ b/services/distributedfiledaemon/include/network/network_agent_template.h @@ -52,7 +52,10 @@ public: void ConnectDeviceAsync(const DeviceInfo info); void DisconnectDevice(const DeviceInfo info); void AcceptSession(std::shared_ptr session); - + std::shared_ptr GetMountPoint() + { + return mountPoint_.lock(); + }; protected: virtual void JoinDomain() = 0; virtual void QuitDomain() = 0; diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 25e12ff25da402b6122869a174fc1a994beb6ab6..23689c6c13671c98a622a5d0e54af0528ad8d487 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -84,14 +84,14 @@ void DeviceManagerAgent::JoinGroup(weak_ptr mp) { unique_lock lock(mpToNetworksMutex_); agent = make_shared(mp); - auto [ignored, inserted] = mpToNetworks_.insert({smp->GetID(), agent}); + auto [ignored, inserted] = mpToNetworks_.insert({ smp->GetID(), agent }); if (!inserted) { stringstream ss; ss << "Failed to join group: Mountpoint existed" << smp->ToString(); throw runtime_error(ss.str()); } } - LOGI("smp id %{public}d, groupId %{public}s", smp->GetID(), smp->GetAuthGroupId().c_str()); + LOGI("smp id %{public}d, is account_less %{pubulic}d", smp->GetID(), agent->GetMountPoint()->isAccountLess()); agent->StartActor(); } @@ -119,43 +119,57 @@ void DeviceManagerAgent::QuitGroup(weak_ptr mp) void DeviceManagerAgent::OfflineAllDevice() { unique_lock lock(mpToNetworksMutex_); - for (auto &&networkAgent : mpToNetworks_) { + for (auto [ignore, net] : cidNetTypeRecord_) { auto cmd = make_unique>(&NetworkAgentTemplate::DisconnectAllDevices); - cmd->UpdateOption({ - .tryTimes_ = 1, - }); - networkAgent.second->Recv(move(cmd)); + net->Recv(move(cmd)); } } void DeviceManagerAgent::ReconnectOnlineDevices() { unique_lock lock(mpToNetworksMutex_); - for (auto &&networkAgent : mpToNetworks_) { + for (auto [ignore, net] : cidNetTypeRecord_) { auto cmd = make_unique>(&NetworkAgentTemplate::ConnectOnlineDevices); cmd->UpdateOption({ .tryTimes_ = MAX_RETRY_COUNT, }); - networkAgent.second->Recv(move(cmd)); + net->Recv(move(cmd)); } } +std::shared_ptr DeviceManagerAgent::FindNetworkBaseTrustRelation(bool isAccountless) +{ + LOGI("enter: isAccountless %{public}d", isAccountless); + for (auto [ignore, net] : mpToNetworks_) { + if (net->GetMountPoint()->isAccountLess() == isAccountless) { + return net; + } + } + LOGE("not find this net in mpToNetworks, isAccountless %{public}d", isAccountless); + return nullptr; +} + void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) { - LOGI("netwrorkId %{public}s, OnDeviceOnline begin", deviceInfo.deviceId); + LOGI("networkId %{public}s, OnDeviceOnline begin", deviceInfo.deviceId); + + // online first query this dev's trust info DeviceInfo info(deviceInfo); - { - unique_lock lock(mpToNetworksMutex_); - for (auto &&networkAgent : mpToNetworks_) { - auto cmd = make_unique>( - &NetworkAgentTemplate::ConnectDeviceAsync, info); - cmd->UpdateOption({ - .tryTimes_ = MAX_RETRY_COUNT, - }); - networkAgent.second->Recv(move(cmd)); - } + QueryRelatedGroups(info.udid_, info.cid_); + + // based on dev's trust info, choose corresponding network agent to obtain socket + unique_lock lock(mpToNetworksMutex_); + auto networkAgent = cidNetTypeRecord_[info.cid_]; + if (networkAgent == nullptr) { + LOGE("cid %{public}s network is null!", info.cid_.c_str()); + return; } - AuthGroupOnlineProc(info); + auto cmd = + make_unique>(&NetworkAgentTemplate::ConnectDeviceAsync, info); + cmd->UpdateOption({ + .tryTimes_ = MAX_RETRY_COUNT, + }); + networkAgent->Recv(move(cmd)); LOGI("OnDeviceOnline end"); } @@ -164,18 +178,17 @@ void DeviceManagerAgent::OnDeviceOffline(const DistributedHardware::DmDeviceInfo { LOGI("OnDeviceOffline begin"); DeviceInfo info(deviceInfo); - { - unique_lock lock(mpToNetworksMutex_); - for (auto &&networkAgent : mpToNetworks_) { - auto cmd = - make_unique>(&NetworkAgentTemplate::DisconnectDevice, info); - cmd->UpdateOption({ - .tryTimes_ = 1, - }); - networkAgent.second->Recv(move(cmd)); - } + + unique_lock lock(mpToNetworksMutex_); + auto networkAgent = cidNetTypeRecord_[info.cid_]; + if (networkAgent == nullptr) { + LOGE("cid %{public}s network is null!", info.cid_.c_str()); + return; } - AuthGroupOfflineProc(info); + + auto cmd = make_unique>(&NetworkAgentTemplate::DisconnectDevice, info); + networkAgent->Recv(move(cmd)); + cidNetTypeRecord_.erase(info.cid_); LOGI("OnDeviceOffline end"); } @@ -198,7 +211,7 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) } } -void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector &groupList) +void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, const std::string &networkId) { LOGI("use udid %{public}s query hichain related groups", udid.c_str()); int ret = InitDeviceAuthService(); @@ -234,73 +247,23 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector return; } + std::vector groupList; groupList = jsonObject.get>(); for (auto &a : groupList) { LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupType] %{public}d,", a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); } - return; -} -void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) -{ - std::vector groupList; - QueryRelatedGroups(info.udid_, groupList); + unique_lock lock(mpToNetworksMutex_); for (const auto &group : groupList) { - if (!CheckIsAuthGroup(group)) { - continue; - } - if (authGroupMap_.find(group.groupId) == authGroupMap_.end()) { - LOGI("groupId %{public}s not exist, map size %{public}d, then mount", group.groupId.c_str(), - authGroupMap_.size()); - std::stringstream ss; - ss << "groupId_" << authGroupMap_.size(); - MountManager::GetInstance()->Mount(make_unique( - Utils::MountArgumentDescriptors::SetAuthGroupMountArgument(ss.str(), group.groupOwner, true))); - std::get<0>(authGroupMap_[group.groupId]) = ss.str(); - } - auto [iter, status] = std::get<1>(authGroupMap_[group.groupId]).insert(info.cid_); - if (status == false) { - LOGI("cid %{public}s has already inserted into groupId %{public}s", info.cid_.c_str(), - group.groupId.c_str()); - continue; + if (CheckIsAuthGroup(group)) { + cidNetTypeRecord_.insert({ networkId, FindNetworkBaseTrustRelation(true) }); + } else { + cidNetTypeRecord_.insert({ networkId, FindNetworkBaseTrustRelation(false) }); } } -} -void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) -{ - for (auto iter = authGroupMap_.begin(); iter != authGroupMap_.end();) { - auto [groupIdMark, set] = iter->second; - auto groupId = iter->first; - if (set.find(info.GetCid()) == set.end()) { - continue; - } - - if (authGroupMap_.find(groupId) == authGroupMap_.end()) { - LOGI("can not find groupId %{public}s ", groupId.c_str()); - continue; - } - std::get<1>(authGroupMap_[groupId]).erase(info.GetCid()); - if (std::get<1>(authGroupMap_[groupId]).empty()) { - std::vector groupList; - if (groupList.size() == 0) { - MountManager::GetInstance()->Umount(groupIdMark); - iter = authGroupMap_.erase(iter); - continue; - } - } - iter++; - } -} - -void DeviceManagerAgent::AllAuthGroupsOfflineProc() -{ - for (auto iter = authGroupMap_.begin(); iter != authGroupMap_.end();) { - auto [groupIdMark, ignore] = iter->second; - MountManager::GetInstance()->Umount(groupIdMark); - authGroupMap_.erase(iter++); - } + return; } bool DeviceManagerAgent::CheckIsAuthGroup(const GroupInfo &group) @@ -331,11 +294,9 @@ void DeviceManagerAgent::OnRemoteDied() LOGI("device manager service died"); StopInstance(); OfflineAllDevice(); // cannot commit a cmd to queue - AllAuthGroupsOfflineProc(); StartInstance(); ReconnectOnlineDevices(); } - DeviceInfo &DeviceManagerAgent::GetLocalDeviceInfo() { return localDeviceInfo_; diff --git a/services/distributedfiledaemon/src/ipc/daemon.cpp b/services/distributedfiledaemon/src/ipc/daemon.cpp index 2db0236d397002d07706d51bb677096f5249398f..f50c2d5701b7af4157adef38d079cbf2e3eeddf9 100644 --- a/services/distributedfiledaemon/src/ipc/daemon.cpp +++ b/services/distributedfiledaemon/src/ipc/daemon.cpp @@ -16,6 +16,8 @@ #include "ipc/daemon.h" #include "mountpoint/mount_manager.h" +#include "multiuser/os_account_observer.h" +#include "os_account_manager.h" #include "system_ability_definition.h" #include "utils_log.h" @@ -39,9 +41,18 @@ void Daemon::PublishSA() LOGI("Init finished successfully"); } -void Daemon::StartManagers() +void Daemon::RegisterOsAccount() { - MountManager::GetInstance(); + OHOS::AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo; + osAccountSubscribeInfo.SetOsAccountSubscribeType(OHOS::AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED); + osAccountSubscribeInfo.SetName("distributed_file_service"); + + auto subScriber = std::make_shared(osAccountSubscribeInfo); + int ret = OHOS::AccountSA::OsAccountManager::SubscribeOsAccount(subScriber); + if (ret != 0) { + LOGE("register os account fail ret %{public}d", ret); + } + LOGI("register os account success, ret %{public}d", ret); } void Daemon::OnStart() @@ -54,7 +65,7 @@ void Daemon::OnStart() try { PublishSA(); - StartManagers(); + RegisterOsAccount(); } catch (const exception &e) { LOGE("%{public}s", e.what()); } diff --git a/services/distributedfiledaemon/src/mountpoint/mount_manager.cpp b/services/distributedfiledaemon/src/mountpoint/mount_manager.cpp index 8e9792f5d6b2613596af9f5bcc0385b7e414ba2a..3f8696eaa0452c963071e4d8b6826cd305d0718d 100644 --- a/services/distributedfiledaemon/src/mountpoint/mount_manager.cpp +++ b/services/distributedfiledaemon/src/mountpoint/mount_manager.cpp @@ -29,81 +29,22 @@ using namespace std; MountManager::MountManager() { - Mount(make_unique(Utils::MountArgumentDescriptors::Alpha())); } MountManager::~MountManager() { - try { - // Umount mountpoints in reverse order to eliminate dependencies - for_each(mountPoints_.rbegin(), mountPoints_.rend(), [this](auto &cur_mp) { Umount(cur_mp); }); - } catch (const exception &e) { - LOGE("%{public}s", e.what()); - } } void MountManager::Mount(unique_ptr mp) { - lock_guard lock(serializer_); - - shared_ptr smp = move(mp); - - auto isExisted = [smp](const auto &cur_mp) { return *smp == *cur_mp; }; - if (!smp || any_of(mountPoints_.begin(), mountPoints_.end(), isExisted)) { - stringstream ss; - ss << "Mount an empty/existent mountpoint" << (smp ? smp->GetMountArgument().GetFullDst() : ""); - LOGE("%{public}s", ss.str().c_str()); - throw runtime_error(ss.str()); - } - try { - smp->Umount(); // try umount one time - } catch (const exception &e) { - LOGE("%{public}s", e.what()); - } - smp->Mount(); - auto dm = DeviceManagerAgent::GetInstance(); - dm->Recv(make_unique>>(&DeviceManagerAgent::JoinGroup, smp)); - mountPoints_.push_back(smp); } void MountManager::Umount(weak_ptr wmp) { - auto smp = wmp.lock(); - lock_guard lock(serializer_); - - decltype(mountPoints_.begin()) it; - auto isExisted = [smp](const auto &cur_mp) { return *smp == *cur_mp; }; - if (!smp || ((it = find_if(mountPoints_.begin(), mountPoints_.end(), isExisted)) == mountPoints_.end())) { - stringstream ss; - ss << "Umount an empty/non-existent mountpoint" << (smp ? smp->GetMountArgument().GetFullDst() : ""); - LOGE("%{public}s", ss.str().c_str()); - throw runtime_error(ss.str()); - } - LOGI("Umount begin"); - smp->Umount(); - auto dm = DeviceManagerAgent::GetInstance(); - dm->Recv(make_unique>>(&DeviceManagerAgent::QuitGroup, smp)); - mountPoints_.erase(it); - LOGI("Umount end"); } void MountManager::Umount(const std::string &groupId) { - if (groupId == "") { - LOGE("groupId is null, no auth group to unmount"); - return; - } - - decltype(mountPoints_.begin()) iter = - find_if(mountPoints_.begin(), mountPoints_.end(), - [groupId](const auto &cur_mp) { return cur_mp->authGroupId_ == groupId; }); - if (iter == mountPoints_.end()) { - stringstream ss; - ss << "Umount not find this auth group id" << groupId; - LOGE("Umount not find this auth group id %{public}s", groupId.c_str()); - throw runtime_error(ss.str()); - } - Umount(*iter); } } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/src/mountpoint/mount_point.cpp b/services/distributedfiledaemon/src/mountpoint/mount_point.cpp index 1586751eabcf219eff8ca75a034d387ce6d20977..8479cf70316adfad5a5a3e81f5f9ada3c18fe9a2 100644 --- a/services/distributedfiledaemon/src/mountpoint/mount_point.cpp +++ b/services/distributedfiledaemon/src/mountpoint/mount_point.cpp @@ -15,9 +15,6 @@ #include "mountpoint/mount_point.h" -#include -#include - #include "utils_directory.h" #include "utils_log.h" @@ -31,53 +28,16 @@ atomic MountPoint::idGen_; MountPoint::MountPoint(const Utils::MountArgument &mountArg) : mountArg_(mountArg) { id_ = idGen_++; - if (mountArg.accountless_) { - authGroupId_ = mountArg.groupId_; - } } void MountPoint::Mount() const { - auto mntArg = GetMountArgument(); - - string src = mntArg.GetFullSrc(); - Utils::ForceCreateDirectory(src, S_IRWXU | S_IRWXG | S_IXOTH, Utils::UID_SYSTEM, Utils::UID_MEDIA_RW); - - string dst = mntArg.GetFullDst(); - Utils::ForceCreateDirectory(dst, S_IRWXU | S_IRWXG | S_IXOTH); - - string cache = mntArg.GetCachePath(); - Utils::ForceCreateDirectory(cache, S_IRWXU | S_IRWXG | S_IXOTH); - - string data = mntArg.GetDataPath(); - Utils::ForceCreateDirectory(data, S_IRWXU | S_IRWXG | S_IXOTH); - - unsigned long flags = mntArg.GetFlags(); - string options = mntArg.OptionsToString(); - int ret = mount(src.c_str(), dst.c_str(), "hmdfs", flags, options.c_str()); - if (ret == -1 && errno != EEXIST && errno != EBUSY) { - auto cond = system_category().default_error_condition(errno); - LOGE("Failed to mount: %{public}d %{public}s", cond.value(), cond.message().c_str()); - throw system_error(errno, system_category()); - } - LOGI("mount sucess: src %{public}s --> dst %{public}s", src.c_str(), dst.c_str()); - - if (mntArg.accountless_) { - Utils::ForceCreateDirectory(dst + "/device_view/local/data/" + mntArg.packageName_, - S_IRWXU | S_IRWXG | S_IXOTH); - } + LOGI("mount"); } void MountPoint::Umount() const { - string dst = GetMountArgument().GetFullDst(); - if (umount2(dst.c_str(), MNT_DETACH) == -1) { - auto cond = system_category().default_error_condition(errno); - LOGE("Failed to umount: %{public}d %{public}s", cond.value(), cond.message().c_str()); - throw system_error(errno, system_category()); - } - - Utils::ForceRemoveDirectory(dst); + LOGE("umount"); } bool MountPoint::operator==(const MountPoint &rop) const diff --git a/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp b/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..086f7a27e11bd487cad5ab0f9c7772fde2b6e4d4 --- /dev/null +++ b/services/distributedfiledaemon/src/multiuser/os_account_observer.cpp @@ -0,0 +1,89 @@ +/* + * 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 "multiuser/os_account_observer.h" + +#include "device/device_manager_agent.h" +#include "utils_log.h" +#include "utils_mount_argument.h" + +namespace OHOS { +namespace Storage { +namespace DistributedFile { +using namespace std; +namespace { +static const std::string SAME_ACCOUNT = "account"; +static const std::string ACCOUNT_LESS = "non_account"; +static constexpr int DEFAULT_ACCOUNT = 100; +} // namespace + +OsAccountObserver::OsAccountObserver(const AccountSA::OsAccountSubscribeInfo &subscribeInfo) + : OsAccountSubscriber(subscribeInfo) +{ + LOGI("init first to create network of default user"); + lock_guard lock(serializer_); + curUsrId = DEFAULT_ACCOUNT; + AddMPInfo(curUsrId, SAME_ACCOUNT); + AddMPInfo(curUsrId, ACCOUNT_LESS); + LOGI("init first to create network of user %{public}d, done", DEFAULT_ACCOUNT); +} + +OsAccountObserver::~OsAccountObserver() +{ +} + +void OsAccountObserver::AddMPInfo(const int id, const std::string &relativePath) +{ + auto smp = make_shared(Utils::MountArgumentDescriptors::Alpha(id, relativePath)); + auto dm = DeviceManagerAgent::GetInstance(); + dm->Recv(make_unique>>(&DeviceManagerAgent::JoinGroup, smp)); + mountPoints_[id].emplace_back(smp); +} + +void OsAccountObserver::OnAccountsChanged(const int &id) +{ + LOGI("user id changed to %{public}d", id); + lock_guard lock(serializer_); + if (curUsrId != -1) { + // first stop curUsrId network + RemoveMPInfo(curUsrId); + } + + // then start new network + curUsrId = id; + AddMPInfo(id, SAME_ACCOUNT); + AddMPInfo(id, ACCOUNT_LESS); + LOGI("user id %{public}d, add network done", curUsrId); +} + +void OsAccountObserver::RemoveMPInfo(const int id) +{ + auto iter = mountPoints_.find(id); + if (iter == mountPoints_.end()) { + LOGE("user id %{public}d not find in map", curUsrId); + return; + } + + auto dm = DeviceManagerAgent::GetInstance(); + for (auto smp : iter->second) { + dm->Recv(make_unique>>(&DeviceManagerAgent::QuitGroup, smp)); + } + mountPoints_.erase(iter); + + LOGE("remove mount info of user id %{public}d", id); +} +} // namespace DistributedFile +} // namespace Storage +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedfiledaemon/src/network/kernel_talker.cpp b/services/distributedfiledaemon/src/network/kernel_talker.cpp index 69b46a0055fba112a52987ebf2f05176e71da2bd..46f73f23f227b09691b846be071c302bf5ae6f23 100644 --- a/services/distributedfiledaemon/src/network/kernel_talker.cpp +++ b/services/distributedfiledaemon/src/network/kernel_talker.cpp @@ -142,9 +142,10 @@ void KernelTalker::PollRun() return; } string ctrlPath = spt->GetMountArgument().GetCtrlPath(); + LOGI("Open node file ctrl path %{public}s", ctrlPath.c_str()); cmdFd = open(ctrlPath.c_str(), O_RDWR); if (cmdFd < 0) { - LOGE("Open node file error %{public}d", errno); + LOGE("Open node file error %{public}d, ctrl path %{public}s", errno, ctrlPath.c_str()); return; } diff --git a/services/distributedfiledaemon/src/network/network_agent_template.cpp b/services/distributedfiledaemon/src/network/network_agent_template.cpp index 507b28782ee646b3074866cb73ac36713aee0e41..c244fcda8cc9588d8d6aba06de15267fea7c359d 100644 --- a/services/distributedfiledaemon/src/network/network_agent_template.cpp +++ b/services/distributedfiledaemon/src/network/network_agent_template.cpp @@ -60,9 +60,6 @@ void NetworkAgentTemplate::ConnectOnlineDevices() .tryTimes_ = MAX_RETRY_COUNT, }); Recv(move(cmd)); - - dma->Recv( - make_unique>(&DeviceManagerAgent::AuthGroupOnlineProc, info)); } } diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 8bc51b4528fad43b1785cbea2206f8644fc4725c..0d88bc1e0a930622d6b4e07fd6c79b12b2198652 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -39,6 +39,7 @@ ohos_moduletest("DistributedFileDaemonServiceTest") { "${services_path}/distributedfiledaemon/src/ipc/daemon_stub.cpp", "${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/kernel_talker.cpp", "${services_path}/distributedfiledaemon/src/network/network_agent_template.cpp", "${services_path}/distributedfiledaemon/src/network/session_pool.cpp", @@ -68,6 +69,7 @@ ohos_moduletest("DistributedFileDaemonServiceTest") { external_deps = [ "dsoftbus_standard:softbus_client", "ipc:ipc_core", + "os_account_standard:os_account_innerkits", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] diff --git a/test/moduletest/src/distributedfiledaemon_service_test.cpp b/test/moduletest/src/distributedfiledaemon_service_test.cpp index 9f29c2907338c95b60cb4820a6a0032920c40733..38199d5d3371b73ca77a1ea287f8b8b0b9e21cdb 100644 --- a/test/moduletest/src/distributedfiledaemon_service_test.cpp +++ b/test/moduletest/src/distributedfiledaemon_service_test.cpp @@ -39,9 +39,10 @@ namespace Test { using namespace testing::ext; using namespace std; -static const string srcHead = "/data/misc_ce/"; +static const string srcHead = "/data/service/el2/"; static const string dstHead = "/mnt/hmdfs/"; -static const string cacheHead = "/data/misc_ce/"; +static const string cacheHead = "/data/service/el2/"; +static const string SAME_ACCOUNT = "account"; std::mutex cmdMutex_; const int KEY_MAX_LEN = 32; @@ -96,7 +97,7 @@ void DistributedFileDaemonServiceTest::TearDown(void) HWTEST_F(DistributedFileDaemonServiceTest, mount_umount_test_001, TestSize.Level1) { auto mp = make_unique( - OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(9527)); + OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(100, SAME_ACCOUNT)); shared_ptr smp = move(mp); @@ -119,9 +120,9 @@ HWTEST_F(DistributedFileDaemonServiceTest, mount_umount_test_001, TestSize.Level */ HWTEST_F(DistributedFileDaemonServiceTest, distributedFileDaemon_service_test_001_mount, TestSize.Level1) { - const int userId = 3333; + const int userId = 101; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); @@ -193,7 +194,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, distributedFileDaemon_service_test_00 const int len = sizeof(userId) / sizeof(userId[0]); for (int i = 0; i < len; i++) { - mountArgument[i] = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId[i]); + mountArgument[i] = Utils::MountArgumentDescriptors::Alpha(userId[i], SAME_ACCOUNT); auto mp = make_unique(mountArgument[i]); smpArr[i] = move(mp); } @@ -265,7 +266,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, distributedFileDaemon_service_test_00 { const int userId = 3337; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); @@ -330,7 +331,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, distributedFileDaemon_service_test_00 { const int userId = 3338; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); @@ -399,7 +400,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, distributedFileDaemon_service_test_00 { const int userId = 3339; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); @@ -501,7 +502,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, TestSize.Level1) { const int userId = 4200; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); @@ -532,7 +533,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, TestSize.Level1) { const int userId = 4201; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); @@ -575,8 +576,8 @@ HWTEST_F(DistributedFileDaemonServiceTest, { const int userId1 = 4202; const int userId2 = 4203; - auto mountArgument1 = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId1); - auto mountArgument2 = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId2); + auto mountArgument1 = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId1, SAME_ACCOUNT); + auto mountArgument2 = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId2, SAME_ACCOUNT); auto mp1 = make_unique(mountArgument1); auto mp2 = make_unique(mountArgument2); shared_ptr smp1 = move(mp1); @@ -613,7 +614,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, TestSize.Level1) { const int userId = 4204; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); @@ -654,7 +655,7 @@ HWTEST_F(DistributedFileDaemonServiceTest, HWTEST_F(DistributedFileDaemonServiceTest, distributedFileDaemon_service_test_015_kernel_notify_deal, TestSize.Level1) { const int userId = 4500; - auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId); + auto mountArgument = OHOS::Storage::DistributedFile::Utils::MountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT); auto mp = make_unique(mountArgument); shared_ptr smp = move(mp); diff --git a/utils/system/include/utils_mount_argument.h b/utils/system/include/utils_mount_argument.h index 2047fdd8970a24622e46fd3cc0127d4dba09ef88..e3cf81f150a36067c9ddf8f8e4f449e63000ecba 100644 --- a/utils/system/include/utils_mount_argument.h +++ b/utils/system/include/utils_mount_argument.h @@ -23,36 +23,31 @@ namespace Storage { namespace DistributedFile { namespace Utils { struct MountArgument final { - int userId_{0}; - bool accountless_{false}; - std::string groupId_; - bool needInitDir_{false}; - bool useCache_{false}; - bool caseSensitive_{false}; - bool enableMergeView_{false}; - bool enableFixupOwnerShip_{false}; - bool enableOfflineStash_{true}; - bool externalFS_{false}; - std::string packageName_; + int userId_ { 0 }; + bool accountless_ { false }; + bool needInitDir_ { false }; + bool useCache_ { false }; + bool caseSensitive_ { false }; + bool enableMergeView_ { false }; + bool enableFixupOwnerShip_ { false }; + bool enableOfflineStash_ { true }; + bool externalFS_ { false }; + std::string relativePath_; std::string GetFullSrc() const; std::string GetFullDst() const; std::string GetCtrlPath() const; std::string GetCachePath() const; std::string OptionsToString() const; - std::string GetDataPath() const; unsigned long GetFlags() const; }; class MountArgumentDescriptors final { public: - static MountArgument Alpha(int userId = 0); - static MountArgument - SetAuthGroupMountArgument(const std::string &groupId, const std::string &packageName, bool accountless); + static MountArgument Alpha(int userId, std::string relativePath); }; } // namespace Utils } // namespace DistributedFile } // namespace Storage } // namespace OHOS #endif // UTILS_MOUNT_ARGUMENT_H - diff --git a/utils/system/src/utils_mount_argument.cpp b/utils/system/src/utils_mount_argument.cpp index c45a60c1c587c4da2e02ea878554c51e79287277..17ac0d3c4b1e6289e5a9fc469ad6fe526ab62311 100644 --- a/utils/system/src/utils_mount_argument.cpp +++ b/utils/system/src/utils_mount_argument.cpp @@ -26,51 +26,27 @@ using namespace std; namespace { static const std::string DATA_POINT = "/data/service/el2/"; static const std::string BASE_MOUNT_POINT = "/mnt/hmdfs/"; -static const std::string AUTH_GROUP_MOUNT_POINT = "/mnt/hmdfs/auth_groups/"; static const std::string SYSFS_HMDFS_PATH = "sys/fs/hmdfs/"; } // namespace string MountArgument::GetFullSrc() const { stringstream ss; - if (!accountless_) { - ss << DATA_POINT << userId_ << "/hmdfs"; - } else { - ss << DATA_POINT << userId_ << "/hmdfs/auth_groups/" << groupId_; - } + ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_; return ss.str(); } string MountArgument::GetFullDst() const { stringstream ss; - if (!accountless_) { - ss << BASE_MOUNT_POINT << userId_ << "/"; - } else { - ss << AUTH_GROUP_MOUNT_POINT << groupId_ << "/"; - } + ss << BASE_MOUNT_POINT << userId_ << "/" << relativePath_; return ss.str(); } string MountArgument::GetCachePath() const { stringstream ss; - if (!accountless_) { - ss << DATA_POINT << userId_ << "/hmdfs/cache/"; - } else { - ss << DATA_POINT << userId_ << "/hmdfs/auth_groups/" << groupId_ << "/cache/"; - } - return ss.str(); -} - -string MountArgument::GetDataPath() const -{ - stringstream ss; - if (!accountless_) { - ss << DATA_POINT << userId_ << "/hmdfs/data/"; - } else { - ss << DATA_POINT << userId_ << "/hmdfs/auth_groups/" << groupId_ << "/data/"; - } + ss << DATA_POINT << userId_ << "/hmdfs/" << relativePath_ << "/cache/"; return ss.str(); } @@ -89,6 +65,7 @@ string MountArgument::GetCtrlPath() const { auto dst = GetFullDst(); auto res = MocklispHash(dst); + stringstream ss; ss << SYSFS_HMDFS_PATH << res << "/cmd"; return ss.str(); @@ -124,39 +101,25 @@ unsigned long MountArgument::GetFlags() const return MS_NODEV; } -MountArgument MountArgumentDescriptors::Alpha(int userId) +MountArgument MountArgumentDescriptors::Alpha(int userId, string relativePath) { MountArgument mountArgument = { - .groupId_ = "default", + .userId_ = userId, .needInitDir_ = true, .useCache_ = true, .enableMergeView_ = true, .enableFixupOwnerShip_ = false, .enableOfflineStash_ = true, .externalFS_ = false, + .relativePath_ = relativePath, }; - mountArgument.userId_ = userId; - return mountArgument; -} -MountArgument MountArgumentDescriptors::SetAuthGroupMountArgument(const std::string &groupId, - const std::string &packageName, - bool accountless) -{ - MountArgument mountArgument = { - .accountless_ = accountless, - .groupId_ = groupId, - .needInitDir_ = true, - .useCache_ = true, - .enableMergeView_ = true, - .enableFixupOwnerShip_ = false, - .enableOfflineStash_ = true, - .externalFS_ = false, - .packageName_ = packageName, - }; + if (relativePath == "non_account") { + mountArgument.accountless_ = true; + } return mountArgument; -} +}; } // namespace Utils } // namespace DistributedFile } // namespace Storage -} // namespace OHOS +} // namespace OHOS \ No newline at end of file