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
16 #include "executor/fd_output.h"
17 #include <unistd.h>
18 #include <fcntl.h>
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 const mode_t FDOutput::OPEN_ARGV = 0664;
23
FDOutput()24 FDOutput::FDOutput() : fd_(-1)
25 {
26 }
27
~FDOutput()28 FDOutput::~FDOutput()
29 {
30 if (fd_ >= 0) {
31 close(fd_);
32 }
33 fd_ = -1;
34 }
35
PreExecute(const std::shared_ptr<DumperParameter>& parameter, StringMatrix dumpDatas)36 DumpStatus FDOutput::PreExecute(const std::shared_ptr<DumperParameter>& parameter,
37 StringMatrix dumpDatas)
38 {
39 if (dumpDatas.get()) {
40 dumpDatas_ = dumpDatas;
41 } else {
42 return DumpStatus::DUMP_FAIL;
43 }
44 ptrReqCtl_ = parameter->getClientCallback();
45 path_ = parameter->GetOutputFilePath();
46 if ((fd_ < 0) && (!path_.empty())) {
47 fd_ = open(path_.c_str(), O_WRONLY | O_CREAT | O_APPEND, OPEN_ARGV);
48 if (fd_ < 0) {
49 return DumpStatus::DUMP_FAIL;
50 }
51 }
52 return DumpStatus::DUMP_OK;
53 }
54
Execute()55 DumpStatus FDOutput::Execute()
56 {
57 if ((ptrReqCtl_ != nullptr) && (dumpDatas_ != nullptr)) {
58 OutMethod();
59 } else {
60 DUMPER_HILOGE(MODULE_COMMON, "FDOutput Execute failed");
61 }
62 return DumpStatus::DUMP_OK;
63 }
64
AfterExecute()65 DumpStatus FDOutput::AfterExecute()
66 {
67 std::lock_guard<std::mutex> lock(mutex_);
68 if (dumpDatas_ != nullptr) {
69 dumpDatas_->clear();
70 }
71 return DumpStatus::DUMP_OK;
72 }
73
OutMethod()74 void FDOutput::OutMethod()
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 for (size_t i = 0; i < dumpDatas_->size(); i++) {
78 std::vector<std::string> line = dumpDatas_->at(i);
79 for (size_t j = 0; j < line.size(); j++) {
80 std::string str = line[j];
81 if ((i < dumpDatas_->size()) && (j == (line.size() - 1))) {
82 NewLineMethod(str);
83 }
84 WriteToFd(str);
85 }
86 }
87 }
88
WriteToFd(std::string &str)89 void FDOutput::WriteToFd(std::string &str)
90 {
91 int rawParamFd = ptrReqCtl_->GetOutputFd();
92 if (rawParamFd > -1) {
93 if (write(rawParamFd, str.c_str(), strlen(str.c_str())) == -1) {
94 DUMPER_HILOGE(MODULE_COMMON, "write to rawParamFd failed, errno: %{public}d", errno);
95 }
96 if (fsync(rawParamFd) == -1) {
97 DUMPER_HILOGD(MODULE_COMMON, "fsync to rawParamFd failed, errno: %{public}d", errno);
98 }
99 }
100 if (fd_ > -1) {
101 if (dprintf(fd_, "%s", str.c_str()) == -1) {
102 DUMPER_HILOGE(MODULE_COMMON, "dprintf to fd_ failed, errno: %{public}d", errno);
103 }
104 }
105 }
106
NewLineMethod(std::string &str)107 void FDOutput::NewLineMethod(std::string &str)
108 {
109 if (str.find("\n") == std::string::npos) { // No line breaks
110 str = str + "\n";
111 }
112 }
113 } // namespace HiviewDFX
114 } // namespace OHOS
115