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
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)
+29 -4
View File
@@ -1,8 +1,33 @@
#!/bin/sh
# Run test suite with the correct configure params
run_make=1
jobs=17
make distclean
./configure --prefix=/usr --exec-prefix= --sysconfdir=/etc --localstatedir=/var \
--enable-x11-common-plugin --with-watchdog --with-keventd \
while [ $# -gt 0 ]; do
case "$1" in
-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'
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/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}
+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
pkglibexec_PROGRAMS = getty logit runparts tmpfiles
if SULOGIN
pkglibexec_PROGRAMS += sulogin
endif
+7 -15
View File
@@ -349,6 +349,12 @@ static void api_cb(uev_t *w, void *arg, int events)
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);
if (sd < 0) {
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);
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:
dbg("Unsupported cmd: %d", rq.cmd);
break;
@@ -613,7 +605,7 @@ int api_init(uev_ctx_t *ctx)
int sd;
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) {
err(1, "Failed starting external API socket");
return 1;
-10
View File
@@ -97,16 +97,6 @@ int client_request(struct init_request *rq, ssize_t len)
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.events = POLLIN | POLLERR | POLLHUP;
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_BYC 132
#define INIT_CMD_SIGNAL 133
#define INIT_CMD_NOTIFY_SOCKET 200 /* For readiness notification socket */
#define INIT_CMD_NACK 254
#define INIT_CMD_ACK 255
+66 -13
View File
@@ -36,6 +36,9 @@
# include <lite/lite.h>
#endif
#define LOG_MAX (200 * 1024)
#define LOG_NUM 5
static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION;
extern int logrotate(char *file, int num, off_t sz);
@@ -87,23 +90,61 @@ reopen:
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]) {
syslog(level, "%s", buf);
return 0;
char *msg = *buf;
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)))
syslog(level, "%s", buf);
do_log(level, buf);
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 *ptr, *prio;
const char *prio;
char *ptr;
int found;
if (!duparg)
prio = arg;
@@ -114,23 +155,34 @@ static int parse_prio(char *arg, int *f, int *l)
if (ptr) {
*ptr++ = 0;
found = -1;
for (int i = 0; facilitynames[i].c_name; i++) {
if (!strcmp(facilitynames[i].c_name, prio)) {
*f = facilitynames[i].c_val;
found = facilitynames[i].c_val;
break;
}
}
if (found == -1)
return 1;
*fac = found;
prio = ptr;
}
found = -1;
for (int i = 0; prioritynames[i].c_name; i++) {
if (!strcmp(prioritynames[i].c_name, prio)) {
*l = prioritynames[i].c_val;
found = prioritynames[i].c_val;
break;
}
}
if (found == -1)
return 1;
*lvl = found;
if (duparg != arg)
free(duparg);
@@ -161,13 +213,14 @@ static int usage(int code)
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 level = LOG_INFO;
int log_opts = LOG_NOWAIT;
off_t size = 200 * 1024;
char *ident = NULL, *logfile = NULL;
off_t size = LOG_MAX;
char buf[512] = "";
int num = LOG_NUM;
int c, rc;
while ((c = getopt(argc, argv, "f:hn:p:r:st:v")) != EOF) {
switch (c) {
-1
View File
@@ -46,7 +46,6 @@ int api_exit (void);
void conf_flush_events(void);
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);
int plugin_exists (hook_point_t no);
+120 -32
View File
@@ -30,6 +30,7 @@
#include <sys/reboot.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <net/if.h>
#ifdef _LIBITE_LITE
@@ -56,6 +57,9 @@
#include "utmp-api.h"
#include "schedule.h"
#define NOTIFY_PATH "@run/finit/notify/%d"
/*
* run tasks block other tasks/services from starting, we track the
* current run task here. For service_step() and service_start().
@@ -68,6 +72,8 @@ static struct wq work = {
int service_interval = SERVICE_INTERVAL_DEFAULT;
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
@@ -208,13 +214,16 @@ static int stdin_redirect(void)
{
int fd;
fd = open("/dev/null", O_RDONLY | O_APPEND);
if (-1 != fd) {
dup2(fd, STDIN_FILENO);
return close(fd);
fd = open("/dev/null", O_RDONLY);
if (fd == -1) {
warn("Failed opening /dev/null for stdin redirect");
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 have_sysklogd = -1;
pid_t pid, svc_pid;
pid_t svc_pid = getpid();
pid_t pid;
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
* 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])
prio = svc->log.prio;
/* Neither sysklogd logger or native logit tool available */
if (!have_sysklogd && !whichp(_PATH_LOGIT)) {
logit(LOG_INFO, _PATH_LOGIT " missing, using syslog for %s instead", svc->name);
fallback_logger(tag, prio);
@@ -360,7 +368,13 @@ static int lredirect(svc_t *svc)
_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];
snprintf(pid, sizeof(pid), "%d", svc_pid);
@@ -581,6 +595,9 @@ static int service_start(svc_t *svc)
int result = 0, do_progress = 1;
char cmdline[CMD_SIZE] = "";
sigset_t nmask, omask;
int pipefd[2];
int fd = -1;
int sd = -1;
pid_t pid;
size_t i;
@@ -626,6 +643,29 @@ static int service_start(svc_t *svc)
if (svc_is_sysv(svc))
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])
do_progress = 0;
@@ -652,38 +692,72 @@ static int service_start(svc_t *svc)
pid = service_fork(svc);
if (pid < 0) {
if (sd != -1)
close(sd);
if (fd != -1)
close(fd);
result = -1;
goto fail;
}
if (pid > 1) {
struct sockaddr_un sun;
size_t len;
dbg("Starting %s as PID %d", svc_ident(svc, NULL, 0), pid);
svc->pid = pid;
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) {
char str[strlen(NOTIFY_PATH) + 32];
char *args[MAX_NUM_SVC_ARGS + 1];
int status;
if (!svc_is_tty(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)) {
wordexp_t we = { 0 };
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))) {
errx(1, "%s: failed wordexp(%s): %d", svc_ident(svc, NULL, 0), svc->cmd, rc);
nomem:
@@ -700,11 +774,11 @@ static int service_start(svc_t *svc)
if (len == 0)
break;
if (svc->notify == SVC_NOTIFY_SYSTEMD || svc->notify == SVC_NOTIFY_S6) {
if (svc->notify == SVC_NOTIFY_S6) {
char *ptr = strstr(arg, "%n");
if (ptr) {
len = snprintf(str, sizeof(str), "%d", client_socket());
len = snprintf(str, sizeof(str), "%d", fd);
if (len > 0 && len <= 2) {
ptr[0] = ' ';
ptr[1] = ' ';
@@ -2389,7 +2463,7 @@ static void service_retry(svc_t *svc)
timeout = ((*restart_cnt) <= (svc->restart_max / 2)) ? 2000 : 5000;
/* If a longer timeout was specified in the conf, use that instead. */
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_unblock(svc);
@@ -2928,14 +3002,15 @@ void service_notify_reconf(void)
* Called when a service sends readiness notification, or when
* 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;
int ready = 0;
char buf[32];
ssize_t len;
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);
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));
return;
}
buf[len] = 0;
/* systemd and s6, respectively. The latter then closes the socket */
if (!strcmp(buf, "READY=1\n") || buf[len - 1] == '\n') {
/* Check for systemd READY=1 or s6 newline termination */
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
* the pidfile plugin.
+1 -1
View File
@@ -85,7 +85,7 @@ typedef enum {
SVC_ONCRASH_SCRIPT,
} svc_oncrash_action_t;
/* 0: none, 1: systemd, 2: s6 */
/* 0: none, 1: finit (native), 2: systemd, 3: s6 */
typedef enum {
SVC_NOTIFY_NONE = 0,
SVC_NOTIFY_PID,
+1 -1
View File
@@ -41,7 +41,7 @@ export PATH
# Not supported by Busybox unshare:
# --cgroup --time
exec "$unshare" \
--user --map-root-user \
--user --map-root-user --map-auto \
--fork --pid --mount-proc \
--mount \
--mount-proc \
+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
+5 -1
View File
@@ -1,4 +1,8 @@
noinst_PROGRAMS = serv
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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
@@ -16,6 +20,9 @@
#include <sysexits.h>
#include <sys/socket.h>
#include <sys/stat.h>
#ifdef HAVE_LIBSYSTEMD
#include "sd-daemon.h"
#endif
#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)
{
FILE *fp = rc ? stderr : stdout;
@@ -152,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"
@@ -160,7 +178,7 @@ static int usage(int rc)
" -h Show help text (this)\n"
" -i IDENT Change process identity, incl. logs, pidfile, etc.\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 FILE Create PID file using FILE\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"
"in the foreground it does not create a PID file by default.\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"
"to others, spice ... guarded by sandworms.\n",
ident);
@@ -192,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;
@@ -256,13 +278,13 @@ int main(int argc, char *argv[])
if (do_pidfile > 0)
pidfile(pidfn);
if (!do_notify) {
const char *sock = getenv("NOTIFY_SOCKET");
if (!do_notify && getenv("NOTIFY_SOCKET"))
do_notify = 1;
/* systemd style */
if (sock)
do_notify = atoi(sock);
}
if (do_notify)
inf("Will notify %s ...", notify_s6 ? "s6" : "systemd");
else
inf("No notify socket ...");
if (do_crash) {
inf("Simulating crash, exiting with code %d", EX_SOFTWARE);
@@ -279,15 +301,28 @@ int main(int argc, char *argv[])
if (reloading) {
if (do_notify > 0) {
/* Issue #343: see notify.sh test for more details */
inf("Delaying notify by 3 seconds for notify.sh ...");
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) {
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);
close(do_notify);
if (close(do_notify))
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)
+2 -2
View File
@@ -15,8 +15,8 @@ test_setup()
{
run "cat >> $FINIT_CONF" <<EOF
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:s6 <service/B/ready> name:C serv -np -i C -N 0 -- C needs B (s6)
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 %n -- C needs B (s6)
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
EOF