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 
11858fbb2eSGeorge Liu #include <fmt/core.h>
12858fbb2eSGeorge Liu 
13cb65ffceSJayanth Othayoth #include <phosphor-logging/elog-errors.hpp>
14cb65ffceSJayanth Othayoth #include <sdbusplus/bus.hpp>
150af74a5eSJayanth Othayoth 
160af74a5eSJayanth Othayoth #include <memory>
178b9b4690SDhruvaraj Subhashchandran #include <vector>
18224882b0SJayanth Othayoth 
19bb410df7SRamesh Iyyar int main()
20224882b0SJayanth Othayoth {
21a320c7caSJayanth Othayoth     using namespace phosphor::logging;
22a320c7caSJayanth Othayoth     using InternalFailure =
23a320c7caSJayanth Othayoth         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
24a320c7caSJayanth Othayoth 
25671fc7f3SJayanth Othayoth     auto bus = sdbusplus::bus::new_default();
26a320c7caSJayanth Othayoth     sd_event* event = nullptr;
27a320c7caSJayanth Othayoth     auto rc = sd_event_default(&event);
28a320c7caSJayanth Othayoth     if (rc < 0)
29a320c7caSJayanth Othayoth     {
30858fbb2eSGeorge Liu         log<level::ERR>(
31858fbb2eSGeorge Liu             fmt::format("Error occurred during the sd_event_default, rc({})",
32858fbb2eSGeorge Liu                         rc)
33858fbb2eSGeorge Liu                 .c_str());
34a320c7caSJayanth Othayoth         report<InternalFailure>();
35a320c7caSJayanth Othayoth         return rc;
36a320c7caSJayanth Othayoth     }
37a320c7caSJayanth Othayoth     phosphor::dump::EventPtr eventP{event};
38a320c7caSJayanth Othayoth     event = nullptr;
39224882b0SJayanth Othayoth 
4019ad0e8aSAlexander Filippov     // Blocking SIGCHLD is needed for calling sd_event_add_child
4119ad0e8aSAlexander Filippov     sigset_t mask;
4219ad0e8aSAlexander Filippov     if (sigemptyset(&mask) < 0)
4319ad0e8aSAlexander Filippov     {
44858fbb2eSGeorge Liu         log<level::ERR>(
45858fbb2eSGeorge Liu             fmt::format("Unable to initialize signal set, errno({})", errno)
46858fbb2eSGeorge Liu                 .c_str());
4719ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4819ad0e8aSAlexander Filippov     }
4919ad0e8aSAlexander Filippov 
5019ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
5119ad0e8aSAlexander Filippov     {
52858fbb2eSGeorge Liu         log<level::ERR>(
53858fbb2eSGeorge Liu             fmt::format("Unable to add signal to signal set, errno({})", errno)
54858fbb2eSGeorge Liu                 .c_str());
5519ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5619ad0e8aSAlexander Filippov     }
5719ad0e8aSAlexander Filippov 
5819ad0e8aSAlexander Filippov     // Block SIGCHLD first, so that the event loop can handle it
5919ad0e8aSAlexander Filippov     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
6019ad0e8aSAlexander Filippov     {
61858fbb2eSGeorge Liu         log<level::ERR>(
62858fbb2eSGeorge Liu             fmt::format("Unable to block signal, errno({})", errno).c_str());
6319ad0e8aSAlexander Filippov         return EXIT_FAILURE;
6419ad0e8aSAlexander Filippov     }
6519ad0e8aSAlexander Filippov 
66224882b0SJayanth Othayoth     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
67224882b0SJayanth Othayoth     sdbusplus::server::manager::manager objManager(bus, DUMP_OBJPATH);
68224882b0SJayanth Othayoth     bus.request_name(DUMP_BUSNAME);
69224882b0SJayanth Othayoth 
70a320c7caSJayanth Othayoth     try
71224882b0SJayanth Othayoth     {
728b9b4690SDhruvaraj Subhashchandran         phosphor::dump::DumpManagerList dumpMgrList{};
738b9b4690SDhruvaraj Subhashchandran         std::unique_ptr<phosphor::dump::bmc::Manager> bmcDumpMgr =
748b9b4690SDhruvaraj Subhashchandran             std::make_unique<phosphor::dump::bmc::Manager>(
758b9b4690SDhruvaraj Subhashchandran                 bus, eventP, BMC_DUMP_OBJPATH, BMC_DUMP_OBJ_ENTRY,
768b9b4690SDhruvaraj Subhashchandran                 BMC_DUMP_PATH);
778b9b4690SDhruvaraj Subhashchandran 
788b9b4690SDhruvaraj Subhashchandran         phosphor::dump::bmc::internal::Manager mgr(bus, *bmcDumpMgr,
79fef66a95SDhruvaraj Subhashchandran                                                    OBJ_INTERNAL);
808b9b4690SDhruvaraj Subhashchandran         dumpMgrList.push_back(std::move(bmcDumpMgr));
818b9b4690SDhruvaraj Subhashchandran 
828b9b4690SDhruvaraj Subhashchandran         phosphor::dump::loadExtensions(bus, dumpMgrList);
838b9b4690SDhruvaraj Subhashchandran 
848b9b4690SDhruvaraj Subhashchandran         // Restore dbus objects of all dumps
858b9b4690SDhruvaraj Subhashchandran         for (auto& dmpMgr : dumpMgrList)
868b9b4690SDhruvaraj Subhashchandran         {
878b9b4690SDhruvaraj Subhashchandran             dmpMgr->restore();
888b9b4690SDhruvaraj Subhashchandran         }
898b9b4690SDhruvaraj Subhashchandran 
90d0f0064eSJayanth Othayoth         phosphor::dump::elog::Watch eWatch(bus, mgr);
91a320c7caSJayanth Othayoth         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
92bcb174bdSJayanth Othayoth 
93a320c7caSJayanth Othayoth         auto rc = sd_event_loop(eventP.get());
94a320c7caSJayanth Othayoth         if (rc < 0)
95a320c7caSJayanth Othayoth         {
96858fbb2eSGeorge Liu             log<level::ERR>(
97858fbb2eSGeorge Liu                 fmt::format("Error occurred during the sd_event_loop, rc({})",
98858fbb2eSGeorge Liu                             rc)
99858fbb2eSGeorge Liu                     .c_str());
100a320c7caSJayanth Othayoth             elog<InternalFailure>();
101a320c7caSJayanth Othayoth         }
102a320c7caSJayanth Othayoth     }
103*9d2d7226SPatrick Williams     catch (const InternalFailure& e)
104a320c7caSJayanth Othayoth     {
105a320c7caSJayanth Othayoth         commit<InternalFailure>();
106a320c7caSJayanth Othayoth         return -1;
107224882b0SJayanth Othayoth     }
108224882b0SJayanth Othayoth 
109224882b0SJayanth Othayoth     return 0;
110224882b0SJayanth Othayoth }
111