Zig reimplementation of mini.c, with identical functionality.
mini.zig: The Zig implementationmini: The compiled executable| C API | Zig API |
|---|---|
fork() | std.posix.fork() |
execl() | std.posix.execveZ() |
stat() | std.posix.fstatatZ() |
waitpid() | std.posix.waitpid() |
sigtimedwait() | extern "c" binding to libc |
sigdelset() | extern "c" binding to libc |
sigprocmask() | std.posix.sigprocmask() |
opendir()/readdir()/closedir() | extern "c" bindings to libc |
getenv() | std.posix.getenvZ() |
nanosleep() | std.time.sleep() |
The isExecutable() function checks if a file is executable by:
posix.fstatatZ() to get file stat infolinux.S.IFREG)linux.S.IXUSR)For each executable file found, the program:
posix.fork() to create a child processposix.execveZ() to replace the process with the executable filesigtimedwait() signal loop to handle child eventsWhen argv[0] ends with "loop", the program enters loop mode:
LOOP_DIR (default /.loop.d/) instead of MINI_DIRLOOP_INTERVAL ms (default 2000ms)The program blocks all signals except crash signals (SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT, SIGTRAP, SIGSYS, SIGTTIN, SIGTTOU), then uses sigtimedwait() to handle:
SIGINT / SIGTERM: graceful exitSIGCHLD: reap children, restart in loop mode| Variable | Description | Default |
|---|---|---|
MINI_DIR | Directory to scan for executables (normal mode) | /.mini.d |
LOOP_DIR | Directory to scan for executables (loop mode) | /.loop.d |
LOOP_INTERVAL | Restart delay in ms (loop mode) | 2000 |
MINI_VERBOSE | Enable verbose output (1 or true) | off |
# Dynamic linking (glibc)
zig build-exe mini.zig -O ReleaseSmall -lc
# Static linking (musl, for containers)
zig build-exe mini.zig -O ReleaseSmall -target x86_64-linux-musl -lc
# Normal mode (scans /.mini.d/)
./mini
# Verbose mode
./mini verbose
# or
MINI_VERBOSE=1 ./mini
# Custom scan directory
MINI_DIR=./scripts ./mini
# Loop mode (symlink or rename executable to end with "loop")
ln -s mini mini-loop
./mini-loop