/* * planck-init - Minimal PID 1 for planck VMs * * Core responsibilities: * - Ensure fd 0/1/2 are attached to /dev/console * - Catch shutdown signals and power off cleanly * - Reap orphaned children forever * * Optional feature blocks live in separate C files below. They are included * into this translation unit on purpose: removing one include cleanly removes * that behavior without changing the tiny static build. * * Feature blocks may also expose inittab builtins. For example: * ::builtin:ipv4 */ #include "planck-init.h" /* * Optional feature blocks. Defaults preserve the full planck-init behavior. * Build scripts can set these to 0 to produce narrower PID 1 variants. */ #ifndef PLANCK_WITH_LOG #define PLANCK_WITH_LOG 1 #endif #ifndef PLANCK_WITH_RUN #define PLANCK_WITH_RUN 1 #endif #ifndef PLANCK_WITH_INITTAB #define PLANCK_WITH_INITTAB 1 #endif #ifndef PLANCK_WITH_IPV4 #define PLANCK_WITH_IPV4 1 #endif /* CoreMark defaults OFF: it is a measurement tool, not part of a minimal PID 1, and it costs * image size. Enable with `component coremark` in the manifest. */ #ifndef PLANCK_WITH_COREMARK #define PLANCK_WITH_COREMARK 0 #endif /* The planck line (mem used=/free=/total=) -- THE measurement. On by default: every stage in the * matrix is compared on it, and a build that boots without printing it produces no data point. */ #ifndef PLANCK_WITH_MEM #define PLANCK_WITH_MEM 1 #endif /* PID 1 makes its own mount points: /dev (and devtmpfs on it, before the console is opened), /proc, /run * (and /sys with `sysfs`). The rootfs then ships none of them. Off by default: a box whose rootfs or initramfs * ships them pays nothing. */ #ifndef PLANCK_WITH_MOUNTPOINTS #define PLANCK_WITH_MOUNTPOINTS 0 #endif /* sysfs on /sys: only for a box where something reads /sys (podman's cgroups). */ #ifndef PLANCK_WITH_SYSFS #define PLANCK_WITH_SYSFS 0 #endif /* `::listen: `: PID 1 holds the socket and starts the program per connection (planck-listen.c). */ #ifndef PLANCK_WITH_LISTEN #define PLANCK_WITH_LISTEN 0 #endif /* `::builtin:mount [options]` (planck-mount.c). */ #ifndef PLANCK_WITH_MOUNT #define PLANCK_WITH_MOUNT 0 #endif /* /dev as a ramfs holding the handful of nodes the box opens, made by PID 1: for a kernel without devtmpfs * (no kdevtmpfs thread, no node per registered device). */ #ifndef PLANCK_WITH_DEVNODES #define PLANCK_WITH_DEVNODES 0 #endif /* The tmpfs on /run. A box that keeps no runtime state (no pid file, no links made at boot) builds `without rundir`. */ /* Power-off through the kernel (needs ACPI). A box whose kernel has none builds `without poweroff`: a power-off or halt * request then resets, and a VM run with -no-reboot ends on the reset. */ #ifndef PLANCK_WITH_POWEROFF #define PLANCK_WITH_POWEROFF 1 #endif #ifndef PLANCK_WITH_RUNDIR #define PLANCK_WITH_RUNDIR 1 #endif #ifndef PLANCK_AUTO_IPV4 #define PLANCK_AUTO_IPV4 1 #endif #ifndef PLANCK_DEFAULT_PATH #define PLANCK_DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" #endif /* Unconditional and FIRST: every component below builds its strings with these, and they exist so * that musl's printf (32,880 bytes on rv32) is never linked. See planck-fmt.c. */ #include "planck-fmt.c" #if PLANCK_WITH_LOG #include "planck-log.c" #endif #if PLANCK_WITH_RUN #include "planck-run.c" #endif #if PLANCK_WITH_LISTEN #include "planck-listen.c" #endif #if PLANCK_WITH_MOUNT #include "planck-mount.c" #endif #if PLANCK_WITH_INITTAB #include "planck-inittab.c" #endif #if PLANCK_WITH_IPV4 #include "planck-ipv4.c" #endif #if PLANCK_WITH_COREMARK #include "planck-coremark.c" #endif #if PLANCK_WITH_MEM #include "planck-mem.c" #else static void planck_mem_report(void) { log_msg("PID 1 alive"); } #endif #ifndef PLANCK_FEATURE_LOG static void log_msg(const char *msg) { (void)msg; } #endif #ifndef PLANCK_FEATURE_RUN static void planck_run(const char *cmd) { (void)cmd; } #endif #ifndef PLANCK_FEATURE_INITTAB static void run_inittab_action(const char *action) { (void)action; } static void process_startup_entries(void) { } static int planck_handle_child_exit(pid_t died) { (void)died; return 0; } #endif #ifndef PLANCK_FEATURE_IPV4 static void planck_ipv4_configure_from_cmdline(void) { } #endif /* The stub every other component already had and this one did not, so `component coremark` could * not actually be turned off: the header declares planck_coremark_run() unconditionally and the * link failed with `undefined reference to planck_coremark_run'. It SAYS SO rather than doing * nothing quietly -- an inittab that asks for a benchmark the binary does not contain should not * look like a benchmark that ran. */ #ifndef PLANCK_FEATURE_COREMARK static void planck_coremark_run(void) { log_msg("coremark: not compiled in"); } #endif static void handle_shutdown(int sig) { char buf[64]; planck_cati(buf, sizeof(buf), planck_cat(buf, sizeof(buf), 0, "shutdown signal "), sig); log_msg(buf); run_inittab_action("shutdown"); /* busybox protocol: reboot(1) sends SIGTERM and expects a REBOOT; poweroff(1)/halt(1) * send SIGUSR2/SIGUSR1 and expect power-off. Everything else powers off too. */ sync(); if (sig == SIGTERM) { log_msg("rebooting"); reboot(RB_AUTOBOOT); } #if !PLANCK_WITH_POWEROFF /* No power-off in this kernel (no ACPI): the kernel would only halt, and the VM would stay up. A reset ends a VM * run with -no-reboot (PVE `reboot: 0`), which is how a planck VM stops. */ log_msg("powering off by reset (no power-off in this kernel; -no-reboot ends the VM)"); reboot(RB_AUTOBOOT); #endif log_msg("powering off"); reboot(RB_POWER_OFF); /* musl sys/reboot.h name; == LINUX_REBOOT_CMD_POWER_OFF */ } #if PLANCK_WITH_DEVNODES #include #include #include /* The kernel has no devtmpfs: /dev is a ramfs and these are the nodes the box opens (console: PID 1 and every log * line; null, zero: shells and svn; urandom, random: apr; tty: a shell asking for its terminal). Modes are exact: * the umask is lifted for the six calls. */ static void mount_devtmpfs(void) { mode_t lNumMask = umask(0); mkdir("/dev", 0755); if (mount("dev", "/dev", "ramfs", 0, "mode=0755") == 0) { mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1)); mknod("/dev/tty", S_IFCHR | 0666, makedev(5, 0)); mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3)); mknod("/dev/zero", S_IFCHR | 0666, makedev(1, 5)); mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8)); mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9)); } umask(lNumMask); } #define PLANCK_MKDIR(iStrPath, iNumMode) mkdir(iStrPath, iNumMode) #elif PLANCK_WITH_MOUNTPOINTS #include #include /* The rootfs ships no /dev: without it the kernel's own devtmpfs mount and its console open fail (harmlessly), * so PID 1 makes the folder and mounts devtmpfs here, before the console is attached. EEXIST/EBUSY (a rootfs or * a kernel that already did it) are ignored. */ static void mount_devtmpfs(void) { mkdir("/dev", 0755); mount("devtmpfs", "/dev", "devtmpfs", 0, 0); } #define PLANCK_MKDIR(iStrPath, iNumMode) mkdir(iStrPath, iNumMode) #else static void mount_devtmpfs(void) { } #define PLANCK_MKDIR(iStrPath, iNumMode) ((void)0) #endif static void attach_console_fds(void) { mount_devtmpfs(); close(0); close(1); close(2); open("/dev/console", O_RDWR); dup(0); dup(0); } /* SUPPORT FOR RUNNING THINGS, not core PID 1 -- so it is gated with the components that need it. * The 050 basis init contains no "proc"/"sysfs"/"PATH" strings at all: with only log+mem there is * nothing to run, nothing to give a PATH to, and nothing that reads /proc. Unconditional, these * cost bytes in the binary whose SIZE IS THE MEASUREMENT. */ #if PLANCK_WITH_RUN || PLANCK_WITH_INITTAB || PLANCK_WITH_IPV4 #include static void mount_base_filesystems(void) { /* core plumbing, not a feature: /proc feeds the ipv4 builtin (/proc/cmdline) and any tooling. * Errors ignored — already-mounted or config-less kernels just move on. */ PLANCK_MKDIR("/proc", 0555); mount("proc", "/proc", "proc", 0, 0); #if PLANCK_WITH_SYSFS PLANCK_MKDIR("/sys", 0555); mount("sysfs", "/sys", "sysfs", 0, 0); #endif #if PLANCK_WITH_RUNDIR /* /run: the writable RAM folder for runtime state (pid files, links made at boot). */ PLANCK_MKDIR("/run", 0755); mount("tmpfs", "/run", "tmpfs", 0, "mode=0755"); #endif } static void setup_default_environment(void) { setenv("PATH", PLANCK_DEFAULT_PATH, 1); } #else static void mount_base_filesystems(void) { } static void setup_default_environment(void) { } #endif int main(int argc, char *argv[]) { (void)argc; (void)argv; if (getpid() != 1) { write(2, "planck-init: must be PID 1\n", 27); return 1; } attach_console_fds(); mount_base_filesystems(); setup_default_environment(); planck_mem_report(); signal(38, handle_shutdown); /* SIGRTMIN+4 = ACPI planck power button */ signal(SIGTERM, handle_shutdown); /* busybox reboot(1) */ signal(SIGINT, handle_shutdown); /* ctrl-alt-del, forwarded by the kernel */ signal(SIGUSR1, handle_shutdown); /* busybox halt(1) */ signal(SIGUSR2, handle_shutdown); /* busybox poweroff(1) — Alpine's shutdown path */ log_msg("running sysinit"); process_startup_entries(); #if PLANCK_AUTO_IPV4 planck_ipv4_configure_from_cmdline(); #endif log_msg("sysinit complete"); #if PLANCK_WITH_LISTEN planck_listen_loop(); /* returns only without listen lines, or when ppoll fails (respawn carries on below) */ #endif for (;;) { int status; pid_t died = wait(&status); if (died <= 0) continue; planck_handle_child_exit(died); } return 0; }