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