master
sit
1#!/bin/sh
2# /bin/situation: derive init and service manager
3
4PATH='/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin'
5export PATH
6
7# source system shell configuration
8if [ -f /etc/profile ]; then ENV="/etc/profile"
9elif [ -f /etc/shrc ]; then ENV="/etc/shrc"
10fi
11export ENV
12
13echo 'checking situation'
14
15# failsafe incase situation is broken
16sh -n $0 || {
17 echo 'situation failed noexec. dropping to emergency shell'
18 exec /bin/sh
19}
20
21echo "starting situation"
22
23echo "mounting fs"
24mount -n -t proc none /proc || {
25 echo "failed to mount /proc. dropping to emergency shell"
26 exec /bin/sh
27}
28
29if ! mountpoint -q /sys 2>/dev/null; then
30 mount -n -t sysfs none /sys || {
31 echo "failed to mount /sys. dropping to emergency shell"
32 exec /bin/sh
33 }
34fi
35
36if ! mountpoint -q /dev 2>/dev/null; then
37 mount -n -t devtmpfs none /dev 2>/dev/null || {
38 mount -n -t tmpfs dev /dev
39 # Create essential device nodes if devtmpfs failed
40 [ -c /dev/null ] || mknod -m 666 /dev/null c 1 3
41 [ -c /dev/zero ] || mknod -m 666 /dev/zero c 1 5
42 [ -c /dev/console ] || mknod -m 600 /dev/console c 5 1
43 [ -c /dev/tty ] || mknod -m 666 /dev/tty c 5 0
44 }
45fi
46
47
48# setup boot logging now that /dev exists
49mkdir -p /var/log 2>/dev/null
50# if [ -d /dev/fd ] && [ -w /var/log ]; then
51# exec 1> >(tee -a /var/log/boot 2>/dev/null) 2>&1
52# fi
53
54# create and mount additional essential filesystems
55mkdir -p /dev/pts /dev/shm /run /tmp
56mount -n -t devpts devpts /dev/pts 2>/dev/null || true
57mount -n -t tmpfs tmpfs /dev/shm 2>/dev/null || true
58mount -n -t tmpfs tmpfs /run 2>/dev/null || true
59mount -n -t tmpfs tmpfs /tmp 2>/dev/null || true
60
61# create utmp directory and file for login tracking
62mkdir -p /var/run /var/log
63touch /var/run/utmp /var/log/wtmp
64chmod 664 /var/run/utmp /var/log/wtmp
65chown root:root /var/run/utmp /var/log/wtmp
66
67# remount root filesystem as read-write
68echo "remounting root fs"
69mount -n -o remount,rw / 2>/dev/null
70
71# basic system setup
72echo "configuring system"
73
74# Read hostname from /etc/hostname if it exists
75if [ -f /etc/hostname ]; then
76 hostname "$(cat /etc/hostname)"
77fi
78
79echo 0 > /proc/sys/kernel/printk
80
81# Allow unprivileged users to use ping via ICMP sockets (no setuid needed)
82# This enables ping for all users without requiring capabilities or setuid
83if [ -w /proc/sys/net/ipv4/ping_group_range ]; then
84 echo "0 2147483647" > /proc/sys/net/ipv4/ping_group_range
85fi
86
87# run rc.local
88[ -x /etc/rc.local ] && {
89 echo 'running rc.local'
90 /etc/rc.local
91 sleep 1
92}
93
94# setup device manager (if available)
95echo 'setting up devices'
96if command -v udevd >/dev/null 2>&1; then
97 if command -v udevadm >/dev/null 2>&1; then
98 udevd --daemon
99 udevadm trigger --action=add
100 udevadm trigger --type=subsystems --action=add
101 udevadm trigger --type=devices --action=add
102 udevadm settle
103 else echo 'udevadm not found'
104 fi
105elif command -v smdev >/dev/null 2>&1; then
106 [ -f /proc/sys/kernel/hotplug ] && echo /bin/smdev > /proc/sys/kernel/hotplug
107 smdev -s
108elif command -v mdev >/dev/null 2>&1; then
109 [ -f /proc/sys/kernel/hotplug ] && echo /bin/mdev > /proc/sys/kernel/hotplug
110 mdev -svd
111else
112 echo 'no device manager'
113fi
114
115# bring up loopback interface
116if command -v ip >/dev/null 2>&1; then
117 ip link set lo up
118fi
119
120# fix device perms after s/mdev populates
121{
122 smdev -s 2>/dev/null || true
123 mdev -s 2>/dev/null || true
124} # rescan one more time, for stuff like snd and fb0
125
126chmod 666 /dev/null /dev/zero /dev/tty 2>/dev/null
127chmod 620 /dev/ttyS0 /dev/tty1 2>/dev/null
128
129# spawn console on serial and VGA with respawning
130echo "spawning getty"
131
132if command -v getty >/dev/null 2>&1; then
133 # BusyBox getty: getty [OPTIONS] BAUD_RATE TTY [TERMTYPE]
134 # Serial console (for -nographic) - respawn if dies
135 (
136 while :; do
137 setsid getty -L 115200 ttyS0 2>&1
138 sleep 1 # prevent rapid respawn on persistent failure
139 done
140 ) &
141
142 (
143 while :; do
144 setsid getty -L 115200 ttyS1 2>&1
145 sleep 1 # prevent rapid respawn on persistent failure
146 done
147 ) &
148
149 # spawn 6 gettys, linux tradition
150 for i in 1 2 3 4 5 6
151 do
152 (
153 while :; do
154 setsid getty -L 38400 tty$i 2>&1
155 sleep 1 # prevent rapid respawn on persistent failure
156 done
157 ) &
158 done
159
160else
161 # Fallback: just exec a shell on console
162 echo "getty not found. starting emergency shell"
163 exec /bin/sh
164fi
165
166# handle shutdowns
167shutdown_system() {
168 act=$1
169 echo "shutting down"
170
171 # terminate all remaining processes
172 echo "stopping everything else"
173 killall5 -15
174 sleep 2
175
176 # force kill the rest
177 sleep 1
178
179 # unmount everything
180 echo "unmounting fs"
181 umount -ar 2>/dev/null
182 sync
183
184 case $act in
185 reboot) echo 'cya soon' ; reboot -f ;;
186 poweroff) echo 'bye' ; poweroff -f ;;
187 halt) echo 'bye damn' ; halt -f ;;
188 esac
189}
190
191trap 'shutdown_system poweroff' TERM
192trap 'shutdown_system reboot' INT
193trap 'shutdown_system halt' USR1
194
195echo "system ready"
196
197#idle loop
198while :; do
199 wait
200done