1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
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 "hook_service.h"
17 
18 #include "hook_manager.h"
19 #include <cinttypes>
20 #include <unistd.h>
21 
22 #include "common.h"
23 #include "logging.h"
24 #include "parameter.h"
25 #include "socket_context.h"
26 
27 namespace OHOS::Developtools::NativeDaemon {
HookService(const ClientConfig& clientConfig, std::shared_ptr<HookManager> hook, bool multipleProcesses)28 HookService::HookService(const ClientConfig& clientConfig, std::shared_ptr<HookManager> hook, bool multipleProcesses)
29     : clientConfig_(clientConfig), hookMgr_(hook), multipleProcesses_(multipleProcesses)
30 {
31     serviceName_ = "HookService";
32     if (getuid() == 0) {
33         StartService(DEFAULT_UNIX_SOCKET_HOOK_FULL_PATH);
34     } else {
35         StartService(DEFAULT_UNIX_SOCKET_HOOK_PATH);
36     }
37 }
38 
~HookService()39 HookService::~HookService()
40 {
41     serviceEntry_ = nullptr;
42 }
43 
StartService(const std::string& unixSocketName)44 bool HookService::StartService(const std::string& unixSocketName)
45 {
46     serviceEntry_ = std::make_shared<ServiceEntry>();
47     if (!serviceEntry_->StartServer(unixSocketName)) {
48         serviceEntry_ = nullptr;
49         PROFILER_LOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
50         return false;
51     }
52     serviceEntry_->RegisterService(*this);
53     return true;
54 }
55 
ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)56 bool HookService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)
57 {
58     if (size != sizeof(int)) {
59         return false;
60     }
61     int peerConfig = *const_cast<int *>(reinterpret_cast<const int *>(buf));
62     if (peerConfig == -1) {
63         return false;
64     }
65     hookMgr_->SetPid(peerConfig);
66     std::string filePath = "/proc/" + std::to_string(peerConfig) + "/cmdline";
67     std::string bundleName;
68     if (!LoadStringFromFile(filePath, bundleName)) {
69         PROFILER_LOG_ERROR(LOG_CORE, "Get process name by pid failed!, pid: %d", peerConfig);
70         return false;
71     }
72     hookMgr_->SetPid(peerConfig);
73     bundleName.resize(strlen(bundleName.c_str()));
74 
75     if (bundleName.substr(0, 2) == "./") { // 2: size
76         bundleName = bundleName.substr(2); // 2: point
77     }
78     size_t found = bundleName.rfind("/");
79     std::string procName;
80     if (found != std::string::npos) {
81         procName = bundleName.substr(found + 1);
82     } else {
83         procName = bundleName;
84     }
85 
86     std::lock_guard<std::mutex> lock(mtx_);
87     if ((!firstProcess_) && (!multipleProcesses_)) {
88         return false;
89     }
90     firstProcess_ = false;
91     auto [eventFd, smbFd] = hookMgr_->GetFds(peerConfig, procName);
92     if (eventFd == smbFd) {
93         PROFILER_LOG_ERROR(LOG_CORE, "Get eventFd and smbFd failed!, name: %s, pid: %d", procName.c_str(), peerConfig);
94         return false;
95     }
96 
97     PROFILER_LOG_DEBUG(LOG_CORE, "ProtocolProc, receive message from hook client, and send hook config to process %d",
98                        peerConfig);
99     context.SendHookConfig(reinterpret_cast<uint8_t *>(&clientConfig_), sizeof(clientConfig_));
100     context.SendFileDescriptor(smbFd);
101     context.SendFileDescriptor(eventFd);
102     hookMgr_->ResetStartupParam();
103     return true;
104 }
105 }