[unmaintained] init scripts for sinit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

359 lines
7.3 KiB

#!/bin/sh
#
# urc - a "better" init script
# (c) ix 2016
#
## CONFIGURATION
# These values are to be used in case urc.conf is
# non-existent or broken
DAEMONS="iptables alsa crond dbus wpa_supplicant dhcpcd acpid"
ENABLED="@dhcpcd"
NETWORK_INTERFACE="eth0"
WIFI_INTERFACE="wlan0"
KBLAYOUT="us"
UDEV="busybox"
TIMEZONE="America/New_York"
custom_restart() { default_restart "$@"; }
custom_start() { default_start "$@"; }
custom_stop() { default_stop "$@"; }
custom_poll() { default_poll "$@"; }
# Source config
. /etc/urc.conf
## FUNCTIONS
# This function defines all the stuff that happens on boot
on_boot() {
echo
echo
echo 'urc is starting.'
echo
echo
echo 'Mounting API filesystem.'
mountpoint -q /proc || mount -t proc proc /proc -o nosuid,noexec,nodev
mountpoint -q /sys || mount -t sysfs sys /sys -o nosuid,noexec,nodev
mountpoint -q /var/run || mount -t tmpfs run /run -o mode=0775,nosuid,nodev
mountpoint -q /dev || mount -t devtmpfs dev /dev -o mode=0755,nosuid
mkdir -p /dev/pts /dev/shm
mountpoint -q /dev/pts || mount -t devpts devpts /dev/pts -o mode=0620,gid=5,nosuid,noexec
mountpoint -q /dev/shm || mount -t tmpfs shm /dev/shm -o mode=1777,nosuid,nodev
echo 'Mounting tmpfs.'
mount -t tmpfs tmpfs /tmp -o mode=1777,nosuid,nodev
echo 'Mounting swap.'
/sbin/swapon -a
echo 'Setting up loopback device.'
/sbin/ip link set up dev lo
echo 'Initializing udev.'
case $UDEV in
systemd)
/usr/lib/systemd/systemd-udevd --daemon
/usr/bin/udevadm trigger --action=add --type=subsystems
/usr/bin/udevadm trigger --action=add --type=devices
;;
eudev)
/sbin/udevd --daemon
/sbin/udevadm trigger --action=add --type=subsystems
/sbin/udevadm trigger --action=add --type=devices
;;
smdev)
/sbin/smdev -s
echo /sbin/smdev > /proc/sys/kernel/hotplug
;;
busybox)
/sbin/busybox mdev -s
echo /sbin/mdev > /proc/sys/kernel/hotplug
;;
*)
echo 'Invalid udev, using busybox.'
/sbin/busybox mdev -s
echo /sbin/mdev > /proc/sys/kernel/hotplug
;;
esac
echo 'Setting hostname.'
echo $HOSTNAME >| /proc/sys/kernel/hostname
echo 'Setting system clock.'
/sbin/hwclock --hctosys
echo 'Mounting fstab.'
mount -a
mount -o remount,rw /
echo 'Setting timezone.'
ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
echo 'Starting daemons.'
for dmn in $ENABLED; do
if [ $(echo $dmn | awk '{ s=substr($0, 1, 1); print s; }') = '@' ]; then
custom_start $(echo $dmn | awk '{ s=substr($0, 2); print s; }') &
else
custom_start "$dmn"
fi
done
echo 'Loading keyboard layout.'
loadkeys $KBLAYOUT
if [ -x '/etc/urc.local' ]; then
echo 'Starting local script.'
/etc/urc.local
fi
echo 'Spawning gettys.'
/sbin/agetty -8 -s 38400 tty1 linux &
/sbin/agetty -8 -s 38400 tty2 linux &
/sbin/agetty -8 -s 38400 tty3 linux &
}
# This function defines all the stuff that happens on shutdown
on_shutdown() {
echo 'Stopping daemons.'
custom_stop all
echo 'Shutting down udev.'
case $UDEV in
systemd) killall systemd-udevd ;;
eudev) killall udevd ;;
esac
echo 'Sending all processes the TERM signal.'
/sbin/busybox killall5 -TERM
sleep 3
echo 'Sending all processes the KILL signal.'
/sbin/busybox killall5 -KILL
echo 'Unmounting API filesystem.'
umount -r /var/run
echo 'Unmounting fstab.'
umount -a -r
echo 'Unmounting swap.'
/sbin/swapoff -a
echo 'Remounting root read-only.'
mount -o remount,ro /
}
# This function starts daemons
default_start() {
echo "Starting "$1"".
case "$1" in
all)
for dmn in $DAEMONS $ENABLED; do
custom_poll "${dmn##@}" || custom_start "${dmn##@}"
done
;;
alsa)
alsactl restore
;;
bitlbee)
su -s /bin/sh -c 'bitlbee -F' bitlbee
;;
dbus)
mkdir -p /var/run/dbus
dbus-uuidgen --ensure
dbus-daemon --system
;;
iptables)
iptables-restore < /etc/iptables/iptables.rules
;;
oss)
soundon
;;
sndio)
sndiod -v 127
;;
sshd)
/usr/sbin/sshd
;;
sysklogd)
/usr/sbin/syslogd
/usr/sbin/klogd
;;
dhcpcd)
if ip link | grep -Fq $NETWORK_INTERFACE; then :; else
echo "Waiting for $NETWORK_INTERFACE to settle."
for i in $(seq 100); do
ip link | grep -Fq $NETWORK_INTERFACE
break
sleep 1
done
fi
dhcpcd -nqb
;;
ntpd)
ntpd -g -u ntp
;;
wpa_supplicant)
wpa_supplicant -Dwext -B -i"$WIFI_INTERFACE" -c/etc/wpa_supplicant.conf
;;
*)
# Fallback: start the command
"$1"
;;
esac
}
# This function stops daemons
default_stop() {
echo "Stopping "$1"".
case "$1" in
all)
for dmn in $DAEMONS $ENABLED; do
custom_poll "${dmn##@}" && custom_stop "${dmn##@}"
done
;;
alsa)
alsactl store
;;
dbus)
killall dbus-launch
killall dbus-daemon
rm /var/run/dbus/dbus.pid
;;
oss)
soundoff
;;
iptables)
for table in $(cat /proc/net/ip_tables_names); do
iptables-restore < /var/lib/iptables/empty-$table.rules
done
;;
sysklogd)
killall syslogd
killall klogd
;;
sndio)
pkill sndiod
;;
*)
# Fallback: kill all processes with the name of the command
killall "$1"
;;
esac
}
# This function restarts daemons
default_restart() {
case "$1" in
*)
custom_stop "$@"
custom_start "$@"
;;
esac
}
# This function checks daemon status
default_poll() {
case "$1" in
alsa)
# Doesn't make much sense for this service
return 0
;;
iptables)
iptables -L -n | grep -m 1 -q '^ACCEPT\|^REJECT' ;;
dbus)
test -e /var/run/dbus/dbus.pid
;;
sysklogd)
pgrep syslogd && pgrep klogd
;;
*)
# Fallback: check if any processes of that name are running
pgrep "(^|/)$1\$" >/dev/null 2>&1
;;
esac
}
check_root() {
if [ "$(id -u)" != "0" ]; then
echo 'urc: Operation not permitted.'
exit 1
fi
}
## EXECUTE
case "$1" in
-h|--help)
echo 'usage: urc [-l|-s|-h|--start|-stop|--restart|--shutdown|--reboot|--init]'
echo 'see urc(8) for more information'
;;
-l|--list)
for dmn in $DAEMONS; do
if custom_poll "$dmn" >/dev/null 2>&1; then
echo "[✓] $dmn"
else
echo "[✘] $dmn"
fi
done
;;
--start)
check_root
cmd="$1"
shift
for dmn in ${@:-$DAEMONS}; do
custom_poll "$dmn" && echo "$dmn is already running." || custom_start "$dmn"
done
;;
--stop|--restart)
check_root
cmd="$1"
shift
for dmn in ${@:-$DAEMONS}; do
custom_poll "$dmn" && custom_$(echo $cmd | sed 's/--//g') "$dmn" || echo "$dmn is not running."
done
;;
--init)
check_root
on_boot
;;
--shutdown)
check_root
on_shutdown
/sbin/busybox poweroff -f
;;
--reboot)
check_root
on_shutdown
/sbin/busybox reboot -f
;;
-s|--suspend)
check_root
echo mem > /sys/power/state
;;
--hibernate)
check_root
echo disk > /sys/power/state
;;
*)
echo 'Invalid option, see -h or call man urc for help.'
exit 1
;;
esac