X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fmkinit;h=4b39890ce4bb3f6be0adac0eadc9e731b6888ef3;hb=66848b15e247b45c536b4122f8c82663e5fc4b83;hp=e9948d1228d26c60f5756fdf14504db5f747bb62;hpb=39fb8b5ed810df3b306373607183e8d4976d7311;p=mkinit diff --git a/src/mkinit b/src/mkinit index e9948d1..4b39890 100755 --- a/src/mkinit +++ b/src/mkinit @@ -4,149 +4,131 @@ # See ../COPYING for terms # GLobals -CMD=/lib/mkinit/cmd -INITCTL=/dev/initctl +COMMAND=boot +TESTING=false +if [ $$ == 1 ]; then + DAEMON=true +else + DAEMON=false +fi + PATH=/lib/mkinit/bin:/bin:/sbin:/usr/bin:/usr/sbin export PATH # Functions function usage { -cat - < 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 htd -l help,test,daemon -- "$@"` + [ $? != 0 ] && + usage + eval set -- "$TMP" -Options: - -h,--help Print usage information - -r,--relaod Prevent spawning listeners when reloading - -t,--test Fake all commands + # Parse options + while [ ! "$1" == "--" ]; do + case "$1" in + -h|--help ) usage ;; + -t|--test ) TESTING=true ;; + -d|--daemon ) DAEMON=true ;; + esac + shift + done + shift + if [ "$*" ]; then + COMMAND=($@) + fi -Command: - boot Execute boot-up procedures - halt Execute shutdown procedures - reload Re-execute init process - mk Execute mk rule - start|stop|restart|zap|status - Start,stop,restart,zap or query status of service - eval Execute command in mkinit process -EOF -exit + # Debugging output + if $TESTING; then + echo 'Options' 1>&2 + echo ' TESTING:' $TESTING 1>&2 + echo ' DAEMON:' $DAEMON 1>&2 + echo ' COMMAND:' $COMMAND 1>&2 + fi } +# Run mk on the init scripts function runamk { - [ "$TEST" ] && export P=true - /usr/lib/plan9/bin/mk \ + 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" + cmd=$1 shift - args="$@" + echo mkinit -- running "$cmd" "$@" case "$cmd" in boot ) - echo - echo "mkinit -- booting" - runamk -a "$args" + runamk -a "$@" ;; restart ) - if [ "$args" ]; then - echo "mkinit -- restarting $args" - runamk "$args-stop" - runamk "$args-start" + if [ "$*" ]; then + runamk "$@"-stop || + runamk "$@"-zap + runamk "$@"-start fi ;; start|stop|zap|status ) - if [ "$args" ]; then - echo "mkinit -- ${cmd}ing $args" - runamk "$args-$cmd" - fi - ;; - mk|runlevel ) - if [ "$args" ]; then - [ "$cmd" = mk ] && - echo "mkinit -- running mk cmd [$args]" - [ "$cmd" = runlevel ] && - echo "mkinit -- entering runlevel $args" - runamk "$args" + if [ "$*" ]; then + runamk "$@-$cmd" fi ;; reload ) - echo "mkinit -- ${cmd}ing" - exec $0 -r ${TEST:+"-t"} + $TESTING && + opt=-t + exec $0 -r $opt + ;; + poweroff|reboot|kexec|halt) + ( runamk "$cmd" "$@" & ) ;; eval ) - eval $args + eval "$@" ;; ?* ) - echo "mkinit -- unknown command [$cmd $args]" + runamk "$cmd" "$@" ;; esac } -# Handle arguments -TEMP=`getopt -n "$0" \ - --options hrt \ - --longoptions help,reload,test \ - -- "$@"` -[ $? != 0 ] && usage -eval set -- "$TEMP" -while true; do - [ "$TEST" ] && echo "\$1=$1" - case "$1" in - -h|--help ) usage ;; - -r|--reload ) RELOAD=true; shift ;; - -t|--test ) TEST=true; shift ;; - -- ) shift; cmd="$1"; - shift; args="$@"; - break ;; - * ) ;; - esac -done - -# Initial boot-up -process $cmd $args - -if [ "$TEST" ]; then - CMD=/tmp/pipe - echo "Options" - echo " test=$TEST" - echo " reload=$RELOAD" - echo " cmd=$cmd" - echo " args=$args" -fi - -if [ ! "$RELOAD" ]; then - # Fork /dev/initctl listener - ( exec 0<&- 1<&- 2<&- - initctld $INITCTL | - while read line; do - echo $line > $CMD - done ) & - - # Fork console listener - # Readline uses stdin,stderr - ( exec 1<&-; - while true; do - while read -e -p "mkinit> " line; do - echo $line > $CMD - history -s $line - done - [ "$TEST" ] && break - exec 0/dev/console - echo "Respawning on /dev/console.." >&2 - sleep 1 - done) <&0 & - - # Close stdin, stderr - exec 0<&- 2<&- -fi +# Process arguments +doopts "$@" -# Kill listeners on exit -trap "pkill -HUP -P $$ initctld" EXIT +# Run whatever was requested +process "${COMMAND[@]}" -# Main loop -while true; do -while read line; do - process $line -done < $CMD +# Fork console listener +while $DAEMON; do + while read -ep "mkinit> " line; do + process $line + history -s $line + done + $TESTING && exit + exec 0/dev/console 2>&1 + echo "Respawning on /dev/console.." + sleep 1 done