blob: ee6cdb74d3da897d85769abd8dac77c1953a2ec0 [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
Ravi Teja46dda592024-01-12 04:54:35 -060017using namespace phosphor::logging;
Ratan Gupta82e1ef92017-06-15 08:39:15 +053018using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ravi Teja46dda592024-01-12 04:54:35 -060019using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta82e1ef92017-06-15 08:39:15 +053020
William A. Kennington III40c29b52022-11-16 17:48:10 -080021static constexpr char propMatch[] =
22 "type='signal',sender='org.freedesktop.hostname1',"
23 "path='/org/freedesktop/hostname1',"
24 "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',"
25 "arg0='org.freedesktop.hostname1'";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053026
William A. Kennington III9ede1b72022-11-21 01:59:28 -080027SystemConfiguration::SystemConfiguration(
28 stdplus::PinnedRef<sdbusplus::bus_t> bus, stdplus::const_zstring objPath) :
Patrick Williamsad205022024-08-16 15:20:07 -040029 Iface(bus, objPath.c_str(), Iface::action::defer_emit), bus(bus),
30 hostnamePropMatch(
31 bus, propMatch,
32 [sc = stdplus::PinnedRef(*this)](sdbusplus::message_t& m) {
33 std::string intf;
34 std::unordered_map<std::string, std::variant<std::string>> values;
35 try
36 {
37 m.read(intf, values);
38 auto it = values.find("Hostname");
39 if (it == values.end())
40 {
41 return;
42 }
43 sc.get().Iface::hostName(std::get<std::string>(it->second));
44 }
45 catch (const std::exception& e)
46 {
47 lg2::error("Hostname match parsing failed: {ERROR}", "ERROR",
48 e);
49 }
50 })
Ratan Gupta82e1ef92017-06-15 08:39:15 +053051{
William A. Kennington III40c29b52022-11-16 17:48:10 -080052 try
53 {
54 std::variant<std::string> name;
Patrick Williamsad205022024-08-16 15:20:07 -040055 auto req =
56 bus.get().new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
57 "org.freedesktop.DBus.Properties", "Get");
William A. Kennington III40c29b52022-11-16 17:48:10 -080058
59 req.append(HOSTNAMED_INTF, "Hostname");
William A. Kennington III9ede1b72022-11-21 01:59:28 -080060 auto reply = req.call();
William A. Kennington III40c29b52022-11-16 17:48:10 -080061 reply.read(name);
62 SystemConfigIntf::hostName(std::get<std::string>(name), true);
63 }
64 catch (const std::exception& e)
65 {
Jagpal Singh Gilla2947b42023-04-17 21:10:14 -070066 lg2::error("Failed to get hostname: {ERROR}", "ERROR", e);
William A. Kennington III40c29b52022-11-16 17:48:10 -080067 }
Ratan Gupta82e1ef92017-06-15 08:39:15 +053068
William A. Kennington IIId99e6db2022-11-15 20:39:45 -080069 emit_object_added();
Ratan Gupta82e1ef92017-06-15 08:39:15 +053070}
71
72std::string SystemConfiguration::hostName(std::string name)
73{
74 if (SystemConfigIntf::hostName() == name)
75 {
76 return name;
77 }
Ratan Guptad30adf82020-06-12 09:47:32 +053078 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053079 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -080080 auto method = bus.get().new_method_call(
81 HOSTNAMED_SVC, HOSTNAMED_OBJ, HOSTNAMED_INTF, "SetStaticHostname");
William A. Kennington III40c29b52022-11-16 17:48:10 -080082 method.append(name, /*interactive=*/false);
Ravi Teja46dda592024-01-12 04:54:35 -060083 method.call();
William A. Kennington III40c29b52022-11-16 17:48:10 -080084 return SystemConfigIntf::hostName(std::move(name));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053085 }
Ravi Teja46dda592024-01-12 04:54:35 -060086 catch (const sdbusplus::exception::SdBusError& e)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053087 {
Ravi Teja46dda592024-01-12 04:54:35 -060088 lg2::error("Failed to set hostname {HOSTNAME}: {ERROR} ", "HOSTNAME",
89 name, "ERROR", e);
90 auto dbusError = e.get_error();
91 if ((dbusError != nullptr) &&
92 (strcmp(dbusError->name,
93 "org.freedesktop.DBus.Error.InvalidArgs") == 0))
94 {
95 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Hostname"),
96 Argument::ARGUMENT_VALUE(name.c_str()));
97 }
Ratan Gupta82e1ef92017-06-15 08:39:15 +053098 }
William A. Kennington III40c29b52022-11-16 17:48:10 -080099 return SystemConfigIntf::hostName();
Ratan Gupta82e1ef92017-06-15 08:39:15 +0530100}
101
Gunnar Mills57d9c502018-09-14 14:42:34 -0500102} // namespace network
103} // namespace phosphor