Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "dump_manager_faultlog.hpp" |
| 4 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 5 | #include "dump_utils.hpp" |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 6 | #include "faultlog_dump_entry.hpp" |
| 7 | |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/elog.hpp> |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 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 |
| 33 | Manager::createDump(phosphor::dump::DumpCreateParams params) |
| 34 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 35 | lg2::info("In dump_manager_fault.cpp createDump"); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 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 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 42 | lg2::info("No additional parameters received"); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 43 | } |
| 44 | else |
| 45 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 46 | lg2::info("Got additional parameters"); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 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 | |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 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 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 72 | lg2::info("faultLogFile is open"); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 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 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 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); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 86 | elog<Open>(ErrnoOpen(errno), PathOpen(objPath.c_str())); |
| 87 | } |
| 88 | |
| 89 | try |
| 90 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 91 | lg2::info("dump_manager_faultlog.cpp: add faultlog entry"); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 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 | |
Asmitha Karunanithi | 74a1f39 | 2021-10-27 03:23:59 -0500 | [diff] [blame] | 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))); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 105 | } |
| 106 | catch (const std::invalid_argument& e) |
| 107 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 108 | lg2::error("Error in creating dump entry, errormsg: {ERROR}, " |
| 109 | "OBJECTPATH: {OBJECT_PATH}, ID: {ID}", |
| 110 | "ERROR", e, "OBJECT_PATH", objPath, "ID", id); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 111 | elog<InternalFailure>(); |
| 112 | } |
| 113 | |
| 114 | lastEntryId++; |
| 115 | |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 116 | lg2::info("End of dump_manager_faultlog.cpp createDump"); |
Claire Weinan | 919f71c | 2022-03-01 19:02:07 -0800 | [diff] [blame] | 117 | return objPath.string(); |
| 118 | } |
| 119 | |
| 120 | } // namespace faultlog |
| 121 | } // namespace dump |
| 122 | } // namespace phosphor |