• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 "network_profiler_socket_service.h"
17 
18 #include <cinttypes>
19 #include <unistd.h>
20 
21 #include "socket_context.h"
22 #include "logging.h"
23 #include "network_profiler_manager.h"
24 
25 namespace OHOS::Developtools::Profiler {
NetworkProfilerSocketService(std::shared_ptr<NetworkProfilerManager> networkMgr)26 NetworkProfilerSocketService::NetworkProfilerSocketService(std::shared_ptr<NetworkProfilerManager> networkMgr)
27     : networkMgr_(networkMgr)
28 {
29     serviceName_ = "NetworkProfilerService";
30     StartService(NETWORK_PROFILER_UNIX_SOCKET_PATH);
31 }
32 
~NetworkProfilerSocketService()33 NetworkProfilerSocketService::~NetworkProfilerSocketService()
34 {
35     serviceEntry_ = nullptr;
36 }
37 
StartService(const std::string & unixSocketName)38 bool NetworkProfilerSocketService::StartService(const std::string& unixSocketName)
39 {
40     serviceEntry_ = std::make_shared<ServiceEntry>();
41     if (!serviceEntry_->StartServer(unixSocketName)) {
42         serviceEntry_ = nullptr;
43         PROFILER_LOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
44         return false;
45     }
46     serviceEntry_->RegisterService(*this);
47     return true;
48 }
49 
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)50 bool NetworkProfilerSocketService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf,
51     const uint32_t size)
52 {
53     if (size != sizeof(int)) {
54         return false;
55     }
56     int pid = *const_cast<int *>(reinterpret_cast<const int *>(buf));
57     if (pid == -1) {
58         return false;
59     }
60 
61     std::string bundleName = GetProcessNameByPid(pid);
62 
63     std::lock_guard<std::mutex> lock(mtx_);
64     auto [eventFd, smbFd] = networkMgr_->GetNetworkProfilerCtx(pid, bundleName);
65     if (eventFd == 0 && smbFd == 0) {
66         PROFILER_LOG_INFO(LOG_CORE, "Wait for process %s to restart", bundleName.c_str());
67         return true;
68     }
69     if (eventFd == smbFd) {
70         PROFILER_LOG_ERROR(LOG_CORE, "Get eventFd and smbFd failed!, name: %s, pid: %d", bundleName.c_str(), pid);
71         return false;
72     }
73 
74     PROFILER_LOG_INFO(LOG_CORE, "ProtocolProc, send eventfd: %d, smbFd: %d, peerPid: %d", eventFd, smbFd, pid);
75     context.SendHookConfig(reinterpret_cast<const uint8_t*>(&config_), sizeof(config_));
76     context.SendFileDescriptor(smbFd);
77     context.SendFileDescriptor(eventFd);
78     return true;
79 }
80 
SetConfig(int32_t shmSize,int32_t flushCount,bool block,int32_t clockType)81 void NetworkProfilerSocketService::SetConfig(int32_t shmSize, int32_t flushCount, bool block, int32_t clockType)
82 {
83     config_.shmSize = shmSize;
84     config_.flushCount = flushCount;
85     config_.block = block;
86     config_.clock = clockType;
87 }
88 } // namespace OHOS::Developtools::Profiler