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