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