Implement GetFileHandle in dump entry interface

Certain applications need to offload the dump in various
ways, for example, performing DMA transfer of the dump to
the host memory. These applications require direct access
to the file to read from the BMC storage.

This commit introduces the GetFileHandle method in the
dump entry interface. This method provides direct access
to the Unix file handle of the dump entry, facilitating
more direct read operations on the dump file.
The returned file descriptor is read-only, which ensures
the integrity of the dump file.

The definition of the file handle is moved from the
specific 'bmc_dump_entry' class to the parent 'dump_entry'
class. This allows all dump types inheriting from
'dump_entry' to use this new method, thus increasing
its utility across various dump types.

The introduction of this method primarily benefits PLDM
for offloading the dump to the host, and bmcweb for
offloading the dump to Redfish clients. By providing
a file handle instead of exposing the file path,
it resolves potential access issues for applications
running in non-root contexts.

Test:
Created a dump and made sure it reached host
successfully
Executed with additional traces
Jun 21 10:56:09 pldmd[1856]: Requesting file handle
Jun 21 10:56:09 pldmd[1856]: File handle received fd=9
Jun 21 10:56:09 pldmd[1856]: Transfer data rc= 0
Jun 21 10:56:09 phosphor-dump-manager[480]: File handle \
request
Jun 21 10:56:09 phosphor-dump-manager[480]: returning fd=11
Jun 21 10:56:09 pldmd[1856]: File handle received fd=9
Jun 21 10:56:09 pldmd[1856]: File read rc=15

In th host:
Service Processor Dump:
  Maximum file size:           0 (0x00000000) bytes (0 MBs)
  Dumps this PHYP IPL:         1
 Pending Dump:
  Dump ID:                     00000009
  File size:                   3749662 \
(0x000000000039371E) bytes (3 MBs)
  SP dump sub type:            0x000000000000000F BMC
  File name:                   \
BMCDUMP.1392A20.00000009.20230621105510
  Dump create time:            06/21/2023 10:55:10.00
  PHYP notify time:            06/21/2023 10:56:00.5918570000
  LP recipient:                1
  LP notify time:              06/21/2023 10:56:00.5918580000

Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
Change-Id: If398940b655b4d90688273f0c76a20d4ead61992
diff --git a/dump_entry.cpp b/dump_entry.cpp
index 0ecb9cb..b37a914 100644
--- a/dump_entry.cpp
+++ b/dump_entry.cpp
@@ -2,16 +2,66 @@
 
 #include "dump_manager.hpp"
 
+#include <fcntl.h>
+
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/lg2.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/event.hpp>
+#include <xyz/openbmc_project/Common/File/error.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+#include <cstring>
+
 namespace phosphor
 {
 namespace dump
 {
 
+using namespace phosphor::logging;
+
 void Entry::delete_()
 {
     // Remove Dump entry D-bus object
     parent.erase(id);
 }
 
+sdbusplus::message::unix_fd Entry::getFileHandle()
+{
+    using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
+    using metadata = xyz::openbmc_project::Common::File::Open;
+    if (file.empty())
+    {
+        lg2::error("Failed to get file handle: File path is empty.");
+        elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
+    }
+
+    if (fdCloseEventSource)
+    {
+        // Return the existing file descriptor
+        return fdCloseEventSource->first;
+    }
+
+    int fd = open(file.c_str(), O_RDONLY | O_NONBLOCK);
+    if (fd == -1)
+    {
+        auto err = errno;
+        lg2::error("Failed to open dump file: id: {ID} error: {ERRNO}", "ID",
+                   id, "ERRNO", std::strerror(errno));
+        elog<Open>(metadata::ERRNO(err), metadata::PATH(file.c_str()));
+    }
+
+    // Create a new Defer event source for closing this fd
+    sdeventplus::Event event = sdeventplus::Event::get_default();
+    auto eventSource = std::make_unique<sdeventplus::source::Defer>(
+        event, [this](auto& /*source*/) { closeFD(); });
+
+    // Store the file descriptor and event source in the optional pair
+    fdCloseEventSource = std::make_pair(fd, std::move(eventSource));
+
+    return fd;
+}
+
 } // namespace dump
 } // namespace phosphor