• 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 "hook_service.h"
17 
18 #include <cinttypes>
19 #include <unistd.h>
20 
21 #include "logging.h"
22 #include "parameter.h"
23 #include "socket_context.h"
24 
25 namespace {
26 const int BUF_MAX_LEN = 10;
27 }  // namespace
28 
HookService(int smbFd,int eventFd,int pid,std::string processName,uint64_t config)29 HookService::HookService(int smbFd,
30                          int eventFd,
31                          int pid,
32                          std::string processName,
33                          uint64_t config)
34     : smbFd_(smbFd), eventFd_(eventFd), hookConfig_(config), pid_(pid), processName_(processName)
35 {
36     serviceName_ = "HookService";
37     if (getuid() == 0) {
38         StartService(DEFAULT_UNIX_SOCKET_HOOK_FULL_PATH);
39     } else {
40         StartService(DEFAULT_UNIX_SOCKET_HOOK_PATH);
41     }
42 }
43 
~HookService()44 HookService::~HookService()
45 {
46     serviceEntry_ = nullptr;
47 }
48 
StartService(const std::string & unixSocketName)49 bool HookService::StartService(const std::string& unixSocketName)
50 {
51     serviceEntry_ = std::make_shared<ServiceEntry>();
52     if (!serviceEntry_->StartServer(unixSocketName)) {
53         serviceEntry_ = nullptr;
54         HILOG_DEBUG(LOG_CORE, "Start IPC Service FAIL");
55         return false;
56     }
57     serviceEntry_->RegisterService(*this);
58     return true;
59 }
60 
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)61 bool HookService::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)
62 {
63     if (size != sizeof(uint64_t)) {
64         HILOG_ERROR(LOG_CORE, "ProtocolProc hook config error");
65     }
66     uint64_t peerConfig = *const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(buf));
67     if (peerConfig == -1u) {
68         return true;
69     }
70     if (pid_ == 0) {
71         printf("Process %" PRIu64 " hook started.\n", peerConfig);
72         pid_ = static_cast<int>(peerConfig);
73     } else if (peerConfig != (uint64_t)pid_) {
74         HILOG_ERROR(LOG_CORE, "ProtocolProc receive peerConfig:%" PRIu64 " not expected", peerConfig);
75         return false;
76     }
77     HILOG_DEBUG(LOG_CORE, "ProtocolProc, receive message from hook client, and send hook config to process %d", pid_);
78     context.SendHookConfig(hookConfig_);
79     context.SendFileDescriptor(smbFd_);
80     context.SendFileDescriptor(eventFd_);
81     return true;
82 }
83