Compare commits

..

No commits in common. "master" and "1.0" have entirely different histories.
master ... 1.0

11 changed files with 90 additions and 471 deletions

13
LICENSE
View File

@ -1,13 +0,0 @@
Copyright (c) 2012-2014, Jack L. Frost <fbt@fleshless.org>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without
fee is hereby granted, provided that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.

View File

@ -1,43 +0,0 @@
# spark-rc makefile
# Please modify config.mk and not this
sinclude config.mk
.PHONY: clean install uninstall all tools
build: rc tools
all: build
tools:
make -C tools
rc: rc.in
sed -r \
-e 's%@ETC@%$(ETC)%' \
-e 's%@BASH_PATH@%$(BASH_PATH)%' \
rc.in > rc
chmod 750 $@
clean:
rm rc
make -C tools clean
install: build
install -dm755 $(BINDIR)
install -dm755 $(ETCDIR)
install -m750 rc $(BINDIR)/rc
install -m644 rc.conf $(ETCDIR)/rc.conf
install -m644 rc.motd $(ETCDIR)/rc.motd
install -m755 tools/killall5 $(BINDIR)/killall5
make -C tools install
uninstall:
rm $(BINDIR)/rc
make -C tools uninstall

View File

@ -1,8 +1,4 @@
# spark-rc
spark-rc
========
A simple rc script to kickstart your system.
## depends
* Enabled sysrq or halt from suckless.org's ubase.
* /proc support.
An example rc to use with spark (or not)

70
bin/rc Executable file
View File

@ -0,0 +1,70 @@
#!/bin/bash
rc.hostname() { hostname "$cfg_hostname"; }
rc.services_start() {
local service_name start_mode
for i in "${cfg_services[@]}"; do
unset service_name
[[ "$i" =~ ^@ ]] && {
service_name="${i##*@}"
start_mode='bg'
}
[[ "$i" =~ ^% ]] && {
service_name="${i##*%}"
start_mode='watch'
}
service_name=${service_name:-$i}
start_mode="${start_mode:-start}"
case "$start_mode" in
bg) "${cfg_initscripts_dir}/$service_name" start &;;
watch) "${cfg_initscripts_dir}/$service_name" watch &;;
start) "${cfg_initscripts_dir}/$service_name" start;;
*) echo "Mode $start_mode not supported";;
esac
done
}
rc.services_stop() {
for i in "${cfg_services[@]}"; do
"${cfg_initscripts_dir}/$i" stop
done
}
rc.boot() {
rc.hostname
rc.modules
rc.services_start
}
rc.shutdown() {
rc.services_stop
}
rc.hostname() {
[[ "$cfg_hostname" ]] && { hostname "$cfg_hostname"; }
}
rc.modules() {
for i in "${cfg_modules[@]}"; do
modprobe "$i"
done
}
source "/etc/rc.conf"
action="$1"
action="${action:-boot}"
case "$action" in
boot)
echo "Welcome to `uname -rs`"
rc.boot
;;
shutdown) rc.shutdown;;
esac

View File

@ -1,10 +0,0 @@
# Make config
USR ?= /usr/local
ETC ?= /etc
BIN ?= $(USR)/bin
BASH_PATH ?= /bin/bash
BINDIR = $(DESTDIR)$(PREFIX)$(BIN)
ETCDIR = $(DESTDIR)$(PREFIX)$(ETC)

17
etc/rc.conf Normal file
View File

@ -0,0 +1,17 @@
# System-wide configuration
export PATH='/usr/local/bin:/usr/bin'
# Where to look for initscripts
cfg_initscripts_dir='/etc/rc.d'
# Hostname
cfg_hostname='changeme'
# Services to start at boot time
cfg_services+=( 'mount' 'udev' 'rc.local' ) # You probably do need these
cfg_services+=( '@syslog-ng' '@crond' '@network' '@sshd' '@dbus' '@alsa' ) # Services starting with '@' start in background.
cfg_services+=( %agetty-tty{2..6} ) # Do not modify this unless you know what you are doing!
# Modules to be loaded at boot time. Most people don't need this
#cfg_modules=( 'tun' )

48
rc.conf
View File

