blob: 62cb1e5f18aed6f6587d76f0e8476159657ff528 [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
Dhruvaraj Subhashchandranca9236c2024-04-17 01:56:04 -05009#include <phosphor-logging/lg2.hpp>
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060010
11#include <stdexcept>
12#include <string>
13
14namespace watchdog
15{
16namespace dump
17{
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060018
19FFDCFile::FFDCFile(const json& calloutDataObject) :
20 calloutData(calloutDataObject.dump())
21{
22 prepareFFDCFile();
23}
24
25FFDCFile::~FFDCFile()
26{
27 // Close file descriptor. Does nothing if descriptor was already closed.
28 if (descriptor.close() == -1)
29 {
Dhruvaraj Subhashchandranca9236c2024-04-17 01:56:04 -050030 lg2::error("Unable to close FFDC file: errormsg({ERRORMSG})",
31 "ERRORMSG", strerror(errno));
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060032 }
33
34 // Delete temporary file. Does nothing if file was already deleted.
35 tempFile.remove();
36}
37
38void FFDCFile::prepareFFDCFile()
39{
40 // Open the temporary file for both reading and writing
41 int fd = open(tempFile.getPath().c_str(), O_RDWR);
42 if (fd == -1)
43 {
Patrick Williams540521e2024-08-16 15:20:03 -040044 throw std::runtime_error{
45 std::string{"Unable to open FFDC file: "} + strerror(errno)};
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060046 }
47
48 ssize_t rc = write(fd, calloutData.c_str(), calloutData.size());
49
50 if (rc == -1)
51 {
Dhruvaraj Subhashchandranca9236c2024-04-17 01:56:04 -050052 lg2::error("Failed to write callout info in file({FILE}), "
53 "errorno({ERRNO}), errormsg({ERRORMSG})",
54 "FILE", tempFile.getPath(), "ERRNO", errno, "ERRORMSG",
55 strerror(errno));
56
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060057 throw std::runtime_error("Failed to write phalPELCallouts info");
58 }
59 else if (rc != static_cast<ssize_t>(calloutData.size()))
60 {
Dhruvaraj Subhashchandranca9236c2024-04-17 01:56:04 -050061 lg2::warning("Could not write all callout info in file({FILE}), "
62 "written byte({WRITTEN}), total byte({TOTAL})",
63 "FILE", tempFile.getPath(), "WRITTEN", rc, "TOTAL",
64 calloutData.size());
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060065 }
66
67 int retCode = lseek(fd, 0, SEEK_SET);
68
69 if (retCode == -1)
70 {
Dhruvaraj Subhashchandranca9236c2024-04-17 01:56:04 -050071 lg2::error("Failed to seek file position to the beginning in "
72 "file({FILE}), errorno({ERRNO}), errormsg({ERRORMSG})",
73 "FILE", tempFile.getPath(), "ERRNO", errno, "ERRORMSG",
74 strerror(errno));
75
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060076 throw std::runtime_error(
Manojkiran Edaeb462522024-06-17 11:05:27 +053077 "Failed to seek file position to the beginning of the file");
Shantappa Teekappanavarb64983f2022-02-01 09:28:10 -060078 }
79
80 // Store file descriptor in FileDescriptor object
81 descriptor.set(fd);
82}
83
84} // namespace dump
85} // namespace watchdog