Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "dump_offload.hpp" |
| 4 | |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 5 | #include <fmt/core.h> |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 6 | #include <sys/socket.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/un.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include <dump_utils.hpp> |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/elog-errors.hpp> |
| 13 | #include <phosphor-logging/elog.hpp> |
| 14 | #include <xyz/openbmc_project/Common/File/error.hpp> |
| 15 | #include <xyz/openbmc_project/Common/error.hpp> |
| 16 | |
Jayanth Othayoth | 0af74a5 | 2021-04-08 03:55:21 -0500 | [diff] [blame] | 17 | #include <fstream> |
| 18 | |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 19 | namespace phosphor |
| 20 | { |
| 21 | namespace dump |
| 22 | { |
| 23 | namespace offload |
| 24 | { |
| 25 | |
| 26 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 27 | using namespace phosphor::logging; |
| 28 | |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 29 | /** @brief API to write data on unix socket. |
| 30 | * |
| 31 | * @param[in] socket - unix socket |
| 32 | * @param[in] buf - buffer |
| 33 | * @param[in] blockSize - size of data |
| 34 | * |
| 35 | * @return void |
| 36 | */ |
| 37 | void writeOnUnixSocket(const int socket, const char* buf, |
| 38 | const uint64_t blockSize) |
| 39 | { |
| 40 | int numOfBytesWrote = 0; |
| 41 | |
| 42 | for (uint64_t i = 0; i < blockSize; i = i + numOfBytesWrote) |
| 43 | { |
| 44 | numOfBytesWrote = 0; |
| 45 | fd_set writeFileDescriptor; |
| 46 | struct timeval timeVal; |
| 47 | timeVal.tv_sec = 5; |
| 48 | timeVal.tv_usec = 0; |
| 49 | |
| 50 | FD_ZERO(&writeFileDescriptor); |
| 51 | FD_SET(socket, &writeFileDescriptor); |
| 52 | int nextFileDescriptor = socket + 1; |
| 53 | |
| 54 | int retVal = select(nextFileDescriptor, NULL, &writeFileDescriptor, |
| 55 | NULL, &timeVal); |
| 56 | if (retVal <= 0) |
| 57 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 58 | log<level::ERR>( |
| 59 | fmt::format("writeOnUnixSocket: select() failed, errno({})", |
| 60 | errno) |
| 61 | .c_str()); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 62 | std::string msg = "select() failed " + std::string(strerror(errno)); |
| 63 | throw std::runtime_error(msg); |
| 64 | } |
| 65 | if ((retVal > 0) && (FD_ISSET(socket, &writeFileDescriptor))) |
| 66 | { |
| 67 | numOfBytesWrote = write(socket, buf + i, blockSize - i); |
| 68 | if (numOfBytesWrote < 0) |
| 69 | { |
| 70 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) |
| 71 | { |
| 72 | numOfBytesWrote = 0; |
| 73 | continue; |
| 74 | } |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 75 | log<level::ERR>( |
| 76 | fmt::format("writeOnUnixSocket: write() failed, errno({})", |
| 77 | errno) |
| 78 | .c_str()); |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 79 | std::string msg = "write() on socket failed " + |
| 80 | std::string(strerror(errno)); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 81 | throw std::runtime_error(msg); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | /**@brief API to setup unix socket. |
| 89 | * |
| 90 | * @param[in] sockPath - unix socket path |
| 91 | * |
| 92 | * @return returns returns socket fd on success |
| 93 | * and on error exception will be thrown |
| 94 | */ |
| 95 | int socketInit(const std::string& sockPath) |
| 96 | { |
| 97 | int unixSocket; |
| 98 | struct sockaddr_un socketAddr; |
| 99 | memset(&socketAddr, 0, sizeof(socketAddr)); |
| 100 | socketAddr.sun_family = AF_UNIX; |
| 101 | if (strnlen(sockPath.c_str(), sizeof(socketAddr.sun_path)) == |
| 102 | sizeof(socketAddr.sun_path)) |
| 103 | { |
| 104 | log<level::ERR>("UNIX socket path too long"); |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 105 | std::string msg = "UNIX socket path is too long " + |
| 106 | std::string(strerror(errno)); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 107 | throw std::length_error(msg); |
| 108 | } |
| 109 | strncpy(socketAddr.sun_path, sockPath.c_str(), |
| 110 | sizeof(socketAddr.sun_path) - 1); |
| 111 | if ((unixSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) |
| 112 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 113 | log<level::ERR>( |
| 114 | fmt::format("socketInit: socket() failed, errno({})", errno) |
| 115 | .c_str()); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 116 | std::string msg = "socket() failed " + std::string(strerror(errno)); |
| 117 | throw std::runtime_error(msg); |
| 118 | } |
| 119 | if (bind(unixSocket, (struct sockaddr*)&socketAddr, sizeof(socketAddr)) == |
| 120 | -1) |
| 121 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 122 | log<level::ERR>( |
| 123 | fmt::format("socketInit: bind() failed, errno({})", errno).c_str()); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 124 | close(unixSocket); |
| 125 | std::string msg = "socket bind failed " + std::string(strerror(errno)); |
| 126 | throw std::runtime_error(msg); |
| 127 | } |
| 128 | if (listen(unixSocket, 1) == -1) |
| 129 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 130 | log<level::ERR>( |
| 131 | fmt::format("socketInit: listen() failed, errno({})", errno) |
| 132 | .c_str()); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 133 | close(unixSocket); |
| 134 | std::string msg = "listen() failed " + std::string(strerror(errno)); |
| 135 | throw std::runtime_error(msg); |
| 136 | } |
| 137 | return unixSocket; |
| 138 | } |
| 139 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 140 | void requestOffload(std::filesystem::path file, uint32_t dumpId, |
| 141 | std::string writePath) |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 142 | { |
| 143 | using namespace sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 144 | using ErrnoOpen = xyz::openbmc_project::Common::File::Open::ERRNO; |
| 145 | using PathOpen = xyz::openbmc_project::Common::File::Open::PATH; |
| 146 | using ErrnoWrite = xyz::openbmc_project::Common::File::Write::ERRNO; |
| 147 | using PathWrite = xyz::openbmc_project::Common::File::Write::PATH; |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 148 | |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 149 | try |
| 150 | { |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 151 | CustomFd unixSocket = socketInit(writePath); |
| 152 | |
| 153 | fd_set readFD; |
| 154 | struct timeval timeVal; |
| 155 | timeVal.tv_sec = 1; |
| 156 | timeVal.tv_usec = 0; |
| 157 | |
| 158 | FD_ZERO(&readFD); |
| 159 | FD_SET(unixSocket(), &readFD); |
| 160 | int numOfFDs = unixSocket() + 1; |
| 161 | |
| 162 | int retVal = select(numOfFDs, &readFD, NULL, NULL, &timeVal); |
| 163 | if (retVal <= 0) |
| 164 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 165 | log<level::ERR>( |
| 166 | fmt::format("select() failed, errno({}), DUMP_ID({})", errno, |
| 167 | dumpId) |
| 168 | .c_str()); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 169 | std::string msg = "select() failed " + std::string(strerror(errno)); |
| 170 | throw std::runtime_error(msg); |
| 171 | } |
| 172 | else if ((retVal > 0) && (FD_ISSET(unixSocket(), &readFD))) |
| 173 | { |
| 174 | CustomFd socketFD = accept(unixSocket(), NULL, NULL); |
| 175 | if (socketFD() < 0) |
| 176 | { |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 177 | log<level::ERR>( |
| 178 | fmt::format("accept() failed, errno({}), DUMP_ID({})", |
| 179 | errno, dumpId) |
| 180 | .c_str()); |
Patrick Williams | 78e8840 | 2023-05-10 07:50:48 -0500 | [diff] [blame] | 181 | std::string msg = "accept() failed " + |
| 182 | std::string(strerror(errno)); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 183 | throw std::runtime_error(msg); |
| 184 | } |
| 185 | |
Dhruvaraj Subhashchandran | ee6f1ba | 2021-05-04 09:44:57 -0500 | [diff] [blame] | 186 | std::ifstream infile{file, std::ios::in | std::ios::binary}; |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 187 | if (!infile.good()) |
| 188 | { |
| 189 | // Unable to open the dump file |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 190 | log<level::ERR>( |
| 191 | fmt::format("Failed to open the dump from file, errno({}), " |
| 192 | "DUMPFILE({}), DUMP_ID({})", |
Dhruvaraj Subhashchandran | ee6f1ba | 2021-05-04 09:44:57 -0500 | [diff] [blame] | 193 | errno, file.c_str(), dumpId) |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 194 | .c_str()); |
Dhruvaraj Subhashchandran | ee6f1ba | 2021-05-04 09:44:57 -0500 | [diff] [blame] | 195 | elog<Open>(ErrnoOpen(errno), PathOpen(file.c_str())); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | infile.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
| 199 | std::ifstream::eofbit); |
| 200 | |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 201 | log<level::INFO>(fmt::format("Opening File for RW, FILENAME({})", |
| 202 | file.filename().c_str()) |
| 203 | .c_str()); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 204 | |
| 205 | std::filebuf* pbuf = infile.rdbuf(); |
| 206 | |
| 207 | // get file size using buffer's members |
| 208 | std::size_t size = pbuf->pubseekoff(0, infile.end, infile.in); |
| 209 | pbuf->pubseekpos(0, infile.in); |
| 210 | |
| 211 | // allocate memory to contain file data |
| 212 | std::unique_ptr<char[]> buffer(new char[size]); |
| 213 | // get file data |
| 214 | pbuf->sgetn(buffer.get(), size); |
| 215 | infile.close(); |
| 216 | |
| 217 | writeOnUnixSocket(socketFD(), buffer.get(), size); |
| 218 | } |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 219 | } |
Patrick Williams | 9d2d722 | 2021-10-06 12:44:44 -0500 | [diff] [blame] | 220 | catch (const std::ifstream::failure& oe) |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 221 | { |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 222 | std::remove(writePath.c_str()); |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 223 | auto err = errno; |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 224 | log<level::ERR>( |
| 225 | fmt::format( |
| 226 | "Failed to open, errormsg({}), OPENINTERFACE({}), DUMP_ID({})", |
Dhruvaraj Subhashchandran | ee6f1ba | 2021-05-04 09:44:57 -0500 | [diff] [blame] | 227 | oe.what(), file.c_str(), dumpId) |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 228 | .c_str()); |
Dhruvaraj Subhashchandran | ee6f1ba | 2021-05-04 09:44:57 -0500 | [diff] [blame] | 229 | elog<Open>(ErrnoOpen(err), PathOpen(file.c_str())); |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 230 | } |
| 231 | catch (const std::exception& e) |
| 232 | { |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 233 | std::remove(writePath.c_str()); |
| 234 | auto err = errno; |
George Liu | 858fbb2 | 2021-07-01 12:25:44 +0800 | [diff] [blame] | 235 | log<level::ERR>(fmt::format("Failed to offload dump, errormsg({}), " |
| 236 | "DUMPFILE({}), DUMP_ID({})", |
| 237 | e.what(), writePath.c_str(), dumpId) |
| 238 | .c_str()); |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 239 | elog<Write>(ErrnoWrite(err), PathWrite(writePath.c_str())); |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 240 | } |
Ravi Teja | ff9c452 | 2020-11-13 05:14:51 -0600 | [diff] [blame] | 241 | std::remove(writePath.c_str()); |
| 242 | return; |
Dhruvaraj Subhashchandran | 580d91d | 2020-04-22 12:29:18 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | } // namespace offload |
| 246 | } // namespace dump |
| 247 | } // namespace phosphor |