-
Notifications
You must be signed in to change notification settings - Fork 22
/
dump_manager_bmc.cpp
271 lines (242 loc) · 8.51 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
#include "config.h"
#include "dump_manager_bmc.hpp"
#include "bmc_dump_entry.hpp"
#include "dump_internal.hpp"
#include "xyz/openbmc_project/Common/error.hpp"
#include "xyz/openbmc_project/Dump/Create/error.hpp"
#include <fmt/core.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <sdeventplus/exception.hpp>
#include <sdeventplus/source/base.hpp>
#include <cmath>
#include <ctime>
namespace phosphor
{
namespace dump
{
namespace bmc
{
using namespace sdbusplus::xyz::openbmc_project::Common::Error;
using namespace phosphor::logging;
namespace internal
{
/** @brief Flag to reject user intiated dump if a dump is in progress*/
// TODO: https://github.com/openbmc/phosphor-debug-collector/issues/19
static bool fUserDumpInProgress = false;
void Manager::create(Type type, std::vector<std::string> fullPaths)
{
dumpMgr.phosphor::dump::bmc::Manager::captureDump(type, fullPaths);
}
} // namespace internal
sdbusplus::message::object_path
Manager::createDump(phosphor::dump::DumpCreateParams params)
{
using NotAllowed =
sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
if (params.size() > 1)
{
log<level::WARNING>(
"BMC dump accepts not more than 1 additional parameter");
}
if (internal::fUserDumpInProgress == true)
{
elog<NotAllowed>(Reason("User initiated dump is already in progress"));
}
log<level::INFO>("User initiated dump started, setting flag");
internal::fUserDumpInProgress = true;
std::filesystem::path objPath;
try
{
// Get the generator id from params
using InvalidArgument =
sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
using Argument = xyz::openbmc_project::Common::InvalidArgument;
using CreateParameters = sdbusplus::xyz::openbmc_project::Dump::server::
Create::CreateParameters;
std::string generatorId;
auto iter = params.find(
sdbusplus::xyz::openbmc_project::Dump::server::Create::
convertCreateParametersToString(CreateParameters::GeneratorId));
if (iter == params.end())
{
log<level::INFO>(
"GeneratorId is not provided. Replacing the string with null");
}
else
{
try
{
generatorId = std::get<std::string>(iter->second);
}
catch (const std::bad_variant_access& e)
{
// Exception will be raised if the input is not string
log<level::ERR>(
"An invalid generatorId passed. It should be a string",
entry("ERROR_MSG=%s", e.what()));
elog<InvalidArgument>(
Argument::ARGUMENT_NAME("GENERATOR_ID"),
Argument::ARGUMENT_VALUE("INVALID INPUT"));
}
}
std::vector<std::string> paths;
auto id = captureDump(Type::UserRequested, paths);
// Entry Object path.
objPath = std::filesystem::path(baseEntryPath) / std::to_string(id);
std::time_t timeStamp = std::time(nullptr);
createEntry(id, objPath, timeStamp, 0, std::string(), generatorId,
phosphor::dump::OperationStatus::InProgress);
}
catch (const std::exception& ex)
{
log<level::INFO>("User initiated dump exception, resetting flag");
internal::fUserDumpInProgress = false;
log<level::ERR>(
fmt::format("Exception caught errormsg({}), rethrowing", ex.what())
.c_str());
throw;
}
return objPath.string();
}
void Manager::createEntry(const uint32_t id, const std::string objPath,
const uint64_t ms, uint64_t fileSize,
const std::filesystem::path& file,
const std::string& generatorId,
phosphor::dump::OperationStatus status)
{
try
{
entries.insert(
std::make_pair(id, std::make_unique<phosphor::dump::bmc::Entry>(
bus, objPath.c_str(), id, ms, fileSize, file,
generatorId, status, *this)));
}
catch (const std::invalid_argument& e)
{
log<level::ERR>(fmt::format("Error in creating BMC dump entry, "
"errormsg({}), OBJECTPATH({}), ID({})",
e.what(), objPath.c_str(), id)
.c_str());
elog<InternalFailure>();
}
}
void Manager::checkAndInitialize()
{
checkAndCreateCoreDump();
}
void Manager::createEntry(const uint32_t id, const std::string objPath,
const uint64_t ms, uint64_t fileSize,
const std::filesystem::path& file,
phosphor::dump::OperationStatus status)
{
createEntry(id, objPath, ms, fileSize, file, "", status);
}
void Manager::checkAndCreateCoreDump()
{
if (std::filesystem::exists(CORE_FILE_DIR) &&
std::filesystem::is_directory(CORE_FILE_DIR))
{
std::vector<std::string> files;
for (auto const& file :
std::filesystem::directory_iterator(CORE_FILE_DIR))
{
if (std::filesystem::is_regular_file(file) &&
(file.path().filename().string().starts_with("core.")))
{
// Consider only file name start with "core."
files.push_back(file.path().string());
}
}
if (!files.empty())
{
log<level::INFO>(
fmt::format("Core file found, files size {}", files.size())
.c_str());
try
{
captureDump(Type::ApplicationCored, files);
}
catch (const std::exception& excp)
{
log<level::ERR>(
fmt::format("Exception received while capturing dump: "
"{}",
excp.what())
.c_str());
}
}
}
}
uint32_t Manager::captureDump(Type type,
const std::vector<std::string>& fullPaths)
{
// 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;
// get dreport type map entry
auto tempType = TypeMap.find(type);
execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i",
id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p",
fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t",
tempType->second.c_str(), nullptr);
// dreport script execution is failed.
auto error = errno;
log<level::ERR>(
fmt::format(
"Error occurred during dreport function execution, errno({})",
error)
.c_str());
elog<InternalFailure>();
}
else if (pid > 0)
{
Child::Callback callback = [this, type, pid](Child&, const siginfo_t*) {
if (type == Type::UserRequested)
{
log<level::INFO>(
"User initiated dump completed, resetting flag");
internal::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
log<level::ERR>(
fmt::format(
"Error occurred during the sdeventplus::source::Child "
"creation ex({})",
ex.what())
.c_str());
elog<InternalFailure>();
}
}
else
{
auto error = errno;
log<level::ERR>(
fmt::format("Error occurred during fork, errno({})", error)
.c_str());
elog<InternalFailure>();
}
return ++lastEntryId;
}
} // namespace bmc
} // namespace dump
} // namespace phosphor