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 | |
| 12 | // systemd service to kick start a target. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 13 | constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1"; |
| 14 | constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1"; |
| 15 | constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1"; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 16 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 17 | constexpr auto METHOD_GET = "Get"; |
| 18 | constexpr auto METHOD_SET = "SetStaticHostname"; |
| 19 | |
| 20 | using namespace phosphor::logging; |
| 21 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 22 | |
| 23 | using SystemConfigIntf = |
| 24 | sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration; |
| 25 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 26 | SystemConfiguration::SystemConfiguration(sdbusplus::bus_t& bus, |
Jiaqing Zhao | 24b5a61 | 2022-04-11 16:46:16 +0800 | [diff] [blame] | 27 | const std::string& objPath) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 28 | Iface(bus, objPath.c_str(), Iface::action::defer_emit), |
Jiaqing Zhao | 24b5a61 | 2022-04-11 16:46:16 +0800 | [diff] [blame] | 29 | bus(bus) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 30 | { |
Jiaqing Zhao | 24b5a61 | 2022-04-11 16:46:16 +0800 | [diff] [blame] | 31 | SystemConfigIntf::hostName(getHostNameFromSystem()); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 32 | |
| 33 | this->emit_object_added(); |
| 34 | } |
| 35 | |
| 36 | std::string SystemConfiguration::hostName(std::string name) |
| 37 | { |
| 38 | if (SystemConfigIntf::hostName() == name) |
| 39 | { |
| 40 | return name; |
| 41 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 42 | auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH, |
| 43 | HOSTNAMED_INTERFACE, METHOD_SET); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 44 | |
| 45 | method.append(name, true); |
| 46 | |
| 47 | if (!bus.call(method)) |
| 48 | { |
| 49 | log<level::ERR>("Failed to set the hostname"); |
| 50 | report<InternalFailure>(); |
| 51 | return SystemConfigIntf::hostName(); |
| 52 | } |
| 53 | |
| 54 | return SystemConfigIntf::hostName(name); |
| 55 | } |
| 56 | |
| 57 | std::string SystemConfiguration::getHostNameFromSystem() const |
| 58 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 59 | try |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 60 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 61 | std::variant<std::string> name; |
| 62 | auto method = |
| 63 | bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH, |
| 64 | PROPERTY_INTERFACE, METHOD_GET); |
| 65 | |
| 66 | method.append(HOSTNAMED_INTERFACE, "Hostname"); |
| 67 | |
| 68 | auto reply = bus.call(method); |
| 69 | |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 70 | reply.read(name); |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 71 | return std::get<std::string>(name); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 72 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 73 | catch (const sdbusplus::exception_t& ex) |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 74 | { |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 75 | log<level::ERR>( |
| 76 | "Failed to get the hostname from systemd-hostnamed service", |
| 77 | entry("ERR=%s", ex.what())); |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 78 | } |
Ratan Gupta | d30adf8 | 2020-06-12 09:47:32 +0530 | [diff] [blame] | 79 | return ""; |
Ratan Gupta | 82e1ef9 | 2017-06-15 08:39:15 +0530 | [diff] [blame] | 80 | } |
| 81 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 82 | } // namespace network |
| 83 | } // namespace phosphor |