1224882b0SJayanth Othayoth #include "config.h"
2cb65ffceSJayanth Othayoth 
38b9b4690SDhruvaraj 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 
11cb65ffceSJayanth Othayoth #include <phosphor-logging/elog-errors.hpp>
12cb65ffceSJayanth Othayoth #include <sdbusplus/bus.hpp>
13*0af74a5eSJayanth Othayoth 
14*0af74a5eSJayanth Othayoth #include <memory>
158b9b4690SDhruvaraj Subhashchandran #include <vector>
16224882b0SJayanth Othayoth 
17bb410df7SRamesh Iyyar int main()
18224882b0SJayanth Othayoth {
19a320c7caSJayanth Othayoth     using namespace phosphor::logging;
20a320c7caSJayanth Othayoth     using InternalFailure =
21a320c7caSJayanth Othayoth         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
22a320c7caSJayanth Othayoth 
23671fc7f3SJayanth Othayoth     auto bus = sdbusplus::bus::new_default();
24a320c7caSJayanth Othayoth     sd_event* event = nullptr;
25a320c7caSJayanth Othayoth     auto rc = sd_event_default(&event);
26a320c7caSJayanth Othayoth     if (rc < 0)
27a320c7caSJayanth Othayoth     {
28a320c7caSJayanth Othayoth         log<level::ERR>("Error occurred during the sd_event_default",
2911eaab7bSGunnar Mills                         entry("RC=%d", rc));
30a320c7caSJayanth Othayoth         report<InternalFailure>();
31a320c7caSJayanth Othayoth         return rc;
32a320c7caSJayanth Othayoth     }
33a320c7caSJayanth Othayoth     phosphor::dump::EventPtr eventP{event};
34a320c7caSJayanth Othayoth     event = nullptr;
35224882b0SJayanth Othayoth 
3619ad0e8aSAlexander Filippov     // Blocking SIGCHLD is needed for calling sd_event_add_child
3719ad0e8aSAlexander Filippov     sigset_t mask;
3819ad0e8aSAlexander Filippov     if (sigemptyset(&mask) < 0)
3919ad0e8aSAlexander Filippov     {
4019ad0e8aSAlexander Filippov         log<level::ERR>("Unable to initialize signal set",
4119ad0e8aSAlexander Filippov                         entry("ERRNO=%d", errno));
4219ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4319ad0e8aSAlexander Filippov     }
4419ad0e8aSAlexander Filippov 
4519ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
4619ad0e8aSAlexander Filippov     {
4719ad0e8aSAlexander Filippov         log<level::ERR>("Unable to add signal to signal set",
4819ad0e8aSAlexander Filippov                         entry("ERRNO=%d", errno));
4919ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5019ad0e8aSAlexander Filippov     }
5119ad0e8aSAlexander Filippov 
5219ad0e8aSAlexander Filippov     // Block SIGCHLD first, so that the event loop can handle it
5319ad0e8aSAlexander Filippov     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
5419ad0e8aSAlexander Filippov     {
5519ad0e8aSAlexander Filippov         log<level::ERR>("Unable to block signal", entry("ERRNO=%d", errno));
5619ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5719ad0e8aSAlexander Filippov     }
5819ad0e8aSAlexander Filippov 
59224882b0SJayanth Othayoth     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
60224882b0SJayanth Othayoth     sdbusplus::server::manager::manager objManager(bus, DUMP_OBJPATH);
61224882b0SJayanth Othayoth     bus.request_name(DUMP_BUSNAME);
62224882b0SJayanth Othayoth 
63a320c7caSJayanth Othayoth     try
64224882b0SJayanth Othayoth     {
658b9b4690SDhruvaraj Subhashchandran         phosphor::dump::DumpManagerList dumpMgrList{};
668b9b4690SDhruvaraj Subhashchandran         std::unique_ptr<phosphor::dump::bmc::Manager> bmcDumpMgr =
678b9b4690SDhruvaraj Subhashchandran             std::make_unique<phosphor::dump::bmc::Manager>(
688b9b4690SDhruvaraj Subhashchandran                 bus, eventP, BMC_DUMP_OBJPATH, BMC_DUMP_OBJ_ENTRY,
698b9b4690SDhruvaraj Subhashchandran                 BMC_DUMP_PATH);
708b9b4690SDhruvaraj Subhashchandran 
718b9b4690SDhruvaraj Subhashchandran         phosphor::dump::bmc::internal::Manager mgr(bus, *bmcDumpMgr,
72fef66a95SDhruvaraj Subhashchandran                                                    OBJ_INTERNAL);
738b9b4690SDhruvaraj Subhashchandran         dumpMgrList.push_back(std::move(bmcDumpMgr));
748b9b4690SDhruvaraj Subhashchandran 
758b9b4690SDhruvaraj Subhashchandran         phosphor::dump::loadExtensions(bus, dumpMgrList);
768b9b4690SDhruvaraj Subhashchandran 
778b9b4690SDhruvaraj Subhashchandran         // Restore dbus objects of all dumps
788b9b4690SDhruvaraj Subhashchandran         for (auto& dmpMgr : dumpMgrList)
798b9b4690SDhruvaraj Subhashchandran         {
808b9b4690SDhruvaraj Subhashchandran             dmpMgr->restore();
818b9b4690SDhruvaraj Subhashchandran         }
828b9b4690SDhruvaraj Subhashchandran 
83d0f0064eSJayanth Othayoth         phosphor::dump::elog::Watch eWatch(bus, mgr);
84a320c7caSJayanth Othayoth         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
85bcb174bdSJayanth Othayoth 
86a320c7caSJayanth Othayoth         auto rc = sd_event_loop(eventP.get());
87a320c7caSJayanth Othayoth         if (rc < 0)
88a320c7caSJayanth Othayoth         {
89a320c7caSJayanth Othayoth             log<level::ERR>("Error occurred during the sd_event_loop",
9011eaab7bSGunnar Mills                             entry("RC=%d", rc));
91a320c7caSJayanth Othayoth             elog<InternalFailure>();
92a320c7caSJayanth Othayoth         }
93a320c7caSJayanth Othayoth     }
94a320c7caSJayanth Othayoth     catch (InternalFailure& e)
95a320c7caSJayanth Othayoth     {
96a320c7caSJayanth Othayoth         commit<InternalFailure>();
97a320c7caSJayanth Othayoth         return -1;
98224882b0SJayanth Othayoth     }
99224882b0SJayanth Othayoth 
100224882b0SJayanth Othayoth     return 0;
101224882b0SJayanth Othayoth }
102