blob: bcc016f30af1375f6be37664aa5c8cb904d50dd4 [file] [log] [blame]
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -06001#include "ffdc_file.hpp"
2
Patrick Williamsa9779c42023-05-10 07:50:20 -05003#include <errno.h> // for errno
4#include <fcntl.h> // for open()
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -06005#include <string.h> // for strerror()
6#include <sys/stat.h> // for open()
7#include <sys/types.h> // for open()
8
9#include <phosphor-logging/log.hpp>
10
Patrick Williams40fccd52023-07-17 11:24:09 -050011#include <format>
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060012#include <stdexcept>
13#include <string>
14
15namespace watchdog
16{
17namespace dump
18{
19using namespace phosphor::logging;
20
21FFDCFile::FFDCFile(const json& calloutDataObject) :
22 calloutData(calloutDataObject.dump())
23{
24 prepareFFDCFile();
25}
26
27FFDCFile::~FFDCFile()
28{
29 // Close file descriptor. Does nothing if descriptor was already closed.
30 if (descriptor.close() == -1)
31 {
Patrick Williams40fccd52023-07-17 11:24:09 -050032 log<level::ERR>(std::format("Unable to close FFDC file: errormsg({})",
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060033 strerror(errno))
34 .c_str());
35 }
36
37 // Delete temporary file. Does nothing if file was already deleted.
38 tempFile.remove();
39}
40
41void FFDCFile::prepareFFDCFile()
42{
43 // Open the temporary file for both reading and writing
44 int fd = open(tempFile.getPath().c_str(), O_RDWR);
45 if (fd == -1)
46 {
47 throw std::runtime_error{std::string{"Unable to open FFDC file: "} +
48 strerror(errno)};
49 }
50
51 ssize_t rc = write(fd, calloutData.c_str(), calloutData.size());
52
53 if (rc == -1)
54 {
Patrick Williams40fccd52023-07-17 11:24:09 -050055 log<level::ERR>(std::format("Failed to write callout info "
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060056 "in file({}), errorno({}), errormsg({})",
57 tempFile.getPath().c_str(), errno,
58 strerror(errno))
59 .c_str());
60 throw std::runtime_error("Failed to write phalPELCallouts info");
61 }
62 else if (rc != static_cast<ssize_t>(calloutData.size()))
63 {
Patrick Williams40fccd52023-07-17 11:24:09 -050064 log<level::WARNING>(std::format("Could not write all callout "
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060065 "info in file({}), written byte({}) "
66 "and total byte({})",
67 tempFile.getPath().c_str(), rc,
68 calloutData.size())
69 .c_str());
70 }
71
72 int retCode = lseek(fd, 0, SEEK_SET);
73
74 if (retCode == -1)
75 {
76 log<level::ERR>(
Patrick Williams40fccd52023-07-17 11:24:09 -050077 std::format("Failed to seek file postion to the beginning"
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060078 "in file({}), errorno({}) "
79 "and errormsg({})",
80 tempFile.getPath().c_str(), errno, strerror(errno))
81 .c_str());
82 throw std::runtime_error(
83 "Failed to seek file postion to the beginning of the file");
84 }
85
86 // Store file descriptor in FileDescriptor object
87 descriptor.set(fd);
88}
89
90} // namespace dump
91} // namespace watchdog