blob: 58b6dfcabd55092a01e1212e8512eb89edf1ba63 [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>
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
17using namespace phosphor::logging;
18using namespace sdbusplus::xyz::openbmc_project::Common::Error;
19
William A. Kennington III40c29b52022-11-16 17:48:10 -080020static constexpr char propMatch[] =
21 "type='signal',sender='org.freedesktop.hostname1',"
22 "path='/org/freedesktop/hostname1',"
23 "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',"
24 "arg0='org.freedesktop.hostname1'";
Ratan Gupta82e1ef92017-06-15 08:39:15 +053025
William A. Kennington III9ede1b72022-11-21 01:59:28 -080026SystemConfiguration::SystemConfiguration(
27 stdplus::PinnedRef<sdbusplus::bus_t> bus, stdplus::const_zstring objPath) :
Patrick Williams166b9592022-03-30 16:09:16 -050028 Iface(bus, objPath.c_str(), Iface::action::defer_emit),
William A. Kennington III9ede1b72022-11-21 01:59:28 -080029 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
William A. Kennington III40c29b52022-11-16 17:48:10 -080036 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -080037 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));
William A. Kennington III40c29b52022-11-16 17:48:10 -080044 }
William A. Kennington III9ede1b72022-11-21 01:59:28 -080045 catch (const std::exception& e)
46 {
47 log<level::ERR>(
48 fmt::format("Hostname match parsing failed: {}", e.what())
49 .c_str(),
50 entry("ERROR=%s", e.what()));
51 }
52 })
Ratan Gupta82e1ef92017-06-15 08:39:15 +053053{
William A. Kennington III40c29b52022-11-16 17:48:10 -080054 try
55 {
56 std::variant<std::string> name;
57 auto req =
William A. Kennington III9ede1b72022-11-21 01:59:28 -080058 bus.get().new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
59 "org.freedesktop.DBus.Properties", "Get");
William A. Kennington III40c29b52022-11-16 17:48:10 -080060
61 req.append(HOSTNAMED_INTF, "Hostname");
William A. Kennington III9ede1b72022-11-21 01:59:28 -080062 auto reply = req.call();
William A. Kennington III40c29b52022-11-16 17:48:10 -080063 reply.read(name);
64 SystemConfigIntf::hostName(std::get<std::string>(name), true);
65 }
66 catch (const std::exception& e)
67 {
68 auto msg = fmt::format("Failed to get hostname: {}", e.what());
69 log<level::ERR>(msg.c_str(), entry("ERROR=%s", e.what()));
70 }
Ratan Gupta82e1ef92017-06-15 08:39:15 +053071
William A. Kennington IIId99e6db2022-11-15 20:39:45 -080072 emit_object_added();
Ratan Gupta82e1ef92017-06-15 08:39:15 +053073}
74
75std::string SystemConfiguration::hostName(std::string name)
76{
77 if (SystemConfigIntf::hostName() == name)
78 {
79 return name;
80 }
Ratan Guptad30adf82020-06-12 09:47:32 +053081 try
Ratan Gupta82e1ef92017-06-15 08:39:15 +053082 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -080083 auto method = bus.get().new_method_call(
84 HOSTNAMED_SVC, HOSTNAMED_OBJ, HOSTNAMED_INTF, "SetStaticHostname");
William A. Kennington III40c29b52022-11-16 17:48:10 -080085 method.append(name, /*interactive=*/false);
William A. Kennington III9ede1b72022-11-21 01:59:28 -080086 bus.get().call_noreply(method);
William A. Kennington III40c29b52022-11-16 17:48:10 -080087 return SystemConfigIntf::hostName(std::move(name));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053088 }
William A. Kennington III40c29b52022-11-16 17:48:10 -080089 catch (const std::exception& e)
Ratan Gupta82e1ef92017-06-15 08:39:15 +053090 {
William A. Kennington III40c29b52022-11-16 17:48:10 -080091 auto msg = fmt::format("Failed to set hostname: {}", e.what());
92 log<level::ERR>(msg.c_str(), entry("ERROR=%s", e.what()));
Ratan Gupta82e1ef92017-06-15 08:39:15 +053093 }
William A. Kennington III40c29b52022-11-16 17:48:10 -080094 return SystemConfigIntf::hostName();
Ratan Gupta82e1ef92017-06-15 08:39:15 +053095}
96
Gunnar Mills57d9c502018-09-14 14:42:34 -050097} // namespace network
98} // namespace phosphor