1 #include "config.h"
2 
3 #include "dump-extensions.hpp"
4 #include "dump_internal.hpp"
5 #include "dump_manager.hpp"
6 #include "dump_manager_bmc.hpp"
7 #include "elog_watch.hpp"
8 #include "watch.hpp"
9 #include "xyz/openbmc_project/Common/error.hpp"
10 
11 #include <fmt/core.h>
12 
13 #include <phosphor-logging/elog-errors.hpp>
14 #include <sdbusplus/bus.hpp>
15 
16 #include <memory>
17 #include <vector>
18 
19 int main()
20 {
21     using namespace phosphor::logging;
22     using InternalFailure =
23         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
24 
25     auto bus = sdbusplus::bus::new_default();
26     sd_event* event = nullptr;
27     auto rc = sd_event_default(&event);
28     if (rc < 0)
29     {
30         log<level::ERR>(
31             fmt::format("Error occurred during the sd_event_default, rc({})",
32                         rc)
33                 .c_str());
34         report<InternalFailure>();
35         return rc;
36     }
37     phosphor::dump::EventPtr eventP{event};
38     event = nullptr;
39 
40     // Blocking SIGCHLD is needed for calling sd_event_add_child
41     sigset_t mask;
42     if (sigemptyset(&mask) < 0)
43     {
44         log<level::ERR>(
45             fmt::format("Unable to initialize signal set, errno({})", errno)
46                 .c_str());
47         return EXIT_FAILURE;
48     }
49 
50     if (sigaddset(&mask, SIGCHLD) < 0)
51     {
52         log<level::ERR>(
53             fmt::format("Unable to add signal to signal set, errno({})", errno)
54                 .c_str());
55         return EXIT_FAILURE;
56     }
57 
58     // Block SIGCHLD first, so that the event loop can handle it
59     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
60     {
61         log<level::ERR>(
62             fmt::format("Unable to block signal, errno({})", errno).c_str());
63         return EXIT_FAILURE;
64     }
65 
66     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
67     sdbusplus::server::manager::manager objManager(bus, DUMP_OBJPATH);
68 
69     try
70     {
71         phosphor::dump::DumpManagerList dumpMgrList{};
72         std::unique_ptr<phosphor::dump::bmc::Manager> bmcDumpMgr =
73             std::make_unique<phosphor::dump::bmc::Manager>(
74                 bus, eventP, BMC_DUMP_OBJPATH, BMC_DUMP_OBJ_ENTRY,
75                 BMC_DUMP_PATH);
76 
77         phosphor::dump::bmc::internal::Manager mgr(bus, *bmcDumpMgr,
78                                                    OBJ_INTERNAL);
79         dumpMgrList.push_back(std::move(bmcDumpMgr));
80 
81         phosphor::dump::loadExtensions(bus, dumpMgrList);
82 
83         // Restore dbus objects of all dumps
84         for (auto& dmpMgr : dumpMgrList)
85         {
86             dmpMgr->restore();
87         }
88 
89         phosphor::dump::elog::Watch eWatch(bus, mgr);
90         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
91 
92         // Daemon is all set up so claim the busname now.
93         bus.request_name(DUMP_BUSNAME);
94 
95         auto rc = sd_event_loop(eventP.get());
96         if (rc < 0)
97         {
98             log<level::ERR>(
99                 fmt::format("Error occurred during the sd_event_loop, rc({})",
100                             rc)
101                     .c_str());
102             elog<InternalFailure>();
103         }
104     }
105     catch (const InternalFailure& e)
106     {
107         commit<InternalFailure>();
108         return -1;
109     }
110 
111     return 0;
112 }
113