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 CONTROL_USE_JSON 19 #include "argument.hpp" 20 #include "manager.hpp" 21 #else 22 #include "json/manager.hpp" 23 #endif 24 #include "sdbusplus.hpp" 25 26 #include <phosphor-logging/log.hpp> 27 #include <sdbusplus/bus.hpp> 28 #include <sdeventplus/event.hpp> 29 #include <sdeventplus/source/signal.hpp> 30 #include <stdplus/signal.hpp> 31 32 using namespace phosphor::fan::control; 33 using namespace phosphor::logging; 34 35 int main(int argc, char* argv[]) 36 { 37 auto event = sdeventplus::Event::get_default(); 38 39 #ifndef CONTROL_USE_JSON 40 phosphor::fan::util::ArgumentParser args(argc, argv); 41 if (argc != 2) 42 { 43 args.usage(argv); 44 return 1; 45 } 46 47 Mode mode; 48 49 if (args["init"] == "true") 50 { 51 mode = Mode::init; 52 } 53 else if (args["control"] == "true") 54 { 55 mode = Mode::control; 56 } 57 else 58 { 59 args.usage(argv); 60 return 1; 61 } 62 #endif 63 64 // Attach the event object to the bus object so we can 65 // handle both sd_events (for the timers) and dbus signals. 66 phosphor::fan::util::SDBusPlus::getBus().attach_event( 67 event.get(), SD_EVENT_PRIORITY_NORMAL); 68 69 try 70 { 71 #ifdef CONTROL_USE_JSON 72 json::Manager manager(event); 73 74 // Handle loading fan control's config file(s) 75 phosphor::fan::JsonConfig config( 76 std::bind(&json::Manager::load, &manager)); 77 78 // Enable SIGHUP handling to reload JSON configs 79 stdplus::signal::block(SIGHUP); 80 sdeventplus::source::Signal signal( 81 event, SIGHUP, 82 std::bind(&json::Manager::sighupHandler, &manager, 83 std::placeholders::_1, std::placeholders::_2)); 84 85 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME); 86 #else 87 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode); 88 89 // Init mode will just set fans to max and delay 90 if (mode == Mode::init) 91 { 92 manager.doInit(event); 93 return 0; 94 } 95 #endif 96 return event.loop(); 97 } 98 // Log the useful metadata on these exceptions and let the app 99 // return 1 so it is restarted without a core dump. 100 catch (phosphor::fan::util::DBusServiceError& e) 101 { 102 log<level::ERR>("Uncaught DBus service lookup failure exception", 103 entry("PATH=%s", e.path.c_str()), 104 entry("INTERFACE=%s", e.interface.c_str())); 105 } 106 catch (phosphor::fan::util::DBusMethodError& e) 107 { 108 log<level::ERR>("Uncaught DBus method failure exception", 109 entry("BUSNAME=%s", e.busName.c_str()), 110 entry("PATH=%s", e.path.c_str()), 111 entry("INTERFACE=%s", e.interface.c_str()), 112 entry("METHOD=%s", e.method.c_str())); 113 } 114 catch (phosphor::fan::util::DBusPropertyError& e) 115 { 116 log<level::ERR>("Uncaught DBus property access failure exception", 117 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("PROPERTY=%s", e.property.c_str())); 121 } 122 123 return 1; 124 } 125