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 #include <malloc.h>
16 #include "hook_socket_client.h"
17
18 #include "common.h"
19 #include "hook_common.h"
20 #include "unix_socket_client.h"
21 #include "logging.h"
22 #include "sampling.h"
23
24 namespace {
25 constexpr int FLUSH_FLAG = 20;
26 std::atomic<uint64_t> g_flushCount = 0;
27 std::atomic<bool> g_disableHook = false;
28 } // namespace
29
HookSocketClient(int pid,ClientConfig * config,Sampling * sampler,void (* disableHookCallback)())30 HookSocketClient::HookSocketClient(int pid, ClientConfig *config, Sampling *sampler, void (*disableHookCallback)())
31 : pid_(pid), config_(config), sampler_(sampler), disableHookCallback_(disableHookCallback)
32 {
33 smbFd_ = 0;
34 eventFd_ = 0;
35 unixSocketClient_ = nullptr;
36 serviceName_ = "HookService";
37 Connect(DEFAULT_UNIX_SOCKET_HOOK_FULL_PATH);
38 }
39
~HookSocketClient()40 HookSocketClient::~HookSocketClient()
41 {
42 if (stackWriter_) {
43 stackWriter_->Flush();
44 PROFILER_LOG_INFO(LOG_CORE, "~HookSocketClient Flush()");
45 }
46 unixSocketClient_ = nullptr;
47 stackWriter_ = nullptr;
48 }
49
Connect(const std::string addrname)50 bool HookSocketClient::Connect(const std::string addrname)
51 {
52 if (unixSocketClient_ != nullptr) {
53 return false;
54 }
55 unixSocketClient_ = std::make_shared<UnixSocketClient>();
56 if (!unixSocketClient_->Connect(addrname, *this)) {
57 unixSocketClient_ = nullptr;
58 return false;
59 }
60
61 unixSocketClient_->SendHookConfig(reinterpret_cast<uint8_t *>(&pid_), sizeof(pid_));
62 return true;
63 }
64
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)65 bool HookSocketClient::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)
66 {
67 CHECK_TRUE(size == sizeof(ClientConfig), true, "HookSocketClient::config config size not match = %u\n", size);
68 *config_ = *reinterpret_cast<ClientConfig *>(const_cast<int8_t*>(buf));
69 config_->maxStackDepth = config_->maxStackDepth > MAX_UNWIND_DEPTH ? MAX_UNWIND_DEPTH : config_->maxStackDepth;
70 std::string configStr = config_->ToString();
71 PROFILER_LOG_INFO(LOG_CORE, "recv hook client config:%s\n", configStr.c_str());
72 sampler_->InitSampling(config_->sampleInterval);
73 PROFILER_LOG_INFO(LOG_CORE, "%s sample interval %" PRIu64 "", __func__, sampler_->GetSampleInterval());
74 smbFd_ = context.ReceiveFileDiscriptor();
75 eventFd_ = context.ReceiveFileDiscriptor();
76 std::string smbName = "hooknativesmb_" + std::to_string(pid_);
77 stackWriter_ = std::make_shared<StackWriter>(smbName, config_->shareMemorySize,
78 smbFd_, eventFd_, config_->isBlocked);
79 struct mallinfo2 mi = mallinfo2();
80 COMMON::PrintMallinfoLog("stackWriter init(byte) => ", mi);
81 return true;
82 }
83
SendStack(const void * data,size_t size)84 bool HookSocketClient::SendStack(const void* data, size_t size)
85 {
86 if (stackWriter_ == nullptr || unixSocketClient_ == nullptr) {
87 return false;
88 }
89
90 if (!unixSocketClient_->SendHeartBeat()) {
91 return false;
92 }
93
94 stackWriter_->WriteTimeout(data, size);
95 stackWriter_->Flush();
96
97 return true;
98 }
99
SendStackWithPayload(const void * data,size_t size,const void * payload,size_t payloadSize)100 bool HookSocketClient::SendStackWithPayload(const void* data, size_t size, const void* payload,
101 size_t payloadSize)
102 {
103 if (stackWriter_ == nullptr || unixSocketClient_ == nullptr || g_disableHook) {
104 return false;
105 } else if (unixSocketClient_->GetClientState() == CLIENT_STAT_THREAD_EXITED) {
106 DisableHook();
107 return false;
108 }
109
110 bool ret = stackWriter_->WriteWithPayloadTimeout(data, size, payload, payloadSize,
111 std::bind(&HookSocketClient::PeerIsConnected, this));
112 if (!ret && config_->isBlocked) {
113 DisableHook();
114 return false;
115 }
116 ++g_flushCount;
117 if (g_flushCount % FLUSH_FLAG == 0) {
118 stackWriter_->Flush();
119 }
120 return true;
121 }
122
Flush()123 void HookSocketClient::Flush()
124 {
125 if (stackWriter_ == nullptr || unixSocketClient_ == nullptr) {
126 return;
127 }
128 stackWriter_->Flush();
129 }
130
DisableHook()131 void HookSocketClient::DisableHook()
132 {
133 bool expected = false;
134 if (g_disableHook.compare_exchange_strong(expected, true, std::memory_order_release, std::memory_order_relaxed)) {
135 HILOG_INFO(LOG_CORE, "%s %p", __func__, disableHookCallback_);
136 if (disableHookCallback_) {
137 disableHookCallback_();
138 }
139 }
140 }
141
PeerIsConnected()142 bool HookSocketClient::PeerIsConnected()
143 {
144 return !unixSocketClient_->IsConnected();
145 }