summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/interfaces/bmcweb/0002-Match-BMCWeb-crashdump-to-the-D-Bus-interface-provid.patch
blob: 995b6275040f82763144fbf936d3baaab6786c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
From 7c55dfb33e035f8a31f11fd3e047a4b674f392ac Mon Sep 17 00:00:00 2001
From: Johnathan Mantey <johnathanx.mantey@intel.com>
Date: Tue, 10 Mar 2020 17:15:28 -0700
Subject: [PATCH] Match BMCWeb crashdump to the D-Bus interface provided by
 crashdump

The crashdump service changed to eliminate hangs, and failures to
retrieve the crashdump data. The BMCWeb crashdump handling code has to
be aligned with the server.

Tested:
Confirmed each of the primary functions operates as expected.
Getting the collection
Getting the entries
Forcing an on demand capture
Polling for the on demand capture to complete
Retrieving the creashdump data
Clearing all of the crashdump content

Change-Id: Ie8fb48369a782d905b942c1f9bef11f387f6463e
Signed-off-by: Johnathan Mantey <johnathanx.mantey@intel.com>
---
 redfish-core/lib/log_services.hpp | 268 +++++++++++++++++-------------
 1 file changed, 150 insertions(+), 118 deletions(-)

diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index f864007..4b13897 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -1575,109 +1575,80 @@ class CrashdumpClear : public Node
     }
 };
 
-std::string getLogCreatedTime(const std::string &crashdump)
-{
-    nlohmann::json crashdumpJson =
-        nlohmann::json::parse(crashdump, nullptr, false);
-    if (crashdumpJson.is_discarded())
-    {
-        return std::string();
-    }
-
-    nlohmann::json::const_iterator cdIt = crashdumpJson.find("crash_data");
-    if (cdIt == crashdumpJson.end())
-    {
-        return std::string();
-    }
-
-    nlohmann::json::const_iterator siIt = cdIt->find("METADATA");
-    if (siIt == cdIt->end())
-    {
-        return std::string();
-    }
-
-    nlohmann::json::const_iterator tsIt = siIt->find("timestamp");
-    if (tsIt == siIt->end())
-    {
-        return std::string();
-    }
-
-    const std::string *logTime = tsIt->get_ptr<const std::string *>();
-    if (logTime == nullptr)
-    {
-        return std::string();
-    }
-
-    std::string redfishDateTime = *logTime;
-    if (redfishDateTime.length() > 2)
-    {
-        redfishDateTime.insert(redfishDateTime.end() - 2, ':');
-    }
-
-    return redfishDateTime;
-}
-
-std::string getLogFileName(const std::string &logTime)
-{
-    // Set the crashdump file name to "crashdump_<logTime>.json" using the
-    // created time without the timezone info
-    std::string fileTime = logTime;
-    size_t plusPos = fileTime.rfind('+');
-    if (plusPos != std::string::npos)
-    {
-        fileTime.erase(plusPos);
-    }
-    return "crashdump_" + fileTime + ".json";
-}
-
 static void logCrashdumpEntry(std::shared_ptr<AsyncResp> asyncResp,
                               const std::string &logID,
                               nlohmann::json &logEntryJson)
 {
-    auto getStoredLogCallback = [asyncResp, logID, &logEntryJson](
-                                    const boost::system::error_code ec,
-                                    const std::variant<std::string> &resp) {
-        if (ec)
-        {
-            BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
-            if (ec.value() ==
-                boost::system::linux_error::bad_request_descriptor)
+    auto getStoredLogCallback =
+        [asyncResp, logID, &logEntryJson](
+            const boost::system::error_code ec,
+            const std::vector<std::pair<std::string, VariantType>> &params) {
+            if (ec)
             {
-                messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
+                BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
+                if (ec.value() ==
+                    boost::system::linux_error::bad_request_descriptor)
+                {
+                    messages::resourceNotFound(asyncResp->res, "LogEntry",
+                                               logID);
+                }
+                else
+                {
+                    messages::internalError(asyncResp->res);
+                }
+                return;
             }
-            else
+
+            std::string timestamp{};
+            std::string filename{};
+            for (auto property : params)
             {
-                messages::internalError(asyncResp->res);
+                if (property.first == "Timestamp")
+                {
+                    const std::string *value =
+                        sdbusplus::message::variant_ns::get_if<std::string>(
+                            &property.second);
+                    if (value != nullptr)
+                    {
+                        timestamp = *value;
+                    }
+                }
+                else if (property.first == "Filename")
+                {
+                    const std::string *value =
+                        sdbusplus::message::variant_ns::get_if<std::string>(
+                            &property.second);
+                    if (value != nullptr)
+                    {
+                        filename = *value;
+                    }
+                }
             }
-            return;
-        }
-        const std::string *log = std::get_if<std::string>(&resp);
-        if (log == nullptr)
-        {
-            messages::internalError(asyncResp->res);
-            return;
-        }
-        std::string logTime = getLogCreatedTime(*log);
-        std::string fileName = getLogFileName(logTime);
 
-        logEntryJson = {
-            {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
-            {"@odata.id",
-             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
-                 logID},
-            {"Name", "CPU Crashdump"},
-            {"Id", logID},
-            {"EntryType", "Oem"},
-            {"OemRecordFormat", "Crashdump URI"},
-            {"Message",
-             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
-                 logID + "/" + fileName},
-            {"Created", std::move(logTime)}};
-    };
+            if (filename.empty() || timestamp.empty())
+            {
+                messages::resourceMissingAtURI(asyncResp->res, logID);
+                return;
+            }
+
+            std::string crashdumpURI =
+                "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
+                logID + "/" + filename;
+            logEntryJson = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
+                            {"@odata.id", "/redfish/v1/Systems/system/"
+                                          "LogServices/Crashdump/Entries/" +
+                                              logID},
+                            {"Name", "CPU Crashdump"},
+                            {"Id", logID},
+                            {"EntryType", "Oem"},
+                            {"OemRecordFormat", "Crashdump URI"},
+                            {"Message", std::move(crashdumpURI)},
+                            {"Created", std::move(timestamp)}};
+        };
     crow::connections::systemBus->async_method_call(
         std::move(getStoredLogCallback), crashdumpObject,
         crashdumpPath + std::string("/") + logID,
-        "org.freedesktop.DBus.Properties", "Get", crashdumpInterface, "Log");
+        "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
 }
 
 class CrashdumpEntryCollection : public Node
