1 #include "config.h"
2 
3 #include "core_manager.hpp"
4 
5 #include <fmt/core.h>
6 
7 #include <phosphor-logging/log.hpp>
8 #include <sdbusplus/exception.hpp>
9 
10 #include <filesystem>
11 #include <regex>
12 
13 namespace phosphor
14 {
15 namespace dump
16 {
17 namespace core
18 {
19 
20 using namespace phosphor::logging;
21 using namespace std;
22 
23 void Manager::watchCallback(const UserMap& fileInfo)
24 {
25     vector<string> files;
26 
27     for (const auto& i : fileInfo)
28     {
29         std::filesystem::path file(i.first);
30         std::string name = file.filename();
31 
32         /*
33           As per coredump source code systemd-coredump uses below format
34           https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
35           /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “
36           systemd-coredump also creates temporary file in core file path prior
37           to actual core file creation. Checking the file name format will help
38           to limit dump creation only for the new core files.
39         */
40         if ("core" == name.substr(0, name.find('.')))
41         {
42             // Consider only file name start with "core."
43             files.push_back(file);
44         }
45     }
46 
47     if (!files.empty())
48     {
49         createHelper(files);
50     }
51 }
52 
53 void Manager::createHelper(const vector<string>& files)
54 {
55     constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
56     constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
57     constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
58     constexpr auto IFACE_INTERNAL("xyz.openbmc_project.Dump.Internal.Create");
59     constexpr auto APPLICATION_CORED =
60         "xyz.openbmc_project.Dump.Internal.Create.Type.ApplicationCored";
61 
62     auto b = sdbusplus::bus::new_default();
63     auto mapper = b.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
64                                     MAPPER_INTERFACE, "GetObject");
65     mapper.append(OBJ_INTERNAL, vector<string>({IFACE_INTERNAL}));
66 
67     map<string, vector<string>> mapperResponse;
68     try
69     {
70         auto mapperResponseMsg = b.call(mapper);
71         mapperResponseMsg.read(mapperResponse);
72     }
73     catch (const sdbusplus::exception_t& e)
74     {
75         log<level::ERR>(
76             fmt::format("Failed to GetObject on Dump.Internal: {}", e.what())
77                 .c_str());
78         return;
79     }
80     if (mapperResponse.empty())
81     {
82         log<level::ERR>("Error reading mapper response");
83         return;
84     }
85 
86     const auto& host = mapperResponse.cbegin()->first;
87     auto m = b.new_method_call(host.c_str(), OBJ_INTERNAL, IFACE_INTERNAL,
88                                "Create");
89     m.append(APPLICATION_CORED, files);
90     try
91     {
92         b.call_noreply(m);
93     }
94     catch (const sdbusplus::exception_t& e)
95     {
96         log<level::ERR>(
97             fmt::format("Failed to create dump: {}", e.what()).c_str());
98     }
99 }
100 
101 } // namespace core
102 } // namespace dump
103 } // namespace phosphor
104