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