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 
GetLogSize() const38 int EventLogCatcher::GetLogSize() const
39 {
40     return logSize_;
41 };
42 
SetLogSize(int size)43 void EventLogCatcher::SetLogSize(int size)
44 {
45     logSize_ = size;
46 }
47 
Initialize(const std::string & strParam1 __UNUSED,int intParam1 __UNUSED,int intParam2 __UNUSED)48 bool EventLogCatcher::Initialize(const std::string &strParam1 __UNUSED, int intParam1 __UNUSED, int intParam2 __UNUSED)
49 {
50     useStreamFilter_ = CommonUtils::IsSpecificCmdExist(SED_EXEC_PATH);
51     catcherStartTime_ = time(nullptr);
52     return true;
53 }
54 
Catch(int fd __UNUSED,int jsonFd __UNUSED)55 int EventLogCatcher::Catch(int fd __UNUSED, int jsonFd __UNUSED)
56 {
57     return 0;
58 }
59 
Stop()60 void EventLogCatcher::Stop()
61 {
62     needStop_ = true;
63 }
64 
AppendFile(int fd,const std::string & fileName) const65 int EventLogCatcher::AppendFile(int fd, const std::string &fileName) const
66 {
67     char buf[BUF_SIZE_4096] = {0};
68 
69     if (fd < 0) {
70         HIVIEW_LOGW("parameter err, fd:%{public}d, filename:%{public}s.", fd, fileName.c_str());
71         return 0;
72     }
73 
74     char path[PATH_MAX] = {0};
75     if (realpath(fileName.c_str(), path) == nullptr) {
76         std::string errStr = "canonicalize failed, file name is " + fileName +
77             ", errno is " + std::to_string(errno) + "\r\n";
78         HIVIEW_LOGW("%{public}s", errStr.c_str());
79         FileUtil::SaveStringToFd(fd, errStr);
80         return 0;
81     }
82 
83     if (fileName != std::string(path)) {
84         HIVIEW_LOGW("fail to check consistency.");
85         return 0;
86     }
87 
88     int srcFd = open(path, O_RDONLY);
89     if (srcFd < 0) {
90         HIVIEW_LOGW("open %{public}s failed. errno is %{public}d", fileName.c_str(), errno);
91         return 0;
92     }
93 
94     int wn = 0;
95     while (true) {
96         int rn = read(srcFd, buf, sizeof(buf));
97         if (rn == -1) {
98             if (errno == EAGAIN) {
99                 continue;
100             } else {
101                 break;
102             }
103         } else if (rn == 0) {
104             break;
105         }
106         wn += write(fd, buf, rn);
107     }
108     close(srcFd);
109     return wn;
110 }
111 
GetDescription() const112 std::string EventLogCatcher::GetDescription() const
113 {
114     return description_;
115 }
116 
GetFdSize(int32_t fd)117 int EventLogCatcher::GetFdSize(int32_t fd)
118 {
119     struct stat fileStat;
120     if (fstat(fd, &fileStat) == -1) {
121         return 0;
122     }
123     return fileStat.st_size;
124 }
125 } // namespace HiviewDFX
126 } // namespace OHOS
127