-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdump_manager_bmc.cpp
365 lines (315 loc) · 11.3 KB
/
dump_manager_bmc.cpp
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "config.h"
#include "dump_manager_bmc.hpp"
#include "bmc_dump_entry.hpp"
#include "dump_types.hpp"
#include "xyz/openbmc_project/Common/error.hpp"
#include "xyz/openbmc_project/Dump/Create/error.hpp"
#include <sys/inotify.h>
#include <unistd.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdeventplus/exception.hpp>
#include <sdeventplus/source/base.hpp>
#include <cmath>
namespace phosphor
{
namespace dump
{
namespace bmc
{
using namespace sdbusplus::xyz::openbmc_project::Common::Error;
using namespace phosphor::logging;
bool Manager::fUserDumpInProgress = false;
constexpr auto BMC_DUMP = "BMC_DUMP";
sdbusplus::message::object_path
Manager::createDump(phosphor::dump::DumpCreateParams params)
{
if (params.size() > CREATE_DUMP_MAX_PARAMS)
{
lg2::warning("BMC dump accepts not more than 2 additional parameters");
}
// Get the originator id and type from params
std::string originatorId;
originatorTypes originatorType;
phosphor::dump::extractOriginatorProperties(params, originatorId,
originatorType);
using CreateParameters =
sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
DumpTypes dumpType = DumpTypes::USER;
std::string type = extractParameter<std::string>(
convertCreateParametersToString(CreateParameters::DumpType), params);
if (!type.empty())
{
dumpType = validateDumpType(type, BMC_DUMP);
}
if (dumpType == DumpTypes::ELOG)
{
dumpType = getErrorDumpType(params);
}
std::string path = extractParameter<std::string>(
convertCreateParametersToString(CreateParameters::FilePath), params);
if ((Manager::fUserDumpInProgress == true) && (dumpType == DumpTypes::USER))
{
lg2::info("Another user initiated dump in progress");
elog<sdbusplus::xyz::openbmc_project::Common::Error::Unavailable>();
}
lg2::info("Initiating new BMC dump with type: {TYPE} path: {PATH}", "TYPE",
dumpTypeToString(dumpType).value_or("unknown").c_str(), "PATH",
path);
auto id = captureDump(dumpType, path);
// Entry Object path.
auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
try
{
uint64_t timeStamp =
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
entries.insert(std::make_pair(
id, std::make_unique<bmc::Entry>(
bus, objPath.c_str(), id, timeStamp, 0, std::string(),
phosphor::dump::OperationStatus::InProgress, originatorId,
originatorType, *this)));
}
catch (const std::invalid_argument& e)
{
lg2::error("Error in creating dump entry, errormsg: {ERROR}, "
"OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
"ERROR", e, "OBJECT_PATH", objPath, "ID", id);
elog<InternalFailure>();
}
if (dumpType == DumpTypes::USER)
{
Manager::fUserDumpInProgress = true;
}
return objPath.string();
}
uint32_t Manager::captureDump(DumpTypes type, const std::string& path)
{
// Get Dump size.
auto size = getAllowedSize();
pid_t pid = fork();
if (pid == 0)
{
std::filesystem::path dumpPath(dumpDir);
auto id = std::to_string(lastEntryId + 1);
dumpPath /= id;
auto strType = dumpTypeToString(type).value_or("unknown");
execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
path.empty() ? "" : path.c_str(), "-t", strType.c_str(), nullptr);
// dreport script execution is failed.
auto error = errno;
lg2::error("Error occurred during dreport function execution, "
"errno: {ERRNO}",
"ERRNO", error);
elog<InternalFailure>();
}
else if (pid > 0)
{
Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
if (type == DumpTypes::USER)
{
lg2::info("User initiated dump completed, resetting flag");
Manager::fUserDumpInProgress = false;
}
this->childPtrMap.erase(pid);
};
try
{
childPtrMap.emplace(pid,
std::make_unique<Child>(eventLoop.get(), pid,
WEXITED | WSTOPPED,
std::move(callback)));
}
catch (const sdeventplus::SdEventError& ex)
{
// Failed to add to event loop
lg2::error(
"Error occurred during the sdeventplus::source::Child creation "
"ex: {ERROR}",
"ERROR", ex);
elog<InternalFailure>();
}
}
else
{
auto error = errno;
lg2::error("Error occurred during fork, errno: {ERRNO}", "ERRNO",
error);
elog<InternalFailure>();
}
return ++lastEntryId;
}
void Manager::createEntry(const std::filesystem::path& file)
{
auto dumpDetails = extractDumpDetails(file);
if (!dumpDetails)
{
lg2::error("Failed to extract dump details from file name: {PATH}",
"PATH", file);
return;
}
auto [id, timestamp, size] = *dumpDetails;
// If there is an existing entry update it and return.
auto dumpEntry = entries.find(id);
if (dumpEntry != entries.end())
{
dynamic_cast<phosphor::dump::bmc::Entry*>(dumpEntry->second.get())
->update(timestamp, std::filesystem::file_size(file), file);
return;
}
// Entry Object path.
auto objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
// TODO: Get the persisted originator id & type
// For now, replacing it with null
try
{
entries.insert(std::make_pair(
id, std::make_unique<bmc::Entry>(
bus, objPath.c_str(), id, timestamp,
std::filesystem::file_size(file), file,
phosphor::dump::OperationStatus::Completed, std::string(),
originatorTypes::Internal, *this)));
}
catch (const std::invalid_argument& e)
{
lg2::error(
"Error in creating dump entry, errormsg: {ERROR}, "
"OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, "
"SIZE: {SIZE}, FILENAME: {FILENAME}",
"ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP",
timestamp, "SIZE", std::filesystem::file_size(file), "FILENAME",
file);
}
}
void Manager::watchCallback(const UserMap& fileInfo)
{
for (const auto& i : fileInfo)
{
// For any new dump file create dump entry object
// and associated inotify watch.
if (IN_CLOSE_WRITE == i.second)
{
if (!std::filesystem::is_directory(i.first))
{
// Don't require filename to be passed, as the path
// of dump directory is stored in the childWatchMap
removeWatch(i.first.parent_path());
// dump file is written now create D-Bus entry
createEntry(i.first);
}
else
{
removeWatch(i.first);
}
}
// Start inotify watch on newly created directory.
else if ((IN_CREATE == i.second) &&
std::filesystem::is_directory(i.first))
{
auto watchObj = std::make_unique<Watch>(
eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first,
std::bind(
std::mem_fn(&phosphor::dump::bmc::Manager::watchCallback),
this, std::placeholders::_1));
childWatchMap.emplace(i.first, std::move(watchObj));
}
}
}
void Manager::removeWatch(const std::filesystem::path& path)
{
// Delete Watch entry from map.
childWatchMap.erase(path);
}
void Manager::restore()
{
std::filesystem::path dir(dumpDir);
if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
{
return;
}
// Dump file path: <DUMP_PATH>/<id>/<filename>
for (const auto& p : std::filesystem::directory_iterator(dir))
{
auto idStr = p.path().filename().string();
// Consider only directories with dump id as name.
// Note: As per design one file per directory.
if ((std::filesystem::is_directory(p.path())) &&
std::all_of(idStr.begin(), idStr.end(), ::isdigit))
{
lastEntryId =
std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr)));
for (const auto& file :
std::filesystem::directory_iterator(p.path()))
{
// Skip .preserve directory
if (file.path().filename() == PRESERVE)
{
continue;
}
// Entry Object path.
auto objPath = std::filesystem::path(baseEntryPath) / idStr;
auto entry = Entry::deserializeEntry(
bus, std::stoul(idStr), objPath.string(), file.path(),
*this);
if (entry != nullptr)
{
entries.insert(
std::make_pair(entry->getDumpId(), std::move(entry)));
}
}
}
}
}
size_t getDirectorySize(const std::string dir)
{
auto size = 0;
for (const auto& p : std::filesystem::recursive_directory_iterator(dir))
{
if (!std::filesystem::is_directory(p))
{
std::uintmax_t fileSize = std::filesystem::file_size(p);
size += std::ceil(static_cast<double>(fileSize) / 1024.0);
}
}
return size;
}
size_t Manager::getAllowedSize()
{
// Get current size of the dump directory.
auto size = getDirectorySize(dumpDir);
// Set the Dump size to Maximum if the free space is greater than
// Dump max size otherwise return the available size.
size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size);
#ifdef BMC_DUMP_ROTATE_CONFIG
// Delete the first existing file until the space is enough
while (size < BMC_DUMP_MIN_SPACE_REQD)
{
auto delEntry = min_element(
entries.begin(), entries.end(),
[](const auto& l, const auto& r) { return l.first < r.first; });
auto delPath = std::filesystem::path(dumpDir) /
std::to_string(delEntry->first);
size += getDirectorySize(delPath);
delEntry->second->delete_();
}
#else
using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
if (size < BMC_DUMP_MIN_SPACE_REQD)
{
// Reached to maximum limit
elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
}
#endif
if (size > BMC_DUMP_MAX_SIZE)
{
size = BMC_DUMP_MAX_SIZE;
}
return size;
}
} // namespace bmc
} // namespace dump
} // namespace phosphor