1 #include "dump_entry.hpp"
2 
3 #include "dump_manager.hpp"
4 
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 
17 namespace phosphor
18 {
19 namespace dump
20 {
21 
22 using namespace phosphor::logging;
23 
24 void Entry::delete_()
25 {
26     // Remove Dump entry D-bus object
27     parent.erase(id);
28 }
29 
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 
66 } // namespace dump
67 } // namespace phosphor
68