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 #include "argument.hpp"
19 #include "fan.hpp"
20 #include "system.hpp"
21 #include "trust_manager.hpp"
22 
23 #include <sdbusplus/bus.hpp>
24 #include <sdeventplus/event.hpp>
25 #include <sdeventplus/source/signal.hpp>
26 #include <stdplus/signal.hpp>
27 
28 using namespace phosphor::fan::monitor;
29 
30 int main(int argc, char* argv[])
31 {
32     auto event = sdeventplus::Event::get_default();
33     auto bus = sdbusplus::bus::new_default();
34     phosphor::fan::util::ArgumentParser args(argc, argv);
35 
36     if (argc != 2)
37     {
38         args.usage(argv);
39         return 1;
40     }
41 
42     Mode mode;
43     if (args["init"] == "true")
44     {
45         mode = Mode::init;
46     }
47     else if (args["monitor"] == "true")
48     {
49         mode = Mode::monitor;
50     }
51     else
52     {
53         args.usage(argv);
54         return 1;
55     }
56 
57     // If using JSON, then everything is handled in a single
58     // step - the init step.  Hopefully these can eventually be
59     // reduced into a single invocation.
60 #ifdef MONITOR_USE_JSON
61     if (mode == Mode::monitor)
62     {
63         return 0;
64     }
65 #endif
66 
67     // Attach the event object to the bus object so we can
68     // handle both sd_events (for the timers) and dbus signals.
69     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
70 
71     System system(mode, bus, event);
72 
73 #ifdef MONITOR_USE_JSON
74     // Enable SIGHUP handling to reload JSON config
75     stdplus::signal::block(SIGHUP);
76     sdeventplus::source::Signal signal(event, SIGHUP,
77                                        std::bind(&System::sighupHandler,
78                                                  &system, std::placeholders::_1,
79                                                  std::placeholders::_2));
80 
81     bus.request_name(THERMAL_ALERT_BUSNAME);
82 #endif
83 
84 #ifndef MONITOR_USE_JSON
85     if (mode == Mode::init)
86     {
87         // Fans were initialized to be functional, exit
88         return 0;
89     }
90 #endif
91 
92     return event.loop();
93 }
94