diff --git a/Makefile b/Makefile index e0b1749..8a5f915 100644 --- a/Makefile +++ b/Makefile @@ -48,6 +48,26 @@ URUNIT_SRC := ${SOURCE_DIR}/urunit.c ${SOURCE_DIR}/main.c ${SOURCE_DIR}/mode.c \ # Header files URUNIT_HDR := $(wildcard ${SOURCE_DIR}/*.h) +#? EMBED a static program (for the target architecture) to build into urunit. +# The early init starts it before the root is mounted, from memory, so the +# initrd stays one file. See "Embedded program" in README.md. +EMBED ?= +#? EMBED_NAME the embedded program's argv[0] (default: embedded) +EMBED_NAME ?= embedded +#? EMBED_WHEN a path that must exist for the embedded program to start (default: always) +EMBED_WHEN ?= +#? EMBED_READY a flag that takes a readiness fd, e.g. --ready-fd (default: no wait) +EMBED_READY ?= +ifneq ($(EMBED),) + EMBED_BLOB := $(BUILD_DIR)/embed_blob.o + URUNIT_OBJ := $(EMBED_BLOB) + CFLAGS += -DURUNIT_EMBED_NAME='"$(EMBED_NAME)"' \ + -DURUNIT_EMBED_WHEN='"$(EMBED_WHEN)"' \ + -DURUNIT_EMBED_READY='"$(EMBED_READY)"' +else + URUNIT_OBJ := +endif + # Unit tests: every source except urunit.c, plus src/tests. Linux only. TEST_SRC := $(wildcard ${SOURCE_DIR}/tests/*.c) TEST_HDR := $(wildcard ${SOURCE_DIR}/tests/*.h) @@ -140,17 +160,28 @@ ifneq ($(TARGET_OS),Linux) endif $(CC) $(CFLAGS) -I${SOURCE_DIR} $(filter-out ${SOURCE_DIR}/urunit.c,$(URUNIT_SRC)) $(TEST_SRC) -o $@ -$(URUNIT_BIN_DYNAMIC): $(URUNIT_SRC) $(URUNIT_HDR) | prepare - $(CC) $(CFLAGS) $(URUNIT_SRC) $(LDFLAGS) -o $@ +# The embedded program: the file's bytes in .rodata, between two symbols +# init.c declares weak, so a build without EMBED leaves them null and nothing +# starts. .incbin keeps the bytes as they are; --noexecstack says what the +# assembler cannot know about them. +ifneq ($(EMBED),) +$(EMBED_BLOB): $(EMBED) | prepare + printf '\t.section .rodata\n\t.balign 16\n\t.globl urunit_embed_blob\nurunit_embed_blob:\n\t.incbin "%s"\n\t.globl urunit_embed_blob_end\nurunit_embed_blob_end:\n' \ + "$(EMBED)" > $(BUILD_DIR)/embed_blob.S + $(CC) -c -Wa,--noexecstack $(BUILD_DIR)/embed_blob.S -o $@ +endif + +$(URUNIT_BIN_DYNAMIC): $(URUNIT_SRC) $(URUNIT_HDR) $(URUNIT_OBJ) | prepare + $(CC) $(CFLAGS) $(URUNIT_SRC) $(URUNIT_OBJ) $(LDFLAGS) -o $@ -$(URUNIT_BIN_DYNAMIC_DEBUG): $(URUNIT_SRC) $(URUNIT_HDR) | prepare - $(CC) $(CFLAGS) -DDEBUG $(URUNIT_SRC) $(LDFLAGS) -o $@ +$(URUNIT_BIN_DYNAMIC_DEBUG): $(URUNIT_SRC) $(URUNIT_HDR) $(URUNIT_OBJ) | prepare + $(CC) $(CFLAGS) -DDEBUG $(URUNIT_SRC) $(URUNIT_OBJ) $(LDFLAGS) -o $@ -$(URUNIT_BIN_STATIC): $(URUNIT_SRC) $(URUNIT_HDR) | prepare - $(CC) $(CFLAGS) $(URUNIT_SRC) $(LDFLAGS) $(STATIC_LDFLAGS) -o $@ +$(URUNIT_BIN_STATIC): $(URUNIT_SRC) $(URUNIT_HDR) $(URUNIT_OBJ) | prepare + $(CC) $(CFLAGS) $(URUNIT_SRC) $(URUNIT_OBJ) $(LDFLAGS) $(STATIC_LDFLAGS) -o $@ -$(URUNIT_BIN_STATIC_DEBUG): $(URUNIT_SRC) $(URUNIT_HDR) | prepare - $(CC) $(CFLAGS) -DDEBUG $(URUNIT_SRC) $(LDFLAGS) $(STATIC_LDFLAGS) -o $@ +$(URUNIT_BIN_STATIC_DEBUG): $(URUNIT_SRC) $(URUNIT_HDR) $(URUNIT_OBJ) | prepare + $(CC) $(CFLAGS) -DDEBUG $(URUNIT_SRC) $(URUNIT_OBJ) $(LDFLAGS) $(STATIC_LDFLAGS) -o $@ ## install Install urunit in PREFIX .PHONY: install diff --git a/README.md b/README.md index 5708023..bf96d62 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,25 @@ virtio-blk serial, and every failure ends the boot. A container boot initrd needs only `/urunit`, with `/init` and `/vz-init` as symlinks to it. On FreeBSD `urunit` is always the supervisor. +### Embedded program + +`make static EMBED=` builds a static program for the target architecture +into `urunit`. The early init starts it before the root is mounted, from a +`memfd`, so the initrd stays one file and the root mount and the workload run +after it: a tracing or monitoring helper that has to be in place first. It is a +child of PID 1, reaped like every other, with its output on `/dev/kmsg`, and +tracefs is mounted at `/sys/kernel/tracing` before it starts. + +The build fixes when and how it runs: + +| Variable | Default | Effect | +| --- | --- | --- | +| `EMBED_NAME` | `embedded` | its `argv[0]` | +| `EMBED_WHEN` | empty: always | a path that must exist for it to start, such as a device the kernel offers only when the feature is there | +| `EMBED_READY` | empty: no wait | a flag that takes a file descriptor, such as `--ready-fd`; the program gets the write end of a pipe through it, and the init waits up to three seconds for one byte, so the workload starts after the program is ready. A program that exits first or stalls is logged and the boot goes on. | + +A build without `EMBED` starts nothing. + ## Building Building `urunit` is as simple as running `make`, but make sure that `make` and diff --git a/src/init.c b/src/init.c index 8d3a505..fdbc840 100644 --- a/src/init.c +++ b/src/init.c @@ -72,11 +72,15 @@ #define _GNU_SOURCE #include +#include +#include #include #include #include #include +#include #include +#include #include "sysops.h" #include "urunit.h" @@ -1219,6 +1223,174 @@ static int init_layers(const struct init_ctx *c, int overlay, char **root, return rc; } +// A program built into this binary (make EMBED=), started before the +// root is mounted, so the root mount and the workload run after it: a tracing +// or monitoring helper that has to be in place first, without adding a file to +// the initrd. A build without EMBED leaves the weak symbols null and nothing +// starts. +// +// It runs from a memfd through fexecve, as a child of PID 1 that the reaping +// loop collects, while this initramfs is still the root; switch_root does not +// touch a process that is already running. Its stdout and stderr go to +// /dev/kmsg. tracefs is mounted at /sys/kernel/tracing first, since a tracing +// helper is the usual reason to start something this early. +// +// The build fixes when and how it runs: +// EMBED_NAME its argv[0] (default "embedded") +// EMBED_WHEN a path that must exist for it to start, e.g. a device the +// kernel offers only when the feature is there; unset, it +// always starts +// EMBED_READY a flag that takes a file descriptor, e.g. --ready-fd. When +// set, the program gets the write end of a pipe through it and +// the init waits, bounded, for one byte before going on; a +// program that exits first or stalls is logged and the boot +// goes on. +extern const unsigned char urunit_embed_blob[] __attribute__((weak)); +extern const unsigned char urunit_embed_blob_end[] __attribute__((weak)); + +#ifndef URUNIT_EMBED_NAME +#define URUNIT_EMBED_NAME "embedded" +#endif +#ifndef URUNIT_EMBED_WHEN +#define URUNIT_EMBED_WHEN "" +#endif +#ifndef URUNIT_EMBED_READY +#define URUNIT_EMBED_READY "" +#endif + +#define INIT_TRACEFS "/sys/kernel/tracing" +// How long the init waits for the readiness byte. A boot should not hang on a +// program that never reports. +#define INIT_EMBED_READY_MS 3000 + +int init_embed_wait_ready(int fd, int timeout_ms) +{ + struct pollfd pfd = { .fd = fd, .events = POLLIN }; + unsigned char byte; + ssize_t n; + int rc; + + do { + rc = poll(&pfd, 1, timeout_ms); + } while (rc < 0 && errno == EINTR); + if (rc <= 0) + return -1; + do { + n = read(fd, &byte, 1); + } while (n < 0 && errno == EINTR); + if (n < 0) + return -1; + return n == 1; +} + +// Returns a memfd holding the embedded bytes, or -1 with errno set. +static int init_embed_fd(void) +{ + const unsigned char *p = urunit_embed_blob; + size_t left = (size_t)(urunit_embed_blob_end - p); + int fd; + + fd = memfd_create(URUNIT_EMBED_NAME, MFD_CLOEXEC); + if (fd < 0) + return -1; + while (left > 0) { + ssize_t n = write(fd, p, left); + + if (n < 0 && errno == EINTR) + continue; + if (n <= 0) { + close(fd); + return -1; + } + p += n; + left -= (size_t)n; + } + return fd; +} + +static void init_start_embedded(const struct init_ctx *c) +{ + const char *name = URUNIT_EMBED_NAME; + const char *when = URUNIT_EMBED_WHEN; + const char *ready_flag = URUNIT_EMBED_READY; + int wait = ready_flag[0] != '\0'; + char ready_arg[16]; + char *argv[4] = { (char *)name, NULL, NULL, NULL }; + int ready[2] = { -1, -1 }; + int fd; + int kmsg; + pid_t pid; + const unsigned char *begin = urunit_embed_blob; + const unsigned char *end = urunit_embed_blob_end; + + if (begin == NULL || end <= begin) + return; + if (when[0] != '\0' && c->ops->exists(when) != 0) + return; + fd = init_embed_fd(); + if (fd < 0) { + init_log(c, "could not stage %s: %s", name, strerror(errno)); + return; + } + if (c->ops->mount("tracefs", INIT_TRACEFS, "tracefs", + MS_NOSUID | MS_NODEV | MS_NOEXEC, NULL) != 0 && + errno != EBUSY) + init_log(c, "could not mount tracefs: %s", strerror(errno)); + if (wait && pipe2(ready, O_CLOEXEC) != 0) { + init_log(c, "could not make the readiness pipe for %s: %s", + name, strerror(errno)); + close(fd); + return; + } + pid = fork(); + if (pid < 0) { + init_log(c, "could not fork for %s: %s", name, strerror(errno)); + if (wait) { + close(ready[0]); + close(ready[1]); + } + close(fd); + return; + } + if (pid == 0) { + if (wait) { + // The write end crosses the exec; the program closes it + // once it is ready. + fcntl(ready[1], F_SETFD, 0); + snprintf(ready_arg, sizeof(ready_arg), "%d", ready[1]); + argv[1] = (char *)ready_flag; + argv[2] = ready_arg; + } + kmsg = open("/dev/kmsg", O_WRONLY | O_CLOEXEC); + if (kmsg >= 0) { + dup2(kmsg, 1); + dup2(kmsg, 2); + } + fexecve(fd, argv, environ); + _exit(127); + } + close(fd); + if (!wait) { + init_log(c, "%s started (pid %d)", name, (int)pid); + return; + } + close(ready[1]); + switch (init_embed_wait_ready(ready[0], INIT_EMBED_READY_MS)) { + case 1: + init_log(c, "%s ready (pid %d)", name, (int)pid); + break; + case 0: + init_log(c, "%s (pid %d) exited before it was ready", name, + (int)pid); + break; + default: + init_log(c, "%s (pid %d) not ready after %d ms; going on without " + "waiting", name, (int)pid, INIT_EMBED_READY_MS); + break; + } + close(ready[0]); +} + int init_run(const struct sysops *ops, enum urunit_mode mode, int argc, char **argv, const char *cmdline, int *out_argc, char ***out_argv) @@ -1256,6 +1428,9 @@ int init_run(const struct sysops *ops, enum urunit_mode mode, int argc, // The clock comes first, before anything checks a certificate. init_set_clock(&c); init_set_hostname(&c); + // Then the embedded program, so the root mount and the workload start + // after it. + init_start_embedded(&c); if (init_rosetta(&c)) return 1; diff --git a/src/sysops.h b/src/sysops.h index 46e8165..82dca34 100644 --- a/src/sysops.h +++ b/src/sysops.h @@ -53,6 +53,12 @@ struct sysops { extern const struct sysops sysops_real; +// Waits up to timeout_ms for the embedded program's readiness byte on fd, the +// read end of the pipe whose write end the program was given. Returns 1 when +// the byte arrived, 0 when the program closed the pipe without writing it (it +// exited first), and -1 when the wait timed out or failed. +int init_embed_wait_ready(int fd, int timeout_ms); + // Runs the early init sequence of mode through ops. cmdline is the text of // /proc/cmdline. When it is NULL, init_run reads the file through // ops->read_file. On success it returns 0 and sets *out_argv to a heap diff --git a/src/tests/test_init.c b/src/tests/test_init.c index 7750a67..03c4c84 100644 --- a/src/tests/test_init.c +++ b/src/tests/test_init.c @@ -1384,10 +1384,30 @@ static void test_init_parsers(void) init_free_mounts(m); } +// The readiness wait: the byte, the program exiting without it (end of +// file), and nothing at all within the timeout. +static void test_embed_wait_ready(void) +{ + int fds[2]; + + CHECK(pipe(fds) == 0); + CHECK(write(fds[1], "1", 1) == 1); + CHECK(init_embed_wait_ready(fds[0], 1000) == 1); + close(fds[1]); + CHECK(init_embed_wait_ready(fds[0], 1000) == 0); + close(fds[0]); + + CHECK(pipe(fds) == 0); + CHECK(init_embed_wait_ready(fds[0], 20) == -1); + close(fds[0]); + close(fds[1]); +} + int run_init_tests(void) { int start = harness_failures; + test_embed_wait_ready(); test_init_linux_virtiofs(); test_init_command_from_config(); test_init_hull_block();