How troglobit/finit works
Project Type and Purpose
• This is “Finit”: a small, fast init system and process supervisor for Linux (written in C).
• Its core purpose is to replace SysV or systemd init: handle bootstrap tasks, parse configuration, start/stop services & one-shot tasks, supervise them, manage runlevels, cgroups, conditions, and expose a control API (initctl).
Key Architectural Elements (to be shown as separate components)
a. Bootstrap & Environment Setup
• Kernel hands off to /sbin/init → launches finit.
• Initial tasks: populate /dev via udev or mdev, set up /run, /tmp, mount filesystems (via built-in tmpfiles support).
b. Configuration Subsystem
• Parser for /etc/finit.conf and /etc/finit.d/*.conf.
• Environment files (e.g. /etc/conf.d/…).
• Tmpfiles.d integration (initial directory/file creation).
c. Core Event Loop & Scheduler
• Single-threaded event loop driving: runlevels, conditions, plugin hooks, service transitions.
• Runlevels state machine (S → 2 by default, 0 for halt, 6 for reboot).
d. Service Supervisor
• Starts services, tasks, run commands, SysV scripts.
• Monitors via PID files or sd_notify / s6 ready notifications.
• Restart logic on crash (respawn) based on runlevels and conditions.
e. Conditions & Dependency Manager
• Watches “conditions” (service PID files, network availability, custom cond files).
• Blocks/unblocks service start accordingly.
f. CGroup Manager
• Detects cgroups v2; assigns init, system, user, root groups.
• Organizes each service/process into its sub-cgroup.
g. Plugin Framework
• Loads dynamic libraries from plugins/.
• Exposes hook points throughout boot and runtime for extensibility (I/O, custom events).
h. Control Interface (initctl)
• UNIX domain socket or IPC channel.
• Commands: start/stop/reload services, runlevels, condition manipulation, status, logs, cgroup introspection.
i. Logging Integration
• Early-boot ring buffer logging (kernel log).
• Redirected logs via logger to syslog daemon once started.
External Dependencies & Interactions
• Kernel (signals for shutdown/halt, /sysfs for console detection).
• udev/mdev for /dev population.
• SysV scripts and third-party daemons (e.g. syslogd, klogd, NTP, getty).
• Filesystem layout: /etc, /run/finit, /var/run, /tmpfiles.d, /cgroup/v2.
Relationships and Data Flow
• Configuration → Core Event Loop → Scheduler decides when to invoke Service Supervisor.
• Service Supervisor spawns child processes; registers them for monitoring.
• Spawned services write PID files → Condition Manager picks up and signals readiness.
• Plugin Framework hooks into scheduler and runtime events (e.g. before/after service start).
• initctl communicates with the event loop through IPC to query status or change configuration at runtime.
• CGroup Manager intercepts service launch calls to set proper cgroup.
Architectural Patterns & Principles
• Reactor/Event-loop pattern to multiplex service events, timers, and IPC.
• Plugin architecture for decoupled extensibility.
• Declarative configuration driving imperative actions.
• Supervisory pattern for process monitoring and automatic restart.
• Run-to-completion vs. parallel vs. sequential tasks (run vs. task vs. service stanzas).
Technologies & Frameworks
• Language: ANSI C.
• Build: autotools (autogen.sh, configure.ac, Makefile.am).
• IPC: UNIX sockets (for initctl).
• OS features: POSIX signals, devtmpfs, cgroups v2, dynamic linker for plugins.