#!/run/kw-lower/bin/busybox sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
# /usr/lib/kw/rc (the kw layer): the first thing a distro's init runs. The
# base has mounted /dev, /proc, /sys, /run, /tmp and /data; this adds what
# every distro needs on top: cgroup2, devpts, /dev/shm, /run/lock, the
# hostname (/etc/hostname, unless it is empty or localhost: unkernel), and
# binfmt_misc with WebAssembly modules (binary format version 1, which WASI
# preview 1 programs are) run on the host's wasm engine through kwwasi,
# which needs a host serving vsock port 1021 (the CLI's --wasi; prototype).
# A distro registers its own formats after this (Alpine: qemu-user).
# There is no udev, so this gives the console user's devices the groups
# and modes udev's rules give them: the framebuffer, DRM's card nodes and
# /dev/udmabuf to video, input devices to input, sound to audio (0660), /dev/fuse and
# DRM's render nodes to everyone (0666), and the host's serial ports
# (/dev/ttyACM*) to dialout, but 0666: the images' user is not in dialout;
# by group name, so on a root without that group (the base shell) the
# device stays root's. The drivers
# make these devices at boot; the ones that come later (a USB device on
# kwusb: /dev/bus/usb/*/* and /dev/ttyUSB*, 0666) get theirs from
# kwhotplug, which this starts (kwbox hotplug: it listens to the kernel's
# uevents). It also makes udev's
# links /dev/fd, /dev/stdin, /dev/stdout and /dev/stderr (bash's <(...)
# needs /dev/fd), and udev's database entry of the camera (kwcam's
# /dev/video*: /run/udev/data/c<major>:<minor> with what v4l_id and
# 60-persistent-v4l.rules give it, and its /dev/v4l/by-path link), since
# libudev lists a device node only once udev has initialised it (that
# entry): GStreamer's device monitor (cheese), guvcview and PipeWire find
# cameras so; nothing else gets an entry, and /run/udev/control (udev
# running) stays absent. As systemd's sysctl defaults do, it lets every group open
# ICMP echo sockets (net.ipv4.ping_group_range): Debian's ping has no file
# capability or setuid bit and needs them as a user. It also has DRM's vblank timer stop 100 ms after its last use
# instead of 5 s (drm.vblankoffdelay): the timer wakes the host about 120
# times a second while it runs. With kw.wayland=1 on the kernel command
# line (the page adds it when it shows Wayland windows) it starts wland's
# relay for the image's user, which dials the page only when an app
# connects (wland --boot; /usr/lib/kw/bin/wland).

PATH=/run/kw-lower/bin
m() { mountpoint -q "$2" || mount -t "$1" ${3:+-o "$3"} "$1" "$2"; }
m cgroup2 /sys/fs/cgroup nosuid,nodev,noexec
mkdir -p /dev/pts /dev/shm
m devpts /dev/pts gid=5,mode=620,ptmxmode=666
m tmpfs /dev/shm nosuid,nodev,mode=1777
[ -e /dev/fd ] || ln -s /proc/self/fd /dev/fd
for f in 0:stdin 1:stdout 2:stderr; do [ -e /dev/${f#*:} ] || ln -s /proc/self/fd/${f%:*} /dev/${f#*:}; done
mkdir -p -m 1777 /run/lock
h=
IFS= read -r h 2>/dev/null < /etc/hostname
case $h in '' | localhost | localhost.*) h=unkernel ;; esac
hostname "$h"
echo '0 2147483647' > /proc/sys/net/ipv4/ping_group_range
d=/proc/sys/fs/binfmt_misc
if [ -d $d ] && { mountpoint -q $d || mount -t binfmt_misc binfmt_misc $d 2>/dev/null; } &&
	[ -x /usr/lib/kw/kwwasi ] && [ ! -e $d/wasi ]; then
	printf '%s\n' ':wasi:M::\x00asm\x01\x00\x00\x00::/usr/lib/kw/kwwasi:P' > $d/register
fi
dev() {
	g=$1 m=$2
	shift 2
	for f; do [ -e "$f" ] && chgrp "$g" "$f" 2>/dev/null && chmod "$m" "$f"; done
}
dev video 660 /dev/fb[0-9]* /dev/dri/card* /dev/udmabuf /dev/video*
dev input 660 /dev/input/event* /dev/input/mice /dev/input/mouse* /dev/input/js*
dev audio 660 /dev/dsp* /dev/audio* /dev/mixer* /dev/snd/*
dev dialout 666 /dev/ttyACM*
for f in /dev/fuse /dev/dri/renderD*; do [ -e "$f" ] && chmod 666 "$f"; done
[ -x /usr/bin/kwbox ] && /usr/bin/kwbox hotplug
for f in /sys/class/video4linux/video*; do
	n= d= i=0
	read -r n 2>/dev/null < $f/name
	read -r d 2>/dev/null < $f/dev
	read -r i 2>/dev/null < $f/index
	[ "$n" = kwcam ] && [ -n "$d" ] || continue
	l=v4l/by-path/platform-kwcam-video-index$i
	mkdir -p /run/udev/data /dev/v4l/by-path
	ln -sfn ../../${f##*/} /dev/$l
	printf 'S:%s\nI:%s\nE:ID_V4L_VERSION=2\nE:ID_V4L_PRODUCT=unkernel host camera\nE:ID_V4L_CAPABILITIES=:capture:\nE:ID_PATH=platform-kwcam\nE:ID_PATH_TAG=platform-kwcam\nG:uaccess\nQ:uaccess\nV:1\n' \
		$l "$(awk '{ printf "%d", $1 * 1000000 }' /proc/uptime)" > /run/udev/data/c$d
done
f=/sys/module/drm/parameters/vblankoffdelay
[ -w $f ] && echo 100 > $f
read -r c < /proc/cmdline
case " $c " in *" kw.wayland=1 "*) [ -x /usr/lib/kw/bin/wland ] && /usr/lib/kw/bin/wland --boot ;; esac
exit 0
