blob: 5e81e99ee6aed3a7826adc05c789a35d2233a3aa [file] [log] [blame]
Matt Spinlere73446e2017-04-10 13:55:52 -05001/**
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 */
Matthew Barthf8ae7a52021-03-05 10:23:43 -060016#include "config.h"
17
18#ifndef CONTROL_USE_JSON
Matt Spinler3ac99022021-10-04 16:02:49 -050019#include "../utils/flight_recorder.hpp"
Matt Spinleree7f6422017-05-09 11:03:14 -050020#include "argument.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050021#include "manager.hpp"
Matthew Barthf8ae7a52021-03-05 10:23:43 -060022#else
Matthew Barth06764942021-03-04 09:28:40 -060023#include "json/manager.hpp"
24#endif
Matthew Barthf8ae7a52021-03-05 10:23:43 -060025#include "sdbusplus.hpp"
Matthew Barth2feab082021-06-29 14:50:14 -050026#include "sdeventplus.hpp"
Matt Spinlere73446e2017-04-10 13:55:52 -050027
Matthew Barth3e1bb272020-05-26 11:09:21 -050028#include <phosphor-logging/log.hpp>
29#include <sdbusplus/bus.hpp>
30#include <sdeventplus/event.hpp>
Matthew Barthe91ac862021-05-25 16:22:17 -050031#include <sdeventplus/source/signal.hpp>
32#include <stdplus/signal.hpp>
Matthew Barth3e1bb272020-05-26 11:09:21 -050033
Matt Spinler7787def2021-10-14 16:33:16 -050034#include <fstream>
35
Matt Spinleree7f6422017-05-09 11:03:14 -050036using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050037using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050038
Matt Spinler7787def2021-10-14 16:33:16 -050039#ifdef CONTROL_USE_JSON
40void dumpFlightRecorder()
41{
42 nlohmann::json data;
43 phosphor::fan::control::json::FlightRecorder::instance().dump(data);
44 std::ofstream file{json::Manager::dumpFile};
45 file << std::setw(4) << data;
46}
47#endif
48
Matt Spinlere73446e2017-04-10 13:55:52 -050049int main(int argc, char* argv[])
50{
Matthew Barth2feab082021-06-29 14:50:14 -050051 auto event = phosphor::fan::util::SDEventPlus::getEvent();
Matt Spinlere73446e2017-04-10 13:55:52 -050052
Matthew Barth06764942021-03-04 09:28:40 -060053#ifndef CONTROL_USE_JSON
54 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinleree7f6422017-05-09 11:03:14 -050055 if (argc != 2)
56 {
57 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070058 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050059 }
60
Matthew Barth14184132017-05-19 14:37:30 -050061 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050062
63 if (args["init"] == "true")
64 {
Matthew Barth14184132017-05-19 14:37:30 -050065 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050066 }
67 else if (args["control"] == "true")
68 {
Matthew Barth14184132017-05-19 14:37:30 -050069 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050070 }
71 else
72 {
73 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070074 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050075 }
Matthew Barth06764942021-03-04 09:28:40 -060076#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050077
Matthew Barth3e1bb272020-05-26 11:09:21 -050078 // Attach the event object to the bus object so we can
79 // handle both sd_events (for the timers) and dbus signals.
Matthew Barth9403a212021-05-17 09:31:50 -050080 phosphor::fan::util::SDBusPlus::getBus().attach_event(
81 event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050082
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050083 try
84 {
Matthew Barth06764942021-03-04 09:28:40 -060085#ifdef CONTROL_USE_JSON
Matt Spinler3ac99022021-10-04 16:02:49 -050086 phosphor::fan::control::json::FlightRecorder::instance().log("main",
87 "Startup");
Matthew Barth9403a212021-05-17 09:31:50 -050088 json::Manager manager(event);
Matthew Barthe91ac862021-05-25 16:22:17 -050089
Matthew Barth3770a1d2021-06-10 15:09:37 -050090 // Handle loading fan control's config file(s)
91 phosphor::fan::JsonConfig config(
92 std::bind(&json::Manager::load, &manager));
93
Matthew Barthe91ac862021-05-25 16:22:17 -050094 // Enable SIGHUP handling to reload JSON configs
95 stdplus::signal::block(SIGHUP);
96 sdeventplus::source::Signal signal(
97 event, SIGHUP,
98 std::bind(&json::Manager::sighupHandler, &manager,
99 std::placeholders::_1, std::placeholders::_2));
100
Matt Spinler2fc0a352021-10-04 15:10:57 -0500101 // Enable SIGUSR1 handling to dump the flight recorder
102 stdplus::signal::block(SIGUSR1);
103 sdeventplus::source::Signal sigUsr1(
104 event, SIGUSR1,
105 std::bind(&json::Manager::sigUsr1Handler, &manager,
106 std::placeholders::_1, std::placeholders::_2));
107
Matthew Barthe91ac862021-05-25 16:22:17 -0500108 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
Matthew Barth06764942021-03-04 09:28:40 -0600109#else
Matthew Barth9403a212021-05-17 09:31:50 -0500110 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -0500111
Matthew Barth3e1bb272020-05-26 11:09:21 -0500112 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500113 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -0500114 {
Matthew Barth06764942021-03-04 09:28:40 -0600115 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500116 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500117 }
Matthew Barth06764942021-03-04 09:28:40 -0600118#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700119 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500120 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500121 // Log the useful metadata on these exceptions and let the app
122 // return 1 so it is restarted without a core dump.
Patrick Williamsddb773b2021-10-06 11:24:49 -0500123 catch (const phosphor::fan::util::DBusServiceError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500124 {
125 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500126 entry("PATH=%s", e.path.c_str()),
127 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500128 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500129 catch (const phosphor::fan::util::DBusMethodError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500130 {
131 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500132 entry("BUSNAME=%s", e.busName.c_str()),
133 entry("PATH=%s", e.path.c_str()),
134 entry("INTERFACE=%s", e.interface.c_str()),
135 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -0500136 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500137 catch (const phosphor::fan::util::DBusPropertyError& e)
Matthew Barth88923a02018-05-11 10:14:44 -0500138 {
139 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500140 entry("BUSNAME=%s", e.busName.c_str()),
141 entry("PATH=%s", e.path.c_str()),
142 entry("INTERFACE=%s", e.interface.c_str()),
143 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500144 }
Matt Spinler3ac99022021-10-04 16:02:49 -0500145 catch (std::exception& e)
146 {
147#ifdef CONTROL_USE_JSON
148 phosphor::fan::control::json::FlightRecorder::instance().log(
149 "main", "Unexpected exception exit");
Matt Spinler7787def2021-10-14 16:33:16 -0500150 dumpFlightRecorder();
Matt Spinler3ac99022021-10-04 16:02:49 -0500151#endif
152 throw;
153 }
154
155#ifdef CONTROL_USE_JSON
156 phosphor::fan::control::json::FlightRecorder::instance().log(
157 "main", "Abnormal exit");
Matt Spinler7787def2021-10-14 16:33:16 -0500158 dumpFlightRecorder();
Matt Spinler3ac99022021-10-04 16:02:49 -0500159#endif
Matt Spinlere73446e2017-04-10 13:55:52 -0500160
William A. Kennington III3e781062018-10-19 17:18:34 -0700161 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500162}