#!/usr/bin/setsid /bin/bash # Copyright (C) 2009 Andy Spencer # See ../COPYING for terms # GLobals COMMAND=boot TESTING=false RELOAD=false if [ $$ == 1 ]; then DAEMON=true else DAEMON=false fi export PLAN9=/opt/plan9 export PATH=/lib/mkinit/bin:/bin:/sbin:/usr/bin:/usr/sbin # Functions function usage { echo 'usage: '$0' [options] [command]' echo echo 'Options:' echo ' -h,--help Print usage information' echo ' -t,--test Fake all commands' echo ' -d,--daemon For spawning stdin listener' echo ' -r,--reload Reload mkinit' echo '' echo 'Command:' echo ' boot Execute boot-up procedures' echo ' halt Execute shutdown procedures' echo ' reload Re-execute init process' echo ' mk Execute mk rule' echo ' start|stop|restart|zap|status ' echo ' Start,stop,restart,zap or query status of service' echo ' eval Execute command in mkinit process' exit } # Handle arguments function doopts { TMP=`getopt -n "$0" -o htdr -l help,test,daemon,reload -- "$@"` [ $? != 0 ] && usage eval set -- "$TMP" # Parse options while [ ! "$1" == "--" ]; do case "$1" in -h|--help ) usage ;; -t|--test ) TESTING=true ;; -d|--daemon ) DAEMON=true ;; -r|--reload ) RELOAD=true ;; esac shift done shift if [ "$*" ]; then COMMAND=($@) fi # Debugging output if $TESTING; then echo 'Options' 1>&2 echo ' TESTING:' $TESTING 1>&2 echo ' DAEMON:' $DAEMON 1>&2 echo ' COMMAND:' $COMMAND 1>&2 echo ' RELOAD:' $RELAOD 1>&2 fi } # Run mk on the init scripts function runamk { if $TESTING; then export P=echo export MKINIT_STATE=/tmp/mkinit_state mkdir -p $MKINIT_STATE fi /opt/plan9/bin/mk \ -f /etc/init.mk \ -i -k "$@" } # Process one command function process { cmd=$1 shift echo mkinit -- running "$cmd" "$@" case "$cmd" in boot ) runamk -a "$@" ;; restart ) if [ "$*" ]; then runamk "$@"-stop || runamk "$@"-zap sleep 0.5 runamk "$@"-start fi ;; start|stop|zap|status ) if [ "$*" ]; then runamk "$@-$cmd" fi ;; reload ) $TESTING && opt=-t exec $0 -r $opt ;; poweroff|reboot|kexec|halt) ( runamk "$cmd" "$@" & ) ;; eval ) eval "$@" ;; ?* ) runamk "$cmd" "$@" ;; esac } # Process arguments doopts "$@" # Run whatever was requested if ! $RELOAD; then process "${COMMAND[@]}" fi # Fork console listener while $DAEMON; do $TESTING || exec 0/dev/tty1 2>&1 while read -ep "mkinit> " line; do process $line history -s $line done $TESTING && exit echo "Respawning on /dev/tty1" sleep 1 done