Jayanth Othayoth | 224882b | 2017-05-04 05:46:45 -0500 | [diff] [blame] | 1 | #include "dump_entry.hpp" |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 2 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 3 | #include "dump_manager.hpp" |
| 4 | |
Dhruvaraj Subhashchandran | 64f8da9 | 2021-12-08 03:48:23 -0600 | [diff] [blame] | 5 | #include <fcntl.h> |
| 6 | |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/elog.hpp> |
| 9 | #include <phosphor-logging/lg2.hpp> |
| 10 | #include <sdeventplus/event.hpp> |
| 11 | #include <sdeventplus/source/event.hpp> |
| 12 | #include <xyz/openbmc_project/Common/File/error.hpp> |
| 13 | #include <xyz/openbmc_project/Common/error.hpp> |
| 14 | |
| 15 | #include <cstring> |
| 16 | |
Jayanth Othayoth | 224882b | 2017-05-04 05:46:45 -0500 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace dump |
| 20 | { |
| 21 | |
Dhruvaraj Subhashchandran | 64f8da9 | 2021-12-08 03:48:23 -0600 | [diff] [blame] | 22 | using namespace phosphor::logging; |
| 23 | |
Jayanth Othayoth | 224882b | 2017-05-04 05:46:45 -0500 | [diff] [blame] | 24 | void Entry::delete_() |
| 25 | { |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 26 | // Remove Dump entry D-bus object |
| 27 | parent.erase(id); |
Jayanth Othayoth | 224882b | 2017-05-04 05:46:45 -0500 | [diff] [blame] | 28 | } |
| 29 | |
Dhruvaraj Subhashchandran | 64f8da9 | 2021-12-08 03:48:23 -0600 | [diff] [blame] | 30 | sdbusplus::message::unix_fd Entry::getFileHandle() |
| 31 | { |
| 32 | using namespace sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 33 | using metadata = xyz::openbmc_project::Common::File::Open; |
| 34 | if (file.empty()) |
| 35 | { |
| 36 | lg2::error("Failed to get file handle: File path is empty."); |
| 37 | elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>(); |
| 38 | } |
| 39 | |
| 40 | if (fdCloseEventSource) |
| 41 | { |
| 42 | // Return the existing file descriptor |
| 43 | return fdCloseEventSource->first; |
| 44 | } |
| 45 | |
| 46 | int fd = open(file.c_str(), O_RDONLY | O_NONBLOCK); |
| 47 | if (fd == -1) |
| 48 | { |
| 49 | auto err = errno; |
| 50 | lg2::error("Failed to open dump file: id: {ID} error: {ERRNO}", "ID", |
| 51 | id, "ERRNO", std::strerror(errno)); |
| 52 | elog<Open>(metadata::ERRNO(err), metadata::PATH(file.c_str())); |
| 53 | } |
| 54 | |
| 55 | // Create a new Defer event source for closing this fd |
| 56 | sdeventplus::Event event = sdeventplus::Event::get_default(); |
| 57 | auto eventSource = std::make_unique<sdeventplus::source::Defer>( |
| 58 | event, [this](auto& /*source*/) { closeFD(); }); |
| 59 | |
| 60 | // Store the file descriptor and event source in the optional pair |
| 61 | fdCloseEventSource = std::make_pair(fd, std::move(eventSource)); |
| 62 | |
| 63 | return fd; |
| 64 | } |
| 65 | |
Jayanth Othayoth | 224882b | 2017-05-04 05:46:45 -0500 | [diff] [blame] | 66 | } // namespace dump |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 67 | } // namespace phosphor |