blob: e1bbf2e7a19ea26fe2d1f7af4f51035b65f0decd [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Ratan Gupta82e1ef92017-06-15 08:39:15 +05303#include "system_configuration.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
Ratan Gupta82e1ef92017-06-15 08:39:15 +05305#include <phosphor-logging/elog-errors.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <phosphor-logging/log.hpp>
7#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82e1ef92017-06-15 08:39:15 +05308
9namespace phosphor
10{
11namespace network
12{
13
14// systemd service to kick start a target.
Gunnar Mills57d9c502018-09-14 14:42:34 -050015constexpr auto HOSTNAMED_SERVICE = "org.freedesktop.hostname1";
16constexpr auto HOSTNAMED_SERVICE_PATH = "/org/freedesktop/hostname1";
17constexpr auto HOSTNAMED_INTERFACE = "org.freedesktop.hostname1";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053018constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
19constexpr auto METHOD_GET = "Get";
20constexpr auto METHOD_SET = "SetStaticHostname";
21
22using namespace phosphor::logging;
23using namespace sdbusplus::xyz::openbmc_project::Common::Error;
24
25using SystemConfigIntf =
26 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
27
Patrick Williamsc38b0712022-07-22 19:26:54 -050028SystemConfiguration::SystemConfiguration(sdbusplus::bus_t& bus,
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080029 const std::string& objPath) :
Patrick Williams166b9592022-03-30 16:09:16 -050030 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080031 bus(bus)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053032{
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080033 SystemConfigIntf::hostName(getHostNameFromSystem());
Ratan Gupta82e1ef92017-06-15 08:39:15 +053034
35 this->emit_object_added();
36}
37
38std::string SystemConfiguration::hostName(std::string name)
39{
40 if (SystemConfigIntf::hostName() == name)
41 {
42 return name;
43 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 auto method = bus.new_method_call(HOSTNAMED_SERVICE, HOSTNAMED_SERVICE_PATH,
45 HOSTNAMED_INTERFACE, METHOD_SET);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053046
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
59std::string SystemConfiguration::getHostNameFromSystem() const
60{
Ratan Guptad30adf82020-06-12 09:47:32 +053061 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053062 {
Ratan Guptad30adf82020-06-12 09:47:32 +053063 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 Gupta82e1ef92017-06-15 08:39:15 +053072 reply.read(name);
Ratan Guptad30adf82020-06-12 09:47:32 +053073 return std::get<std::string>(name);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053074 }
Patrick Williamsc38b0712022-07-22 19:26:54 -050075 catch (const sdbusplus::exception_t& ex)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053076 {
Ratan Guptad30adf82020-06-12 09:47:32 +053077 log<level::ERR>(
78 "Failed to get the hostname from systemd-hostnamed service",
79 entry("ERR=%s", ex.what()));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053080 }
Ratan Guptad30adf82020-06-12 09:47:32 +053081 return "";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053082}
83
Gunnar Mills57d9c502018-09-14 14:42:34 -050084} // namespace network
85} // namespace phosphor