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         phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
87 #else
88         Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
89 
90         // Init mode will just set fans to max and delay
91         if (mode == Mode::init)
92         {
93             manager.doInit(event);
94             return 0;
95         }
96 #endif
97         return event.loop();
98     }
99     // Log the useful metadata on these exceptions and let the app
100     // return 1 so it is restarted without a core dump.
101     catch (phosphor::fan::util::DBusServiceError& e)
102     {
103         log<level::ERR>("Uncaught DBus service lookup failure exception",
104                         entry("PATH=%s", e.path.c_str()),
105                         entry("INTERFACE=%s", e.interface.c_str()));
106     }
107     catch (phosphor::fan::util::DBusMethodError& e)
108     {
109         log<level::ERR>("Uncaught DBus method failure exception",
110                         entry("BUSNAME=%s", e.busName.c_str()),
111                         entry("PATH=%s", e.path.c_str()),
112                         entry("INTERFACE=%s", e.interface.c_str()),
113                         entry("METHOD=%s", e.method.c_str()));
114     }
115     catch (phosphor::fan::util::DBusPropertyError& e)
116     {
117         log<level::ERR>("Uncaught DBus property access failure exception",
118                         entry("BUSNAME=%s", e.busName.c_str()),
119                         entry("PATH=%s", e.path.c_str()),
120                         entry("INTERFACE=%s", e.interface.c_str()),
121                         entry("PROPERTY=%s", e.property.c_str()));
122     }
123 
124     return 1;
125 }
126