blob: ba9c6129ef03cd3697bd593ae1013d38fadaed0e [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 Spinleree7f6422017-05-09 11:03:14 -050034using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050035using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050036
Matt Spinlere73446e2017-04-10 13:55:52 -050037int main(int argc, char* argv[])
38{
Matthew Barth2feab082021-06-29 14:50:14 -050039 auto event = phosphor::fan::util::SDEventPlus::getEvent();
Matt Spinlere73446e2017-04-10 13:55:52 -050040
Matthew Barth06764942021-03-04 09:28:40 -060041#ifndef CONTROL_USE_JSON
42 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinleree7f6422017-05-09 11:03:14 -050043 if (argc != 2)
44 {
45 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070046 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050047 }
48
Matthew Barth14184132017-05-19 14:37:30 -050049 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050050
51 if (args["init"] == "true")
52 {
Matthew Barth14184132017-05-19 14:37:30 -050053 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050054 }
55 else if (args["control"] == "true")
56 {
Matthew Barth14184132017-05-19 14:37:30 -050057 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050058 }
59 else
60 {
61 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070062 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050063 }
Matthew Barth06764942021-03-04 09:28:40 -060064#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050065
Matthew Barth3e1bb272020-05-26 11:09:21 -050066 // Attach the event object to the bus object so we can
67 // handle both sd_events (for the timers) and dbus signals.
Matthew Barth9403a212021-05-17 09:31:50 -050068 phosphor::fan::util::SDBusPlus::getBus().attach_event(
69 event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050070
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050071 try
72 {
Matthew Barth06764942021-03-04 09:28:40 -060073#ifdef CONTROL_USE_JSON
Matt Spinler3ac99022021-10-04 16:02:49 -050074 phosphor::fan::control::json::FlightRecorder::instance().log("main",
75 "Startup");
Matthew Barth9403a212021-05-17 09:31:50 -050076 json::Manager manager(event);
Matthew Barthe91ac862021-05-25 16:22:17 -050077
Matthew Barth3770a1d2021-06-10 15:09:37 -050078 // Handle loading fan control's config file(s)
79 phosphor::fan::JsonConfig config(
80 std::bind(&json::Manager::load, &manager));
81
Matthew Barthe91ac862021-05-25 16:22:17 -050082 // Enable SIGHUP handling to reload JSON configs
83 stdplus::signal::block(SIGHUP);
84 sdeventplus::source::Signal signal(
85 event, SIGHUP,
86 std::bind(&json::Manager::sighupHandler, &manager,
87 std::placeholders::_1, std::placeholders::_2));
88
Matt Spinler2fc0a352021-10-04 15:10:57 -050089 // Enable SIGUSR1 handling to dump the flight recorder
90 stdplus::signal::block(SIGUSR1);
91 sdeventplus::source::Signal sigUsr1(
92 event, SIGUSR1,
93 std::bind(&json::Manager::sigUsr1Handler, &manager,
94 std::placeholders::_1, std::placeholders::_2));
95
Matthew Barthe91ac862021-05-25 16:22:17 -050096 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
Matthew Barth06764942021-03-04 09:28:40 -060097#else
Matthew Barth9403a212021-05-17 09:31:50 -050098 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -050099
Matthew Barth3e1bb272020-05-26 11:09:21 -0500100 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500101 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -0500102 {
Matthew Barth06764942021-03-04 09:28:40 -0600103 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500104 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500105 }
Matthew Barth06764942021-03-04 09:28:40 -0600106#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700107 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500108 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500109 // Log the useful metadata on these exceptions and let the app
110 // return 1 so it is restarted without a core dump.
Patrick Williamsddb773b2021-10-06 11:24:49 -0500111 catch (const phosphor::fan::util::DBusServiceError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500112 {
113 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500114 entry("PATH=%s", e.path.c_str()),
115 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500116 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500117 catch (const phosphor::fan::util::DBusMethodError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500118 {
119 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500120 entry("BUSNAME=%s", e.busName.c_str()),
121 entry("PATH=%s", e.path.c_str()),
122 entry("INTERFACE=%s", e.interface.c_str()),
123 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -0500124 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500125 catch (const phosphor::fan::util::DBusPropertyError& e)
Matthew Barth88923a02018-05-11 10:14:44 -0500126 {
127 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500128 entry("BUSNAME=%s", e.busName.c_str()),
129 entry("PATH=%s", e.path.c_str()),
130 entry("INTERFACE=%s", e.interface.c_str()),
131 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500132 }
Matt Spinler3ac99022021-10-04 16:02:49 -0500133 catch (std::exception& e)
134 {
135#ifdef CONTROL_USE_JSON
136 phosphor::fan::control::json::FlightRecorder::instance().log(
137 "main", "Unexpected exception exit");
138 phosphor::fan::control::json::FlightRecorder::instance().dump();
139#endif
140 throw;
141 }
142
143#ifdef CONTROL_USE_JSON
144 phosphor::fan::control::json::FlightRecorder::instance().log(
145 "main", "Abnormal exit");
146 phosphor::fan::control::json::FlightRecorder::instance().dump();
147#endif
Matt Spinlere73446e2017-04-10 13:55:52 -0500148
William A. Kennington III3e781062018-10-19 17:18:34 -0700149 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500150}