libsystemd: new library, refactor sd-daemon.[ch] test code

This commit introduces a bare-bones replacement for libsystemd:

 - Build .so file and add --with-libsystemd to configure
 - Add capabilities support to test/src/serv.c
 - Update tests to account for a Finit built w/o libsystemd support

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2025-07-02 16:42:06 +02:00
parent 1396177706
commit da83462731
12 changed files with 100 additions and 13 deletions
+12 -1
View File
@@ -1,5 +1,5 @@
ACLOCAL_AMFLAGS = -I m4
SUBDIRS = man plugins src system test tmpfiles.d
SUBDIRS = man plugins src system tmpfiles.d
doc_DATA = README.md LICENSE contrib/finit.conf
EXTRA_DIST = README.md LICENSE ChangeLog.md contrib/finit.conf
@@ -9,6 +9,17 @@ endif
if DOC
SUBDIRS += doc
endif
if LIBSYSTEMD
SUBDIRS += libsystemd
endif
if LIBSYSTEMD
SUBDIRS += test
# Explicit dependency to ensure libsystemd is built before test
test: libsystemd
else
SUBDIRS += test
endif
if BASH
bashdir = $(BASH_DIR)
+18 -8
View File
@@ -12,6 +12,7 @@ AC_CONFIG_FILES([Makefile
contrib/debian/Makefile contrib/debian/finit.d/Makefile contrib/debian/finit.d/available/Makefile
contrib/void/Makefile contrib/void/finit.d/Makefile contrib/void/finit.d/available/Makefile
doc/Makefile
libsystemd/Makefile libsystemd/libsystemd.pc
man/Makefile
plugins/Makefile
src/Makefile
@@ -168,6 +169,10 @@ AC_ARG_WITH(watchdog,
AS_HELP_STRING([--with-watchdog@<:@=DEV@:>@], [Enable built-in watchdog, default: /dev/watchdog]),
[watchdog=$withval], [with_watchdog=no watchdog=])
AC_ARG_WITH(libsystemd,
AS_HELP_STRING([--with-libsystemd], [Build replacement libsystemd library, default: yes]),
[with_libsystemd=$withval], [with_libsystemd=yes])
AC_ARG_WITH(hook-scripts-path,
AS_HELP_STRING([--with-hook-scripts-path=DIR], [Base directory for hook scripts, default $libexecdir/finit/hook]),
[hook_scripts_path=$withval], [hook_scripts_path=yes])
@@ -308,6 +313,9 @@ AS_IF([test "x$with_watchdog" != "xno"], [
AC_DEFINE_UNQUOTED(WDT_DEVNODE, "$watchdog", [Built-in watchdog device node])], [
watchdog=])
AS_IF([test "x$with_libsystemd" != "xno"], [
AC_DEFINE(HAVE_LIBSYSTEMD, 1, [Build replacement libsystemd library support])])
AS_IF([test "x$enable_hook_scripts_plugin" != "xno"], [
AS_IF([test "x$hook_scripts_path" = "xyes"], [hook_scripts_path=$libexecdir/finit/hook])
AC_EXPAND_DIR(hook_scripts_path, "$hook_scripts_path")
@@ -319,14 +327,15 @@ AS_IF([test "x$with_plugin_path" != "xno"], [
AC_DEFINE_UNQUOTED(EXTERNAL_PLUGIN_PATH, "$plugin_path", [Built-in default: /usr/lib/finit/plugins:/usr/local/lib/finit/plugins])])
# Control build with automake flags
AM_CONDITIONAL(BASH, [test "x$bash_dir" != "xno"])
AM_CONDITIONAL(STATIC, [test "x$enable_static" = "xyes"])
AM_CONDITIONAL(KEVENTD, [test "x$with_keventd" != "xno"])
AM_CONDITIONAL(SULOGIN, [test "x$with_sulogin" != "xno"])
AM_CONDITIONAL(WATCHDOGD, [test "x$with_watchdog" != "xno"])
AM_CONDITIONAL(DOC, [test "x$enable_doc" = "xyes"])
AM_CONDITIONAL(CONTRIB, [test "x$enable_contrib" = "xyes"])
AM_CONDITIONAL(TESTSERV, [test "x$enable_testserv_plugin" = "xyes"])
AM_CONDITIONAL(BASH, [test "x$bash_dir" != "xno"])
AM_CONDITIONAL(STATIC, [test "x$enable_static" = "xyes"])
AM_CONDITIONAL(KEVENTD, [test "x$with_keventd" != "xno"])
AM_CONDITIONAL(SULOGIN, [test "x$with_sulogin" != "xno"])
AM_CONDITIONAL(WATCHDOGD, [test "x$with_watchdog" != "xno"])
AM_CONDITIONAL(LIBSYSTEMD, [test "x$with_libsystemd" != "xno"])
AM_CONDITIONAL(DOC, [test "x$enable_doc" = "xyes"])
AM_CONDITIONAL(CONTRIB, [test "x$enable_contrib" = "xyes"])
AM_CONDITIONAL(TESTSERV, [test "x$enable_testserv_plugin" = "xyes"])
# Workaround for as-of-yet unreleased runstatedir support, planned for
# autoconf 2.70, which some major distros have backported.
@@ -408,6 +417,7 @@ Optional features:
Built-in sulogin......: $with_sulogin $sulogin
Built-in watchdogd....: $with_watchdog $watchdog
Built-in logrotate....: $enable_logrotate
Replacement libsystemd: $with_libsystemd
Use cgroup v2.........: $enable_cgroup
Parse kernel cmdline..: $enable_kernel_cmdline
Keep kernel logging...: $enable_kernel_logging
+7
View File
@@ -0,0 +1,7 @@
.deps/*
.libs/*
.dirstamp
*.lo
libsystemd.*
Makefile
Makefile.in
+12
View File
@@ -0,0 +1,12 @@
# libsystemd replacement library
lib_LTLIBRARIES = libsystemd.la
libsystemd_la_SOURCES = sd-daemon.c sd-daemon.h
libsystemd_la_LDFLAGS = -version-info 0:0:0
# pkg-config support
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libsystemd.pc
# Install header to systemd/ subdirectory
systemddir = $(includedir)/systemd
systemd_HEADERS = sd-daemon.h
+10
View File
@@ -0,0 +1,10 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: libsystemd
Description: Finit replacement for systemd notification library
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lsystemd
Cflags: -I${includedir}
+1
View File
@@ -9,6 +9,7 @@ endif
sbin_PROGRAMS = finit initctl
pkglibexec_PROGRAMS = getty logit runparts tmpfiles
if SULOGIN
pkglibexec_PROGRAMS += sulogin
endif
+9
View File
@@ -25,6 +25,15 @@ test_one()
type=$1
service=$2
# Skip systemd tests if serv doesn't have libsystemd support
if [ "$type" = "systemd" ]; then
if ! "$TEST_DIR/src/serv" -C | grep -q "libsystemd"; then
sep
say "Skipping systemd test - serv built without libsystemd support"
return 0
fi
fi
sep
# num=$(run "find /proc/1/fd |wc -l")
+1 -1
View File
@@ -1,5 +1,5 @@
/Makefile
/Makefile.in
/.libs/
/.deps/
/serv
+6 -2
View File
@@ -1,4 +1,8 @@
noinst_PROGRAMS = serv
serv_SOURCES = serv.c sd-daemon.c sd-daemon.h
serv_CPPFLAGS = -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE -D_DEFAULT_SOURCE
serv_SOURCES = serv.c
serv_CPPFLAGS = -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE -D_DEFAULT_SOURCE -I$(top_builddir)
if LIBSYSTEMD
serv_CPPFLAGS += -I$(top_srcdir)/libsystemd
serv_SOURCES += $(top_srcdir)/libsystemd/sd-daemon.c
endif
+24 -1
View File
@@ -5,6 +5,10 @@
* in foregrund it does not create a PID file by default.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
@@ -16,7 +20,9 @@
#include <sysexits.h>
#include <sys/socket.h>
#include <sys/stat.h>
#ifdef HAVE_LIBSYSTEMD
#include "sd-daemon.h"
#endif
#define PROGNM "serv"
@@ -146,6 +152,16 @@ static void pidfile(char *pidfn)
}
}
static int capabilities(void)
{
#ifdef HAVE_LIBSYSTEMD
puts("libsystemd");
#endif
puts("s6");
return 0;
}
static int usage(int rc)
{
FILE *fp = rc ? stderr : stdout;
@@ -153,6 +169,7 @@ static int usage(int rc)
fprintf(fp,
"%s [-nhp] [-P FILE]\n"
"\n"
" -C Show capabilities and exit\n"
" -c Crash (exit) immediately\n"
" -e K:V Verify K environment variable is V value\n"
" -E K Verify K is not set in the environment\n"
@@ -195,11 +212,13 @@ int main(int argc, char *argv[])
char cmd[80];
int c;
while ((c = getopt(argc, argv, "ce:E:f:F:hi:nN:pP:r:")) != EOF) {
while ((c = getopt(argc, argv, "cCe:E:f:F:hi:nN:pP:r:")) != EOF) {
switch (c) {
case 'c':
do_crash = 1;
break;
case 'C':
return capabilities();
case 'e':
verify_env(optarg);
break;
@@ -295,11 +314,15 @@ int main(int argc, char *argv[])
err(1, "Failed closing notify socket");
do_notify = 0;
} else {
#ifdef HAVE_LIBSYSTEMD
int rc;
inf("Notifying Finit on NOTIFY_SOCKET");
rc = sd_notify(0, "READY=1");
inf("sd_notify () => %d", rc);
#else
inf("Skipping systemd notify - built without libsystemd support");
#endif
}
}
if (do_pidfile > 0)