• 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 "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)55 int EventLogCatcher::Catch(int fd __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         HIVIEW_LOGW("canonicalize failed.");
77         return 0;
78     }
79 
80     if (fileName != std::string(path)) {
81         HIVIEW_LOGW("fail to check consistency.");
82         return 0;
83     }
84 
85     int srcFd = open(path, O_RDONLY);
86     if (srcFd < 0) {
87         HIVIEW_LOGW("open %{public}s failed.", fileName.c_str());
88         return 0;
89     }
90 
91     int wn = 0;
92     while (true) {
93         int rn = read(srcFd, buf, sizeof(buf));
94         if (rn == -1) {
95             if (errno == EAGAIN) {
96                 continue;
97             } else {
98                 break;
99             }
100         } else if (rn == 0) {
101             break;
102         }
103         wn += write(fd, buf, rn);
104     }
105     close(srcFd);
106     return wn;
107 }
108 
GetDescription() const109 std::string EventLogCatcher::GetDescription() const
110 {
111     return description_;
112 }
113 
GetFdSize(int32_t fd)114 int EventLogCatcher::GetFdSize(int32_t fd)
115 {
116     struct stat fileStat;
117     if (fstat(fd, &fileStat) == -1) {
118         return 0;
119     }
120     return fileStat.st_size;
121 }
122 } // namespace HiviewDFX
123 } // namespace OHOS
124