1 /*
2 * Copyright (C) 2022 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 #include "mtp_service.h"
16 #include "media_log.h"
17 #include "mtp_file_observer.h"
18 #include <thread>
19
20 #include "accesstoken_kit.h"
21 #include "media_log.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24 using namespace std;
25 namespace OHOS {
26 namespace Media {
27 std::shared_ptr<MtpService> MtpService::mtpServiceInstance_{nullptr};
28 std::mutex MtpService::instanceLock_;
29
SetAccessTokenPermission(const std::string &processName, const std::vector<std::string> &permission, uint64_t &tokenId)30 static void SetAccessTokenPermission(const std::string &processName,
31 const std::vector<std::string> &permission, uint64_t &tokenId)
32 {
33 auto perms = std::make_unique<const char *[]>(permission.size());
34 for (size_t i = 0; i < permission.size(); i++) {
35 perms[i] = permission[i].c_str();
36 }
37
38 NativeTokenInfoParams infoInstance = {
39 .dcapsNum = 0,
40 .permsNum = permission.size(),
41 .aclsNum = 0,
42 .dcaps = nullptr,
43 .perms = perms.get(),
44 .acls = nullptr,
45 .processName = processName.c_str(),
46 .aplStr = "system_basic",
47 };
48 tokenId = GetAccessTokenId(&infoInstance);
49 if (tokenId == 0) {
50 MEDIA_ERR_LOG("Get Acess Token Id Failed");
51 return;
52 }
53 int ret = SetSelfTokenID(tokenId);
54 if (ret != 0) {
55 MEDIA_ERR_LOG("Set Acess Token Id Failed");
56 return;
57 }
58 ret = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
59 if (ret < 0) {
60 MEDIA_ERR_LOG("Reload Native Token Info Failed");
61 return;
62 }
63 }
64
MtpService(void)65 MtpService::MtpService(void) : monitorPtr_(nullptr), isMonitorRun_(false)
66 {
67 }
68
GetInstance()69 std::shared_ptr<MtpService> MtpService::GetInstance()
70 {
71 if (mtpServiceInstance_ == nullptr) {
72 std::lock_guard<std::mutex> lockGuard(instanceLock_);
73 mtpServiceInstance_ = std::shared_ptr<MtpService>(new MtpService());
74 if (mtpServiceInstance_ != nullptr) {
75 mtpServiceInstance_->Init();
76 }
77 }
78
79 return mtpServiceInstance_;
80 }
81
Init()82 void MtpService::Init()
83 {
84 monitorPtr_ = make_shared<MtpMonitor>();
85
86 vector<string> perms;
87 perms.push_back("ohos.permission.READ_MEDIA");
88 perms.push_back("ohos.permission.WRITE_MEDIA");
89 perms.push_back("ohos.permission.MEDIA_LOCATION");
90 perms.push_back("ohos.permission.FILE_ACCESS_MANAGER");
91 perms.push_back("ohos.permission.GET_BUNDLE_INFO_PRIVILEGED");
92 uint64_t tokenId = 0;
93 SetAccessTokenPermission("MTPServerService", perms, tokenId);
94 }
95
StartService()96 void MtpService::StartService()
97 {
98 if (!isMonitorRun_) {
99 monitorPtr_->Start();
100 MtpFileObserver::GetInstance().StartFileInotify();
101 isMonitorRun_ = true;
102 }
103 }
104
StopService()105 void MtpService::StopService()
106 {
107 monitorPtr_->Stop();
108 MtpFileObserver::GetInstance().StopFileInotify();
109 }
110 } // namespace Media
111 } // namespace OHOS