1 #include "config.h"
2 
3 #include "dump_manager_faultlog.hpp"
4 
5 #include "dump_utils.hpp"
6 #include "faultlog_dump_entry.hpp"
7 
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/elog.hpp>
10 #include <phosphor-logging/lg2.hpp>
11 #include <xyz/openbmc_project/Common/File/error.hpp>
12 #include <xyz/openbmc_project/Common/error.hpp>
13 
14 #include <filesystem>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18 
19 namespace phosphor
20 {
21 namespace dump
22 {
23 namespace faultlog
24 {
25 
26 using namespace phosphor::logging;
27 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
28 using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
29 using ErrnoOpen = xyz::openbmc_project::Common::File::Open::ERRNO;
30 using PathOpen = xyz::openbmc_project::Common::File::Open::PATH;
31 
32 sdbusplus::message::object_path
createDump(phosphor::dump::DumpCreateParams params)33     Manager::createDump(phosphor::dump::DumpCreateParams params)
34 {
35     lg2::info("In dump_manager_fault.cpp createDump");
36 
37     // Currently we ignore the parameters.
38     // TODO phosphor-debug-collector/issues/22: Check parameter values and
39     // exit early if we don't receive the expected parameters
40     if (params.empty())
41     {
42         lg2::info("No additional parameters received");
43     }
44     else
45     {
46         lg2::info("Got additional parameters");
47     }
48 
49     // Get the originator id and type from params
50     std::string originatorId;
51     originatorTypes originatorType;
52 
53     phosphor::dump::extractOriginatorProperties(params, originatorId,
54                                                 originatorType);
55 
56     // Get the id
57     auto id = lastEntryId + 1;
58     auto idString = std::to_string(id);
59     auto objPath = std::filesystem::path(baseEntryPath) / idString;
60 
61     std::filesystem::path faultLogFilePath(std::string(FAULTLOG_DUMP_PATH) +
62                                            idString);
63     std::ofstream faultLogFile;
64 
65     errno = 0;
66 
67     faultLogFile.open(faultLogFilePath,
68                       std::ofstream::out | std::fstream::trunc);
69 
70     if (faultLogFile.is_open())
71     {
72         lg2::info("faultLogFile is open");
73 
74         faultLogFile << "This is faultlog file #" << idString << " at "
75                      << std::string(FAULTLOG_DUMP_PATH) + idString << std::endl;
76 
77         faultLogFile.close();
78     }
79     else
80     {
81         lg2::error(
82             "Failed to open fault log file at {FILE_PATH}, errno: {ERRNO}, "
83             "strerror: {STRERROR}, OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
84             "FILE_PATH", faultLogFilePath, "ERRNO", errno, "STRERROR",
85             strerror(errno), "OBJECT_PATH", objPath, "ID", id);
86         elog<Open>(ErrnoOpen(errno), PathOpen(objPath.c_str()));
87     }
88 
89     try
90     {
91         lg2::info("dump_manager_faultlog.cpp: add faultlog entry");
92 
93         uint64_t timestamp =
94             std::chrono::duration_cast<std::chrono::microseconds>(
95                 std::chrono::system_clock::now().time_since_epoch())
96                 .count();
97 
98         entries.insert(
99             std::make_pair(id, std::make_unique<faultlog::Entry>(
100                                    bus, objPath.c_str(), id, timestamp,
101                                    std::filesystem::file_size(faultLogFilePath),
102                                    faultLogFilePath,
103                                    phosphor::dump::OperationStatus::Completed,
104                                    originatorId, originatorType, *this)));
105     }
106     catch (const std::invalid_argument& e)
107     {
108         lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
109                    "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
110                    "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
111         elog<InternalFailure>();
112     }
113 
114     lastEntryId++;
115 
116     lg2::info("End of dump_manager_faultlog.cpp createDump");
117     return objPath.string();
118 }
119 
120 } // namespace faultlog
121 } // namespace dump
122 } // namespace phosphor
123