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