X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fmkinit;h=4d8c5899351c254a3ead31b614a5603ef8c2b3b2;hb=9dbffaa9fcf78a81968865e371abfb978a8b8168;hp=581e66214606e6395daeb3ca93bb0062be7239a3;hpb=3928288d9e80e2ff76e7e5d83678b92c4d275c3c;p=mkinit diff --git a/src/mkinit b/src/mkinit index 581e662..4d8c589 100755 --- a/src/mkinit +++ b/src/mkinit @@ -1,111 +1,134 @@ -#!/bin/sh +#!/bin/bash # Copyright (C) 2009 Andy Spencer # See ../COPYING for terms +# GLobals +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 - -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 + if $TESTING; then + export P=echo + export MKINIT_STATE=/tmp/mkinit_state + mkdir -p $MKINIT_STATE + fi /usr/lib/plan9/bin/mk \ -f /etc/init.mk \ -i -k "$@" } -# Handle arguments -TEMP=`getopt -n "$0" \ - --options th \ - --longoptions test,help \ - -- "$@"` -[ $? != 0 ] && usage -eval set -- "$TEMP" -while true; do - [ "$TEST" ] && echo "\$1=$1" - case "$1" in - -h|--help ) usage ;; - -t|--test ) TEST=true; shift ;; - -- ) shift; cmd="$1"; - shift; args="$@"; - break ;; - * ) ;; - esac -done -if [ "$TEST" ]; then - echo "Options" - echo " test=$TEST" - echo " cmd=$cmd" - echo " args=$args" -fi - -# Main loop -while true; do +# Process one command +function process { + cmd=$1 + shift + echo mkinit -- running "$cmd" "$@" case "$cmd" in boot ) - echo - echo "mkinit -- booting" - if runamk -a "$args" && ! [ "$TEST" ]; then - # booted successuflly, redirect input - echo "skipping redirect" - #pipe=/lib/mkinit/cmd - #[ -p pipe ] || mkfifo $pipe - #exec 0< $pipe - fi - ;; - halt|reboot|poweroff ) - echo "mkinit -- ${cmd}ing" - runamk "halt" - if [ "$args" -o "$cmd" = reboot -o "$cmd" = poweroff ]; then - # mk handles syncing and logging message - ${TEST:+echo} $cmd -ndf $args - fi + runamk -a "$@" ;; restart ) - if [ "$args" ]; then - echo "mkinit -- restarting $args" - runamk "stop-$args" - runamk "start-$args" + if [ "$*" ]; then + runamk "$@"-stop || + runamk "$@"-zap + runamk "$@"-start fi ;; start|stop|zap|status ) - if [ "$args" ]; then - echo "mkinit -- ${cmd}ing $args" - runamk "${cmd}-$args" - fi - ;; - mk ) - if [ "$args" ]; then - echo "mkinit -- running mk cmd [$args]" - runamk "$args" + if [ "$*" ]; then + runamk "$@-$cmd" fi ;; reload ) - exec /sbin/mkinit ${TEST:+"-t"} + $TESTING && + opt=-t + exec $0 -r $opt + ;; + poweroff|reboot|kexec|halt) + ( runamk "$cmd" "$@" & ) ;; eval ) - $args + eval "$@" ;; ?* ) - echo "unknown command [$cmd $args]" + runamk "$cmd" "$@" ;; esac - read -e -p "mkinit> " cmd args - history -s $cmd $args +} + +# Process arguments +doopts "$@" + +# Run whatever was requested +process "${COMMAND[@]}" + +# 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