blob: a302ace3c732a87455a88ccdc3d4f7da7ef3ecb2 [file] [log] [blame]
Ratan Gupta82e1ef92017-06-15 08:39:15 +05301#include "system_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
Ratan Gupta82e1ef92017-06-15 08:39:15 +05303#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07004#include <phosphor-logging/log.hpp>
5#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82e1ef92017-06-15 08:39:15 +05306
7namespace phosphor
8{
9namespace network
10{
11
12// systemd service to kick start a target.
Gunnar Mills57d9c502018-09-14 14:42:34 -050013constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1";
14constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1";
15constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053016constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
17constexpr auto METHOD_GET = "Get";
18constexpr auto METHOD_SET = "SetStaticHostname";
19
20using namespace phosphor::logging;
21using namespace sdbusplus::xyz::openbmc_project::Common::Error;
22
23using SystemConfigIntf =
24 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
25
Patrick Williamsc38b0712022-07-22 19:26:54 -050026SystemConfiguration::SystemConfiguration(sdbusplus::bus_t& bus,
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080027 const std::string& objPath) :
Patrick Williams166b9592022-03-30 16:09:16 -050028 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080029 bus(bus)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053030{
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080031 SystemConfigIntf::hostName(getHostNameFromSystem());
Ratan Gupta82e1ef92017-06-15 08:39:15 +053032
33 this->emit_object_added();
34}
35
36std::string SystemConfiguration::hostName(std::string name)
37{
38 if (SystemConfigIntf::hostName() == name)
39 {
40 return name;
41 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050042 auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
43 HOSTNAMED_INTERFACE, METHOD_SET);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053044
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
57std::string SystemConfiguration::getHostNameFromSystem() const
58{
Ratan Guptad30adf82020-06-12 09:47:32 +053059 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053060 {
Ratan Guptad30adf82020-06-12 09:47:32 +053061 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 Gupta82e1ef92017-06-15 08:39:15 +053070 reply.read(name);
Ratan Guptad30adf82020-06-12 09:47:32 +053071 return std::get<std::string>(name);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053072 }
Patrick Williamsc38b0712022-07-22 19:26:54 -050073 catch (const sdbusplus::exception_t& ex)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053074 {
Ratan Guptad30adf82020-06-12 09:47:32 +053075 log<level::ERR>(
76 "Failed to get the hostname from systemd-hostnamed service",
77 entry("ERR=%s", ex.what()));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053078 }
Ratan Guptad30adf82020-06-12 09:47:32 +053079 return "";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053080}
81
Gunnar Mills57d9c502018-09-14 14:42:34 -050082} // namespace network
83} // namespace phosphor