blob: dcd4679513c3849869d07966ed1ef137ee776fbc [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 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
Matthew Barth06764942021-03-04 09:28:40 -060022#include "json/manager.hpp"
23#endif
Matthew Barthf8ae7a52021-03-05 10:23:43 -060024#include "sdbusplus.hpp"
Matthew Barth2feab082021-06-29 14:50:14 -050025#include "sdeventplus.hpp"
Matt Spinlere73446e2017-04-10 13:55:52 -050026
Matthew Barth3e1bb272020-05-26 11:09:21 -050027#include <phosphor-logging/log.hpp>
28#include <sdbusplus/bus.hpp>
29#include <sdeventplus/event.hpp>
Matthew Barthe91ac862021-05-25 16:22:17 -050030#include <sdeventplus/source/signal.hpp>
31#include <stdplus/signal.hpp>
Matthew Barth3e1bb272020-05-26 11:09:21 -050032
Matt Spinleree7f6422017-05-09 11:03:14 -050033using namespace phosphor::fan::control;
Matthew Barth8600d9a2017-06-23 14:38:05 -050034using namespace phosphor::logging;
Matt Spinleree7f6422017-05-09 11:03:14 -050035
Matt Spinlere73446e2017-04-10 13:55:52 -050036int main(int argc, char* argv[])
37{
Matthew Barth2feab082021-06-29 14:50:14 -050038 auto event = phosphor::fan::util::SDEventPlus::getEvent();
Matt Spinlere73446e2017-04-10 13:55:52 -050039
Matthew Barth06764942021-03-04 09:28:40 -060040#ifndef CONTROL_USE_JSON
41 phosphor::fan::util::ArgumentParser args(argc, argv);
Matt Spinleree7f6422017-05-09 11:03:14 -050042 if (argc != 2)
43 {
44 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070045 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050046 }
47
Matthew Barth14184132017-05-19 14:37:30 -050048 Mode mode;
Matt Spinleree7f6422017-05-09 11:03:14 -050049
50 if (args["init"] == "true")
51 {
Matthew Barth14184132017-05-19 14:37:30 -050052 mode = Mode::init;
Matt Spinleree7f6422017-05-09 11:03:14 -050053 }
54 else if (args["control"] == "true")
55 {
Matthew Barth14184132017-05-19 14:37:30 -050056 mode = Mode::control;
Matt Spinleree7f6422017-05-09 11:03:14 -050057 }
58 else
59 {
60 args.usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070061 return 1;
Matt Spinleree7f6422017-05-09 11:03:14 -050062 }
Matthew Barth06764942021-03-04 09:28:40 -060063#endif
Matt Spinleree7f6422017-05-09 11:03:14 -050064
Matthew Barth3e1bb272020-05-26 11:09:21 -050065 // Attach the event object to the bus object so we can
66 // handle both sd_events (for the timers) and dbus signals.
Matthew Barth9403a212021-05-17 09:31:50 -050067 phosphor::fan::util::SDBusPlus::getBus().attach_event(
68 event.get(), SD_EVENT_PRIORITY_NORMAL);
Matthew Barth8600d9a2017-06-23 14:38:05 -050069
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050070 try
71 {
Matthew Barth06764942021-03-04 09:28:40 -060072#ifdef CONTROL_USE_JSON
Matthew Barth9403a212021-05-17 09:31:50 -050073 json::Manager manager(event);
Matthew Barthe91ac862021-05-25 16:22:17 -050074
Matthew Barth3770a1d2021-06-10 15:09:37 -050075 // Handle loading fan control's config file(s)
76 phosphor::fan::JsonConfig config(
77 std::bind(&json::Manager::load, &manager));
78
Matthew Barthe91ac862021-05-25 16:22:17 -050079 // Enable SIGHUP handling to reload JSON configs
80 stdplus::signal::block(SIGHUP);
81 sdeventplus::source::Signal signal(
82 event, SIGHUP,
83 std::bind(&json::Manager::sighupHandler, &manager,
84 std::placeholders::_1, std::placeholders::_2));
85
Matt Spinler2fc0a352021-10-04 15:10:57 -050086 // Enable SIGUSR1 handling to dump the flight recorder
87 stdplus::signal::block(SIGUSR1);
88 sdeventplus::source::Signal sigUsr1(
89 event, SIGUSR1,
90 std::bind(&json::Manager::sigUsr1Handler, &manager,
91 std::placeholders::_1, std::placeholders::_2));
92
Matthew Barthe91ac862021-05-25 16:22:17 -050093 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME);
Matthew Barth06764942021-03-04 09:28:40 -060094#else
Matthew Barth9403a212021-05-17 09:31:50 -050095 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode);
Matt Spinleree7f6422017-05-09 11:03:14 -050096
Matthew Barth3e1bb272020-05-26 11:09:21 -050097 // Init mode will just set fans to max and delay
Matt Spinlerba7b5fe2018-04-25 15:26:10 -050098 if (mode == Mode::init)
Matthew Barth8600d9a2017-06-23 14:38:05 -050099 {
Matthew Barth06764942021-03-04 09:28:40 -0600100 manager.doInit(event);
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500101 return 0;
Matthew Barth8600d9a2017-06-23 14:38:05 -0500102 }
Matthew Barth06764942021-03-04 09:28:40 -0600103#endif
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700104 return event.loop();
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500105 }
Matthew Barth3e1bb272020-05-26 11:09:21 -0500106 // Log the useful metadata on these exceptions and let the app
107 // return 1 so it is restarted without a core dump.
Patrick Williamsddb773b2021-10-06 11:24:49 -0500108 catch (const phosphor::fan::util::DBusServiceError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500109 {
110 log<level::ERR>("Uncaught DBus service lookup failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500111 entry("PATH=%s", e.path.c_str()),
112 entry("INTERFACE=%s", e.interface.c_str()));
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500113 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500114 catch (const phosphor::fan::util::DBusMethodError& e)
Matt Spinlerba7b5fe2018-04-25 15:26:10 -0500115 {
116 log<level::ERR>("Uncaught DBus method failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500117 entry("BUSNAME=%s", e.busName.c_str()),
118 entry("PATH=%s", e.path.c_str()),
119 entry("INTERFACE=%s", e.interface.c_str()),
120 entry("METHOD=%s", e.method.c_str()));
Matt Spinlere73446e2017-04-10 13:55:52 -0500121 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500122 catch (const phosphor::fan::util::DBusPropertyError& e)
Matthew Barth88923a02018-05-11 10:14:44 -0500123 {
124 log<level::ERR>("Uncaught DBus property access failure exception",
Matthew Barth3e1bb272020-05-26 11:09:21 -0500125 entry("BUSNAME=%s", e.busName.c_str()),
126 entry("PATH=%s", e.path.c_str()),
127 entry("INTERFACE=%s", e.interface.c_str()),
128 entry("PROPERTY=%s", e.property.c_str()));
Matthew Barth88923a02018-05-11 10:14:44 -0500129 }
Matt Spinlere73446e2017-04-10 13:55:52 -0500130
William A. Kennington III3e781062018-10-19 17:18:34 -0700131 return 1;
Matt Spinlere73446e2017-04-10 13:55:52 -0500132}