Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 3 | #include "xyz/openbmc_project/Network/Client/server.hpp" |
| 4 | |
| 5 | #include <phosphor-logging/log.hpp> |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <sdbusplus/server/object.hpp> |
| 8 | #include <string> |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace rsyslog_config |
| 13 | { |
| 14 | |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 15 | using namespace phosphor::logging; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 16 | using NetworkClient = sdbusplus::xyz::openbmc_project::Network::server::Client; |
| 17 | using Iface = sdbusplus::server::object::object<NetworkClient>; |
Deepak Kodihalli | aabb92e | 2018-10-17 05:03:20 -0500 | [diff] [blame] | 18 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 19 | |
| 20 | /** @class Server |
| 21 | * @brief Configuration for rsyslog server |
| 22 | * @details A concrete implementation of the |
| 23 | * xyz.openbmc_project.Network.Client API, in order to |
| 24 | * provide remote rsyslog server's address and port. |
| 25 | */ |
| 26 | class Server : public Iface |
| 27 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 28 | public: |
| 29 | Server() = delete; |
| 30 | Server(const Server&) = delete; |
| 31 | Server& operator=(const Server&) = delete; |
| 32 | Server(Server&&) = delete; |
| 33 | Server& operator=(Server&&) = delete; |
| 34 | virtual ~Server() = default; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 35 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 36 | /** @brief Constructor to put object onto bus at a dbus path. |
| 37 | * @param[in] bus - Bus to attach to. |
| 38 | * @param[in] path - Path to attach at. |
| 39 | * @param[in] filePath - rsyslog remote logging config file |
| 40 | */ |
| 41 | Server(sdbusplus::bus::bus& bus, const std::string& path, |
| 42 | const char* filePath) : |
Patrick Williams | 6ef6b25 | 2022-03-30 14:28:27 -0500 | [diff] [blame] | 43 | Iface(bus, path.c_str(), Iface::action::defer_emit), |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 44 | configFilePath(filePath), |
| 45 | hostnameChange( |
| 46 | bus, |
| 47 | sdbusRule::propertiesChanged("/org/freedesktop/hostname1", |
| 48 | "org.freedesktop.hostname1"), |
| 49 | std::bind(std::mem_fn(&Server::hostnameChanged), this, |
| 50 | std::placeholders::_1)) |
| 51 | { |
| 52 | try |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 53 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 54 | restore(configFilePath.c_str()); |
| 55 | } |
| 56 | catch (const std::exception& e) |
| 57 | { |
| 58 | log<level::ERR>(e.what()); |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 61 | emit_object_added(); |
| 62 | } |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 63 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 64 | using NetworkClient::address; |
| 65 | using NetworkClient::port; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 66 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 67 | /** @brief Override that updates rsyslog config file as well |
| 68 | * @param[in] value - remote server address |
| 69 | * @returns value of changed address |
| 70 | */ |
| 71 | virtual std::string address(std::string value) override; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 72 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 73 | /** @brief Override that updates rsyslog config file as well |
| 74 | * @param[in] value - remote server port |
| 75 | * @returns value of changed port |
| 76 | */ |
| 77 | virtual uint16_t port(uint16_t value) override; |
Deepak Kodihalli | 2ce7b2c | 2018-08-31 04:22:55 -0500 | [diff] [blame] | 78 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 79 | /** @brief Restart rsyslog's systemd unit |
| 80 | */ |
| 81 | virtual void restart(); |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 82 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 83 | private: |
| 84 | /** @brief Update remote server address and port in |
| 85 | * rsyslog config file. |
| 86 | * @param[in] serverAddress - remote server address |
| 87 | * @param[in] serverPort - remote server port |
| 88 | * @param[in] filePath - rsyslog config file path |
| 89 | */ |
| 90 | void writeConfig(const std::string& serverAddress, uint16_t serverPort, |
| 91 | const char* filePath); |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame] | 92 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 93 | /** @brief Checks if input IP address is valid (uses getaddrinfo) |
| 94 | * @param[in] address - server address |
| 95 | * @returns true if valid, false otherwise |
| 96 | */ |
| 97 | bool addressValid(const std::string& address); |
Deepak Kodihalli | 9fab279 | 2018-08-28 07:47:11 -0500 | [diff] [blame] | 98 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 99 | /** @brief Populate existing config into D-Bus properties |
| 100 | * @param[in] filePath - rsyslog config file path |
| 101 | */ |
| 102 | void restore(const char* filePath); |
Deepak Kodihalli | aabb92e | 2018-10-17 05:03:20 -0500 | [diff] [blame] | 103 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 104 | std::string configFilePath{}; |
Deepak Kodihalli | aabb92e | 2018-10-17 05:03:20 -0500 | [diff] [blame] | 105 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 106 | /** @brief React to hostname change |
| 107 | * @param[in] msg - sdbusplus message |
| 108 | */ |
Patrick Williams | 0e734df | 2021-04-16 20:09:03 -0500 | [diff] [blame] | 109 | void hostnameChanged(sdbusplus::message::message& /*msg*/) |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 110 | { |
| 111 | restart(); |
| 112 | } |
| 113 | |
| 114 | sdbusplus::bus::match_t hostnameChange; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace rsyslog_config |
| 118 | } // namespace phosphor |