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