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 "/bin/sh" > /proc/sys/kernel/modprobe  # disable modprobe
 80echo 0 > /proc/sys/kernel/printk
 81
 82# Allow unprivileged users to use ping via ICMP sockets (no setuid needed)
 83# This enables ping for all users without requiring capabilities or setuid
 84if [ -w /proc/sys/net/ipv4/ping_group_range ]; then
 85    echo "0 2147483647" > /proc/sys/net/ipv4/ping_group_range
 86fi
 87
 88# run rc.local
 89[ -x /etc/rc.local ] && {
 90	echo 'running rc.local'
 91	/etc/rc.local
 92	sleep 1
 93}
 94
 95# setup device manager (if available)
 96echo 'setting up devices'
 97if command -v udevd >/dev/null 2>&1; then
 98    if command -v udevadm >/dev/null 2>&1; then
 99        udevd --daemon
100        udevadm trigger --action=add
101        udevadm trigger --type=subsystems --action=add
102        udevadm trigger --type=devices --action=add
103        udevadm settle
104    else echo 'udevadm not found'
105    fi
106elif command -v smdev >/dev/null 2>&1; then
107    [ -f /proc/sys/kernel/hotplug ] && echo /bin/smdev > /proc/sys/kernel/hotplug
108    smdev -s
109elif command -v mdev >/dev/null 2>&1; then
110    [ -f /proc/sys/kernel/hotplug ] && echo /bin/mdev > /proc/sys/kernel/hotplug
111    mdev -svd
112else
113	echo 'no device manager'
114fi
115
116# bring up loopback interface
117if command -v ip >/dev/null 2>&1; then
118    ip link set lo up
119fi
120
121# fix device perms after s/mdev populates
122{
123    smdev -s 2>/dev/null || true
124    mdev -s 2>/dev/null || true
125} # rescan one more time, for stuff like snd and fb0
126
127chmod 666 /dev/null /dev/zero /dev/tty 2>/dev/null
128chmod 620 /dev/ttyS0 /dev/tty1 2>/dev/null
129
130# spawn console on serial and VGA with respawning
131echo "spawning getty"
132
133if command -v getty >/dev/null 2>&1; then
134    # BusyBox getty: getty [OPTIONS] BAUD_RATE TTY [TERMTYPE]
135    # Serial console (for -nographic) - respawn if dies
136    (
137        while :; do
138            setsid getty -L 115200 ttyS0 2>&1
139            sleep 1  # prevent rapid respawn on persistent failure
140        done
141    ) &
142
143    (
144        while :; do
145            setsid getty -L 115200 ttyS1 2>&1
146            sleep 1  # prevent rapid respawn on persistent failure
147        done
148    ) &
149
150    # spawn 6 gettys, linux tradition
151    for i in 1 2 3 4 5 6
152    do
153	(
154            while :; do
155		setsid getty -L 38400 tty$i 2>&1
156		sleep 1  # prevent rapid respawn on persistent failure
157            done
158	) &
159    done
160    
161else
162    # Fallback: just exec a shell on console
163    echo "getty not found. starting emergency shell"
164    exec /bin/sh
165fi
166
167# handle shutdowns
168shutdown_system() {
169    act=$1
170    echo "shutting down"
171    
172    # terminate all remaining processes
173    echo "stopping everything else"
174    killall5 -15
175    sleep 2
176
177    # force kill the rest
178    sleep 1
179
180    # unmount everything
181    echo "unmounting fs"
182    umount -ar 2>/dev/null
183    sync
184    
185    case $act in
186    	reboot) echo 'cya soon' ; reboot -f ;;
187    	poweroff) echo 'bye' ; poweroff -f ;;
188    	halt) echo 'bye damn' ; halt -f ;;
189    esac
190}
191
192trap 'shutdown_system poweroff' TERM 
193trap 'shutdown_system reboot' INT
194trap 'shutdown_system halt' USR1
195
196echo "system ready"
197
198#idle loop
199while :; do
200    wait
201done