| Shantappa Teekappanavar | b64983f | 2022-02-01 09:28:10 -0600 | [diff] [blame] | 1 | #include "ffdc_file.hpp" | 
 | 2 |  | 
| Patrick Williams | a9779c4 | 2023-05-10 07:50:20 -0500 | [diff] [blame] | 3 | #include <errno.h>     // for errno | 
 | 4 | #include <fcntl.h>     // for open() | 
| Shantappa Teekappanavar | b64983f | 2022-02-01 09:28:10 -0600 | [diff] [blame] | 5 | #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 Williams | 40fccd5 | 2023-07-17 11:24:09 -0500 | [diff] [blame] | 11 | #include <format> | 
| Shantappa Teekappanavar | b64983f | 2022-02-01 09:28:10 -0600 | [diff] [blame] | 12 | #include <stdexcept> | 
 | 13 | #include <string> | 
 | 14 |  | 
 | 15 | namespace watchdog | 
 | 16 | { | 
 | 17 | namespace dump | 
 | 18 | { | 
 | 19 | using namespace phosphor::logging; | 
 | 20 |  | 
 | 21 | FFDCFile::FFDCFile(const json& calloutDataObject) : | 
 | 22 |     calloutData(calloutDataObject.dump()) | 
 | 23 | { | 
 | 24 |     prepareFFDCFile(); | 
 | 25 | } | 
 | 26 |  | 
 | 27 | FFDCFile::~FFDCFile() | 
 | 28 | { | 
 | 29 |     // Close file descriptor.  Does nothing if descriptor was already closed. | 
 | 30 |     if (descriptor.close() == -1) | 
 | 31 |     { | 
| Patrick Williams | 40fccd5 | 2023-07-17 11:24:09 -0500 | [diff] [blame] | 32 |         log<level::ERR>(std::format("Unable to close FFDC file: errormsg({})", | 
| Shantappa Teekappanavar | b64983f | 2022-02-01 09:28:10 -0600 | [diff] [blame] | 33 |                                     strerror(errno)) | 
 | 34 |                             .c_str()); | 
 | 35 |     } | 
 | 36 |  | 
 | 37 |     // Delete temporary file.  Does nothing if file was already deleted. | 
 | 38 |     tempFile.remove(); | 
 | 39 | } | 
 | 40 |  | 
 | 41 | void 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 Williams | 40fccd5 | 2023-07-17 11:24:09 -0500 | [diff] [blame] | 55 |         log<level::ERR>(std::format("Failed to write callout info " | 
| Shantappa Teekappanavar | b64983f | 2022-02-01 09:28:10 -0600 | [diff] [blame] | 56 |                                     "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 Williams | 40fccd5 | 2023-07-17 11:24:09 -0500 | [diff] [blame] | 64 |         log<level::WARNING>(std::format("Could not write all callout " | 
| Shantappa Teekappanavar | b64983f | 2022-02-01 09:28:10 -0600 | [diff] [blame] | 65 |                                         "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 Williams | 40fccd5 | 2023-07-17 11:24:09 -0500 | [diff] [blame] | 77 |             std::format("Failed to seek file postion to the beginning" | 
| Shantappa Teekappanavar | b64983f | 2022-02-01 09:28:10 -0600 | [diff] [blame] | 78 |                         "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 |