1 #include "config.h"
2 
3 #include "dump_manager_system.hpp"
4 
5 #include "dump_utils.hpp"
6 #include "op_dump_consts.hpp"
7 #include "system_dump_entry.hpp"
8 #include "xyz/openbmc_project/Common/error.hpp"
9 
10 #include <fmt/core.h>
11 
12 #include <phosphor-logging/elog-errors.hpp>
13 #include <phosphor-logging/elog.hpp>
14 
15 namespace openpower
16 {
17 namespace dump
18 {
19 namespace system
20 {
21 
22 using namespace phosphor::logging;
23 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
24 
25 void Manager::notify(uint32_t dumpId, uint64_t size)
26 {
27 
28     // Get the timestamp
29     std::time_t timeStamp = std::time(nullptr);
30 
31     // System dump can get created due to a fault in server
32     // or by request from user. A system dump by fault is
33     // first reported here, but for a user requested dump an
34     // entry will be created first with invalid source id.
35     // Since there can be only one system dump creation at a time,
36     // if there is an entry with invalid sourceId update that.
37     for (auto& entry : entries)
38     {
39         openpower::dump::system::Entry* sysEntry =
40             dynamic_cast<openpower::dump::system::Entry*>(entry.second.get());
41         if (sysEntry->sourceDumpId() == INVALID_SOURCE_ID)
42         {
43             sysEntry->update(timeStamp, size, dumpId);
44             return;
45         }
46     }
47 
48     // Get the id
49     auto id = lastEntryId + 1;
50     auto idString = std::to_string(id);
51     auto objPath = std::filesystem::path(baseEntryPath) / idString;
52 
53     try
54     {
55         entries.insert(std::make_pair(
56             id, std::make_unique<system::Entry>(
57                     bus, objPath.c_str(), id, timeStamp, size, dumpId,
58                     phosphor::dump::OperationStatus::Completed, *this)));
59     }
60     catch (const std::invalid_argument& e)
61     {
62         log<level::ERR>(
63             fmt::format(
64                 "Error in creating system dump entry, errormsg({}), "
65                 "OBJECTPATH({}), ID({}), TIMESTAMP({}),SIZE({}), SOURCEID({})",
66                 e.what(), objPath.c_str(), id, timeStamp, size, dumpId)
67                 .c_str());
68         report<InternalFailure>();
69         return;
70     }
71     lastEntryId++;
72     return;
73 }
74 
75 sdbusplus::message::object_path
76     Manager::createDump(phosphor::dump::DumpCreateParams params)
77 {
78     constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
79     constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
80     constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
81     constexpr auto DIAG_MOD_TARGET = "obmc-host-crash@0.target";
82 
83     if (!params.empty())
84     {
85         log<level::WARNING>("System dump accepts no additional parameters");
86     }
87 
88     using NotAllowed =
89         sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
90     using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
91 
92     // Allow creating system dump only when the host is up.
93     if (!phosphor::dump::isHostRunning())
94     {
95         elog<NotAllowed>(
96             Reason("System dump can be initiated only when the host is up"));
97         return std::string();
98     }
99 
100     auto b = sdbusplus::bus::new_default();
101     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
102                                       SYSTEMD_INTERFACE, "StartUnit");
103     method.append(DIAG_MOD_TARGET); // unit to activate
104     method.append("replace");
105     bus.call_noreply(method);
106 
107     auto id = lastEntryId + 1;
108     auto idString = std::to_string(id);
109     auto objPath = std::filesystem::path(baseEntryPath) / idString;
110     std::time_t timeStamp = std::time(nullptr);
111 
112     try
113     {
114         entries.insert(std::make_pair(
115             id, std::make_unique<system::Entry>(
116                     bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
117                     phosphor::dump::OperationStatus::InProgress, *this)));
118     }
119     catch (const std::invalid_argument& e)
120     {
121         log<level::ERR>(
122             fmt::format("Error in creating system dump entry, errormsg({}), "
123                         "OBJECTPATH({}), ID({})",
124                         e.what(), objPath.c_str(), id)
125                 .c_str());
126         elog<InternalFailure>();
127         return std::string();
128     }
129     lastEntryId++;
130     return objPath.string();
131 }
132 
133 } // namespace system
134 } // namespace dump
135 } // namespace openpower
136