@ -1,48 +0,0 @@
# A default PATH
export PATH='/usr/local/bin:/usr/local/sbin:/usr/bin'
# Locale
export LC_ALL='en_US.UTF-8'
# Hostname
cfg_hostname='spark'
# Timezone
# Uncomment and change this to yours
#cfg_timezone='Europe/Moscow'
# Early fsck, if you don't have such a step in your initrd
cfg_early_fsck=1
# Services that start with @ are executed in parallel
cfg_services+=(
'mount' 'sysctl' 'rsyslogd'
'@lo.iface' '@scron' @agetty-tty{2..6}
)
# Uncomment and add modules you want to be loaded at boot time here
#cfg_modules=( )
# Make C-A-D perform a soft reset
ctrlaltdel soft
# Virtual filesystems
cfg_mounts=(
'run:tmpfs:/run'
'tmp:tmpfs:/tmp'
'proc:proc:/proc:defaults,hidepid=2'
'sys:sysfs:/sys'
'dev:devtmpfs:/dev'
'pts:devpts:/dev/pts:noexec,nosuid,gid=5,mode=0620'
'mqueue:mqueue:/dev/mqueue:noexec,nosuid,nodev'
'shm:tmpfs:/dev/shm:defaults,mode=0777'
)
# Some temporary directories and files
cfg_tmpdirs+=( '/run/lock' '/run/lock/lvm' '/run/lvm' '/run/user' )
cfg_tmpfiles+=( '/run/utmp' )
# How long do we wait for all processes to die nicely?
cfg_killall5_timeout=30

222
rc.in
View File

@ -1,222 +0,0 @@
#!@BASH_PATH@
# vim: ft=zsh
# Functions
rc.rescue() { exec "${cfg_rc_rescue_shell:-"$SHELL"}"; }
rc.motd() {
[[ -f "/etc/rc.motd" ]] && {
while read; do
printf "%s\n" "$REPLY"
done < "/etc/rc.motd"
}
return 0
}
rc.tmpfiles() {
printf '%s\n' "${cfg_tmpdirs[@]}" | while IFS=':' read dir perm own grp; do
mkdir -pm "${perm:-755}" "$dir"
chown -c "${own:-root}:${grp:-root}" "$dir"
done
printf '%s\n' "${cfg_tmpfiles[@]}" | while IFS=':' read file perm own grp; do
> "$file"
chmod -c "${perm:-644}" "$file"
chown -c "${own:-root}:${grp:-root}" "$file"
done
}
rc.mount() {
for fs in "${cfg_mounts[@]}"; do
echo "$fs" | while IFS=':' read device fs_type mountpoint mount_options; do
if mountpoint -q "$mountpoint"; then
if [[ "$mount_options" ]]; then
mount -o "remount,$mount_options" "$mountpoint"
fi
else
if ! [[ -d "$mountpoint" ]]; then
mkdir -p "$mountpoint"
fi
if ! [[ "$mount_options" ]]; then
mount_options='defaults'
fi
mount "$device" -n -t "$fs_type" -o "$mount_options" "$mountpoint"
fi
done
done
}
rc.parse_cmdline() {
[[ -f "/proc/cmdline" ]] && {
boot_cmdline=( $(</proc/cmdline) )
}
for i in "${boot_cmdline[@]}"; do
case "$i" in
init.single) rc.rescue;;
esac
done
}
rc.services_start() {
local service_name start_mode
for i in "${cfg_services[@]}"; do
unset service_name
[[ "$i" =~ ^@ ]] && {
service_name="${i##*@}"
start_mode='bg'
}
[[ "$i" =~ ^% ]] && {
service_name="${i##*%}"
start_mode='watch'
}
service_name=${service_name:-$i}
start_mode="${start_mode:-start}"
case "$start_mode" in
bg) service "$service_name" start &;;
start) service "$service_name" start;;
*) echo "Mode $start_mode not supported";;
esac
done
}
rc.services_stop() {
echo "Stopping services..."
local service_name
for i in "${cfg_services[@]}"; do
service_name="${i##*@}"; service_name="${service_name##*%}"
service "$service_name" stop &>/dev/null &
done
wait
}
rc.stop_everything() {
echo "Politely asking all processes to shut down..."
killall5 -s 15 -w "$cfg_killall5_timeout"
echo "Killing the remaning ones..."
killall5 -s 9
}
rc.unmount_everything() {
echo "Unmounting filesystems..."
umount -r -a
}
rc.remount_root() {
echo "Remounting / read-only..."
mount / -o remount,ro
}
rc.boot() {
(( cfg_early_fsck )) && rc.fsck
rc.mount
rc.tmpfiles
rc.hostname
rc.timezone
rc.modules
rc.services_start
wait
rc.motd
}
rc.halt() {
if type -P halt &>/dev/null; then
function rc.halt_poweroff { halt -p; }
function rc.halt_reboot { halt -r; }
elif (( $(</proc/sys/kernel/sysrq) )); then
function rc.halt_poweroff { echo 'o' > /proc/sysrq-trigger; }
function rc.halt_reboot { echo 'b' > /proc/sysrq-trigger; }
else
printf 'Cannot halt, please install halt from suckless.org ubase or enable sysrq.\n'
return 1
fi
case "$action" in
halt) :;;
poweroff|shutdown) rc.halt_poweroff;;
reboot|*) rc.halt_reboot;;
esac
}
rc.sync() {
echo "Syncing disks."
sync
}
rc.shutdown() {
rc.services_stop
rc.stop_everything
rc.sync
rc.unmount_everything
rc.remount_root
echo "Halt complete."
rc.halt
}
rc.hostname() {
[[ -f '/etc/hostname' ]] && { hostname "$(</etc/hostname)"; }
[[ "$cfg_hostname" ]] && { hostname "$cfg_hostname"; }
}
rc.modules() {
for i in "${cfg_modules[@]}"; do
modprobe "$i"
done
}
rc.timezone() {
[[ "$cfg_timezone" ]] && {
ln -fs "/usr/share/zoneinfo/${cfg_timezone}" /etc/localtime
}
}
rc.fsck() {
declare root_rw=0
touch /rc-write-test && root_rw=1
(( root_rw )) && mount -o remount,ro /
fsck -A -C -p
(( root_rw )) && mount -o remount,rw /
}
rc.main() {
source "@ETC@/rc.conf"
# Default XDG_RUNTIME_DIR for all non-system users
# rtkit:x:133:133:RealtimeKit:/proc:/sbin/nologin
# Also add one for root
cfg_tmpdirs+=( "/run/user/0:700:0:0" )
while IFS=':' read -r _ _ uid gid _; do
if (( uid >= 1000 )); then
cfg_tmpdirs+=( "/run/user/$uid:700:$uid:$gid" )
fi
done < /etc/passwd
action="${1:-boot}"
case "$action" in
boot)
echo "Welcome to $(uname -rs)"
rc.boot
;;
poweroff|reboot|shutdown|halt)
rc.shutdown
;;
esac
}
# Main part
rc.main "$@"

