blob: ae46caa05cb1f0b6d8c66dc3231271508f9350b3 [file] [log] [blame]
Matt Spinlere73446e2017-04-10 13:55:52 -05001/**
Mike Cappsb2e9a4f2022-06-13 10:15:42 -04002 * Copyright © 2022 IBM Corporation
Matt Spinlere73446e2017-04-10 13:55:52 -05003 *
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 Spinleree7f6422017-05-09 11:03:14 -050019#include "argument.hpp"
Matt Spinlere10416e2017-04-10 14:15:53 -050020#include "manager.hpp"
Matthew Barthf8ae7a52021-03-05 10:23:43 -060021#else
Mike Cappsa0819562022-06-13 10:17:10 -040022#include "../utils/flight_recorder.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060023#include "json/manager.hpp"
24#endif
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040025
Mike Cappsbf8e56f2022-06-29 14:23:07 -040026#include "dbus_paths.hpp"
Matthew Barthf8ae7a52021-03-05 10:23:43 -060027#include "sdbusplus.hpp"
Matthew Barth2feab082021-06-29 14:50:14 -050028#include "sdeventplus.hpp"
Matt Spinlere73446e2017-04-10 13:55:52 -050029
Matthew Barth3e1bb272020-05-26 11:09:21 -050030#include <phosphor-logging/log.hpp>
31#include <sdbusplus/bus.hpp>
32#include <sdeventplus/event.hpp>
Matthew Barthe91ac862021-05-25 16:22:17 -050033#include <sdeventplus/source/signal.hpp>
34#include <stdplus/signal.hpp>
Matthew Barth3e1bb272020-05-26 11:09:21 -050035
Matt Spinler7787def2021-10-14 16:33:16 -050036#include <fstream>
37
Matt Spinleree7f6422017-05-09 11:03:14 -050038using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050039using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050040
Matt Spinler7787def2021-10-14 16:33:16 -050041#ifdef CONTROL_USE_JSON
42void dumpFlightRecorder()
43{
44 nlohmann::json data;
45 phosphor::fan::control::json::FlightRecorder::instance().dump(data);
46 std::ofstream file{json::Manager::dumpFile};
47 file << std::setw(4) << data;
48}
49#endif
50
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040051int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
Matt Spinlere73446e2017-04-10 13:55:52 -050052{
Matthew Barth2feab082021-06-29 14:50:14 -050053 auto event = phosphor::fan::util::SDEventPlus::getEvent();
Matt Spinlere73446e2017-04-10 13:55:52 -050054
Matthew Barth06764942021-03-04 09:28:40 -060055#ifndef CONTROL_USE_JSON
56 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinleree7f6422017-05-09 11:03:14 -050057 if (argc != 2)
58 {
59 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070060 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050061 }
62
Matthew Barth14184132017-05-19 14:37:30 -050063 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050064
65 if (args["init"] == "true")
66 {
Matthew Barth14184132017-05-19 14:37:30 -050067 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050068 }
69 else if (args["control"] == "true")
70 {
Matthew Barth14184132017-05-19 14:37:30 -050071 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050072 }
73 else
74 {
75 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070076 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050077 }
Matthew Barth06764942021-03-04 09:28:40 -060078#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050079
Matthew Barth3e1bb272020-05-26 11:09:21 -050080 // Attach the event object to the bus object so we can
81 // handle both sd_events (for the timers) and dbus signals.
Matthew Barth9403a212021-05-17 09:31:50 -050082 phosphor::fan::util::SDBusPlus::getBus().attach_event(
83 event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050084
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050085 try
86 {
Matthew Barth06764942021-03-04 09:28:40 -060087#ifdef CONTROL_USE_JSON
Matt Spinler3ac99022021-10-04 16:02:49 -050088 phosphor::fan::control::json::FlightRecorder::instance().log("main",
89 "Startup");
Matthew Barth9403a212021-05-17 09:31:50 -050090 json::Manager manager(event);
Matthew Barthe91ac862021-05-25 16:22:17 -050091
Matthew Barth3770a1d2021-06-10 15:09:37 -050092 // Handle loading fan control's config file(s)
93 phosphor::fan::JsonConfig config(
94 std::bind(&json::Manager::load, &manager));
95
Matthew Barthe91ac862021-05-25 16:22:17 -050096 // Enable SIGHUP handling to reload JSON configs
97 stdplus::signal::block(SIGHUP);
98 sdeventplus::source::Signal signal(
99 event, SIGHUP,
100 std::bind(&json::Manager::sighupHandler, &manager,
101 std::placeholders::_1, std::placeholders::_2));
102
Matt Spinler2fc0a352021-10-04 15:10:57 -0500103 // Enable SIGUSR1 handling to dump the flight recorder
104 stdplus::signal::block(SIGUSR1);
105 sdeventplus::source::Signal sigUsr1(
106 event, SIGUSR1,
107 std::bind(&json::Manager::sigUsr1Handler, &manager,
108 std::placeholders::_1, std::placeholders::_2));
109
Matthew Barthe91ac862021-05-25 16:22:17 -0500110 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
Matthew Barth06764942021-03-04 09:28:40 -0600111#else
Matthew Barth9403a212021-05-17 09:31:50 -0500112 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -0500113
Matthew Barth3e1bb272020-05-26 11:09:21 -0500114 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500115 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -0500116 {
Matthew Barth06764942021-03-04 09:28:40 -0600117 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500118 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500119 }
Matthew Barth06764942021-03-04 09:28:40 -0600120#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700121 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500122 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500123 // Log the useful metadata on these exceptions and let the app
124 // return 1 so it is restarted without a core dump.
Patrick Williamsddb773b2021-10-06 11:24:49 -0500125 catch (const phosphor::fan::util::DBusServiceError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500126 {
127 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500128 entry("PATH=%s", e.path.c_str()),
129 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500130 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500131 catch (const phosphor::fan::util::DBusMethodError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500132 {
133 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500134 entry("BUSNAME=%s", e.busName.c_str()),
135 entry("PATH=%s", e.path.c_str()),
136 entry("INTERFACE=%s", e.interface.c_str()),
137 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -0500138 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500139 catch (const phosphor::fan::util::DBusPropertyError& e)
Matthew Barth88923a02018-05-11 10:14:44 -0500140 {
141 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500142 entry("BUSNAME=%s", e.busName.c_str()),
143 entry("PATH=%s", e.path.c_str()),
144 entry("INTERFACE=%s", e.interface.c_str()),
145 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500146 }
Matt Spinler3ac99022021-10-04 16:02:49 -0500147 catch (std::exception& e)
148 {
149#ifdef CONTROL_USE_JSON
150 phosphor::fan::control::json::FlightRecorder::instance().log(
151 "main", "Unexpected exception exit");
Matt Spinler7787def2021-10-14 16:33:16 -0500152 dumpFlightRecorder();
Matt Spinler3ac99022021-10-04 16:02:49 -0500153#endif
154 throw;
155 }
156
157#ifdef CONTROL_USE_JSON
158 phosphor::fan::control::json::FlightRecorder::instance().log(
159 "main", "Abnormal exit");
Matt Spinler7787def2021-10-14 16:33:16 -0500160 dumpFlightRecorder();
Matt Spinler3ac99022021-10-04 16:02:49 -0500161#endif
Matt Spinlere73446e2017-04-10 13:55:52 -0500162
William A. Kennington III3e781062018-10-19 17:18:34 -0700163 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500164}