blob: 8e0967ed942c9109406d85d12f14f7225a7bf165 [file] [log] [blame]
Claire Weinan919f71c2022-03-01 19:02:07 -08001#include "config.h"
2
3#include "dump_manager_faultlog.hpp"
4
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -05005#include "dump_utils.hpp"
Claire Weinan919f71c2022-03-01 19:02:07 -08006#include "faultlog_dump_entry.hpp"
7
Claire Weinan919f71c2022-03-01 19:02:07 -08008#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/elog.hpp>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050010#include <phosphor-logging/lg2.hpp>
Claire Weinan919f71c2022-03-01 19:02:07 -080011#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
19namespace phosphor
20{
21namespace dump
22{
23namespace faultlog
24{
25
26using namespace phosphor::logging;
27using namespace sdbusplus::xyz::openbmc_project::Common::Error;
28using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
29using ErrnoOpen = xyz::openbmc_project::Common::File::Open::ERRNO;
30using PathOpen = xyz::openbmc_project::Common::File::Open::PATH;
31
32sdbusplus::message::object_path
33 Manager::createDump(phosphor::dump::DumpCreateParams params)
34{
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050035 lg2::info("In dump_manager_fault.cpp createDump");
Claire Weinan919f71c2022-03-01 19:02:07 -080036
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 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050042 lg2::info("No additional parameters received");
Claire Weinan919f71c2022-03-01 19:02:07 -080043 }
44 else
45 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050046 lg2::info("Got additional parameters");
Claire Weinan919f71c2022-03-01 19:02:07 -080047 }
48
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050049 // 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
Claire Weinan919f71c2022-03-01 19:02:07 -080056 // 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 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050072 lg2::info("faultLogFile is open");
Claire Weinan919f71c2022-03-01 19:02:07 -080073
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 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050081 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);
Claire Weinan919f71c2022-03-01 19:02:07 -080086 elog<Open>(ErrnoOpen(errno), PathOpen(objPath.c_str()));
87 }
88
89 try
90 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050091 lg2::info("dump_manager_faultlog.cpp: add faultlog entry");
Claire Weinan919f71c2022-03-01 19:02:07 -080092
93 uint64_t timestamp =
94 std::chrono::duration_cast<std::chrono::microseconds>(
95 std::chrono::system_clock::now().time_since_epoch())
96 .count();
97
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -050098 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)));
Claire Weinan919f71c2022-03-01 19:02:07 -0800105 }
106 catch (const std::invalid_argument& e)
107 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500108 lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
109 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
110 "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
Claire Weinan919f71c2022-03-01 19:02:07 -0800111 elog<InternalFailure>();
112 }
113
114 lastEntryId++;
115
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500116 lg2::info("End of dump_manager_faultlog.cpp createDump");
Claire Weinan919f71c2022-03-01 19:02:07 -0800117 return objPath.string();
118}
119
120} // namespace faultlog
121} // namespace dump
122} // namespace phosphor