@@ -1827,38 +1798,99 @@ class CrashdumpFile : public Node
         const std::string &logID = params[0];
         const std::string &fileName = params[1];
 
-        auto getStoredLogCallback = [asyncResp, logID, fileName](
-                                        const boost::system::error_code ec,
-                                        const std::variant<std::string> &resp) {
-            if (ec)
-            {
-                BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
-                messages::internalError(asyncResp->res);
-                return;
-            }
-            const std::string *log = std::get_if<std::string>(&resp);
-            if (log == nullptr)
-            {
-                messages::internalError(asyncResp->res);
-                return;
-            }
+        auto getStoredLogCallback =
+            [asyncResp, logID, fileName](
+                const boost::system::error_code ec,
+                const std::vector<std::pair<std::string, VariantType>> &resp) {
+                if (ec)
+                {
+                    BMCWEB_LOG_DEBUG << "failed to get log ec: "
+                                     << ec.message();
+                    messages::internalError(asyncResp->res);
+                    return;
+                }
 
-            // Verify the file name parameter is correct
-            if (fileName != getLogFileName(getLogCreatedTime(*log)))
-            {
-                messages::resourceMissingAtURI(asyncResp->res, fileName);
-                return;
-            }
+                std::string dbusFilename{};
+                std::string dbusTimestamp{};
+                std::string dbusFilepath{};
 
-            // Configure this to be a file download when accessed from a browser
-            asyncResp->res.addHeader("Content-Disposition", "attachment");
-            asyncResp->res.body() = *log;
-        };
+                for (auto property : resp)
+                {
+                    if (property.first == "Timestamp")
+                    {
+                        const std::string *value =
+                            sdbusplus::message::variant_ns::get_if<std::string>(
+                                &property.second);
+                        if (value != nullptr)
+                        {
+                            dbusTimestamp = *value;
+                        }
+                    }
+                    else if (property.first == "Filename")
+                    {
+                        const std::string *value =
+                            sdbusplus::message::variant_ns::get_if<std::string>(
+                                &property.second);
+                        if (value != nullptr)
+                        {
+                            dbusFilename = *value;
+                        }
+                    }
+                    else if (property.first == "Log")
+                    {
+                        const std::string *value =
+                            sdbusplus::message::variant_ns::get_if<std::string>(
+                                &property.second);
+                        if (value != nullptr)
+                        {
+                            dbusFilepath = *value;
+                        }
+                    }
+                }
+
+                if (dbusFilename.empty() || dbusTimestamp.empty() ||
+                    dbusFilepath.empty())
+                {
+                    messages::resourceMissingAtURI(asyncResp->res, fileName);
+                    return;
+                }
+
+                // Verify the file name parameter is correct
+                if (fileName != dbusFilename)
+                {
+                    messages::resourceMissingAtURI(asyncResp->res, fileName);
+                    return;
+                }
+
+                if (!std::filesystem::exists(dbusFilepath))
+                {
+                    messages::resourceMissingAtURI(asyncResp->res, fileName);
+                    return;
+                }
+                std::ifstream ifs(dbusFilepath, std::ios::in |
+                                                    std::ios::binary |
+                                                    std::ios::ate);
+                std::ifstream::pos_type fileSize = ifs.tellg();
+                if (fileSize < 0)
+                {
+                    messages::generalError(asyncResp->res);
+                    return;
+                }
+                ifs.seekg(0, std::ios::beg);
+
+                std::string dumpData;
+                dumpData.reserve(static_cast<unsigned int>(fileSize));
+                ifs.read(dumpData.data(), static_cast<int>(fileSize));
+
+                // Configure this to be a file download when accessed from
+                // a browser
+                asyncResp->res.addHeader("Content-Disposition", "attachment");
+                asyncResp->res.body() = dumpData.data();
+            };
         crow::connections::systemBus->async_method_call(
             std::move(getStoredLogCallback), crashdumpObject,
             crashdumpPath + std::string("/") + logID,
-            "org.freedesktop.DBus.Properties", "Get", crashdumpInterface,
-            "Log");
+            "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
     }
 };
 
-- 
2.25.1