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