• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_STORAGE_DAEMON_MOUNT_MANAGER_H
17 #define OHOS_STORAGE_DAEMON_MOUNT_MANAGER_H
18 
19 #include <fstream>
20 #include <list>
21 #include <map>
22 #include <string>
23 #include <mutex>
24 #include <vector>
25 #include <sys/types.h>
26 #include <nocopyable.h>
27 
28 namespace OHOS {
29 namespace StorageDaemon {
30 struct DirInfo {
31     const std::string path;
32     mode_t mode;
33     uid_t uid;
34     gid_t gid;
35 };
36 
37 struct ProcessInfo {
38     int pid;
39     std::string name;
40 };
41 
42 constexpr uid_t OID_ROOT = 0;
43 constexpr uid_t OID_SYSTEM = 1000;
44 constexpr uid_t OID_FILE_MANAGER = 1006;
45 constexpr uid_t OID_USER_DATA_RW = 1008;
46 constexpr uid_t OID_DFS = 1009;
47 constexpr uid_t OID_BACKUP = 1089;
48 constexpr uid_t OID_DFS_SHARE = 3822;
49 constexpr uid_t OID_TEE = 6668;
50 constexpr uid_t OID_DEVICE_AUTH = 3333;
51 constexpr uid_t OID_HUKS = 3510;
52 constexpr uid_t OID_ASSET = 6226;
53 constexpr uid_t OID_DDMS = 3012;
54 constexpr uid_t OID_HWID = 7008;
55 constexpr uid_t OID_DLP_CREDENTIAL = 3553;
56 constexpr uid_t OID_ACCOUNT = 3058;
57 constexpr uid_t OID_HIVIEW = 1201;
58 constexpr uid_t OID_PARENT_CONTROL = 7007;
59 constexpr uid_t OID_AV_SESSION = 6700;
60 constexpr uid_t USER_ID_BASE = 200000;
61 
62 class MountManager final {
63 public:
64     MountManager();
65     virtual ~MountManager() = default;
66     static std::shared_ptr<MountManager> GetInstance();
67     int32_t MountByUser(int32_t userId);
68     int32_t UmountByUser(int32_t userId);
69     int32_t PrepareHmdfsDirs(int32_t userId);
70     int32_t PrepareFileManagerDirs(int32_t userId);
71     int32_t DestroyHmdfsDirs(int32_t userId);
72     int32_t DestroyFileManagerDirs(int32_t userId);
73     int32_t DestroySystemServiceDirs(int32_t userId);
74     int32_t CloudMount(int32_t userId, const std::string& path);
75     int32_t CloudTwiceMount(int32_t userId);
76     int32_t MountCryptoPathAgain(uint32_t userId);
77     int32_t MountDfsDocs(int32_t userId, const std::string &relativePath,
78         const std::string &networkId, const std::string &deviceId);
79     int32_t UMountDfsDocs(int32_t userId, const std::string &relativePath,
80         const std::string &networkId, const std::string &deviceId);
81     int32_t UMountAllPath(int32_t userId, std::list<std::string> &mountFailList);
82     int32_t UMountByList(std::list<std::string> &list, std::list<std::string> &mountFailList);
83     void SetCloudState(bool active);
84     int32_t RestoreconSystemServiceDirs(int32_t userId);
85     int32_t FindMountPointsToMap(std::map<std::string, std::list<std::string>> &mountMap, int32_t userId);
86     void MountPointToList(std::list<std::string> &hmdfsList, std::list<std::string> &hmfsList,
87         std::list<std::string> &sharefsList, std::string &line, int32_t userId);
88     int32_t FindAndKillProcess(int32_t userId, std::list<std::string> &mountFailList);
89     bool CheckMaps(const std::string &path, const std::string &prefix, std::list<std::string> &mountFailList);
90     bool CheckSymlink(const std::string &path, const std::string &prefix, std::list<std::string> &mountFailList);
91     bool GetProcessInfo(const std::string &filename, ProcessInfo &info);
92     void KillProcess(std::vector<ProcessInfo> &processInfo);
93     bool PidUsingFlag(std::string &pidPath, const std::string &prefix, std::list<std::string> &mountFailList);
94     void UmountFailRadar(std::vector<ProcessInfo> &processInfo);
95     void MountSandboxPath(const std::vector<std::string> &srcPaths, const std::vector<std::string> &dstPaths,
96                           const std::string &bundleName, const std::string &userId);
97     bool CheckMountFileByUser(int32_t userId);
98     bool CloudDirFlag(const std::string &path);
99 
100 private:
101     bool SupportHmdfs();
102     int32_t CreateVirtualDirs(int32_t userId);
103     int32_t HmdfsMount(int32_t userId);
104     int32_t HmdfsMount(int32_t userId, std::string relativePath, bool mountCloudDisk = false);
105     int32_t HmdfsTwiceMount(int32_t userId, std::string relativePath);
106     int32_t HmdfsUMount(int32_t userId, std::string relativePath);
107     int32_t SharefsMount(int32_t userId);
108     int32_t LocalMount(int32_t userId);
109     int32_t LocalUMount(int32_t userId);
110     int32_t SetFafQuotaProId(int32_t userId);
111     int32_t CreateSystemServiceDirs(int32_t userId);
112     void MountCloudForUsers(void);
113     void UMountCloudForUsers(void);
114     void PrepareFileManagerDir(int32_t userId);
115     int32_t CloudUMount(int32_t userId);
116     bool CheckPathValid(const std::string &bundleNameStr, uint32_t userId);
117 
118     DISALLOW_COPY_AND_MOVE(MountManager);
119 
120     static std::shared_ptr<MountManager> instance_;
121     const std::vector<DirInfo> hmdfsDirVec_;
122     const std::vector<DirInfo> virtualDir_;
123     const std::vector<DirInfo> systemServiceDir_;
124     const std::vector<DirInfo> fileManagerDir_;
125     std::mutex mountMutex_;
126     std::vector<int32_t> fuseToMountUsers_;
127     std::vector<int32_t> fuseMountedUsers_;
128     bool cloudReady_{false};
129 };
130 } // STORAGE_DAEMON
131 } // OHOS
132 
133 #endif // OHOS_STORAGE_DAEMON_USER_MANAGER_H
134