• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ipc/daemon.h"
17 
18 #include "mountpoint/mount_manager.h"
19 #include "os_account_manager.h"
20 #include "system_ability_definition.h"
21 #include "utils_log.h"
22 
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
26 using namespace std;
27 
28 REGISTER_SYSTEM_ABILITY_BY_ID(Daemon, FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, true);
29 
PublishSA()30 void Daemon::PublishSA()
31 {
32     LOGI("Begin to init");
33     if (!registerToService_) {
34         bool ret = SystemAbility::Publish(this);
35         if (!ret) {
36             throw runtime_error("Failed to publish the daemon");
37         }
38         registerToService_ = true;
39     }
40     LOGI("Init finished successfully");
41 }
42 
RegisterOsAccount()43 void Daemon::RegisterOsAccount()
44 {
45     OHOS::AccountSA::OsAccountSubscribeInfo osAccountSubscribeInfo;
46     osAccountSubscribeInfo.SetOsAccountSubscribeType(OHOS::AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED);
47     osAccountSubscribeInfo.SetName("distributed_file_service");
48 
49     subScriber_ = std::make_shared<OsAccountObserver>(osAccountSubscribeInfo);
50     int ret = OHOS::AccountSA::OsAccountManager::SubscribeOsAccount(subScriber_);
51     if (ret != 0) {
52         LOGE("register os account fail ret %{public}d", ret);
53         return;
54     }
55     LOGI("register os account success, ret %{public}d", ret);
56 }
57 
OnStart()58 void Daemon::OnStart()
59 {
60     LOGI("Begin to start service");
61     if (state_ == ServiceRunningState::STATE_RUNNING) {
62         LOGD("Daemon has already started");
63         return;
64     }
65 
66     try {
67         PublishSA();
68         RegisterOsAccount();
69     } catch (const exception &e) {
70         LOGE("%{public}s", e.what());
71     }
72 
73     state_ = ServiceRunningState::STATE_RUNNING;
74     LOGI("Start service successfully");
75 }
76 
OnStop()77 void Daemon::OnStop()
78 {
79     LOGI("Begin to stop");
80     state_ = ServiceRunningState::STATE_NOT_START;
81     registerToService_ = false;
82     int32_t ret = OHOS::AccountSA::OsAccountManager::UnsubscribeOsAccount(subScriber_);
83     if (ret != 0) {
84         LOGI("UnsubscribeOsAccount failed, ret %{public}d", ret);
85     }
86     subScriber_ = nullptr;
87     LOGI("Stop finished successfully");
88 }
89 
EchoServerDemo(const string & echoStr)90 int32_t Daemon::EchoServerDemo(const string &echoStr)
91 {
92     return 0;
93 }
94 } // namespace DistributedFile
95 } // namespace Storage
96 } // namespace OHOS