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"
7*919f71c6SClaire 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 
12858fbb2eSGeorge Liu #include <fmt/core.h>
13858fbb2eSGeorge Liu 
14cb65ffceSJayanth Othayoth #include <phosphor-logging/elog-errors.hpp>
15cb65ffceSJayanth Othayoth #include <sdbusplus/bus.hpp>
160af74a5eSJayanth Othayoth 
170af74a5eSJayanth Othayoth #include <memory>
188b9b4690SDhruvaraj Subhashchandran #include <vector>
19224882b0SJayanth Othayoth 
20bb410df7SRamesh Iyyar int main()
21224882b0SJayanth Othayoth {
22a320c7caSJayanth Othayoth     using namespace phosphor::logging;
23a320c7caSJayanth Othayoth     using InternalFailure =
24a320c7caSJayanth Othayoth         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
25a320c7caSJayanth Othayoth 
26671fc7f3SJayanth Othayoth     auto bus = sdbusplus::bus::new_default();
27a320c7caSJayanth Othayoth     sd_event* event = nullptr;
28a320c7caSJayanth Othayoth     auto rc = sd_event_default(&event);
29a320c7caSJayanth Othayoth     if (rc < 0)
30a320c7caSJayanth Othayoth     {
31858fbb2eSGeorge Liu         log<level::ERR>(
32858fbb2eSGeorge Liu             fmt::format("Error occurred during the sd_event_default, rc({})",
33858fbb2eSGeorge Liu                         rc)
34858fbb2eSGeorge Liu                 .c_str());
35a320c7caSJayanth Othayoth         report<InternalFailure>();
36a320c7caSJayanth Othayoth         return rc;
37a320c7caSJayanth Othayoth     }
38a320c7caSJayanth Othayoth     phosphor::dump::EventPtr eventP{event};
39a320c7caSJayanth Othayoth     event = nullptr;
40224882b0SJayanth Othayoth 
4119ad0e8aSAlexander Filippov     // Blocking SIGCHLD is needed for calling sd_event_add_child
4219ad0e8aSAlexander Filippov     sigset_t mask;
4319ad0e8aSAlexander Filippov     if (sigemptyset(&mask) < 0)
4419ad0e8aSAlexander Filippov     {
45858fbb2eSGeorge Liu         log<level::ERR>(
46858fbb2eSGeorge Liu             fmt::format("Unable to initialize signal set, errno({})", errno)
47858fbb2eSGeorge Liu                 .c_str());
4819ad0e8aSAlexander Filippov         return EXIT_FAILURE;
4919ad0e8aSAlexander Filippov     }
5019ad0e8aSAlexander Filippov 
5119ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
5219ad0e8aSAlexander Filippov     {
53858fbb2eSGeorge Liu         log<level::ERR>(
54858fbb2eSGeorge Liu             fmt::format("Unable to add signal to signal set, errno({})", errno)
55858fbb2eSGeorge Liu                 .c_str());
5619ad0e8aSAlexander Filippov         return EXIT_FAILURE;
5719ad0e8aSAlexander Filippov     }
5819ad0e8aSAlexander Filippov 
5919ad0e8aSAlexander Filippov     // Block SIGCHLD first, so that the event loop can handle it
6019ad0e8aSAlexander Filippov     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
6119ad0e8aSAlexander Filippov     {
62858fbb2eSGeorge Liu         log<level::ERR>(
63858fbb2eSGeorge Liu             fmt::format("Unable to block signal, errno({})", errno).c_str());
6419ad0e8aSAlexander Filippov         return EXIT_FAILURE;
6519ad0e8aSAlexander Filippov     }
6619ad0e8aSAlexander Filippov 
67224882b0SJayanth Othayoth     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
68224882b0SJayanth Othayoth     sdbusplus::server::manager::manager objManager(bus, DUMP_OBJPATH);
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 
82*919f71c6SClaire Weinan         std::unique_ptr<phosphor::dump::faultlog::Manager> faultLogMgr =
83*919f71c6SClaire Weinan             std::make_unique<phosphor::dump::faultlog::Manager>(
84*919f71c6SClaire Weinan                 bus, FAULTLOG_DUMP_OBJPATH, FAULTLOG_DUMP_OBJ_ENTRY,
85*919f71c6SClaire Weinan                 FAULTLOG_DUMP_PATH);
86*919f71c6SClaire Weinan         dumpMgrList.push_back(std::move(faultLogMgr));
87*919f71c6SClaire Weinan 
888b9b4690SDhruvaraj Subhashchandran         phosphor::dump::loadExtensions(bus, dumpMgrList);
898b9b4690SDhruvaraj Subhashchandran 
908b9b4690SDhruvaraj Subhashchandran         // Restore dbus objects of all dumps
918b9b4690SDhruvaraj Subhashchandran         for (auto& dmpMgr : dumpMgrList)
928b9b4690SDhruvaraj Subhashchandran         {
938b9b4690SDhruvaraj Subhashchandran             dmpMgr->restore();
948b9b4690SDhruvaraj Subhashchandran         }
958b9b4690SDhruvaraj Subhashchandran 
96d0f0064eSJayanth Othayoth         phosphor::dump::elog::Watch eWatch(bus, mgr);
97a320c7caSJayanth Othayoth         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
98bcb174bdSJayanth Othayoth 
999e682c51SPatrick Williams         // Daemon is all set up so claim the busname now.
1009e682c51SPatrick Williams         bus.request_name(DUMP_BUSNAME);
1019e682c51SPatrick Williams 
102a320c7caSJayanth Othayoth         auto rc = sd_event_loop(eventP.get());
103a320c7caSJayanth Othayoth         if (rc < 0)
104a320c7caSJayanth Othayoth         {
105858fbb2eSGeorge Liu             log<level::ERR>(
106858fbb2eSGeorge Liu                 fmt::format("Error occurred during the sd_event_loop, rc({})",
107858fbb2eSGeorge Liu                             rc)
108858fbb2eSGeorge Liu                     .c_str());
109a320c7caSJayanth Othayoth             elog<InternalFailure>();
110a320c7caSJayanth Othayoth         }
111a320c7caSJayanth Othayoth     }
1129d2d7226SPatrick Williams     catch (const InternalFailure& e)
113a320c7caSJayanth Othayoth     {
114a320c7caSJayanth Othayoth         commit<InternalFailure>();
115a320c7caSJayanth Othayoth         return -1;
116224882b0SJayanth Othayoth     }
117224882b0SJayanth Othayoth 
118224882b0SJayanth Othayoth     return 0;
119224882b0SJayanth Othayoth }
120