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 "argument.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 
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     phosphor::fan::util::ArgumentParser args(argc, argv);
45 
46     if (argc != 2)
47     {
48         args.usage(argv);
49         return 1;
50     }
51 
52     if (args["init"] == "true")
53     {
54         mode = Mode::init;
55     }
56     else if (args["monitor"] == "true")
57     {
58         mode = Mode::monitor;
59     }
60     else
61     {
62         args.usage(argv);
63         return 1;
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 
75     phosphor::fan::JsonConfig config(std::bind(&System::start, &system));
76 
77     // Enable SIGHUP handling to reload JSON config
78     stdplus::signal::block(SIGHUP);
79     sdeventplus::source::Signal signal(event, SIGHUP,
80                                        std::bind(&System::sighupHandler,
81                                                  &system, std::placeholders::_1,
82                                                  std::placeholders::_2));
83 
84     // Enable SIGUSR1 handling to dump debug data
85     stdplus::signal::block(SIGUSR1);
86     sdeventplus::source::Signal sigUsr1(
87         event, SIGUSR1,
88         std::bind(&System::dumpDebugData, &system, std::placeholders::_1,
89                   std::placeholders::_2));
90 
91     bus.request_name(THERMAL_ALERT_BUSNAME);
92 #else
93     system.start();
94 
95     if (mode == Mode::init)
96     {
97         // Fans were initialized to be functional, exit
98         return 0;
99     }
100 #endif
101 
102     return event.loop();
103 }
104