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 MONITOR_USE_JSON
19 #include <CLI/CLI.hpp>
20 #endif
21 #include "fan.hpp"
22 #ifdef MONITOR_USE_JSON
23 #include "dbus_paths.hpp"
24 #include "json_config.hpp"
25 #include "json_parser.hpp"
26 #endif
27 #include "system.hpp"
28 #include "trust_manager.hpp"
29 
30 #include <sdbusplus/bus.hpp>
31 #include <sdeventplus/event.hpp>
32 #include <sdeventplus/source/signal.hpp>
33 #include <stdplus/signal.hpp>
34 
35 using namespace phosphor::fan::monitor;
36 
main(int argc,char * argv[])37 int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
38 {
39     auto event = sdeventplus::Event::get_default();
40     auto bus = sdbusplus::bus::new_default();
41     Mode mode = Mode::init;
42 
43 #ifndef MONITOR_USE_JSON
44     CLI::App app{"Phosphor Fan Monitor"};
45 
46     bool init = false;
47     bool monitor = false;
48     app.add_flag("-i,--init", init, "Set fans to functional");
49     app.add_flag("-m,--monitor", monitor, "Start fan functional monitoring");
50     app.require_option();
51 
52     try
53     {
54         app.parse(argc, argv);
55     }
56     catch (const CLI::Error& e)
57     {
58         return app.exit(e);
59     }
60 
61     if (init)
62     {
63         mode = Mode::init;
64     }
65     else if (monitor)
66     {
67         mode = Mode::monitor;
68     }
69 #endif
70 
71     // Attach the event object to the bus object so we can
72     // handle both sd_events (for the timers) and dbus signals.
73     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
74 
75     System system(mode, bus, event);
76 
77 #ifdef MONITOR_USE_JSON
78 
79     phosphor::fan::JsonConfig config(std::bind(&System::start, &system));
80 
81     // Enable SIGHUP handling to reload JSON config
82     stdplus::signal::block(SIGHUP);
83     sdeventplus::source::Signal signal(
84         event, SIGHUP,
85         std::bind(&System::sighupHandler, &system, std::placeholders::_1,
86                   std::placeholders::_2));
87 
88     // Enable SIGUSR1 handling to dump debug data
89     stdplus::signal::block(SIGUSR1);
90     sdeventplus::source::Signal sigUsr1(
91         event, SIGUSR1,
92         std::bind(&System::dumpDebugData, &system, std::placeholders::_1,
93                   std::placeholders::_2));
94 
95     bus.request_name(THERMAL_ALERT_BUSNAME);
96 #else
97     system.start();
98 
99     if (mode == Mode::init)
100     {
101         // Fans were initialized to be functional, exit
102         return 0;
103     }
104 #endif
105 
106     return event.loop();
107 }
108