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> |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 4 | #include <phosphor-logging/lg2.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 | |
Ravi Teja | 46dda59 | 2024-01-12 04:54:35 -0600 | [diff] [blame] | 17 | using namespace phosphor::logging; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 18 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ravi Teja | 46dda59 | 2024-01-12 04:54:35 -0600 | [diff] [blame] | 19 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 20 | |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 21 | static constexpr char propMatch[] = |
| 22 | "type='signal',sender='org.freedesktop.hostname1'," |
| 23 | "path='/org/freedesktop/hostname1'," |
| 24 | "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'," |
| 25 | "arg0='org.freedesktop.hostname1'"; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 26 | |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 27 | SystemConfiguration::SystemConfiguration( |
| 28 | stdplus::PinnedRef<sdbusplus::bus_t> bus, stdplus::const_zstring objPath) : |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame] | 29 | Iface(bus, objPath.c_str(), Iface::action::defer_emit), 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 |
| 36 | { |
| 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)); |
| 44 | } |
| 45 | catch (const std::exception& e) |
| 46 | { |
| 47 | lg2::error("Hostname match parsing failed: {ERROR}", "ERROR", |
| 48 | e); |
| 49 | } |
| 50 | }) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 51 | { |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 52 | try |
| 53 | { |
| 54 | std::variant<std::string> name; |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame] | 55 | auto req = |
| 56 | bus.get().new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ, |
| 57 | "org.freedesktop.DBus.Properties", "Get"); |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 58 | |
| 59 | req.append(HOSTNAMED_INTF, "Hostname"); |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 60 | auto reply = req.call(); |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 61 | reply.read(name); |
| 62 | SystemConfigIntf::hostName(std::get<std::string>(name), true); |
| 63 | } |
| 64 | catch (const std::exception& e) |
| 65 | { |
Jagpal Singh Gill | a2947b4 | 2023-04-17 21:10:14 -0700 | [diff] [blame] | 66 | lg2::error("Failed to get hostname: {ERROR}", "ERROR", e); |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 67 | } |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 68 | |
William A. Kennington III | d99e6db | 2022-11-15 20:39:45 -0800 | [diff] [blame] | 69 | emit_object_added(); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | std::string SystemConfiguration::hostName(std::string name) |
| 73 | { |
| 74 | if (SystemConfigIntf::hostName() == name) |
| 75 | { |
| 76 | return name; |
| 77 | } |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 78 | try |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 79 | { |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 80 | auto method = bus.get().new_method_call( |
| 81 | HOSTNAMED_SVC, HOSTNAMED_OBJ, HOSTNAMED_INTF, "SetStaticHostname"); |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 82 | method.append(name, /*interactive=*/false); |
Ravi Teja | 46dda59 | 2024-01-12 04:54:35 -0600 | [diff] [blame] | 83 | method.call(); |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 84 | return SystemConfigIntf::hostName(std::move(name)); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 85 | } |
Ravi Teja | 46dda59 | 2024-01-12 04:54:35 -0600 | [diff] [blame] | 86 | catch (const sdbusplus::exception::SdBusError& e) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 87 | { |
Ravi Teja | 46dda59 | 2024-01-12 04:54:35 -0600 | [diff] [blame] | 88 | lg2::error("Failed to set hostname {HOSTNAME}: {ERROR} ", "HOSTNAME", |
| 89 | name, "ERROR", e); |
| 90 | auto dbusError = e.get_error(); |
| 91 | if ((dbusError != nullptr) && |
| 92 | (strcmp(dbusError->name, |
| 93 | "org.freedesktop.DBus.Error.InvalidArgs") == 0)) |
| 94 | { |
| 95 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Hostname"), |
| 96 | Argument::ARGUMENT_VALUE(name.c_str())); |
| 97 | } |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 98 | } |
William A. Kennington III | 40c29b5 | 2022-11-16 17:48:10 -0800 | [diff] [blame] | 99 | return SystemConfigIntf::hostName(); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 100 | } |
| 101 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 102 | } // namespace network |
| 103 | } // namespace phosphor |