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 "disk/disk_config.h"
19 #include "disk/disk_info.h"
20 #include "disk/disk_manager.h"
21 #include "ipc/storage_daemon.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "netlink/netlink_manager.h"
25 #include "storage_service_log.h"
26 #include "system_ability_definition.h"
27 #include "utils/string_utils.h"
28
29 using namespace OHOS;
30
31 const int CONFIG_PARAM_NUM = 6;
32 static const std::string CONFIG_PTAH = "/system/etc/init/config.txt";
33
ParasConfig(StorageDaemon::DiskManager * dm)34 static bool ParasConfig(StorageDaemon::DiskManager *dm)
35 {
36 std::ifstream infile;
37 infile.open(CONFIG_PTAH);
38 if (!infile) {
39 LOGE("Cannot open config");
40 return false;
41 }
42
43 while (infile) {
44 std::string line;
45 std::getline(infile, line);
46 if (line.empty()) {
47 LOGI("Param config complete");
48 break;
49 }
50
51 std::string token = " ";
52 auto split = StorageDaemon::SplitLine(line, token);
53 if (split.size() != CONFIG_PARAM_NUM) {
54 LOGE("Invalids config line: number of parameters is incorrect");
55 continue;
56 }
57
58 auto it = split.begin();
59 if (*it != "sysPattern") {
60 LOGE("Invalids config line: no sysPattern");
61 continue;
62 }
63
64 auto sysPattern = *(++it);
65 if (*(++it) != "label") {
66 LOGE("Invalids config line: no label");
67 continue;
68 }
69
70 auto label = *(++it);
71 if (*(++it) != "flag") {
72 LOGE("Invalids config line: no flag");
73 continue;
74 }
75
76 it++;
77 int flag = std::atoi((*it).c_str());
78 auto diskConfig = std::make_shared<StorageDaemon::DiskConfig>(sysPattern, label, flag);
79 dm->AddDiskConfig(diskConfig);
80 }
81
82 infile.close();
83 return true;
84 }
85
main()86 int main()
87 {
88 LOGI("storage_daemon start");
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 IPCSkeleton::JoinWorkThread();
122
123 return 0;
124 }
125