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>
13*d1f670feSDhruvaraj 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     {
30*d1f670feSDhruvaraj Subhashchandran         lg2::error("Error occurred during the sd_event_default, rc: {RC}", "RC",
31*d1f670feSDhruvaraj 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     {
42*d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to initialize signal set, errno: {ERRNO}", "ERRNO",
43*d1f670feSDhruvaraj Subhashchandran                    errno);
4419ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4519ad0e8aSAlexander Filippov     }
4619ad0e8aSAlexander Filippov 
4719ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
4819ad0e8aSAlexander Filippov     {
49*d1f670feSDhruvaraj Subhashchandran         lg2::error("Unable to add signal to signal set, errno: {ERRNO}",
50*d1f670feSDhruvaraj 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     {
57*d1f670feSDhruvaraj 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);
748b9b4690SDhruvaraj Subhashchandran         dumpMgrList.push_back(std::move(bmcDumpMgr));
758b9b4690SDhruvaraj Subhashchandran 
76919f71c6SClaire Weinan         std::unique_ptr<phosphor::dump::faultlog::Manager> faultLogMgr =
77919f71c6SClaire Weinan             std::make_unique<phosphor::dump::faultlog::Manager>(
78919f71c6SClaire Weinan                 bus, FAULTLOG_DUMP_OBJPATH, FAULTLOG_DUMP_OBJ_ENTRY,
79919f71c6SClaire Weinan                 FAULTLOG_DUMP_PATH);
80919f71c6SClaire Weinan         dumpMgrList.push_back(std::move(faultLogMgr));
81919f71c6SClaire Weinan 
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 
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         {
99*d1f670feSDhruvaraj Subhashchandran             lg2::error("Error occurred during the sd_event_loop, rc: {RC}",
100*d1f670feSDhruvaraj 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