#!/opt/plan9/bin/rc # Copyright (C) 2009 Andy Spencer # See ../COPYING for terms # GLobals COMMAND=boot TESTING=false if(~ $pid 1) DAEMON=true if not DAEMON=false PATH=/lib/mkinit/bin:/bin:/sbin:/usr/bin:/usr/sbin # Functions fn 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 '' 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 fn doopts { tmp=`{getopt -n $0 -o htd -l help,test,daemon -- $*} if(~ $status 0) usage eval '*=('$"tmp')' # Parse options while(! ~ $1 --){ switch($1){ case -h --help usage case -t --test TESTING=true case -d --daemon DAEMON=true } shift } shift if(! ~ $#* 0) COMMAND=$* # Debugging output if($TESTING){ echo 'Options' >[2=1] echo ' TESTING:' $TESTING >[2=1] echo ' DAEMON:' $DAEMON >[2=1] echo ' COMMAND:' $COMMAND >[2=1] } } # Fake readline fn prompt { echo -n $1 >[1=2] line } # Run mk on the init scripts fn runamk { if($TESTING){ P=echo MKINIT_STATE=/tmp/mkinit_state mkdir -p $MKINIT_STATE } /opt/plan9/bin/mk \ -f /etc/init.mk \ -i -k $* } # Process one command fn process { cmd=$1 shift echo mkinit -- running "$"cmd" "$"*" switch($cmd){ case boot runamk -a $* case restart if(~ $arg ?*){ if (! runamk $"*-stop) runamk $"*-zap runamk $"*-start } case start stop zap status if(~ $arg ?*) runamk $"*-$cmd case reload if($TESTING) opt=-t exec $0 -r $opt case poweroff reboot kexec halt { runamk $cmd $* & } case eval eval $* case ?* runamk $cmd $* } } # Process arguments doopts $* # Run whatever was requested process $COMMAND # Fork console listener fn listen1 { while(line=`{prompt 'mkinit> '}) process $line } if($DAEMON){ listen1 if ($TESTING) exit while($DAEMON) { echo Respawning on /dev/console.. sleep 1 listen1 <[0]/dev/console \ >[1]/dev/console \ >[2=1] } }