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