1224882b0SJayanth Othayoth #include "config.h"
2cb65ffceSJayanth Othayoth 
3*8b9b4690SDhruvaraj Subhashchandran #include "dump-extensions.hpp"
4a320c7caSJayanth Othayoth #include "dump_internal.hpp"
5cb65ffceSJayanth Othayoth #include "dump_manager.hpp"
6fef66a95SDhruvaraj Subhashchandran #include "dump_manager_bmc.hpp"
7d0f0064eSJayanth Othayoth #include "elog_watch.hpp"
8cb65ffceSJayanth Othayoth #include "watch.hpp"
9cb65ffceSJayanth Othayoth #include "xyz/openbmc_project/Common/error.hpp"
10cb65ffceSJayanth Othayoth 
11*8b9b4690SDhruvaraj Subhashchandran #include <memory>
12cb65ffceSJayanth Othayoth #include <phosphor-logging/elog-errors.hpp>
13cb65ffceSJayanth Othayoth #include <sdbusplus/bus.hpp>
14*8b9b4690SDhruvaraj Subhashchandran #include <vector>
15224882b0SJayanth Othayoth 
16bb410df7SRamesh Iyyar int main()
17224882b0SJayanth Othayoth {
18a320c7caSJayanth Othayoth     using namespace phosphor::logging;
19a320c7caSJayanth Othayoth     using InternalFailure =
20a320c7caSJayanth Othayoth         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
21a320c7caSJayanth Othayoth 
22671fc7f3SJayanth Othayoth     auto bus = sdbusplus::bus::new_default();
23a320c7caSJayanth Othayoth     sd_event* event = nullptr;
24a320c7caSJayanth Othayoth     auto rc = sd_event_default(&event);
25a320c7caSJayanth Othayoth     if (rc < 0)
26a320c7caSJayanth Othayoth     {
27a320c7caSJayanth Othayoth         log<level::ERR>("Error occurred during the sd_event_default",
2811eaab7bSGunnar Mills                         entry("RC=%d", rc));
29a320c7caSJayanth Othayoth         report<InternalFailure>();
30a320c7caSJayanth Othayoth         return rc;
31a320c7caSJayanth Othayoth     }
32a320c7caSJayanth Othayoth     phosphor::dump::EventPtr eventP{event};
33a320c7caSJayanth Othayoth     event = nullptr;
34224882b0SJayanth Othayoth 
3519ad0e8aSAlexander Filippov     // Blocking SIGCHLD is needed for calling sd_event_add_child
3619ad0e8aSAlexander Filippov     sigset_t mask;
3719ad0e8aSAlexander Filippov     if (sigemptyset(&mask) < 0)
3819ad0e8aSAlexander Filippov     {
3919ad0e8aSAlexander Filippov         log<level::ERR>("Unable to initialize signal set",
4019ad0e8aSAlexander Filippov                         entry("ERRNO=%d", errno));
4119ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4219ad0e8aSAlexander Filippov     }
4319ad0e8aSAlexander Filippov 
4419ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
4519ad0e8aSAlexander Filippov     {
4619ad0e8aSAlexander Filippov         log<level::ERR>("Unable to add signal to signal set",
4719ad0e8aSAlexander Filippov                         entry("ERRNO=%d", errno));
4819ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4919ad0e8aSAlexander Filippov     }
5019ad0e8aSAlexander Filippov 
5119ad0e8aSAlexander Filippov     // Block SIGCHLD first, so that the event loop can handle it
5219ad0e8aSAlexander Filippov     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
5319ad0e8aSAlexander Filippov     {
5419ad0e8aSAlexander Filippov         log<level::ERR>("Unable to block signal", entry("ERRNO=%d", errno));
5519ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5619ad0e8aSAlexander Filippov     }
5719ad0e8aSAlexander Filippov 
58224882b0SJayanth Othayoth     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
59224882b0SJayanth Othayoth     sdbusplus::server::manager::manager objManager(bus, DUMP_OBJPATH);
60224882b0SJayanth Othayoth     bus.request_name(DUMP_BUSNAME);
61224882b0SJayanth Othayoth 
62a320c7caSJayanth Othayoth     try
63224882b0SJayanth Othayoth     {
64*8b9b4690SDhruvaraj Subhashchandran         phosphor::dump::DumpManagerList dumpMgrList{};
65*8b9b4690SDhruvaraj Subhashchandran         std::unique_ptr<phosphor::dump::bmc::Manager> bmcDumpMgr =
66*8b9b4690SDhruvaraj Subhashchandran             std::make_unique<phosphor::dump::bmc::Manager>(
67*8b9b4690SDhruvaraj Subhashchandran                 bus, eventP, BMC_DUMP_OBJPATH, BMC_DUMP_OBJ_ENTRY,
68*8b9b4690SDhruvaraj Subhashchandran                 BMC_DUMP_PATH);
69*8b9b4690SDhruvaraj Subhashchandran 
70*8b9b4690SDhruvaraj Subhashchandran         phosphor::dump::bmc::internal::Manager mgr(bus, *bmcDumpMgr,
71fef66a95SDhruvaraj Subhashchandran                                                    OBJ_INTERNAL);
72*8b9b4690SDhruvaraj Subhashchandran         dumpMgrList.push_back(std::move(bmcDumpMgr));
73*8b9b4690SDhruvaraj Subhashchandran 
74*8b9b4690SDhruvaraj Subhashchandran         phosphor::dump::loadExtensions(bus, dumpMgrList);
75*8b9b4690SDhruvaraj Subhashchandran 
76*8b9b4690SDhruvaraj Subhashchandran         // Restore dbus objects of all dumps
77*8b9b4690SDhruvaraj Subhashchandran         for (auto& dmpMgr : dumpMgrList)
78*8b9b4690SDhruvaraj Subhashchandran         {
79*8b9b4690SDhruvaraj Subhashchandran             dmpMgr->restore();
80*8b9b4690SDhruvaraj Subhashchandran         }
81*8b9b4690SDhruvaraj Subhashchandran 
82d0f0064eSJayanth Othayoth         phosphor::dump::elog::Watch eWatch(bus, mgr);
83a320c7caSJayanth Othayoth         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
84bcb174bdSJayanth Othayoth 
85a320c7caSJayanth Othayoth         auto rc = sd_event_loop(eventP.get());
86a320c7caSJayanth Othayoth         if (rc < 0)
87a320c7caSJayanth Othayoth         {
88a320c7caSJayanth Othayoth             log<level::ERR>("Error occurred during the sd_event_loop",
8911eaab7bSGunnar Mills                             entry("RC=%d", rc));
90a320c7caSJayanth Othayoth             elog<InternalFailure>();
91a320c7caSJayanth Othayoth         }
92a320c7caSJayanth Othayoth     }
93a320c7caSJayanth Othayoth     catch (InternalFailure& e)
94a320c7caSJayanth Othayoth     {
95a320c7caSJayanth Othayoth         commit<InternalFailure>();
96a320c7caSJayanth Othayoth         return -1;
97224882b0SJayanth Othayoth     }
98224882b0SJayanth Othayoth 
99224882b0SJayanth Othayoth     return 0;
100224882b0SJayanth Othayoth }
101