Skip to content

Commit

Permalink
make libsystemd optional
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Mar 8, 2024
1 parent 3bf4ad6 commit 97740ba
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ You need:
- a C++20 compliant compiler (e.g. gcc or clang)
- `libfmt <https://fmt.dev/>`__
- `LuaJIT <http://luajit.org/>`__
- `systemd <https://www.freedesktop.org/wiki/Software/systemd/>`__
- `Meson 0.56 <http://mesonbuild.com/>`__ and `Ninja <https://ninja-build.org/>`__

Optional dependencies:

- `libsodium <https://www.libsodium.org/>`__
- `systemd <https://www.freedesktop.org/wiki/Software/systemd/>`__

Get the source code::

Expand Down
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cm4all-passage (0.19) unstable; urgency=low

* lua: add libsodium bindings
* make libsystemd optional

--

Expand Down
11 changes: 8 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ add_global_arguments(common_flags, language: 'cpp')
add_global_arguments(compiler.get_supported_arguments(test_global_cxxflags), language: 'cpp')
add_project_arguments(compiler.get_supported_arguments(test_cxxflags), language: 'cpp')

libsystemd = dependency('libsystemd')
libsystemd = dependency('libsystemd', required: get_option('systemd'))

inc = include_directories('src', 'libcommon/src')

Expand All @@ -114,10 +114,14 @@ subdir('libcommon/src/event')
subdir('libcommon/src/net')
subdir('libcommon/src/net/control')
subdir('libcommon/src/event/net')
subdir('libcommon/src/event/systemd')
subdir('libcommon/src/lua/io')
subdir('libcommon/src/lua/net')

if libsystemd.found()
subdir('libcommon/src/event/systemd')
libsystemd = event_systemd_dep
endif

if sodium_dep.found()
subdir('libcommon/src/lua/sodium')
else
Expand All @@ -133,6 +137,7 @@ spawn_dep = declare_dependency(link_with: spawn,
dependencies: [event_dep])

conf.set('HAVE_LIBSODIUM', sodium_dep.found())
conf.set('HAVE_LIBSYSTEMD', libsystemd.found())
configure_file(output: 'config.h', configuration: conf)

executable('cm4all-passage',
Expand Down Expand Up @@ -160,7 +165,7 @@ executable('cm4all-passage',
lua_sodium_dep,
control_client_dep,
event_net_dep,
event_systemd_dep,
libsystemd,
spawn_dep,
fmt_dep,
],
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
option('sodium', type: 'feature', description: 'libsodium support')
option('systemd', type: 'feature', description: 'systemd support (using libsystemd)')

option('documentation', type: 'feature',
description: 'Build documentation')
8 changes: 8 additions & 0 deletions src/Instance.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ extern "C" {
#include <lauxlib.h>
}

#ifdef HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include <stdexcept>

Expand Down Expand Up @@ -55,6 +57,8 @@ Instance::AddListener(SocketAddress address, Lua::ValuePtr &&handler)
AddListener(MakeListener(address), std::move(handler));
}

#ifdef HAVE_LIBSYSTEMD

void
Instance::AddSystemdListener(Lua::ValuePtr &&handler)
{
Expand All @@ -70,6 +74,8 @@ Instance::AddSystemdListener(Lua::ValuePtr &&handler)
Lua::ValuePtr(handler));
}

#endif // HAVE_LIBSYSTEMD

void
Instance::Check()
{
Expand All @@ -83,7 +89,9 @@ Instance::OnShutdown() noexcept
shutdown_listener.Disable();
sighup_event.Disable();

#ifdef HAVE_LIBSYSTEMD
systemd_watchdog.Disable();
#endif

event_loop.Break();
}
Expand Down
8 changes: 8 additions & 0 deletions src/Instance.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#include "event/Loop.hxx"
#include "event/ShutdownListener.hxx"
#include "event/SignalEvent.hxx"
#include "config.h"

#ifdef HAVE_LIBSYSTEMD
#include "event/systemd/Watchdog.hxx"
#endif

#include <forward_list>

Expand All @@ -25,7 +29,9 @@ class Instance final {
ShutdownListener shutdown_listener{event_loop, BIND_THIS_METHOD(OnShutdown)};
SignalEvent sighup_event;

#ifdef HAVE_LIBSYSTEMD
Systemd::Watchdog systemd_watchdog{event_loop};
#endif

ZombieReaper zombie_reaper{event_loop};

Expand Down Expand Up @@ -55,11 +61,13 @@ public:
void AddListener(SocketAddress address,
Lua::ValuePtr &&handler);

#ifdef HAVE_LIBSYSTEMD
/**
* Listen for incoming connections on sockets passed by systemd
* (systemd socket activation).
*/
void AddSystemdListener(Lua::ValuePtr &&handler);
#endif // HAVE_LIBSYSTEMD

void Check();

Expand Down
18 changes: 15 additions & 3 deletions src/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ extern "C" {
#include <lualib.h>
}

#ifdef HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // for chdir()

#ifdef HAVE_LIBSYSTEMD

static int systemd_magic = 42;

static bool
Expand All @@ -43,6 +47,8 @@ IsSystemdMagic(lua_State *L, int idx)
lua_touserdata(L, idx) == &systemd_magic;
}

#endif // HAVE_LIBSYSTEMD

static int
l_passage_listen(lua_State *L)
try {
Expand All @@ -56,15 +62,17 @@ try {

auto handler = std::make_shared<Lua::Value>(L, Lua::StackIndex(2));

if (IsSystemdMagic(L, 1)) {
instance.AddSystemdListener(std::move(handler));
} else if (lua_isstring(L, 1)) {
if (lua_isstring(L, 1)) {
const char *address_string = lua_tostring(L, 1);

AllocatedSocketAddress address;
address.SetLocal(address_string);

instance.AddListener(address, std::move(handler));
#ifdef HAVE_LIBSYSTEMD
} if (IsSystemdMagic(L, 1)) {
instance.AddSystemdListener(std::move(handler));
#endif
} else
luaL_argerror(L, 1, "path expected");

Expand All @@ -85,7 +93,9 @@ SetupConfigState(lua_State *L, Instance &instance)
Lua::InitSocketAddress(L);
RegisterLuaResolver(L);

#ifdef HAVE_LIBSYSTEMD
Lua::SetGlobal(L, "systemd", Lua::LightUserData(&systemd_magic));
#endif

Lua::SetGlobal(L, "passage_listen",
Lua::MakeCClosure(l_passage_listen,
Expand Down Expand Up @@ -139,8 +149,10 @@ Run(const CommandLine &cmdline)

SetupRuntimeState(instance.GetLuaState());

#ifdef HAVE_LIBSYSTEMD
/* tell systemd we're ready */
sd_notify(0, "READY=1");
#endif

instance.GetEventLoop().Run();

Expand Down

0 comments on commit 97740ba

Please sign in to comment.