1224882b0SJayanth Othayoth #include "config.h"
2cb65ffceSJayanth Othayoth 
38b9b4690SDhruvaraj Subhashchandran #include "dump-extensions.hpp"
4cb65ffceSJayanth Othayoth #include "dump_manager.hpp"
5fef66a95SDhruvaraj Subhashchandran #include "dump_manager_bmc.hpp"
6919f71c6SClaire Weinan #include "dump_manager_faultlog.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>
12d1f670feSDhruvaraj Subhashchandran #include <phosphor-logging/lg2.hpp>
13cb65ffceSJayanth Othayoth #include <sdbusplus/bus.hpp>
140af74a5eSJayanth Othayoth 
150af74a5eSJayanth Othayoth #include <memory>
168b9b4690SDhruvaraj Subhashchandran #include <vector>
17224882b0SJayanth Othayoth 
main()18bb410df7SRamesh Iyyar int main()
19224882b0SJayanth Othayoth {
20a320c7caSJayanth Othayoth     using namespace phosphor::logging;
21a320c7caSJayanth Othayoth     using InternalFailure =
22a320c7caSJayanth Othayoth         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
23a320c7caSJayanth Othayoth 
24671fc7f3SJayanth Othayoth     auto bus = sdbusplus::bus::new_default();
25a320c7caSJayanth Othayoth     sd_event* event = nullptr;
26a320c7caSJayanth Othayoth     auto rc = sd_event_default(&event);
27a320c7caSJayanth Othayoth     if (rc < 0)
28a320c7caSJayanth Othayoth     {
29d1f670feSDhruvaraj Subhashchandran         lg2::error("Error occurred during the sd_event_default, rc: {RC}", "RC",
30d1f670feSDhruvaraj Subhashchandran                    rc);
31a320c7caSJayanth Othayoth         report<InternalFailure>();
32a320c7caSJayanth Othayoth         return rc;
33a320c7caSJayanth Othayoth     }
34a320c7caSJayanth Othayoth     phosphor::dump::EventPtr eventP{event};
35a320c7caSJayanth Othayoth     event = nullptr;
36224882b0SJayanth Othayoth 
3719ad0e8aSAlexander Filippov     // Blocking SIGCHLD is needed for calling sd_event_add_child
3819ad0e8aSAlexander Filippov     sigset_t mask;
3919ad0e8aSAlexander Filippov     if (sigemptyset(&mask) < 0)
4019ad0e8aSAlexander Filippov     {
41d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to initialize signal set, errno: {ERRNO}", "ERRNO",
42d1f670feSDhruvaraj Subhashchandran                    errno);
4319ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4419ad0e8aSAlexander Filippov     }
4519ad0e8aSAlexander Filippov 
4619ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
4719ad0e8aSAlexander Filippov     {
48d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to add signal to signal set, errno: {ERRNO}",
49d1f670feSDhruvaraj Subhashchandran                    "ERRNO", errno);
5019ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5119ad0e8aSAlexander Filippov     }
5219ad0e8aSAlexander Filippov 
5319ad0e8aSAlexander Filippov     // Block SIGCHLD first, so that the event loop can handle it
5419ad0e8aSAlexander Filippov     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
5519ad0e8aSAlexander Filippov     {
56d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to block signal, errno: {ERRNO}", "ERRNO", errno);
5719ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5819ad0e8aSAlexander Filippov     }
5919ad0e8aSAlexander Filippov 
60224882b0SJayanth Othayoth     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
619b18bf2dSPatrick Williams     sdbusplus::server::manager_t objManager(bus, DUMP_OBJPATH);
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 
71*e4350f93SDhruvaraj Subhashchandran         phosphor::dump::bmc::Manager* ptrBmcDumpMgr = bmcDumpMgr.get();
72*e4350f93SDhruvaraj Subhashchandran 
738b9b4690SDhruvaraj Subhashchandran         dumpMgrList.push_back(std::move(bmcDumpMgr));
748b9b4690SDhruvaraj Subhashchandran 
75919f71c6SClaire Weinan         std::unique_ptr<phosphor::dump::faultlog::Manager> faultLogMgr =
76919f71c6SClaire Weinan             std::make_unique<phosphor::dump::faultlog::Manager>(
77919f71c6SClaire Weinan                 bus, FAULTLOG_DUMP_OBJPATH, FAULTLOG_DUMP_OBJ_ENTRY,
78919f71c6SClaire Weinan                 FAULTLOG_DUMP_PATH);
79919f71c6SClaire Weinan         dumpMgrList.push_back(std::move(faultLogMgr));
80919f71c6SClaire Weinan 
818b9b4690SDhruvaraj Subhashchandran         phosphor::dump::loadExtensions(bus, dumpMgrList);
828b9b4690SDhruvaraj Subhashchandran 
838b9b4690SDhruvaraj Subhashchandran         // Restore dbus objects of all dumps
848b9b4690SDhruvaraj Subhashchandran         for (auto& dmpMgr : dumpMgrList)
858b9b4690SDhruvaraj Subhashchandran         {
868b9b4690SDhruvaraj Subhashchandran             dmpMgr->restore();
878b9b4690SDhruvaraj Subhashchandran         }
888b9b4690SDhruvaraj Subhashchandran 
89*e4350f93SDhruvaraj Subhashchandran         phosphor::dump::elog::Watch eWatch(bus, *ptrBmcDumpMgr);
90*e4350f93SDhruvaraj Subhashchandran 
91a320c7caSJayanth Othayoth         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
92bcb174bdSJayanth Othayoth 
939e682c51SPatrick Williams         // Daemon is all set up so claim the busname now.
949e682c51SPatrick Williams         bus.request_name(DUMP_BUSNAME);
959e682c51SPatrick Williams 
96a320c7caSJayanth Othayoth         auto rc = sd_event_loop(eventP.get());
97a320c7caSJayanth Othayoth         if (rc < 0)
98a320c7caSJayanth Othayoth         {
99d1f670feSDhruvaraj Subhashchandran             lg2::error("Error occurred during the sd_event_loop, rc: {RC}",
100d1f670feSDhruvaraj Subhashchandran                        "RC", rc);
101a320c7caSJayanth Othayoth             elog<InternalFailure>();
102a320c7caSJayanth Othayoth         }
103a320c7caSJayanth Othayoth     }
1049d2d7226SPatrick Williams     catch (const InternalFailure& e)
105a320c7caSJayanth Othayoth     {
106a320c7caSJayanth Othayoth         commit<InternalFailure>();
107a320c7caSJayanth Othayoth         return -1;
108224882b0SJayanth Othayoth     }
109224882b0SJayanth Othayoth 
110224882b0SJayanth Othayoth     return 0;
111224882b0SJayanth Othayoth }
112