1 /*
2 * Copyright (c) 2025 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 "fault_common_util.h"
17
18 #include <fstream>
19 #include <sys/wait.h>
20 #include <sys/socket.h>
21 #include <sys/syscall.h>
22 #include "dfx_define.h"
23 #include "dfx_log.h"
24 #include "dfx_trace.h"
25 #include "dfx_util.h"
26 #include "fault_logger_daemon.h"
27 #include "procinfo.h"
28 #include "proc_util.h"
29
30 namespace OHOS {
31 namespace HiviewDFX {
32
33 namespace {
34 constexpr const char* const FAULTLOGGERD_SERVICE_TAG = "FAULT_COMMON_UTIL";
35 }
36
37 namespace FaultCommonUtil {
GetUcredByPeerCred(struct ucred & rcred,int32_t connectionFd)38 bool GetUcredByPeerCred(struct ucred& rcred, int32_t connectionFd)
39 {
40 socklen_t credSize = sizeof(rcred);
41 if (getsockopt(connectionFd, SOL_SOCKET, SO_PEERCRED, &rcred, &credSize) != 0) {
42 DFXLOGE("%{public}s :: Failed to GetCredential, errno: %{public}d", FAULTLOGGERD_SERVICE_TAG, errno);
43 return false;
44 }
45 return true;
46 }
47
SendSignalToHapWatchdogThread(pid_t pid,const siginfo_t & si)48 int32_t SendSignalToHapWatchdogThread(pid_t pid, const siginfo_t& si)
49 {
50 long uid = 0;
51 uint64_t sigBlk = 0;
52 if (!GetUidAndSigBlk(pid, uid, sigBlk)) {
53 return ResponseCode::DEFAULT_ERROR_CODE;
54 };
55 constexpr long minUid = 10000; // 10000 : minimum uid for hap
56 if (uid < minUid || IsSigDumpMask(sigBlk)) {
57 return ResponseCode::DEFAULT_ERROR_CODE;
58 }
59
60 pid_t tid = GetTidByThreadName(pid, "OS_DfxWatchdog");
61 if (tid <= 0) {
62 return ResponseCode::DEFAULT_ERROR_CODE;
63 }
64 #ifndef FAULTLOGGERD_TEST
65 if (syscall(SYS_rt_tgsigqueueinfo, pid, tid, si.si_signo, &si) != 0) {
66 DFXLOGE("%{public}s :: Failed to SYS_rt_tgsigqueueinfo signal(%{public}d), errno(%{public}d).",
67 FAULTLOGGERD_SERVICE_TAG, si.si_signo, errno);
68 return ResponseCode::SDK_DUMP_NOPROC;
69 }
70 #endif
71 return ResponseCode::REQUEST_SUCCESS;
72 }
73
SendSignalToProcess(pid_t pid,const siginfo_t & si)74 int32_t SendSignalToProcess(pid_t pid, const siginfo_t& si)
75 {
76 auto ret = SendSignalToHapWatchdogThread(pid, si);
77 if (ret == ResponseCode::SDK_DUMP_NOPROC || ret == ResponseCode::REQUEST_SUCCESS) {
78 return ret;
79 }
80 #ifndef FAULTLOGGERD_TEST
81 if (syscall(SYS_rt_sigqueueinfo, pid, si.si_signo, &si) != 0) {
82 DFXLOGE("%{public}s :: Failed to SYS_rt_sigqueueinfo signal(%{public}d), errno(%{public}d).",
83 FAULTLOGGERD_SERVICE_TAG, si.si_signo, errno);
84 return ResponseCode::SDK_DUMP_NOPROC;
85 }
86 #endif
87 return ResponseCode::REQUEST_SUCCESS;
88 }
89 }
90 }
91 }