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