blob: bccdb02f12667d1fcdd76bc5aaa85baa4d4fbba3 [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
William A. Kennington III40c29b52022-11-16 17:48:10 -080012static constexpr char HOSTNAMED_SVC[] = "org.freedesktop.hostname1";
13static constexpr char HOSTNAMED_OBJ[] = "/org/freedesktop/hostname1";
14static constexpr char HOSTNAMED_INTF[] = "org.freedesktop.hostname1";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053015
16using namespace phosphor::logging;
17using namespace sdbusplus::xyz::openbmc_project::Common::Error;
18
William A. Kennington III40c29b52022-11-16 17:48:10 -080019static constexpr char propMatch[] =
20 "type='signal',sender='org.freedesktop.hostname1',"
21 "path='/org/freedesktop/hostname1',"
22 "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',"
23 "arg0='org.freedesktop.hostname1'";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053024
Patrick Williamsc38b0712022-07-22 19:26:54 -050025SystemConfiguration::SystemConfiguration(sdbusplus::bus_t& bus,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070026 stdplus::const_zstring objPath) :
Patrick Williams166b9592022-03-30 16:09:16 -050027 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
William A. Kennington III40c29b52022-11-16 17:48:10 -080028 bus(bus), hostnamePropMatch(bus, propMatch, [&](sdbusplus::message_t& m) {
29 std::string intf;
30 std::unordered_map<std::string, std::variant<std::string>> values;
31 try
32 {
33 m.read(intf, values);
34 auto it = values.find("Hostname");
35 if (it == values.end())
36 {
37 return;
38 }
39 Iface::hostName(std::get<std::string>(it->second));
40 }
41 catch (const std::exception& e)
42 {
43 log<level::ERR>(
44 fmt::format("Hostname match parsing failed: {}", e.what())
45 .c_str(),
46 entry("ERROR=%s", e.what()));
47 }
48 })
Ratan Gupta82e1ef92017-06-15 08:39:15 +053049{
William A. Kennington III40c29b52022-11-16 17:48:10 -080050 try
51 {
52 std::variant<std::string> name;
53 auto req =
54 bus.new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
55 "org.freedesktop.DBus.Properties", "Get");
56
57 req.append(HOSTNAMED_INTF, "Hostname");
58 auto reply = bus.call(req);
59 reply.read(name);
60 SystemConfigIntf::hostName(std::get<std::string>(name), true);
61 }
62 catch (const std::exception& e)
63 {
64 auto msg = fmt::format("Failed to get hostname: {}", e.what());
65 log<level::ERR>(msg.c_str(), entry("ERROR=%s", e.what()));
66 }
Ratan Gupta82e1ef92017-06-15 08:39:15 +053067
William A. Kennington IIId99e6db2022-11-15 20:39:45 -080068 emit_object_added();
Ratan Gupta82e1ef92017-06-15 08:39:15 +053069}
70
71std::string SystemConfiguration::hostName(std::string name)
72{
73 if (SystemConfigIntf::hostName() == name)
74 {
75 return name;
76 }
Ratan Guptad30adf82020-06-12 09:47:32 +053077 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053078 {
William A. Kennington III40c29b52022-11-16 17:48:10 -080079 auto method = bus.new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
80 HOSTNAMED_INTF, "SetStaticHostname");
81 method.append(name, /*interactive=*/false);
82 bus.call_noreply(method);
83 return SystemConfigIntf::hostName(std::move(name));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053084 }
William A. Kennington III40c29b52022-11-16 17:48:10 -080085 catch (const std::exception& e)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053086 {
William A. Kennington III40c29b52022-11-16 17:48:10 -080087 auto msg = fmt::format("Failed to set hostname: {}", e.what());
88 log<level::ERR>(msg.c_str(), entry("ERROR=%s", e.what()));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053089 }
William A. Kennington III40c29b52022-11-16 17:48:10 -080090 return SystemConfigIntf::hostName();
Ratan Gupta82e1ef92017-06-15 08:39:15 +053091}
92
Gunnar Mills57d9c502018-09-14 14:42:34 -050093} // namespace network
94} // namespace phosphor