Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 1 | #include "system_configuration.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
| 5 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace network |
| 10 | { |
| 11 | |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 12 | static constexpr char HOSTNAMED_SVC[] = "org.freedesktop.hostname1"; |
| 13 | static constexpr char HOSTNAMED_OBJ[] = "/org/freedesktop/hostname1"; |
| 14 | static constexpr char HOSTNAMED_INTF[] = "org.freedesktop.hostname1"; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 18 | |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 19 | static constexpr char propMatch[] = |
| 20 | "type='signal',sender='org.freedesktop.hostname1'," |
| 21 | "path='/org/freedesktop/hostname1'," |
| 22 | "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'," |
| 23 | "arg0='org.freedesktop.hostname1'"; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 24 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 25 | SystemConfiguration::SystemConfiguration(sdbusplus::bus_t& bus, |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 26 | stdplus::const_zstring objPath) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 27 | Iface(bus, objPath.c_str(), Iface::action::defer_emit), |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 28 | bus(bus), hostnamePropMatch(bus, propMatch, [&](sdbusplus::message_t& m) { |
| 29 | std::string intf; |
| 30 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 31 | try |
| 32 | { |
| 33 | m.read(intf, values); |
| 34 | auto it = values.find("Hostname"); |
| 35 | if (it == values.end()) |
| 36 | { |
| 37 | return; |
| 38 | } |
| 39 | Iface::hostName(std::get<std::string>(it->second)); |
| 40 | } |
| 41 | catch (const std::exception& e) |
| 42 | { |
| 43 | log<level::ERR>( |
| 44 | fmt::format("Hostname match parsing failed: {}", e.what()) |
| 45 | .c_str(), |
| 46 | entry("ERROR=%s", e.what())); |
| 47 | } |
| 48 | }) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 49 | { |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 50 | try |
| 51 | { |
| 52 | std::variant<std::string> name; |
| 53 | auto req = |
| 54 | bus.new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ, |
| 55 | "org.freedesktop.DBus.Properties", "Get"); |
| 56 | |
| 57 | req.append(HOSTNAMED_INTF, "Hostname"); |
| 58 | auto reply = bus.call(req); |
| 59 | reply.read(name); |
| 60 | SystemConfigIntf::hostName(std::get<std::string>(name), true); |
| 61 | } |
| 62 | catch (const std::exception& e) |
| 63 | { |
| 64 | auto msg = fmt::format("Failed to get hostname: {}", e.what()); |
| 65 | log<level::ERR>(msg.c_str(), entry("ERROR=%s", e.what())); |
| 66 | } |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 67 | |
William A. Kennington III | d99e6db | 2022-11-15 20:39:45 -0800 | [diff] [blame] | 68 | emit_object_added(); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | std::string SystemConfiguration::hostName(std::string name) |
| 72 | { |
| 73 | if (SystemConfigIntf::hostName() == name) |
| 74 | { |
| 75 | return name; |
| 76 | } |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 77 | try |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 78 | { |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 79 | auto method = bus.new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ, |
| 80 | HOSTNAMED_INTF, "SetStaticHostname"); |
| 81 | method.append(name, /*interactive=*/false); |
| 82 | bus.call_noreply(method); |
| 83 | return SystemConfigIntf::hostName(std::move(name)); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 84 | } |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 85 | catch (const std::exception& e) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 86 | { |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 87 | auto msg = fmt::format("Failed to set hostname: {}", e.what()); |
| 88 | log<level::ERR>(msg.c_str(), entry("ERROR=%s", e.what())); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 89 | } |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 90 | return SystemConfigIntf::hostName(); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 91 | } |
| 92 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 93 | } // namespace network |
| 94 | } // namespace phosphor |