xref: /openbmc/phosphor-fan-presence/control/main.cpp (revision 2fc0a35d8d0a32865f87b9f5c52aeef3518cf52f)
1 /**
2  * Copyright © 2017 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "config.h"
17 
18 #ifndef CONTROL_USE_JSON
19 #include "argument.hpp"
20 #include "manager.hpp"
21 #else
22 #include "json/manager.hpp"
23 #endif
24 #include "sdbusplus.hpp"
25 #include "sdeventplus.hpp"
26 
27 #include <phosphor-logging/log.hpp>
28 #include <sdbusplus/bus.hpp>
29 #include <sdeventplus/event.hpp>
30 #include <sdeventplus/source/signal.hpp>
31 #include <stdplus/signal.hpp>
32 
33 using namespace phosphor::fan::control;
34 using namespace phosphor::logging;
35 
36 int main(int argc, char* argv[])
37 {
38     auto event = phosphor::fan::util::SDEventPlus::getEvent();
39 
40 #ifndef CONTROL_USE_JSON
41     phosphor::fan::util::ArgumentParser args(argc, argv);
42     if (argc != 2)
43     {
44         args.usage(argv);
45         return 1;
46     }
47 
48     Mode mode;
49 
50     if (args["init"] == "true")
51     {
52         mode = Mode::init;
53     }
54     else if (args["control"] == "true")
55     {
56         mode = Mode::control;
57     }
58     else
59     {
60         args.usage(argv);
61         return 1;
62     }
63 #endif
64 
65     // Attach the event object to the bus object so we can
66     // handle both sd_events (for the timers) and dbus signals.
67     phosphor::fan::util::SDBusPlus::getBus().attach_event(
68         event.get(), SD_EVENT_PRIORITY_NORMAL);
69 
70     try
71     {
72 #ifdef CONTROL_USE_JSON
73         json::Manager manager(event);
74 
75         // Handle loading fan control's config file(s)
76         phosphor::fan::JsonConfig config(
77             std::bind(&json::Manager::load, &manager));
78 
79         // Enable SIGHUP handling to reload JSON configs
80         stdplus::signal::block(SIGHUP);
81         sdeventplus::source::Signal signal(
82             event, SIGHUP,
83             std::bind(&json::Manager::sighupHandler, &manager,
84                       std::placeholders::_1, std::placeholders::_2));
85 
86         // Enable SIGUSR1 handling to dump the flight recorder
87         stdplus::signal::block(SIGUSR1);
88         sdeventplus::source::Signal sigUsr1(
89             event, SIGUSR1,
90             std::bind(&json::Manager::sigUsr1Handler, &manager,
91                       std::placeholders::_1, std::placeholders::_2));
92 
93         phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
94 #else
95         Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
96 
97         // Init mode will just set fans to max and delay
98         if (mode == Mode::init)
99         {
100             manager.doInit(event);
101             return 0;
102         }
103 #endif
104         return event.loop();
105     }
106     // Log the useful metadata on these exceptions and let the app
107     // return 1 so it is restarted without a core dump.
108     catch (const phosphor::fan::util::DBusServiceError& e)
109     {
110         log<level::ERR>("Uncaught DBus service lookup failure exception",
111                         entry("PATH=%s", e.path.c_str()),
112                         entry("INTERFACE=%s", e.interface.c_str()));
113     }
114     catch (const phosphor::fan::util::DBusMethodError& e)
115     {
116         log<level::ERR>("Uncaught DBus method failure exception",
117                         entry("BUSNAME=%s", e.busName.c_str()),
118                         entry("PATH=%s", e.path.c_str()),
119                         entry("INTERFACE=%s", e.interface.c_str()),
120                         entry("METHOD=%s", e.method.c_str()));
121     }
122     catch (const phosphor::fan::util::DBusPropertyError& e)
123     {
124         log<level::ERR>("Uncaught DBus property access failure exception",
125                         entry("BUSNAME=%s", e.busName.c_str()),
126                         entry("PATH=%s", e.path.c_str()),
127                         entry("INTERFACE=%s", e.interface.c_str()),
128                         entry("PROPERTY=%s", e.property.c_str()));
129     }
130 
131     return 1;
132 }
133