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