rousette: Import upstream patch to remove systemd dependancy

This commit is contained in:
Mattias Walström
2024-06-19 15:35:51 +02:00
parent f819279aea
commit dadf81baf7
2 changed files with 110 additions and 77 deletions
@@ -1,3 +1,3 @@
service name:rousette notify:none log <pid/confd> \
[12345] rousette \
[12345] rousette --syslog \
-- RESTCONF server
+109 -76
View File
@@ -1,25 +1,31 @@
From 6277d5f51f3a66df027bdd1752ea0ad58beb26fa Mon Sep 17 00:00:00 2001
From 561c28496691fc1ac17e755bcc0ff3ec8c3b1bf0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= <lazzer@gmail.com>
Date: Mon, 27 May 2024 18:29:15 +0200
Subject: [PATCH] Make systemd optional
Date: Tue, 28 May 2024 09:28:38 +0200
Subject: [PATCH] Change systemd from a required dependency to an optional
dependency.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Organization: Addiva Elektronik
If compile with systemd, set USE_SYSTEMD variable
In addition to journal logging, enable logging to syslog and stdout.
Signed-off-by: Mattias Walström <lazzer@gmail.com>
Change-Id: I90cb9169e8fdf9103c31bc983a1ec8a5de3dd26c
---
CMakeLists.txt | 13 ++++++++++---
src/restconf/main.cpp | 36 +++++++++++++++++++++++++++---------
2 files changed, 37 insertions(+), 12 deletions(-)
CMakeLists.txt | 14 +++++++++++---
README.md | 3 ++-
ci/pre.yaml | 6 ++++++
src/configure.cmake.h.in | 3 +++
src/restconf/main.cpp | 36 ++++++++++++++++++++++++++++++++----
5 files changed, 54 insertions(+), 8 deletions(-)
create mode 100644 src/configure.cmake.h.in
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7f82660..0f5bed9 100644
index 7f82660..43313ba 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -72,10 +72,12 @@ find_package(spdlog REQUIRED)
@@ -72,11 +72,16 @@ find_package(spdlog REQUIRED)
find_package(PkgConfig)
pkg_check_modules(nghttp2 REQUIRED IMPORTED_TARGET libnghttp2_asio>=0.0.90 libnghttp2)
find_package(Boost REQUIRED COMPONENTS system thread)
@@ -27,109 +33,136 @@ index 7f82660..0f5bed9 100644
pkg_check_modules(SYSREPO-CPP REQUIRED IMPORTED_TARGET sysrepo-cpp>=1.1.0)
pkg_check_modules(LIBYANG-CPP REQUIRED IMPORTED_TARGET libyang-cpp>=1.1.0)
-pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
+if(USE_SYSTEMD)
+ add_definitions(-DUSE_SYSTEMD=1)
+ pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
+endif()
+pkg_check_modules(SYSTEMD IMPORTED_TARGET libsystemd)
pkg_check_modules(PAM REQUIRED IMPORTED_TARGET pam)
+pkg_check_modules(DOCOPT REQUIRED IMPORTED_TARGET docopt)
+if(SYSTEMD_FOUND)
+ set(HAVE_SYSTEMD TRUE)
+endif()
+
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/configure.cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/configure.cmake.h)
add_library(rousette-http STATIC
@@ -116,7 +118,12 @@ add_executable(watch-operational-ds
src/http/EventStream.cpp
@@ -116,7 +121,10 @@ add_executable(watch-operational-ds
target_link_libraries(watch-operational-ds PUBLIC rousette-sysrepo)
add_executable(rousette src/restconf/main.cpp)
-target_link_libraries(rousette PUBLIC rousette-restconf PkgConfig::SYSTEMD)
+
+if(USE_SYSTEMD)
+ target_link_libraries(rousette PUBLIC rousette-restconf PkgConfig::SYSTEMD)
+else()
+ target_link_libraries(rousette PUBLIC rousette-restconf)
+target_link_libraries(rousette PUBLIC rousette-restconf PkgConfig::DOCOPT)
+if(SYSTEMD_FOUND)
+ target_link_libraries(rousette PUBLIC PkgConfig::SYSTEMD)
+endif()
install(TARGETS
# clock-demo
diff --git a/README.md b/README.md
index 68b4b99..55b8241 100644
--- a/README.md
+++ b/README.md
@@ -37,12 +37,13 @@ The anonymous user access is disabled whenever these rules are not met.
- [nghttp2-asio](https://github.com/nghttp2/nghttp2-asio) - asynchronous C++ library for HTTP/2
- [sysrepo-cpp](https://github.com/sysrepo/sysrepo-cpp) - object-oriented bindings of the [*sysrepo*](https://github.com/sysrepo/sysrepo) library
- [libyang-cpp](https://github.com/CESNET/libyang-cpp) - C++ bindings for *libyang*
-- systemd - the shared library for logging to `sd-journal`
- [PAM](http://www.linux-pam.org/) - for authentication
- [spdlog](https://github.com/gabime/spdlog) - Very fast, header-only/compiled, C++ logging library
+- [docopt-cpp](https://github.com/docopt/docopt.cpp) - command-line argument parser
- Boost's system and thread
- C++20 compiler (e.g., GCC 10.x+, clang 10+)
- CMake 3.19+
+- optionally systemd - the shared library for logging to `sd-journal`
- optionally for built-in tests, [Doctest](https://github.com/onqtam/doctest/) as a C++ unit test framework
- optionally for built-in tests, [trompeloeil](https://github.com/rollbear/trompeloeil) for mock objects in C++
- optionally for built-in tests, [`pam_matrix` and `pam_wrapper`](https://cwrap.org/pam_wrapper.html) for PAM mocking
diff --git a/ci/pre.yaml b/ci/pre.yaml
index 51f1c66..325dbf4 100644
--- a/ci/pre.yaml
+++ b/ci/pre.yaml
@@ -11,3 +11,9 @@
name: pam_wrapper
state: present
become: true
+
+ - name: install docopt-cpp
+ package:
+ name: docopt-cpp-devel
+ state: present
+ become: true
diff --git a/src/configure.cmake.h.in b/src/configure.cmake.h.in
new file mode 100644
index 0000000..bc26981
--- /dev/null
+++ b/src/configure.cmake.h.in
@@ -0,0 +1,3 @@
+#pragma once
+
+#cmakedefine HAVE_SYSTEMD
diff --git a/src/restconf/main.cpp b/src/restconf/main.cpp
index bba3cbf..3162dcd 100644
index bba3cbf..2bc9a86 100644
--- a/src/restconf/main.cpp
+++ b/src/restconf/main.cpp
@@ -3,13 +3,17 @@
*
* Written by Jan Kundrát <jan.kundrat@cesnet.cz>
*
-*/
+ */
#include <csignal>
@@ -9,14 +9,30 @@
#include <cstdio>
#include <cstdlib>
#include <inttypes.h>
+#ifdef USE_SYSTEMD
#include <spdlog/sinks/systemd_sink.h>
+#else
+#include <spdlog/sinks/syslog_sink.h>
-#include <spdlog/sinks/systemd_sink.h>
+
+#include "configure.cmake.h" /* Expose HAVE_SYSTEMD */
+
+#ifdef HAVE_SYSTEMD
+ #include <spdlog/sinks/systemd_sink.h>
+#endif
+#include <spdlog/sinks/syslog_sink.h>
#include <spdlog/sinks/ansicolor_sink.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -19,6 +23,7 @@
#include <unistd.h>
+#include <docopt.h>
#include <spdlog/spdlog.h>
#include <sysrepo-cpp/Session.hpp>
#include "restconf/Server.h"
+static const char usage[] =
+ R"(Rousette - RESTCONF server
+Usage:
+ rousette [--syslog] [--help]
+Options:
+ -h --help Show this screen.
+ --syslog Log to syslog.
+)";
+#ifdef HAVE_SYSTEMD
namespace {
+#ifdef USE_SYSTEMD
/** @short Is stderr connected to journald? Not thread safe. */
bool is_journald_active()
{
@@ -39,29 +44,42 @@ bool is_journald_active()
}
/** @short Provide better levels, see https://github.com/gabime/spdlog/pull/1292#discussion_r340777258 */
-template<typename Mutex>
+template <typename Mutex>
class journald_sink : public spdlog::sinks::systemd_sink<Mutex> {
public:
journald_sink()
{
this->syslog_levels_ = {/* spdlog::level::trace */ LOG_DEBUG,
- /* spdlog::level::debug */ LOG_INFO,
- /* spdlog::level::info */ LOG_NOTICE,
- /* spdlog::level::warn */ LOG_WARNING,
- /* spdlog::level::err */ LOG_ERR,
- /* spdlog::level::critical */ LOG_CRIT,
- /* spdlog::level::off */ LOG_ALERT};
+ /* spdlog::level::debug */ LOG_INFO,
+ /* spdlog::level::info */ LOG_NOTICE,
+ /* spdlog::level::warn */ LOG_WARNING,
+ /* spdlog::level::err */ LOG_ERR,
+ /* spdlog::level::critical */ LOG_CRIT,
+ /* spdlog::level::off */ LOG_ALERT};
@@ -54,13 +70,25 @@ public:
}
};
+#else
+void configure_syslog_logger()
+{
+ auto syslog_sink = std::make_shared<spdlog::sinks::syslog_sink_mt>("rousette", LOG_PID, LOG_USER, true);
+ auto logger = std::make_shared<spdlog::logger>("rousette", syslog_sink);
+ spdlog::set_default_logger(logger);
+ spdlog::set_level(spdlog::level::trace);
+}
+#endif
}
-
-int main(int argc [[maybe_unused]], char* argv [[maybe_unused]] [])
+int main(int argc [[maybe_unused]], char* argv [[maybe_unused]][])
+#endif
+int main(int argc, char* argv [])
{
+#ifdef USE_SYSTEMD
if (is_journald_active()) {
- if (is_journald_active()) {
+ auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true,""/* version */, true);
+
+ if (args["--syslog"].asBool()) {
+ auto syslog_sink = std::make_shared<spdlog::sinks::syslog_sink_mt>("rousette", LOG_PID, LOG_USER, true);
+ auto logger = std::make_shared<spdlog::logger>("rousette", syslog_sink);
+ spdlog::set_default_logger(logger);
+#ifdef HAVE_SYSTEMD
+ } else if (is_journald_active()) {
auto sink = std::make_shared<journald_sink<std::mutex>>();
auto logger = std::make_shared<spdlog::logger>("rousette", sink);
spdlog::set_default_logger(logger);
}
+#else
+ configure_syslog_logger();
+#endif
+ } else {
+ auto stdout_sink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>();
+ auto logger = std::make_shared<spdlog::logger>("rousette", stdout_sink);
+ spdlog::set_default_logger(logger);
}
spdlog::set_level(spdlog::level::trace);
/* We will parse URIs using boost::spirit's alnum/alpha/... matchers which are locale-dependent.
--
2.34.1