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 #include "event_log_catcher.h"
16 
17 #include <string>
18 
19 #include <climits>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include "securec.h"
26 
27 #include "common_utils.h"
28 #include "defines.h"
29 #include "file_util.h"
30 #include "hiview_logger.h"
31 namespace OHOS {
32 namespace HiviewDFX {
33 DEFINE_LOG_LABEL(0xD002D01, "EventLogger-EventLogCatcher");
34 namespace {
35     constexpr char SED_EXEC_PATH[] = "/system/bin/sed";
36 }
37 
GetName() const38 std::string EventLogCatcher::GetName() const
39 {
40     return name_;
41 }
42 
GetLogSize() const43 int EventLogCatcher::GetLogSize() const
44 {
45     return logSize_;
46 };
47 
SetLogSize(int size)48 void EventLogCatcher::SetLogSize(int size)
49 {
50     logSize_ = size;
51 }
52 
Initialize(const std::string &strParam1 __UNUSED, int intParam1 __UNUSED, int intParam2 __UNUSED)53 bool EventLogCatcher::Initialize(const std::string &strParam1 __UNUSED, int intParam1 __UNUSED, int intParam2 __UNUSED)
54 {
55     useStreamFilter_ = CommonUtils::IsSpecificCmdExist(SED_EXEC_PATH);
56     catcherStartTime_ = time(nullptr);
57     return true;
58 }
59 
Catch(int fd __UNUSED, int jsonFd __UNUSED)60 int EventLogCatcher::Catch(int fd __UNUSED, int jsonFd __UNUSED)
61 {
62     return 0;
63 }
64 
Stop()65 void EventLogCatcher::Stop()
66 {
67     needStop_ = true;
68 }
69 
AppendFile(int fd, const std::string &fileName) const70 int EventLogCatcher::AppendFile(int fd, const std::string &fileName) const
71 {
72     char buf[BUF_SIZE_4096] = {0};
73 
74     if (fd < 0) {
75         HIVIEW_LOGW("parameter err, fd:%{public}d, filename:%{public}s.", fd, fileName.c_str());
76         return 0;
77     }
78 
79     char path[PATH_MAX] = {0};
80     if (realpath(fileName.c_str(), path) == nullptr) {
81         std::string errStr = "canonicalize failed, file name is " + fileName +
82             ", errno is " + std::to_string(errno) + "\r\n";
83         HIVIEW_LOGW("%{public}s", errStr.c_str());
84         FileUtil::SaveStringToFd(fd, errStr);
85         return 0;
86     }
87 
88     if (fileName != std::string(path)) {
89         HIVIEW_LOGW("fail to check consistency.");
90         return 0;
91     }
92 
93     int srcFd = open(path, O_RDONLY);
94     if (srcFd < 0) {
95         HIVIEW_LOGW("open %{public}s failed. errno is %{public}d", fileName.c_str(), errno);
96         return 0;
97     }
98 
99     int wn = 0;
100     while (true) {
101         int rn = read(srcFd, buf, sizeof(buf));
102         if (rn == -1) {
103             if (errno == EAGAIN) {
104                 continue;
105             } else {
106                 break;
107             }
108         } else if (rn == 0) {
109             break;
110         }
111         wn += write(fd, buf, rn);
112     }
113     close(srcFd);
114     return wn;
115 }
116 
GetDescription() const117 std::string EventLogCatcher::GetDescription() const
118 {
119     return description_;
120 }
121 
GetFdSize(int32_t fd)122 int EventLogCatcher::GetFdSize(int32_t fd)
123 {
124     struct stat fileStat;
125     if (fstat(fd, &fileStat) == -1) {
126         return 0;
127     }
128     return fileStat.st_size;
129 }
130 } // namespace HiviewDFX
131 } // namespace OHOS
132