commit 482c53e
chld
·
2026-08-27 22:25:46 +0000 UTC
parent 482c53e
init: import from derivelinux/situation@a656e093aa
2 files changed,
+260,
-0
A
sit
+20,
-0
1@@ -0,0 +1,20 @@
2+#!/bin/sh
3+set -x
4+
5+# install.sh for installing to other systems
6+
7+sh -n ./situation || {
8+ echo 'situation failed noexec. not installing'
9+ exit 1
10+}
11+
12+sh -n ./sctl|| {
13+ echo 'spectacle failed noexec. not installing'
14+ exit 1
15+}
16+
17+cp ./situation /bin/situation
18+ln -sf /bin/situation /bin/init
19+ln -sf /bin/situation /sbin/init
20+
21+cp ./sctl /bin/sctl
A
sit
+240,
-0
1@@ -0,0 +1,240 @@
2+#!/bin/sh
3+# /bin/situation — dèrive init and service manager
4+
5+PATH='/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
6+export PATH
7+
8+# source system shell configuration
9+if [ -f /etc/profile ]; then ENV="/etc/profile"
10+elif [ -f /etc/shrc ]; then ENV="/etc/shrc"
11+fi
12+export ENV
13+
14+echo 'checking situation'
15+
16+# failsafe incase situation is broken
17+sh -n $0 || {
18+ echo 'situation failed noexec. dropping to emergency shell'
19+ exec /bin/sh
20+}
21+
22+echo "starting situation"
23+
24+echo "mounting fs"
25+mount -n -t proc none /proc || {
26+ echo "failed to mount /proc. dropping to emergency shell"
27+ exec /bin/sh
28+}
29+
30+if ! mountpoint -q /sys 2>/dev/null; then
31+ mount -n -t sysfs none /sys || {
32+ echo "failed to mount /sys. dropping to emergency shell"
33+ exec /bin/sh
34+ }
35+fi
36+
37+if ! mountpoint -q /dev 2>/dev/null; then
38+ mount -n -t devtmpfs none /dev 2>/dev/null || {
39+ mount -n -t tmpfs dev /dev
40+ # Create essential device nodes if devtmpfs failed
41+ [ -c /dev/null ] || mknod -m 666 /dev/null c 1 3
42+ [ -c /dev/zero ] || mknod -m 666 /dev/zero c 1 5
43+ [ -c /dev/console ] || mknod -m 600 /dev/console c 5 1
44+ [ -c /dev/tty ] || mknod -m 666 /dev/tty c 5 0
45+ }
46+fi
47+
48+
49+# setup boot logging now that /dev exists
50+mkdir -p /var/log 2>/dev/null
51+# if [ -d /dev/fd ] && [ -w /var/log ]; then
52+# exec 1> >(tee -a /var/log/boot 2>/dev/null) 2>&1
53+# fi
54+
55+# create and mount additional essential filesystems
56+mkdir -p /dev/pts /dev/shm /run /tmp
57+mount -n -t devpts devpts /dev/pts 2>/dev/null || true
58+mount -n -t tmpfs tmpfs /dev/shm 2>/dev/null || true
59+mount -n -t tmpfs tmpfs /run 2>/dev/null || true
60+mount -n -t tmpfs tmpfs /tmp 2>/dev/null || true
61+
62+# create utmp directory and file for login tracking
63+mkdir -p /var/run /var/log
64+touch /var/run/utmp /var/log/wtmp
65+chmod 664 /var/run/utmp /var/log/wtmp
66+chown root:root /var/run/utmp /var/log/wtmp
67+
68+# remount root filesystem as read-write
69+echo "remounting root fs"
70+mount -n -o remount,rw / 2>/dev/null
71+
72+# basic system setup
73+echo "configuring system"
74+
75+# Read hostname from /etc/hostname if it exists
76+if [ -f /etc/hostname ]; then
77+ hostname "$(cat /etc/hostname)"
78+fi
79+
80+echo "/bin/sh" > /proc/sys/kernel/modprobe # disable modprobe
81+echo 0 > /proc/sys/kernel/printk
82+
83+# Allow unprivileged users to use ping via ICMP sockets (no setuid needed)
84+# This enables ping for all users without requiring capabilities or setuid
85+if [ -w /proc/sys/net/ipv4/ping_group_range ]; then
86+ echo "0 2147483647" > /proc/sys/net/ipv4/ping_group_range
87+fi
88+
89+# setup device manager (if available)
90+echo 'setting up devices'
91+if command -v udevd >/dev/null 2>&1; then
92+ if command -v udevadm >/dev/null 2>&1; then
93+ udevd --daemon
94+ udevadm trigger --action=add
95+ udevadm trigger --type=subsystems --action=add
96+ udevadm trigger --type=devices --action=add
97+ udevadm settle
98+ else echo 'udevadm not found'
99+ fi
100+elif command -v smdev >/dev/null 2>&1; then
101+ [ -f /proc/sys/kernel/hotplug ] && echo /bin/smdev > /proc/sys/kernel/hotplug
102+ smdev -s
103+elif command -v mdev >/dev/null 2>&1; then
104+ [ -f /proc/sys/kernel/hotplug ] && echo /bin/mdev > /proc/sys/kernel/hotplug
105+ mdev -s
106+else
107+ echo 'no device manager'
108+fi
109+
110+# fix device perms after s/mdev populates
111+chmod 666 /dev/null /dev/zero /dev/tty 2>/dev/null
112+chmod 620 /dev/ttyS0 /dev/tty1 2>/dev/null
113+
114+# bring up loopback interface
115+if command -v ip >/dev/null 2>&1; then
116+ ip link set lo up
117+fi
118+
119+# start services in sorted order with pid tracking
120+echo "starting services"
121+mkdir -p /etc/sv/pid
122+if [ -d /etc/sv/on ]; then
123+ for svc in $(ls /etc/sv/on/ 2>/dev/null | sort); do
124+ svc_path="/etc/sv/on/$svc"
125+ [ -x "$svc_path" ] || continue
126+ # Get the actual service name from symlink target
127+ svc_name=$(basename "$(readlink "$svc_path")")
128+ echo "starting $svc_name"
129+ "$svc_path" &
130+ echo $! > "/etc/sv/pid/$svc_name.pid"
131+ done
132+fi
133+
134+# run rc.local
135+[ -x /etc/rc.local ] && {
136+ echo 'running rc.local'
137+ /etc/rc.local
138+}
139+
140+echo 'killing device managers'
141+{
142+ pkill udevd
143+ pkill smdev
144+ pkill mdev
145+}>/dev/null 2>&1
146+
147+# spawn console on serial and VGA with respawning
148+echo "spawning getty"
149+
150+# wait a moment for devices to settle
151+sleep 1
152+
153+if command -v getty >/dev/null 2>&1; then
154+ # BusyBox getty: getty [OPTIONS] BAUD_RATE TTY [TERMTYPE]
155+ # Serial console (for -nographic) - respawn if dies
156+ (
157+ while :; do
158+ setsid getty -L 115200 ttyS0 2>&1
159+ sleep 1 # prevent rapid respawn on persistent failure
160+ done
161+ ) &
162+
163+ # VGA console - respawn if dies
164+ (
165+ while :; do
166+ setsid getty -L 38400 tty1 2>&1
167+ sleep 1 # prevent rapid respawn on persistent failure
168+ done
169+ ) &
170+
171+ (
172+ while :; do
173+ setsid getty -L 115200 ttyS1 2>&1
174+ sleep 1 # prevent rapid respawn on persistent failure
175+ done
176+ ) &
177+
178+ # VGA console - respawn if dies
179+ (
180+ while :; do
181+ setsid getty -L 38400 tty2 2>&1
182+ sleep 1 # prevent rapid respawn on persistent failure
183+ done
184+ ) &
185+else
186+ # Fallback: just exec a shell on console
187+ echo "getty not found. starting emergency shell"
188+ exec /bin/sh
189+fi
190+
191+# handle shutdowns
192+shutdown_system() {
193+ act=$1
194+ echo "shutting down"
195+
196+ # Stop services in reverse order using saved PIDs
197+ if [ -d /etc/sv/pid ]; then
198+ echo "stopping services"
199+ for pidfile in $(ls -r /etc/sv/pid/*.pid 2>/dev/null); do
200+ pid=$(cat "$pidfile" 2>/dev/null)
201+ svc=$(basename "$pidfile" .pid)
202+ if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
203+ echo "stopped $svc (pid $pid)"
204+ kill -TERM "$pid" 2>/dev/null
205+ fi
206+ done
207+ sleep 2
208+ fi
209+
210+ # terminate all remaining processes
211+ echo "stopping everything else"
212+ killall5 -15
213+ sleep 2
214+
215+ # force kill the rest
216+ sleep 1
217+
218+ # unmount everything
219+ echo "unmounting fs"
220+ umount -ar 2>/dev/null
221+ sync
222+
223+ echo "bye"
224+ #$act -f
225+ case $act in
226+ reboot) echo 'cya soon' ; reboot -f ;;
227+ poweroff) echo 'bye' ; poweroff -f ;;
228+ halt) echo 'bye' ; halt -f ;;
229+ esac
230+}
231+
232+trap 'shutdown_system poweroff' TERM
233+trap 'shutdown_system reboot' INT
234+trap 'shutdown_system halt' USR1
235+
236+echo "system ready"
237+
238+#idle loop
239+while :; do
240+ wait
241+done