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