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"
7919f71c6SClaire Weinan #include "dump_manager_faultlog.hpp"
8d0f0064eSJayanth Othayoth #include "elog_watch.hpp"
9cb65ffceSJayanth Othayoth #include "watch.hpp"
10cb65ffceSJayanth Othayoth #include "xyz/openbmc_project/Common/error.hpp"
11cb65ffceSJayanth Othayoth 
12cb65ffceSJayanth Othayoth #include <phosphor-logging/elog-errors.hpp>
13d1f670feSDhruvaraj Subhashchandran #include <phosphor-logging/lg2.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     {
30d1f670feSDhruvaraj Subhashchandran         lg2::error("Error occurred during the sd_event_default, rc: {RC}", "RC",
31d1f670feSDhruvaraj Subhashchandran                    rc);
32a320c7caSJayanth Othayoth         report<InternalFailure>();
33a320c7caSJayanth Othayoth         return rc;
34a320c7caSJayanth Othayoth     }
35a320c7caSJayanth Othayoth     phosphor::dump::EventPtr eventP{event};
36a320c7caSJayanth Othayoth     event = nullptr;
37224882b0SJayanth Othayoth 
3819ad0e8aSAlexander Filippov     // Blocking SIGCHLD is needed for calling sd_event_add_child
3919ad0e8aSAlexander Filippov     sigset_t mask;
4019ad0e8aSAlexander Filippov     if (sigemptyset(&mask) < 0)
4119ad0e8aSAlexander Filippov     {
42d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to initialize signal set, errno: {ERRNO}", "ERRNO",
43d1f670feSDhruvaraj Subhashchandran                    errno);
4419ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4519ad0e8aSAlexander Filippov     }
4619ad0e8aSAlexander Filippov 
4719ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
4819ad0e8aSAlexander Filippov     {
49d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to add signal to signal set, errno: {ERRNO}",
50d1f670feSDhruvaraj Subhashchandran                    "ERRNO", errno);
5119ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5219ad0e8aSAlexander Filippov     }
5319ad0e8aSAlexander Filippov 
5419ad0e8aSAlexander Filippov     // Block SIGCHLD first, so that the event loop can handle it
5519ad0e8aSAlexander Filippov     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
5619ad0e8aSAlexander Filippov     {
57d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to block signal, errno: {ERRNO}", "ERRNO", errno);
5819ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5919ad0e8aSAlexander Filippov     }
6019ad0e8aSAlexander Filippov 
61224882b0SJayanth Othayoth     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
629b18bf2dSPatrick Williams     sdbusplus::server::manager_t objManager(bus, DUMP_OBJPATH);
63224882b0SJayanth Othayoth 
64a320c7caSJayanth Othayoth     try
65224882b0SJayanth Othayoth     {
668b9b4690SDhruvaraj Subhashchandran         phosphor::dump::DumpManagerList dumpMgrList{};
678b9b4690SDhruvaraj Subhashchandran         std::unique_ptr<phosphor::dump::bmc::Manager> bmcDumpMgr =
688b9b4690SDhruvaraj Subhashchandran             std::make_unique<phosphor::dump::bmc::Manager>(
698b9b4690SDhruvaraj Subhashchandran                 bus, eventP, BMC_DUMP_OBJPATH, BMC_DUMP_OBJ_ENTRY,
708b9b4690SDhruvaraj Subhashchandran                 BMC_DUMP_PATH);
718b9b4690SDhruvaraj Subhashchandran 
728b9b4690SDhruvaraj Subhashchandran         phosphor::dump::bmc::internal::Manager mgr(bus, *bmcDumpMgr,
73fef66a95SDhruvaraj Subhashchandran                                                    OBJ_INTERNAL);
74*e4350f93SDhruvaraj Subhashchandran         phosphor::dump::bmc::Manager* ptrBmcDumpMgr = bmcDumpMgr.get();
75*e4350f93SDhruvaraj Subhashchandran 
768b9b4690SDhruvaraj Subhashchandran         dumpMgrList.push_back(std::move(bmcDumpMgr));
778b9b4690SDhruvaraj Subhashchandran 
78919f71c6SClaire Weinan         std::unique_ptr<phosphor::dump::faultlog::Manager> faultLogMgr =
79919f71c6SClaire Weinan             std::make_unique<phosphor::dump::faultlog::Manager>(
80919f71c6SClaire Weinan                 bus, FAULTLOG_DUMP_OBJPATH, FAULTLOG_DUMP_OBJ_ENTRY,
81919f71c6SClaire Weinan                 FAULTLOG_DUMP_PATH);
82919f71c6SClaire Weinan         dumpMgrList.push_back(std::move(faultLogMgr));
83919f71c6SClaire Weinan 
848b9b4690SDhruvaraj Subhashchandran         phosphor::dump::loadExtensions(bus, dumpMgrList);
858b9b4690SDhruvaraj Subhashchandran 
868b9b4690SDhruvaraj Subhashchandran         // Restore dbus objects of all dumps
878b9b4690SDhruvaraj Subhashchandran         for (auto& dmpMgr : dumpMgrList)
888b9b4690SDhruvaraj Subhashchandran         {
898b9b4690SDhruvaraj Subhashchandran             dmpMgr->restore();
908b9b4690SDhruvaraj Subhashchandran         }
918b9b4690SDhruvaraj Subhashchandran 
92*e4350f93SDhruvaraj Subhashchandran         phosphor::dump::elog::Watch eWatch(bus, *ptrBmcDumpMgr);
93*e4350f93SDhruvaraj Subhashchandran 
94a320c7caSJayanth Othayoth         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
95bcb174bdSJayanth Othayoth 
969e682c51SPatrick Williams         // Daemon is all set up so claim the busname now.
979e682c51SPatrick Williams         bus.request_name(DUMP_BUSNAME);
989e682c51SPatrick Williams 
99a320c7caSJayanth Othayoth         auto rc = sd_event_loop(eventP.get());
100a320c7caSJayanth Othayoth         if (rc < 0)
101a320c7caSJayanth Othayoth         {
102d1f670feSDhruvaraj Subhashchandran             lg2::error("Error occurred during the sd_event_loop, rc: {RC}",
103d1f670feSDhruvaraj Subhashchandran                        "RC", rc);
104a320c7caSJayanth Othayoth             elog<InternalFailure>();
105a320c7caSJayanth Othayoth         }
106a320c7caSJayanth Othayoth     }
1079d2d7226SPatrick Williams     catch (const InternalFailure& e)
108a320c7caSJayanth Othayoth     {
109a320c7caSJayanth Othayoth         commit<InternalFailure>();
110a320c7caSJayanth Othayoth         return -1;
111224882b0SJayanth Othayoth     }
112224882b0SJayanth Othayoth 
113224882b0SJayanth Othayoth     return 0;
114224882b0SJayanth Othayoth }
115