blob: b37a9141251f81f5e839d23cf585883e20b82795 [file] [log] [blame]
Jayanth Othayoth224882b2017-05-04 05:46:45 -05001#include "dump_entry.hpp"
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05002
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05003#include "dump_manager.hpp"
4
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -06005#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 Othayoth224882b2017-05-04 05:46:45 -050017namespace phosphor
18{
19namespace dump
20{
21
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -060022using namespace phosphor::logging;
23
Jayanth Othayoth224882b2017-05-04 05:46:45 -050024void Entry::delete_()
25{
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050026 // Remove Dump entry D-bus object
27 parent.erase(id);
Jayanth Othayoth224882b2017-05-04 05:46:45 -050028}
29
Dhruvaraj Subhashchandran64f8da92021-12-08 03:48:23 -060030sdbusplus::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 Othayoth224882b2017-05-04 05:46:45 -050066} // namespace dump
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050067} // namespace phosphor