-
Notifications
You must be signed in to change notification settings - Fork 22
/
dump_manager_bmcstored.cpp
211 lines (180 loc) · 5.78 KB
/
dump_manager_bmcstored.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
#include "config.h"
#include "bmc_dump_entry.hpp"
#include "dump_internal.hpp"
#include "dump_manager_bmc.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 <cmath>
#include <ctime>
#include <regex>
namespace phosphor
{
namespace dump
{
namespace bmc_stored
{
using namespace sdbusplus::xyz::openbmc_project::Common::Error;
using namespace phosphor::logging;
uint64_t timeToEpoch(std::string timeStr)
{
std::tm t{};
std::istringstream ss(timeStr);
ss >> std::get_time(&t, "%Y%m%d%H%M%S");
if (ss.fail())
{
throw std::runtime_error{"Invalid human readable time value"};
}
return mktime(&t);
}
void Manager::createEntry(const std::filesystem::path& file)
{
std::regex file_regex(dumpFilenameFormat.c_str());
std::smatch match;
std::string name = file.filename();
if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0)))
{
log<level::ERR>(fmt::format("Invalid Dump file name, FILENAME({})",
file.filename().c_str())
.c_str());
return;
}
auto idString = match[FILENAME_DUMP_ID_POS];
auto ts = match[FILENAME_EPOCHTIME_POS];
uint64_t timestamp = 1000 * 1000;
if (TIMESTAMP_FORMAT == 1)
{
timestamp *= timeToEpoch(ts);
}
else
{
timestamp *= stoull(ts);
}
auto id = stoul(idString);
// If there is an existing entry update it and return.
auto dumpEntry = entries.find(id);
if (dumpEntry != entries.end())
{
dynamic_cast<phosphor::dump::bmc_stored::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);
try
{
createEntry(id, objPath, timestamp, std::filesystem::file_size(file),
file, phosphor::dump::OperationStatus::Completed);
}
catch (const InternalFailure& e)
{
log<level::ERR>(
fmt::format(
"Error in creating dump entry, errormsg({}), OBJECTPATH({}), "
"ID({}), TIMESTAMP({}), SIZE({}), FILENAME({})",
e.what(), objPath.c_str(), id, timestamp,
std::filesystem::file_size(file), file.filename().c_str())
.c_str());
return;
}
}
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());
}
else
{
removeWatch(i.first);
}
createEntry(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 directory's 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)));
auto fileIt = std::filesystem::directory_iterator(p.path());
// Create dump entry d-bus object.
if (fileIt != std::filesystem::end(fileIt))
{
createEntry(fileIt->path());
}
}
}
}
size_t Manager::getAllowedSize()
{
using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error;
using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON;
uint64_t size = 0;
// Get current size of the dump directory.
for (const auto& p : std::filesystem::recursive_directory_iterator(dumpDir))
{
if (!std::filesystem::is_directory(p))
{
size += std::ceil(std::filesystem::file_size(p) / 1024.0);
}
}
// Set the Dump size to Maximum if the free space is greater than
// Dump max size otherwise return the available size.
size = (size > allocatedSize ? 0 : allocatedSize - size);
if (size < minDumpSize)
{
// Reached to maximum limit
elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps"));
}
if (size > maxDumpSize)
{
size = maxDumpSize;
}
return size;
}
} // namespace bmc_stored
} // namespace dump
} // namespace phosphor