View File

@ -1 +0,0 @@
Boot finished. Switch to tty2 to login.

View File

@ -1,29 +0,0 @@
# spark-rc tools makefile
# Please modify config.mk and not this
sinclude ../config.mk
.PHONY: clean install uninstall all
build: killall5
all: build
killall5: killall5.in
sed -r \
-e 's%@BASH_PATH@%$(BASH_PATH)%' \
killall5.in > killall5
chmod 750 $@
clean:
rm killall5
install: build
install -dm755 $(BINDIR)
install -dm755 $(ETCDIR)
install -m750 killall5 $(BINDIR)/killall5
uninstall:
rm $(BINDIR)/killall5

View File

@ -1,98 +0,0 @@
#!@BASH_PATH@
# A simple reimplementation of killall5 from sysvinit-tools
get_my_tree() {
local my_pid
read my_pid garbage < "/proc/self/stat"
child="$my_pid"
while [[ "$parent" != 1 ]]; do
read a b c parent d < "/proc/$child/stat";
child="$parent"
echo "$parent"
done
}
get_all_procs() {
local processlist
for i in /proc/*/exe; do
process_dir="${i%/*}"
process_pid="${process_dir##*/}"
readlink -e "$i" &>/dev/null && {
echo "$process_pid"
}
done
}
get_procs_to_kill() {
for i in "${all_procs[@]}"; do
unset skip
for p in "${my_tree[@]}"; do
[[ "$i" == "$p" ]] && { skip=1; }
done
[[ "$skip" ]] || { echo "$i"; }
done
}
pids_exist() {
for i in "$@"; do
if [[ -d "/proc/$i" ]]; then
return 0
fi
done
return 1
}
pid_wait() {
declare counter
while pids_exist "$@"; do
if (( counter == timeout )); then
return 1
fi
sleep 1
(( counter++ ))
done
}
main() {
timeout=30
signal=15
while (( $# )); do
case $1 in
-w)
flag_wait=1
timeout=$2
shift
;;
-s)
signal=$2
shift
;;
esac
shift
done
my_tree=( $(get_my_tree) self thread-self )
all_procs=( $(get_all_procs) )
procs_to_kill=( $(get_procs_to_kill) )
kill -n "$signal" "${procs_to_kill[@]}" 2>/dev/null
if (( flag_wait )); then
pid_wait "${procs_to_kill[@]}"
fi
return 0
}
main "$@"