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 // Enable SIGHUP handling to reload JSON configs 75 stdplus::signal::block(SIGHUP); 76 sdeventplus::source::Signal signal( 77 event, SIGHUP, 78 std::bind(&json::Manager::sighupHandler, &manager, 79 std::placeholders::_1, std::placeholders::_2)); 80 81 phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME); 82 #else 83 Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode); 84 85 // Init mode will just set fans to max and delay 86 if (mode == Mode::init) 87 { 88 manager.doInit(event); 89 return 0; 90 } 91 #endif 92 return event.loop(); 93 } 94 // Log the useful metadata on these exceptions and let the app 95 // return 1 so it is restarted without a core dump. 96 catch (phosphor::fan::util::DBusServiceError& e) 97 { 98 log<level::ERR>("Uncaught DBus service lookup failure exception", 99 entry("PATH=%s", e.path.c_str()), 100 entry("INTERFACE=%s", e.interface.c_str())); 101 } 102 catch (phosphor::fan::util::DBusMethodError& e) 103 { 104 log<level::ERR>("Uncaught DBus method failure exception", 105 entry("BUSNAME=%s", e.busName.c_str()), 106 entry("PATH=%s", e.path.c_str()), 107 entry("INTERFACE=%s", e.interface.c_str()), 108 entry("METHOD=%s", e.method.c_str())); 109 } 110 catch (phosphor::fan::util::DBusPropertyError& e) 111 { 112 log<level::ERR>("Uncaught DBus property access failure exception", 113 entry("BUSNAME=%s", e.busName.c_str()), 114 entry("PATH=%s", e.path.c_str()), 115 entry("INTERFACE=%s", e.interface.c_str()), 116 entry("PROPERTY=%s", e.property.c_str())); 117 } 118 119 return 1; 120 } 121