blob: f817dfe70d543556e31e7cf701c02d02e5aaa6a5 [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>
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -07004#include <phosphor-logging/lg2.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -08005#include <stdplus/pinned.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta82e1ef92017-06-15 08:39:15 +05307
8namespace phosphor
9{
10namespace network
11{
12
William A. Kennington III40c29b52022-11-16 17:48:10 -080013static constexpr char HOSTNAMED_SVC[] = "org.freedesktop.hostname1";
14static constexpr char HOSTNAMED_OBJ[] = "/org/freedesktop/hostname1";
15static constexpr char HOSTNAMED_INTF[] = "org.freedesktop.hostname1";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053016
Ratan Gupta82e1ef92017-06-15 08:39:15 +053017using 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
William A. Kennington III9ede1b72022-11-21 01:59:28 -080025SystemConfiguration::SystemConfiguration(
26 stdplus::PinnedRef<sdbusplus::bus_t> bus, stdplus::const_zstring objPath) :
Patrick Williams166b9592022-03-30 16:09:16 -050027 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
William A. Kennington III9ede1b72022-11-21 01:59:28 -080028 bus(bus),
29 hostnamePropMatch(
30 bus, propMatch,
31 [sc = stdplus::PinnedRef(*this)](sdbusplus::message_t& m) {
32 std::string intf;
33 std::unordered_map<std::string, std::variant<std::string>> values;
34 try
William A. Kennington III40c29b52022-11-16 17:48:10 -080035 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -080036 m.read(intf, values);
37 auto it = values.find("Hostname");
38 if (it == values.end())
39 {
40 return;
41 }
42 sc.get().Iface::hostName(std::get<std::string>(it->second));
William A. Kennington III40c29b52022-11-16 17:48:10 -080043 }
William A. Kennington III9ede1b72022-11-21 01:59:28 -080044 catch (const std::exception& e)
45 {
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070046 lg2::error("Hostname match parsing failed: {ERROR}", "ERROR",
47 e);
William A. Kennington III9ede1b72022-11-21 01:59:28 -080048 }
49 })
Ratan Gupta82e1ef92017-06-15 08:39:15 +053050{
William A. Kennington III40c29b52022-11-16 17:48:10 -080051 try
52 {
53 std::variant<std::string> name;
54 auto req =
William A. Kennington III9ede1b72022-11-21 01:59:28 -080055 bus.get().new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
56 "org.freedesktop.DBus.Properties", "Get");
William A. Kennington III40c29b52022-11-16 17:48:10 -080057
58 req.append(HOSTNAMED_INTF, "Hostname");
William A. Kennington III9ede1b72022-11-21 01:59:28 -080059 auto reply = req.call();
William A. Kennington III40c29b52022-11-16 17:48:10 -080060 reply.read(name);
61 SystemConfigIntf::hostName(std::get<std::string>(name), true);
62 }
63 catch (const std::exception& e)
64 {
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070065 lg2::error("Failed to get hostname: {ERROR}", "ERROR", e);
William A. Kennington III40c29b52022-11-16 17:48:10 -080066 }
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 III9ede1b72022-11-21 01:59:28 -080079 auto method = bus.get().new_method_call(
80 HOSTNAMED_SVC, HOSTNAMED_OBJ, HOSTNAMED_INTF, "SetStaticHostname");
William A. Kennington III40c29b52022-11-16 17:48:10 -080081 method.append(name, /*interactive=*/false);
William A. Kennington III9ede1b72022-11-21 01:59:28 -080082 bus.get().call_noreply(method);
William A. Kennington III40c29b52022-11-16 17:48:10 -080083 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 {
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070087 lg2::error("Failed to set hostname: {ERROR}", "ERROR", e);
Ratan Gupta82e1ef92017-06-15 08:39:15 +053088 }
William A. Kennington III40c29b52022-11-16 17:48:10 -080089 return SystemConfigIntf::hostName();
Ratan Gupta82e1ef92017-06-15 08:39:15 +053090}
91
Gunnar Mills57d9c502018-09-14 14:42:34 -050092} // namespace network
93} // namespace phosphor