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 #include <fstream>
17 #include <unistd.h>
18 
19 #ifdef EXTERNAL_STORAGE_MANAGER
20 #include "disk/disk_config.h"
21 #include "disk/disk_info.h"
22 #include "disk/disk_manager.h"
23 #include "netlink/netlink_manager.h"
24 #endif
25 #include "ipc/storage_daemon.h"
26 #include "ipc_skeleton.h"
27 #include "iservice_registry.h"
28 #include "storage_service_errno.h"
29 #include "storage_service_log.h"
30 #include "system_ability_definition.h"
31 #include "user/user_manager.h"
32 #include "utils/string_utils.h"
33 #ifdef DFS_SERVICE
34 #include "cloud_daemon_manager.h"
35 #endif
36 
37 using namespace OHOS;
38 #ifdef DFS_SERVICE
39 using namespace OHOS::FileManagement::CloudFile;
40 #endif
41 using CloudListener = StorageDaemon::StorageDaemon::SystemAbilityStatusChangeListener;
42 
43 #ifdef EXTERNAL_STORAGE_MANAGER
44 const int CONFIG_PARAM_NUM = 6;
45 static const std::string CONFIG_PTAH = "/system/etc/storage_daemon/disk_config";
46 
ParasConfig(StorageDaemon::DiskManager * dm)47 static bool ParasConfig(StorageDaemon::DiskManager *dm)
48 {
49     if (dm == nullptr) {
50         LOGE("Unable to get DiskManger");
51         return false;
52     }
53     std::ifstream infile;
54     infile.open(CONFIG_PTAH);
55     if (!infile) {
56         LOGE("Cannot open config");
57         return false;
58     }
59 
60     while (infile) {
61         std::string line;
62         std::getline(infile, line);
63         if (line.empty()) {
64             LOGI("Param config complete");
65             break;
66         }
67 
68         std::string token = " ";
69         auto split = StorageDaemon::SplitLine(line, token);
70         if (split.size() != CONFIG_PARAM_NUM) {
71             LOGE("Invalids config line: number of parameters is incorrect");
72             continue;
73         }
74 
75         auto it = split.begin();
76         if (*it != "sysPattern") {
77             LOGE("Invalids config line: no sysPattern");
78             continue;
79         }
80 
81         auto sysPattern = *(++it);
82         if (*(++it) != "label") {
83             LOGE("Invalids config line: no label");
84             continue;
85         }
86 
87         auto label = *(++it);
88         if (*(++it) != "flag") {
89             LOGE("Invalids config line: no flag");
90             continue;
91         }
92 
93         it++;
94         int flag = std::atoi((*it).c_str());
95         auto diskConfig =  std::make_shared<StorageDaemon::DiskConfig>(sysPattern, label, flag);
96         dm->AddDiskConfig(diskConfig);
97     }
98 
99     infile.close();
100     return true;
101 }
102 #endif
103 
104 static const int32_t SLEEP_TIME_INTERVAL_3MS = 3 * 1000;
105 
main()106 int main()
107 {
108     LOGW("storage_daemon start");
109     do {
110         auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
111         if (samgr != nullptr) {
112             LOGE("samgr is not null");
113             sptr<StorageDaemon::StorageDaemon> sd(new StorageDaemon::StorageDaemon());
114             int ret = samgr->AddSystemAbility(STORAGE_MANAGER_DAEMON_ID, sd);
115             LOGI("AddSystemAbility: ret: %{public}d, errno: %{public}d", ret, errno);
116             sptr<CloudListener> listenter(new CloudListener());
117             ret = samgr->SubscribeSystemAbility(FILEMANAGEMENT_CLOUD_DAEMON_SERVICE_SA_ID, listenter);
118             LOGI("SubscribeSystemAbility for CLOUD_DAEMON_SERVICE: ret: %{public}d, errno: %{public}d", ret, errno);
119             ret = samgr->SubscribeSystemAbility(ACCESS_TOKEN_MANAGER_SERVICE_ID, listenter);
120             LOGI("SubscribeSystemAbility for MANAGER_SERVICE: ret: %{public}d, errno: %{public}d", ret, errno);
121             break;
122         }
123         usleep(SLEEP_TIME_INTERVAL_3MS);
124     } while (true);
125     LOGW("samgr GetSystemAbilityManager finish");
126 
127 #ifdef EXTERNAL_STORAGE_MANAGER
128     StorageDaemon::NetlinkManager *nm = StorageDaemon::NetlinkManager::Instance();
129     if (!nm) {
130         LOGE("Unable to create NetlinkManager");
131         return -1;
132     };
133 
134     if (nm->Start()) {
135         LOGE("Unable to start NetlinkManager");
136         return -1;
137     }
138 
139     StorageDaemon::DiskManager *dm = StorageDaemon::DiskManager::Instance();
140     if (!dm) {
141         LOGE("Unable to create DiskManger");
142         return -1;
143     }
144 
145     if (!ParasConfig(dm)) {
146         LOGE("Paras config failed");
147         return -1;
148     }
149 #endif
150     LOGW("storage_daemon main function execute finish.");
151     IPCSkeleton::JoinWorkThread();
152     return 0;
153 }
154