Merge branch 'refactor-systemd-readiness'

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2025-07-04 16:45:37 +02:00
commit dedb56a9d0
22 files changed
+603 -103

No files matched your search

+12 -1
View File
@@ -1,5 +1,5 @@
ACLOCAL_AMFLAGS = -I m4 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 doc_DATA = README.md LICENSE contrib/finit.conf
EXTRA_DIST = README.md LICENSE ChangeLog.md contrib/finit.conf EXTRA_DIST = README.md LICENSE ChangeLog.md contrib/finit.conf
@@ -9,6 +9,17 @@ endif
if DOC if DOC
SUBDIRS += doc SUBDIRS += doc
endif 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 if BASH
bashdir = $(BASH_DIR) bashdir = $(BASH_DIR)
+29 -4
View File
@@ -1,8 +1,33 @@
#!/bin/sh #!/bin/sh
# Run test suite with the correct configure params # Run test suite with the correct configure params
run_make=1
jobs=17
make distclean while [ $# -gt 0 ]; do
./configure --prefix=/usr --exec-prefix= --sysconfdir=/etc --localstatedir=/var \ case "$1" in
--enable-x11-common-plugin --with-watchdog --with-keventd \ -n|--no-make)
run_make=0
;;
-j|--jobs)
shift
jobs="$1"
;;
*)
echo "Usage: $0 [--no-make] [-j N]"
exit 1
;;
esac
shift
done
if [ "$run_make" -eq 1 ]; then
make distclean
fi
./configure --prefix=/usr --exec-prefix= --sysconfdir=/etc --localstatedir=/var \
--enable-x11-common-plugin --with-watchdog --with-keventd \
CFLAGS='-fsanitize=address -ggdb' CFLAGS='-fsanitize=address -ggdb'
make -j17 clean check
if [ "$run_make" -eq 1 ]; then
make -j"$jobs" clean check
fi
+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/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 contrib/void/Makefile contrib/void/finit.d/Makefile contrib/void/finit.d/available/Makefile
doc/Makefile doc/Makefile
libsystemd/Makefile libsystemd/libsystemd.pc
man/Makefile man/Makefile
plugins/Makefile plugins/Makefile
src/Makefile src/Makefile
@@ -168,6 +169,10 @@ AC_ARG_WITH(watchdog,
AS_HELP_STRING([--with-watchdog@<:@=DEV@:>@], [Enable built-in watchdog, default: /dev/watchdog]), AS_HELP_STRING([--with-watchdog@<:@=DEV@:>@], [Enable built-in watchdog, default: /dev/watchdog]),
[watchdog=$withval], [with_watchdog=no 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, AC_ARG_WITH(hook-scripts-path,
AS_HELP_STRING([--with-hook-scripts-path=DIR], [Base directory for hook scripts, default $libexecdir/finit/hook]), 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]) [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])], [ AC_DEFINE_UNQUOTED(WDT_DEVNODE, "$watchdog", [Built-in watchdog device node])], [
watchdog=]) 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$enable_hook_scripts_plugin" != "xno"], [
AS_IF([test "x$hook_scripts_path" = "xyes"], [hook_scripts_path=$libexecdir/finit/hook]) AS_IF([test "x$hook_scripts_path" = "xyes"], [hook_scripts_path=$libexecdir/finit/hook])
AC_EXPAND_DIR(hook_scripts_path, "$hook_scripts_path") 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])]) 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 # Control build with automake flags
AM_CONDITIONAL(BASH, [test "x$bash_dir" != "xno"]) AM_CONDITIONAL(BASH, [test "x$bash_dir" != "xno"])
AM_CONDITIONAL(STATIC, [test "x$enable_static" = "xyes"]) AM_CONDITIONAL(STATIC, [test "x$enable_static" = "xyes"])
AM_CONDITIONAL(KEVENTD, [test "x$with_keventd" != "xno"]) AM_CONDITIONAL(KEVENTD, [test "x$with_keventd" != "xno"])
AM_CONDITIONAL(SULOGIN, [test "x$with_sulogin" != "xno"]) AM_CONDITIONAL(SULOGIN, [test "x$with_sulogin" != "xno"])
AM_CONDITIONAL(WATCHDOGD, [test "x$with_watchdog" != "xno"]) AM_CONDITIONAL(WATCHDOGD, [test "x$with_watchdog" != "xno"])
AM_CONDITIONAL(DOC, [test "x$enable_doc" = "xyes"]) AM_CONDITIONAL(LIBSYSTEMD, [test "x$with_libsystemd" != "xno"])
AM_CONDITIONAL(CONTRIB, [test "x$enable_contrib" = "xyes"]) AM_CONDITIONAL(DOC, [test "x$enable_doc" = "xyes"])
AM_CONDITIONAL(TESTSERV, [test "x$enable_testserv_plugin" = "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 # Workaround for as-of-yet unreleased runstatedir support, planned for
# autoconf 2.70, which some major distros have backported. # autoconf 2.70, which some major distros have backported.
@@ -408,6 +417,7 @@ Optional features:
Built-in sulogin......: $with_sulogin $sulogin Built-in sulogin......: $with_sulogin $sulogin
Built-in watchdogd....: $with_watchdog $watchdog Built-in watchdogd....: $with_watchdog $watchdog
Built-in logrotate....: $enable_logrotate Built-in logrotate....: $enable_logrotate
Replacement libsystemd: $with_libsystemd
Use cgroup v2.........: $enable_cgroup Use cgroup v2.........: $enable_cgroup
Parse kernel cmdline..: $enable_kernel_cmdline Parse kernel cmdline..: $enable_kernel_cmdline
Keep kernel logging...: $enable_kernel_logging 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}
+217
View File
@@ -0,0 +1,217 @@
/* SPDX-License-Identifier: MIT-0 */
/*
* systemd notify protocol, supporting readiness notification on startup
* and reloading, according to the protocol defined at:
* https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html
*
* This protocol is guaranteed to be stable as per:
* https://systemd.io/PORTABILITY_AND_STABILITY/
*/
#define _GNU_SOURCE 1
#include <errno.h>
#include <inttypes.h>
#include <signal.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include "sd-daemon.h"
/*
* NOTE: As per spec., this function does not return POSIX OK(0) but
* rather "a positive value", i.e., 1.
*/
int sd_notify(int unset_environment, const char *state)
{
struct sockaddr_un sun = {
.sun_family = AF_UNIX,
};
size_t len, sun_len;
const char *path;
ssize_t written;
int sd;
if (!state)
return -EINVAL;
len = strlen(state);
if (len == 0)
return -EINVAL;
/* If the variable is not set, the protocol is a noop */
path = getenv("NOTIFY_SOCKET");
if (!path)
return 0; /* Not set? Nothing to do */
/* Only AF_UNIX is supported, with path or abstract sockets */
if (path[0] != '/' && path[0] != '@')
return -EAFNOSUPPORT;
/* Ensure there is room for NUL byte */
sun_len = strlen(path);
if (sun_len >= sizeof(sun.sun_path))
return -E2BIG;
strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
if (sun.sun_path[0] == '@')
sun.sun_path[0] = 0;
sd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sd == -1)
return -errno;
if (connect(sd, (struct sockaddr *)&sun, offsetof(struct sockaddr_un, sun_path) + sun_len) == -1) {
close(sd);
return -errno;
}
if (unset_environment)
unsetenv("NOTIFY_SOCKET");
written = write(sd, state, len);
if (written != (ssize_t) len) {
close(sd);
return written < 0 ? -errno : -EPROTO;
}
return 1;
}
/*
* Printf-style notification function
*/
int sd_notifyf(int unset_environment, const char *format, ...)
{
va_list ap;
char *state;
int ret;
if (!format)
return -EINVAL;
va_start(ap, format);
ret = vasprintf(&state, format, ap);
va_end(ap);
if (ret < 0)
return -ENOMEM;
ret = sd_notify(unset_environment, state);
free(state);
return ret;
}
/*
* PID-specific notification function
*/
int sd_pid_notify(pid_t pid, int unset_environment, const char *state)
{
char path[256], *env;
size_t len;
int ret;
if (!state)
return -EINVAL;
len = strlen(state);
if (len == 0)
return -EINVAL;
if (pid == 0)
pid = getpid();
env = getenv("NOTIFY_SOCKET");
if (env) {
env = strdup(env);
if (!env)
return -errno;
}
snprintf(path, sizeof(path), "@run/finit/notify/%d", pid);
setenv("NOTIFY_SOCKET", path, 1);
ret = sd_notify(unset_environment, state);
if (env) {
setenv("NOTIFY_SOCKET", env, 1);
free(env);
}
return ret;
}
/*
* PID-specific printf-style notification function
*/
int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...)
{
va_list ap;
char *state;
int ret;
if (!format)
return -EINVAL;
va_start(ap, format);
ret = vasprintf(&state, format, ap);
va_end(ap);
if (ret < 0)
return -ENOMEM;
ret = sd_pid_notify(pid, unset_environment, state);
free(state);
return ret;
}
/*
* Socket activation stub - Finit doesn't support socket activation yet
* Returns 0 (no file descriptors passed)
*/
int sd_listen_fds(int unset_environment)
{
if (unset_environment) {
unsetenv("LISTEN_PID");
unsetenv("LISTEN_FDS");
unsetenv("LISTEN_FDNAMES");
}
return 0;
}
/*
* Watchdog stub - Finit doesn't support watchdog yet
* Returns 0 (no watchdog enabled)
*/
int sd_watchdog_enabled(int unset_environment, uint64_t *usec)
{
if (unset_environment) {
unsetenv("WATCHDOG_PID");
unsetenv("WATCHDOG_USEC");
}
if (usec)
*usec = 0;
return 0;
}
/*
* System detection function
* Returns 1 to indicate systemd compatibility (trick applications)
*/
int sd_booted(void)
{
return 1;
}
+38
View File
@@ -0,0 +1,38 @@
#ifndef SD_DAEMON_H_
#define SD_DAEMON_H_
#include <stdint.h>
#include <sys/types.h>
#define SD_LISTEN_FDS_START 3
/* systemd logging defines for stderr parsing by Finit */
#define SD_EMERG "<0>" /* system is unusable */
#define SD_ALERT "<1>" /* action must be taken immediately */
#define SD_CRIT "<2>" /* critical conditions */
#define SD_ERR "<3>" /* error conditions */
#define SD_WARNING "<4>" /* warning conditions */
#define SD_NOTICE "<5>" /* normal but significant condition */
#define SD_INFO "<6>" /* informational */
#define SD_DEBUG "<7>" /* debug-level messages */
/* Basic notification function */
int sd_notify(int unset_environment, const char *state);
/* Printf-style notification functions */
int sd_notifyf(int unset_environment, const char *format, ...);
/* PID-specific notification functions */
int sd_pid_notify(pid_t pid, int unset_environment, const char *state);
int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...);
/* Socket activation stubs */
int sd_listen_fds(int unset_environment);
/* Watchdog stub */
int sd_watchdog_enabled(int unset_environment, uint64_t *usec);
/* System detection */
int sd_booted(void);
#endif /* SD_DAEMON_H_ */
+1
View File
@@ -9,6 +9,7 @@ endif
sbin_PROGRAMS = finit initctl sbin_PROGRAMS = finit initctl
pkglibexec_PROGRAMS = getty logit runparts tmpfiles pkglibexec_PROGRAMS = getty logit runparts tmpfiles
if SULOGIN if SULOGIN
pkglibexec_PROGRAMS += sulogin pkglibexec_PROGRAMS += sulogin
endif endif
+7 -15
View File
@@ -349,6 +349,12 @@ static void api_cb(uev_t *w, void *arg, int events)
goto error; goto error;
} }
/*
* TODO: refactor to use accept4() and a new uev handler for the
* client socket instead of risking blocking PID 1 if the client
* is misbehaving.
*/
// sd = accept4(w->fd, NULL, NULL, SOCK_NONBLOCK);
sd = accept(w->fd, NULL, NULL); sd = accept(w->fd, NULL, NULL);
if (sd < 0) { if (sd < 0) {
err(1, "Failed serving API request"); err(1, "Failed serving API request");
@@ -565,20 +571,6 @@ static void api_cb(uev_t *w, void *arg, int events)
result = do_signal(rq.data, sizeof(rq.data), rq.runlevel); result = do_signal(rq.data, sizeof(rq.data), rq.runlevel);
break; break;
case INIT_CMD_NOTIFY_SOCKET:
svc = svc_find_by_pid(rq.runlevel);
if (!svc) {
errx(1, "Unknown PID, cannot register notify socket");
result = 1;
break;
}
if (uev_io_init(ctx, &svc->notify_watcher, service_notify_cb, svc, sd, UEV_READ)) {
err(1, "Failed initializing %s readiness notifier", svc_ident(svc, NULL, 0));
break;
}
return; /* Don't close sd, used for notify */
default: default:
dbg("Unsupported cmd: %d", rq.cmd); dbg("Unsupported cmd: %d", rq.cmd);
break; break;
@@ -613,7 +605,7 @@ int api_init(uev_ctx_t *ctx)
int sd; int sd;
dbg("Setting up external API socket ..."); dbg("Setting up external API socket ...");
sd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0); sd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (-1 == sd) { if (-1 == sd) {
err(1, "Failed starting external API socket"); err(1, "Failed starting external API socket");
return 1; return 1;
-10
View File
@@ -97,16 +97,6 @@ int client_request(struct init_request *rq, ssize_t len)
return -1; return -1;
} }
/*
* Exception, only command that don't need to wait for a
* response from Finit. Registers the client socket as a
* readiness notification socket with Finit
*/
if (rq->cmd == INIT_CMD_NOTIFY_SOCKET) {
rq->cmd = INIT_CMD_ACK;
return 0;
}
pfd.fd = sd; pfd.fd = sd;
pfd.events = POLLIN | POLLERR | POLLHUP; pfd.events = POLLIN | POLLERR | POLLHUP;
if ((rc = poll(&pfd, 1, REQUEST_TIMEOUT)) <= 0) { if ((rc = poll(&pfd, 1, REQUEST_TIMEOUT)) <= 0) {
-1
View File
@@ -99,7 +99,6 @@
#define INIT_CMD_SVC_FIND 131 #define INIT_CMD_SVC_FIND 131
#define INIT_CMD_SVC_FIND_BYC 132 #define INIT_CMD_SVC_FIND_BYC 132
#define INIT_CMD_SIGNAL 133 #define INIT_CMD_SIGNAL 133
#define INIT_CMD_NOTIFY_SOCKET 200 /* For readiness notification socket */
#define INIT_CMD_NACK 254 #define INIT_CMD_NACK 254
#define INIT_CMD_ACK 255 #define INIT_CMD_ACK 255
+66 -13
View File
@@ -36,6 +36,9 @@
# include <lite/lite.h> # include <lite/lite.h>
#endif #endif
#define LOG_MAX (200 * 1024)
#define LOG_NUM 5
static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION; static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION;
extern int logrotate(char *file, int num, off_t sz); extern int logrotate(char *file, int num, off_t sz);
@@ -87,23 +90,61 @@ reopen:
return fclose(fp); return fclose(fp);
} }
static int logit(int level, char *buf, size_t len) /*
* Parse possible systemd style log <level> prefix from message
* For details, see libsystemd/sd-daemon.h
*
* Returns the level, and updates buf to point past the prefix
*/
static int parse_level(char **buf, int default_level)
{ {
if (buf[0]) { char *msg = *buf;
syslog(level, "%s", buf);
return 0; if (msg[0] == '<' && msg[2] == '>' && msg[1] >= '0' && msg[1] <= '7') {
int level;
switch (msg[1]) {
case '0': level = LOG_EMERG; break;
case '1': level = LOG_ALERT; break;
case '2': level = LOG_CRIT; break;
case '3': level = LOG_ERR; break;
case '4': level = LOG_WARNING; break;
case '5': level = LOG_NOTICE; break;
case '6': level = LOG_INFO; break;
case '7': level = LOG_DEBUG; break;
default: level = default_level; break;
}
*buf = msg + 3;
return (default_level & ~LOG_PRIMASK) | level;
} }
return default_level;
}
static int do_log(int level, char *msg)
{
syslog(parse_level(&msg, level), "%s", msg);
return 0;
}
static int logit(int level, char *buf, size_t len)
{
if (buf[0])
return do_log(level, buf);
while ((fgets(buf, len, stdin))) while ((fgets(buf, len, stdin)))
syslog(level, "%s", buf); do_log(level, buf);
return 0; return 0;
} }
static int parse_prio(char *arg, int *f, int *l) static int parse_prio(char *arg, int *fac, int *lvl)
{ {
char *duparg = strdup(arg); char *duparg = strdup(arg);
char *ptr, *prio; const char *prio;
char *ptr;
int found;
if (!duparg) if (!duparg)
prio = arg; prio = arg;
@@ -114,23 +155,34 @@ static int parse_prio(char *arg, int *f, int *l)
if (ptr) { if (ptr) {
*ptr++ = 0; *ptr++ = 0;
found = -1;
for (int i = 0; facilitynames[i].c_name; i++) { for (int i = 0; facilitynames[i].c_name; i++) {
if (!strcmp(facilitynames[i].c_name, prio)) { if (!strcmp(facilitynames[i].c_name, prio)) {
*f = facilitynames[i].c_val; found = facilitynames[i].c_val;
break; break;
} }
} }
if (found == -1)
return 1;
*fac = found;
prio = ptr; prio = ptr;
} }
found = -1;
for (int i = 0; prioritynames[i].c_name; i++) { for (int i = 0; prioritynames[i].c_name; i++) {
if (!strcmp(prioritynames[i].c_name, prio)) { if (!strcmp(prioritynames[i].c_name, prio)) {
*l = prioritynames[i].c_val; found = prioritynames[i].c_val;
break; break;
} }
} }
if (found == -1)
return 1;
*lvl = found;
if (duparg != arg) if (duparg != arg)
free(duparg); free(duparg);
@@ -161,13 +213,14 @@ static int usage(int code)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int c, rc, num = 5; char *ident = NULL, *logfile = NULL;
int log_opts = LOG_NOWAIT;
int facility = LOG_USER; int facility = LOG_USER;
int level = LOG_INFO; int level = LOG_INFO;
int log_opts = LOG_NOWAIT; off_t size = LOG_MAX;
off_t size = 200 * 1024;
char *ident = NULL, *logfile = NULL;
char buf[512] = ""; char buf[512] = "";
int num = LOG_NUM;
int c, rc;
while ((c = getopt(argc, argv, "f:hn:p:r:st:v")) != EOF) { while ((c = getopt(argc, argv, "f:hn:p:r:st:v")) != EOF) {
switch (c) { switch (c) {
-1
View File
@@ -46,7 +46,6 @@ int api_exit (void);
void conf_flush_events(void); void conf_flush_events(void);
void service_monitor (pid_t lost, int status); void service_monitor (pid_t lost, int status);
void service_notify_cb(uev_t *w, void *arg, int events);
const char *plugin_hook_str (hook_point_t no); const char *plugin_hook_str (hook_point_t no);
int plugin_exists (hook_point_t no); int plugin_exists (hook_point_t no);
+120 -32
View File
@@ -30,6 +30,7 @@
#include <sys/reboot.h> #include <sys/reboot.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/un.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <net/if.h> #include <net/if.h>
#ifdef _LIBITE_LITE #ifdef _LIBITE_LITE
@@ -56,6 +57,9 @@
#include "utmp-api.h" #include "utmp-api.h"
#include "schedule.h" #include "schedule.h"
#define NOTIFY_PATH "@run/finit/notify/%d"
/* /*
* run tasks block other tasks/services from starting, we track the * run tasks block other tasks/services from starting, we track the
* current run task here. For service_step() and service_start(). * current run task here. For service_step() and service_start().
@@ -68,6 +72,8 @@ static struct wq work = {
int service_interval = SERVICE_INTERVAL_DEFAULT; int service_interval = SERVICE_INTERVAL_DEFAULT;
static void svc_set_state(svc_t *svc, svc_state_t new_state); static void svc_set_state(svc_t *svc, svc_state_t new_state);
static void service_notify_cb(uev_t *w, void *arg, int events);
/** /**
* service_timeout_cb - libuev callback wrapper for service timeouts * service_timeout_cb - libuev callback wrapper for service timeouts
@@ -208,13 +214,16 @@ static int stdin_redirect(void)
{ {
int fd; int fd;
fd = open("/dev/null", O_RDONLY | O_APPEND); fd = open("/dev/null", O_RDONLY);
if (-1 != fd) { if (fd == -1) {
dup2(fd, STDIN_FILENO); warn("Failed opening /dev/null for stdin redirect");
return close(fd); return -1;
} }
return -1; dup2(fd, STDIN_FILENO);
close(fd);
return 0;
} }
/* /*
@@ -261,12 +270,10 @@ static void fallback_logger(char *ident, char *prio)
static int lredirect(svc_t *svc) static int lredirect(svc_t *svc)
{ {
static int have_sysklogd = -1; static int have_sysklogd = -1;
pid_t pid, svc_pid; pid_t svc_pid = getpid();
pid_t pid;
int fd; int fd;
svc_pid = getpid();
dbg("%s pid: %d", svc_ident(svc, NULL, 0), svc_pid);
/* /*
* Open PTY to connect to logger. A pty isn't buffered * Open PTY to connect to logger. A pty isn't buffered
* like a pipe, and it eats newlines so they aren't logged * like a pipe, and it eats newlines so they aren't logged
@@ -335,6 +342,7 @@ static int lredirect(svc_t *svc)
if (svc->log.prio[0]) if (svc->log.prio[0])
prio = svc->log.prio; prio = svc->log.prio;
/* Neither sysklogd logger or native logit tool available */
if (!have_sysklogd && !whichp(_PATH_LOGIT)) { if (!have_sysklogd && !whichp(_PATH_LOGIT)) {
logit(LOG_INFO, _PATH_LOGIT " missing, using syslog for %s instead", svc->name); logit(LOG_INFO, _PATH_LOGIT " missing, using syslog for %s instead", svc->name);
fallback_logger(tag, prio); fallback_logger(tag, prio);
@@ -360,7 +368,13 @@ static int lredirect(svc_t *svc)
_exit(1); _exit(1);
} }
if (have_sysklogd) { /*
* For now, let systemd programs go via our native logit
* tool. It supports systemd logging defines for stderr
* parsing. The only real downside is that it cannot do
* PID faking, like sysklogd's logger tool.
*/
if (have_sysklogd && svc->notify != SVC_NOTIFY_SYSTEMD) {
char pid[16]; char pid[16];
snprintf(pid, sizeof(pid), "%d", svc_pid); snprintf(pid, sizeof(pid), "%d", svc_pid);
@@ -581,6 +595,9 @@ static int service_start(svc_t *svc)
int result = 0, do_progress = 1; int result = 0, do_progress = 1;
char cmdline[CMD_SIZE] = ""; char cmdline[CMD_SIZE] = "";
sigset_t nmask, omask; sigset_t nmask, omask;
int pipefd[2];
int fd = -1;
int sd = -1;
pid_t pid; pid_t pid;
size_t i; size_t i;
@@ -626,6 +643,29 @@ static int service_start(svc_t *svc)
if (svc_is_sysv(svc)) if (svc_is_sysv(svc))
logit(LOG_CONSOLE | LOG_NOTICE, "Calling '%s start' ...", cmdline); logit(LOG_CONSOLE | LOG_NOTICE, "Calling '%s start' ...", cmdline);
switch (svc->notify) {
case SVC_NOTIFY_S6:
if (pipe(pipefd) == -1) {
err(1, "%s: failed opening pipe for s6 notify", svc_ident(svc, NULL, 0));
svc_missing(svc);
return 1;
}
fd = pipefd[1];
sd = pipefd[0];
break;
case SVC_NOTIFY_SYSTEMD:
sd = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
if (sd == -1) {
err(1, "%s: failed opening notify socket", svc_ident(svc, NULL, 0));
svc_missing(svc);
return 1;
}
break;
default:
break;
}
if (!svc->desc[0]) if (!svc->desc[0])
do_progress = 0; do_progress = 0;
@@ -652,38 +692,72 @@ static int service_start(svc_t *svc)
pid = service_fork(svc); pid = service_fork(svc);
if (pid < 0) { if (pid < 0) {
if (sd != -1)
close(sd);
if (fd != -1)
close(fd);
result = -1; result = -1;
goto fail; goto fail;
} }
if (pid > 1) { if (pid > 1) {
struct sockaddr_un sun;
size_t len;
dbg("Starting %s as PID %d", svc_ident(svc, NULL, 0), pid); dbg("Starting %s as PID %d", svc_ident(svc, NULL, 0), pid);
svc->pid = pid; svc->pid = pid;
svc->start_time = jiffies(); svc->start_time = jiffies();
switch (svc->notify) {
case SVC_NOTIFY_SYSTEMD:
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
snprintf(sun.sun_path, sizeof(sun.sun_path), NOTIFY_PATH, pid);
len = strlen(sun.sun_path);
sun.sun_path[0] = 0;
result = bind(sd, (struct sockaddr *)&sun, offsetof(struct sockaddr_un, sun_path) + len);
if (result == -1) {
err(1, "%s: failed binding to notify socket", svc_ident(svc, NULL, 0));
close(sd);
break;
}
/* fallthrough */
case SVC_NOTIFY_S6:
close(fd); /* client-end of pipefd for s6 notify */
result = uev_io_init(ctx, &svc->notify_watcher, service_notify_cb, svc, sd, UEV_READ);
if (result < 0) {
err(1, "%s: failed setting up notify callback", svc_ident(svc, NULL, 0));
if (svc->notify == SVC_NOTIFY_S6)
close(fd);
close(sd);
break;
}
default:
break;
}
} else if (pid == 0) { } else if (pid == 0) {
char str[strlen(NOTIFY_PATH) + 32];
char *args[MAX_NUM_SVC_ARGS + 1]; char *args[MAX_NUM_SVC_ARGS + 1];
int status; int status;
if (!svc_is_tty(svc)) if (!svc_is_tty(svc))
redirect(svc); redirect(svc);
switch (svc->notify) {
case SVC_NOTIFY_SYSTEMD:
snprintf(str, sizeof(str), NOTIFY_PATH, getpid());
setenv("NOTIFY_SOCKET", str, 1);
/* fallthrough */
case SVC_NOTIFY_S6:
close(sd);
break;
default:
break;
}
if (!svc_is_sysv(svc)) { if (!svc_is_sysv(svc)) {
wordexp_t we = { 0 }; wordexp_t we = { 0 };
int rc; int rc;
if (svc->notify == SVC_NOTIFY_SYSTEMD || svc->notify == SVC_NOTIFY_S6) {
if (client_command(INIT_CMD_NOTIFY_SOCKET)) {
err(1, "%s: failed creating notify socket", svc_ident(svc, NULL, 0));
client_disconnect();
/* does not change parent, restarting may work. */
svc->notify = SVC_NOTIFY_NONE;
} else {
char val[20];
snprintf(val, sizeof(val), "%d", client_socket());
setenv("NOTIFY_SOCKET", val, 1);
}
}
if ((rc = wordexp(svc->cmd, &we, 0))) { if ((rc = wordexp(svc->cmd, &we, 0))) {
errx(1, "%s: failed wordexp(%s): %d", svc_ident(svc, NULL, 0), svc->cmd, rc); errx(1, "%s: failed wordexp(%s): %d", svc_ident(svc, NULL, 0), svc->cmd, rc);
nomem: nomem:
@@ -700,11 +774,11 @@ static int service_start(svc_t *svc)
if (len == 0) if (len == 0)
break; break;
if (svc->notify == SVC_NOTIFY_SYSTEMD || svc->notify == SVC_NOTIFY_S6) { if (svc->notify == SVC_NOTIFY_S6) {
char *ptr = strstr(arg, "%n"); char *ptr = strstr(arg, "%n");
if (ptr) { if (ptr) {
len = snprintf(str, sizeof(str), "%d", client_socket()); len = snprintf(str, sizeof(str), "%d", fd);
if (len > 0 && len <= 2) { if (len > 0 && len <= 2) {
ptr[0] = ' '; ptr[0] = ' ';
ptr[1] = ' '; ptr[1] = ' ';
@@ -2389,7 +2463,7 @@ static void service_retry(svc_t *svc)
timeout = ((*restart_cnt) <= (svc->restart_max / 2)) ? 2000 : 5000; timeout = ((*restart_cnt) <= (svc->restart_max / 2)) ? 2000 : 5000;
/* If a longer timeout was specified in the conf, use that instead. */ /* If a longer timeout was specified in the conf, use that instead. */
svc->restart_tmo = max(svc->restart_tmo, timeout); svc->restart_tmo = max(svc->restart_tmo, timeout);
logit(LOG_CONSOLE|LOG_WARNING, "Service %s[%d] died, restarting in %d msec (%d/%d)", logit(LOG_CONSOLE|LOG_WARNING, "Service %s[%d] died, restarting (retry in %d msec) (attempt: %d/%d)",
svc_ident(svc, NULL, 0), svc->oldpid, svc->restart_tmo, *restart_cnt, svc->restart_max); svc_ident(svc, NULL, 0), svc->oldpid, svc->restart_tmo, *restart_cnt, svc->restart_max);
svc_unblock(svc); svc_unblock(svc);
@@ -2928,14 +3002,15 @@ void service_notify_reconf(void)
* Called when a service sends readiness notification, or when * Called when a service sends readiness notification, or when
* the service closes its end of the IPC connection. * the service closes its end of the IPC connection.
*/ */
void service_notify_cb(uev_t *w, void *arg, int events) static void service_notify_cb(uev_t *w, void *arg, int events)
{ {
svc_t *svc = (svc_t *)arg; svc_t *svc = (svc_t *)arg;
int ready = 0;
char buf[32]; char buf[32];
ssize_t len; ssize_t len;
if (UEV_ERROR == events) { if (UEV_ERROR == events) {
dbg("Spurious problem with %s notify callback, restarting.", svc_ident(svc, NULL, 0)); dbg("%s: spurious problem with notify callback, restarting.", svc_ident(svc, NULL, 0));
uev_io_start(w); uev_io_start(w);
return; return;
} }
@@ -2945,11 +3020,24 @@ void service_notify_cb(uev_t *w, void *arg, int events)
warn("Failed reading notification from %s", svc_ident(svc, NULL, 0)); warn("Failed reading notification from %s", svc_ident(svc, NULL, 0));
return; return;
} }
buf[len] = 0; buf[len] = 0;
/* systemd and s6, respectively. The latter then closes the socket */ /* Check for systemd READY=1 or s6 newline termination */
if (!strcmp(buf, "READY=1\n") || buf[len - 1] == '\n') { if (svc->notify == SVC_NOTIFY_SYSTEMD) {
char *token = strtok(buf, "\n");
while (token) {
if (!strcmp(token, "READY=1")) {
ready = 1;
break;
}
token = strtok(NULL, "\n");
}
} else if (svc->notify == SVC_NOTIFY_S6 && buf[len - 1] == '\n') {
ready = 1;
}
if (ready) {
/* /*
* native (pidfile) services are marked as started by * native (pidfile) services are marked as started by
* the pidfile plugin. * the pidfile plugin.
+1 -1
View File
@@ -85,7 +85,7 @@ typedef enum {
SVC_ONCRASH_SCRIPT, SVC_ONCRASH_SCRIPT,
} svc_oncrash_action_t; } svc_oncrash_action_t;
/* 0: none, 1: systemd, 2: s6 */ /* 0: none, 1: finit (native), 2: systemd, 3: s6 */
typedef enum { typedef enum {
SVC_NOTIFY_NONE = 0, SVC_NOTIFY_NONE = 0,
SVC_NOTIFY_PID, SVC_NOTIFY_PID,
+1 -1
View File
@@ -41,7 +41,7 @@ export PATH
# Not supported by Busybox unshare: # Not supported by Busybox unshare:
# --cgroup --time # --cgroup --time
exec "$unshare" \ exec "$unshare" \
--user --map-root-user \ --user --map-root-user --map-auto \
--fork --pid --mount-proc \ --fork --pid --mount-proc \
--mount \ --mount \
--mount-proc \ --mount-proc \
+9
View File
@@ -25,6 +25,15 @@ test_one()
type=$1 type=$1
service=$2 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 sep
# num=$(run "find /proc/1/fd |wc -l") # num=$(run "find /proc/1/fd |wc -l")
+1 -1
View File
@@ -1,5 +1,5 @@
/Makefile /Makefile
/Makefile.in /Makefile.in
/.libs/
/.deps/ /.deps/
/serv /serv
+5 -1
View File
@@ -1,4 +1,8 @@
noinst_PROGRAMS = serv noinst_PROGRAMS = serv
serv_SOURCES = serv.c serv_SOURCES = serv.c
serv_CPPFLAGS = -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE -D_DEFAULT_SOURCE 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
+47 -12
View File
@@ -5,6 +5,10 @@
* in foregrund it does not create a PID file by default. * in foregrund it does not create a PID file by default.
*/ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <paths.h> #include <paths.h>
@@ -16,6 +20,9 @@
#include <sysexits.h> #include <sysexits.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef HAVE_LIBSYSTEMD
#include "sd-daemon.h"
#endif
#define PROGNM "serv" #define PROGNM "serv"
@@ -145,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) static int usage(int rc)
{ {
FILE *fp = rc ? stderr : stdout; FILE *fp = rc ? stderr : stdout;
@@ -152,6 +169,7 @@ static int usage(int rc)
fprintf(fp, fprintf(fp,
"%s [-nhp] [-P FILE]\n" "%s [-nhp] [-P FILE]\n"
"\n" "\n"
" -C Show capabilities and exit\n"
" -c Crash (exit) immediately\n" " -c Crash (exit) immediately\n"
" -e K:V Verify K environment variable is V value\n" " -e K:V Verify K environment variable is V value\n"
" -E K Verify K is not set in the environment\n" " -E K Verify K is not set in the environment\n"
@@ -160,7 +178,7 @@ static int usage(int rc)
" -h Show help text (this)\n" " -h Show help text (this)\n"
" -i IDENT Change process identity, incl. logs, pidfile, etc.\n" " -i IDENT Change process identity, incl. logs, pidfile, etc.\n"
" -n Run in foreground\n" " -n Run in foreground\n"
" -N SOCK Send 'READY=1\\n' on $NOTIFY_SOCKET or SOCK\n" " -N SOCK Send '\\n' on SOCK (integer), for s6 readiness\n"
" -p Create PID file despite running in foreground\n" " -p Create PID file despite running in foreground\n"
" -P FILE Create PID file using FILE\n" " -P FILE Create PID file using FILE\n"
" -r SVC Call initctl to restart service SVC (self)\n" " -r SVC Call initctl to restart service SVC (self)\n"
@@ -170,6 +188,8 @@ static int usage(int rc)
"to let the rest of the system know it's done. When the program runs\n" "to let the rest of the system know it's done. When the program runs\n"
"in the foreground it does not create a PID file by default.\n" "in the foreground it does not create a PID file by default.\n"
"\n" "\n"
"When acting as a systemd daemon, this program expects NOTIFY_SOCKET\n"
"\n"
"Regardless of how This daemon is started it provides a single service\n" "Regardless of how This daemon is started it provides a single service\n"
"to others, spice ... guarded by sandworms.\n", "to others, spice ... guarded by sandworms.\n",
ident); ident);
@@ -192,11 +212,13 @@ int main(int argc, char *argv[])
char cmd[80]; char cmd[80];
int c; 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) { switch (c) {
case 'c': case 'c':
do_crash = 1; do_crash = 1;
break; break;
case 'C':
return capabilities();
case 'e': case 'e':
verify_env(optarg); verify_env(optarg);
break; break;
@@ -256,13 +278,13 @@ int main(int argc, char *argv[])
if (do_pidfile > 0) if (do_pidfile > 0)
pidfile(pidfn); pidfile(pidfn);
if (!do_notify) { if (!do_notify && getenv("NOTIFY_SOCKET"))
const char *sock = getenv("NOTIFY_SOCKET"); do_notify = 1;
/* systemd style */ if (do_notify)
if (sock) inf("Will notify %s ...", notify_s6 ? "s6" : "systemd");
do_notify = atoi(sock); else
} inf("No notify socket ...");
if (do_crash) { if (do_crash) {
inf("Simulating crash, exiting with code %d", EX_SOFTWARE); inf("Simulating crash, exiting with code %d", EX_SOFTWARE);
@@ -279,15 +301,28 @@ int main(int argc, char *argv[])
if (reloading) { if (reloading) {
if (do_notify > 0) { if (do_notify > 0) {
/* Issue #343: see notify.sh test for more details */
inf("Delaying notify by 3 seconds for notify.sh ..."); inf("Delaying notify by 3 seconds for notify.sh ...");
sleep(3); sleep(3);
inf("Notifying Finit on socket %d, READY=1", do_notify);
if (write(do_notify, "READY=1\n", 8) == -1)
err(1, "Failed sending ready notification to Finit");
if (notify_s6) { if (notify_s6) {
inf("Notifying Finit on socket %d", do_notify);
if (write(do_notify, "\n", 1) < 1)
err(1, "Failed sending ready notification to Finit");
inf("s6 notify, closing socket %d ...", do_notify); inf("s6 notify, closing socket %d ...", do_notify);
close(do_notify); if (close(do_notify))
err(1, "Failed closing notify socket");
do_notify = 0; 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) if (do_pidfile > 0)
+2 -2
View File
@@ -15,8 +15,8 @@ test_setup()
{ {
run "cat >> $FINIT_CONF" <<EOF run "cat >> $FINIT_CONF" <<EOF
service log:stdout notify:pid name:A serv -np -i A -- A (pid) service log:stdout notify:pid name:A serv -np -i A -- A (pid)
service log:stdout notify:systemd <pid/A> name:B serv -np -i B -N 0 -- B needs A (systemd) service log:stdout notify:systemd <pid/A> name:B serv -np -i B -- B needs A (systemd)
service log:stdout notify:s6 <service/B/ready> name:C serv -np -i C -N 0 -- C needs B (s6) service log:stdout notify:s6 <service/B/ready> name:C serv -np -i C -N %n -- C needs B (s6)
service log:stdout notify:none <pid/A> name:D type:forking serv -i D -- D needs A (forking) service log:stdout notify:none <pid/A> name:D type:forking serv -i D -- D needs A (forking)
task <service/C/ready,service/D/ready> name:allup initctl cond set allup -- Everything is up task <service/C/ready,service/D/ready> name:allup initctl cond set allup -- Everything is up
EOF EOF