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