blob: 93d56dc863e85a2c112c070038dfb18c7f4367ff [file] [log] [blame]
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05001#include "config.h"
2
3#include "dump_manager_system.hpp"
4
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -06005#include "dump_utils.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05006#include "system_dump_entry.hpp"
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -05007#include "xyz/openbmc_project/Common/error.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05008
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -05009#include <phosphor-logging/elog-errors.hpp>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050010#include <phosphor-logging/elog.hpp>
11
12namespace phosphor
13{
14namespace dump
15{
16namespace system
17{
18
19using namespace phosphor::logging;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050020using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050021
Dhruvaraj Subhashchandranf37c5c32020-12-17 22:08:19 -060022void Manager::notify(uint32_t dumpId, uint64_t size)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050023{
24
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050025 // Get the timestamp
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050026 std::time_t timeStamp = std::time(nullptr);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050027
28 // System dump can get created due to a fault in server
29 // or by request from user. A system dump by fault is
30 // first reported here, but for a user requested dump an
31 // entry will be created first with invalid source id.
32 // Since there can be only one system dump creation at a time,
33 // if there is an entry with invalid sourceId update that.
34 for (auto& entry : entries)
35 {
36 phosphor::dump::system::Entry* sysEntry =
37 dynamic_cast<phosphor::dump::system::Entry*>(entry.second.get());
38 if (sysEntry->sourceDumpId() == INVALID_SOURCE_ID)
39 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050040 sysEntry->update(timeStamp, size, dumpId);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050041 return;
42 }
43 }
44
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050045 // Get the id
46 auto id = lastEntryId + 1;
47 auto idString = std::to_string(id);
48 auto objPath = fs::path(baseEntryPath) / idString;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050049
50 try
51 {
52 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050053 id, std::make_unique<system::Entry>(
54 bus, objPath.c_str(), id, timeStamp, size, dumpId,
55 phosphor::dump::OperationStatus::Completed, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050056 }
57 catch (const std::invalid_argument& e)
58 {
59 log<level::ERR>(e.what());
60 log<level::ERR>("Error in creating system dump entry",
61 entry("OBJECTPATH=%s", objPath.c_str()),
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050062 entry("ID=%d", id), entry("TIMESTAMP=%ull", timeStamp),
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050063 entry("SIZE=%d", size), entry("SOURCEID=%d", dumpId));
64 report<InternalFailure>();
65 return;
66 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050067 lastEntryId++;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050068 return;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050069}
70
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050071sdbusplus::message::object_path
72 Manager::createDump(std::map<std::string, std::string> params)
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -050073{
74 constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
75 constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
76 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
77 constexpr auto DIAG_MOD_TARGET = "obmc-host-diagnostic-mode@0.target";
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050078
79 if (!params.empty())
80 {
81 log<level::WARNING>("System dump accepts no additional parameters");
82 }
83
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -060084 using NotAllowed =
85 sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
86 using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
87
88 // Allow creating system dump only when the host is up.
89 if (!phosphor::dump::isHostRunning())
90 {
91 elog<NotAllowed>(
92 Reason("System dump can be initiated only when the host is up"));
93 return std::string();
94 }
95
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -050096 auto b = sdbusplus::bus::new_default();
97 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
98 SYSTEMD_INTERFACE, "StartUnit");
99 method.append(DIAG_MOD_TARGET); // unit to activate
100 method.append("replace");
101 bus.call_noreply(method);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500102
103 auto id = lastEntryId + 1;
104 auto idString = std::to_string(id);
105 auto objPath = fs::path(baseEntryPath) / idString;
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500106 std::time_t timeStamp = std::time(nullptr);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500107
108 try
109 {
110 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500111 id, std::make_unique<system::Entry>(
112 bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
113 phosphor::dump::OperationStatus::InProgress, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500114 }
115 catch (const std::invalid_argument& e)
116 {
117 log<level::ERR>(e.what());
118 log<level::ERR>("Error in creating system dump entry",
119 entry("OBJECTPATH=%s", objPath.c_str()),
120 entry("ID=%d", id));
121 elog<InternalFailure>();
122 return std::string();
123 }
124 lastEntryId++;
125 return objPath.string();
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -0500126}
127
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500128} // namespace system
129} // namespace dump
130} // namespace phosphor