blob: dc3e8699cb79c97a91b4cb7eb654610cd7cb344f [file] [log] [blame]
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05001#include "config.h"
Jayanth Othayothd02153c2017-07-02 22:29:42 -05002
3#include "core_manager.hpp"
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05004
George Liu858fbb22021-07-01 12:25:44 +08005#include <fmt/core.h>
6
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05007#include <phosphor-logging/log.hpp>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05008#include <sdbusplus/exception.hpp>
Jayanth Othayothd02153c2017-07-02 22:29:42 -05009
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050010#include <filesystem>
11#include <regex>
12
Jayanth Othayothd02153c2017-07-02 22:29:42 -050013namespace phosphor
14{
15namespace dump
16{
17namespace core
18{
Jayanth Othayothd02153c2017-07-02 22:29:42 -050019
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050020using namespace phosphor::logging;
21using namespace std;
22
Jayanth Othayothbf6ec602017-08-28 01:48:49 -050023void Manager::watchCallback(const UserMap& fileInfo)
Jayanth Othayothd02153c2017-07-02 22:29:42 -050024{
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050025 vector<string> files;
26
Jayanth Othayothd02153c2017-07-02 22:29:42 -050027 for (const auto& i : fileInfo)
28 {
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050029 std::filesystem::path file(i.first);
Jayanth Othayoth9a56bfa2017-08-26 03:03:47 -050030 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 */
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050040 if ("core" == name.substr(0, name.find('.')))
Jayanth Othayoth9a56bfa2017-08-26 03:03:47 -050041 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050042 // Consider only file name start with "core."
Jayanth Othayoth9a56bfa2017-08-26 03:03:47 -050043 files.push_back(file);
Jayanth Othayothd02153c2017-07-02 22:29:42 -050044 }
45 }
Jayanth Othayoth9a56bfa2017-08-26 03:03:47 -050046
47 if (!files.empty())
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050048 {
49 createHelper(files);
50 }
51}
52
Jayanth Othayothbf6ec602017-08-28 01:48:49 -050053void Manager::createHelper(const vector<string>& files)
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050054{
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 =
Jayanth Othayoth9a56bfa2017-08-26 03:03:47 -050060 "xyz.openbmc_project.Dump.Internal.Create.Type.ApplicationCored";
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050061
62 auto b = sdbusplus::bus::new_default();
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050063 auto mapper = b.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
64 MAPPER_INTERFACE, "GetObject");
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050065 mapper.append(OBJ_INTERNAL, vector<string>({IFACE_INTERNAL}));
66
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050067 map<string, vector<string>> mapperResponse;
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070068 try
69 {
Lei YU0eadeb72021-07-23 15:47:42 +080070 auto mapperResponseMsg = b.call(mapper);
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070071 mapperResponseMsg.read(mapperResponse);
72 }
Patrick Williams52ac69c2021-09-02 09:37:16 -050073 catch (const sdbusplus::exception::exception& e)
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070074 {
Lei YU0eadeb72021-07-23 15:47:42 +080075 log<level::ERR>(
76 fmt::format("Failed to GetObject on Dump.Internal: {}", e.what())
77 .c_str());
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070078 return;
79 }
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050080 if (mapperResponse.empty())
81 {
82 log<level::ERR>("Error reading mapper response");
83 return;
84 }
85
86 const auto& host = mapperResponse.cbegin()->first;
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050087 auto m =
88 b.new_method_call(host.c_str(), OBJ_INTERNAL, IFACE_INTERNAL, "Create");
Jayanth Othayothd3273ea2017-07-12 22:55:32 -050089 m.append(APPLICATION_CORED, files);
Lei YU0eadeb72021-07-23 15:47:42 +080090 try
91 {
92 b.call_noreply(m);
93 }
Patrick Williams52ac69c2021-09-02 09:37:16 -050094 catch (const sdbusplus::exception::exception& e)
Lei YU0eadeb72021-07-23 15:47:42 +080095 {
96 log<level::ERR>(
97 fmt::format("Failed to create dump: {}", e.what()).c_str());
98 }
Jayanth Othayothd02153c2017-07-02 22:29:42 -050099}
100
Jayanth Othayothd02153c2017-07-02 22:29:42 -0500101} // namespace core
102} // namespace dump
103} // namespace phosphor