1224882b0SJayanth Othayoth #include "config.h"
2cb65ffceSJayanth Othayoth 
3a320c7caSJayanth Othayoth #include "dump_internal.hpp"
4cb65ffceSJayanth Othayoth #include "dump_manager.hpp"
5d0f0064eSJayanth Othayoth #include "elog_watch.hpp"
6cb65ffceSJayanth Othayoth #include "watch.hpp"
7cb65ffceSJayanth Othayoth #include "xyz/openbmc_project/Common/error.hpp"
8cb65ffceSJayanth Othayoth 
9cb65ffceSJayanth Othayoth #include <phosphor-logging/elog-errors.hpp>
10cb65ffceSJayanth Othayoth #include <sdbusplus/bus.hpp>
11224882b0SJayanth Othayoth 
12224882b0SJayanth Othayoth int main(int argc, char* argv[])
13224882b0SJayanth Othayoth {
14a320c7caSJayanth Othayoth     using namespace phosphor::logging;
15a320c7caSJayanth Othayoth     using InternalFailure =
16a320c7caSJayanth Othayoth         sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17a320c7caSJayanth Othayoth 
18671fc7f3SJayanth Othayoth     auto bus = sdbusplus::bus::new_default();
19a320c7caSJayanth Othayoth     sd_event* event = nullptr;
20a320c7caSJayanth Othayoth     auto rc = sd_event_default(&event);
21a320c7caSJayanth Othayoth     if (rc < 0)
22a320c7caSJayanth Othayoth     {
23a320c7caSJayanth Othayoth         log<level::ERR>("Error occurred during the sd_event_default",
2411eaab7bSGunnar Mills                         entry("RC=%d", rc));
25a320c7caSJayanth Othayoth         report<InternalFailure>();
26a320c7caSJayanth Othayoth         return rc;
27a320c7caSJayanth Othayoth     }
28a320c7caSJayanth Othayoth     phosphor::dump::EventPtr eventP{event};
29a320c7caSJayanth Othayoth     event = nullptr;
30224882b0SJayanth Othayoth 
31*19ad0e8aSAlexander Filippov     // Blocking SIGCHLD is needed for calling sd_event_add_child
32*19ad0e8aSAlexander Filippov     sigset_t mask;
33*19ad0e8aSAlexander Filippov     if (sigemptyset(&mask) < 0)
34*19ad0e8aSAlexander Filippov     {
35*19ad0e8aSAlexander Filippov         log<level::ERR>("Unable to initialize signal set",
36*19ad0e8aSAlexander Filippov                         entry("ERRNO=%d", errno));
37*19ad0e8aSAlexander Filippov         return EXIT_FAILURE;
38*19ad0e8aSAlexander Filippov     }
39*19ad0e8aSAlexander Filippov 
40*19ad0e8aSAlexander Filippov     if (sigaddset(&mask, SIGCHLD) < 0)
41*19ad0e8aSAlexander Filippov     {
42*19ad0e8aSAlexander Filippov         log<level::ERR>("Unable to add signal to signal set",
43*19ad0e8aSAlexander Filippov                         entry("ERRNO=%d", errno));
44*19ad0e8aSAlexander Filippov         return EXIT_FAILURE;
45*19ad0e8aSAlexander Filippov     }
46*19ad0e8aSAlexander Filippov 
47*19ad0e8aSAlexander Filippov     // Block SIGCHLD first, so that the event loop can handle it
48*19ad0e8aSAlexander Filippov     if (sigprocmask(SIG_BLOCK, &mask, nullptr) < 0)
49*19ad0e8aSAlexander Filippov     {
50*19ad0e8aSAlexander Filippov         log<level::ERR>("Unable to block signal", entry("ERRNO=%d", errno));
51*19ad0e8aSAlexander Filippov         return EXIT_FAILURE;
52*19ad0e8aSAlexander Filippov     }
53*19ad0e8aSAlexander Filippov 
54224882b0SJayanth Othayoth     // Add sdbusplus ObjectManager for the 'root' path of the DUMP manager.
55224882b0SJayanth Othayoth     sdbusplus::server::manager::manager objManager(bus, DUMP_OBJPATH);
56224882b0SJayanth Othayoth     bus.request_name(DUMP_BUSNAME);
57224882b0SJayanth Othayoth 
58a320c7caSJayanth Othayoth     try
59224882b0SJayanth Othayoth     {
60a320c7caSJayanth Othayoth         phosphor::dump::Manager manager(bus, eventP, DUMP_OBJPATH);
6143096598SJayanth Othayoth         // Restore dump d-bus objects.
6243096598SJayanth Othayoth         manager.restore();
63d3273eadSJayanth Othayoth         phosphor::dump::internal::Manager mgr(bus, manager, OBJ_INTERNAL);
64d0f0064eSJayanth Othayoth         phosphor::dump::elog::Watch eWatch(bus, mgr);
65a320c7caSJayanth Othayoth         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
66bcb174bdSJayanth Othayoth 
67a320c7caSJayanth Othayoth         auto rc = sd_event_loop(eventP.get());
68a320c7caSJayanth Othayoth         if (rc < 0)
69a320c7caSJayanth Othayoth         {
70a320c7caSJayanth Othayoth             log<level::ERR>("Error occurred during the sd_event_loop",
7111eaab7bSGunnar Mills                             entry("RC=%d", rc));
72a320c7caSJayanth Othayoth             elog<InternalFailure>();
73a320c7caSJayanth Othayoth         }
74a320c7caSJayanth Othayoth     }
75a320c7caSJayanth Othayoth     catch (InternalFailure& e)
76a320c7caSJayanth Othayoth     {
77a320c7caSJayanth Othayoth         commit<InternalFailure>();
78a320c7caSJayanth Othayoth         return -1;
79224882b0SJayanth Othayoth     }
80224882b0SJayanth Othayoth 
81224882b0SJayanth Othayoth     return 0;
82224882b0SJayanth Othayoth }
83