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