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