From: Andy Spencer Date: Fri, 16 Oct 2009 02:07:12 +0000 (+0000) Subject: rcifying things, (rc version of mkinit is broken) X-Git-Url: http://pileus.org/git/?p=mkinit;a=commitdiff_plain;h=06d69826e46160c370293857f9bc30420c8d91da rcifying things, (rc version of mkinit is broken) --- diff --git a/src/mkinit.rc b/src/mkinit.rc new file mode 100755 index 0000000..4a6116e --- /dev/null +++ b/src/mkinit.rc @@ -0,0 +1,174 @@ +#!/usr/lib/plan9/bin/rc + +# Copyright (C) 2009 Andy Spencer +# See ../COPYING for terms + +# GLobals +TEST=false +RELOAD=false +FIFO=/lib/mkinit/cmd +INITCTL=/dev/initctl +PATH=/lib/mkinit/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/lib/plan9/bin + +# Functions +fn usage { + echo 'usage: '$0' [options] [command]' + echo + echo 'Options:' + echo ' -h,--help Print usage information' + echo ' -r,--relaod Prevent spawning listeners when reloading' + echo ' -t,--test Fake all commands' + 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 +} + +# Fake readline +fn prompt { + echo -n $1 >[1=2] + read +} + +# Run mk on the init scripts +fn runamk { + if ($TEST) + P=true + /usr/lib/plan9/bin/mk \ + -f /etc/init.mk \ + -i -k $* +} + +# Process one command +fn process { + cmd=$1 + shift + arg=$* + switch($cmd){ + case boot + echo + echo mkinit -- booting + runamk -a $arg + + case restart + if(~ $arg ?*){ + echo mkinit -- restarting $arg + runamk $arg-stop + runamk $arg-start + } + + case start stop zap status + if(~ $arg ?*){ + echo mkinit -- $cmd^ing $arg + runamk $arg-$cmd + } + + case mk runlevel + if(~ $arg ?*){ + if (~ $cmd mk) + echo mkinit -- running mk cmd [$arg] + if (~ $cmd runlevel) + echo mkinit -- entering runlevel $arg + runamk $arg + } + + case reload + echo mkinit -- $cmd^ing + if($TEST) + opt=-t + exec $0 -r $opt + + case eval + eval $arg + + case ?* + echo mkinit -- unknown command [$cmd] [$arg] + } +} + +# Handle arguments +tmp=`{getopt -n $0 \ + --options hrt \ + --longoptions help,reload,test \ + -- $*} +if(~ $status ?*) + usage +eval '*=('$"tmp')' +while(! ~ $1 --){ + if ($TEST) + echo '$1=' $1 + switch($1){ + case -h --help + usage + + case -r --reload + RELOAD=true + + case -t --test + TEST=true + FIFO=/tmp/pipe + INITCTL=/tmp/initctl + fn sigint { pkill -HUP -P $pid } + + } + shift +} +shift; cmd=$1 +shift; arg=$* + +# Debugging output +if($TEST){ + echo 'Options' + echo ' test:' $TEST + echo ' reload:' $RELOAD + echo ' cmd:' $cmd + echo ' arg:' $arg +} + +# Create fifos if they don't exist +if(test ! -e $FIFO) + mkfifo $FIFO +if(test ! -e $INITCTL) + mkfifo $INITCTL + +# Initial boot-up +process $cmd $arg + +# Fork listeners +if(!$RELOAD){ + # Fork /dev/initctl listener + { initctld $INITCTL | + while(line=`{read}) + echo $line > $FIFO + } <[0=] >[1=] >[2=] & + + # Fork console listener + # Readline uses stdin,stderr + if($TEST) { + tty=`{tty} + { while(line=`{prompt 'mkinit> '}) { + echo line: $line + echo $line > $FIFO + } } <$"tty >[1=2] & + } + if not { + { while(true){ + while(line=`{prompt 'mkinit> '}) + echo $line > $FIFO + echo Respawning on /dev/console.. + sleep 1 + } } /dev/console >[1=2] + } +} + +# Main loop +while(true) + { while(line=`{read}) + process $line + } < $FIFO >[2=1] diff --git a/src/respawn b/src/respawn index 9917c44..11a677a 100755 --- a/src/respawn +++ b/src/respawn @@ -1,9 +1,10 @@ -#!/bin/sh +#!/usr/lib/plan9/bin/rc # Copyright (C) 2009 Andy Spencer # See ../COPYING for terms -while (true); do - "$@" +while(true){ + $* echo Process $! died, waiting 1 second to respawn -done + sleep 1 +} diff --git a/src/service b/src/service index d4b7a7d..b5ca605 100755 --- a/src/service +++ b/src/service @@ -1,41 +1,42 @@ -#!/bin/bash +#!/usr/lib/plan9/bin/rc # Copyright (C) 2009 Andy Spencer # See ../COPYING for terms -function usage { -cat - < [start-|stop-] -Action: - -u,--is-up Test is service is running - -d,--is-down Test is service is stopped - -U,--set-up Set service to running - -D,--set-down Set service to stopped - -q,--query Print the state of a service -EOF -exit +fn usage { + echo 'usage: '$0' [start-|stop-]' + echo 'Action:' + echo ' -u,--is-up Test is service is running' + echo ' -d,--is-down Test is service is stopped' + echo ' -U,--set-up Set service to running' + echo ' -D,--set-down Set service to stopped' + echo ' -q,--query Print the state of a service' + exit } # Main STATE=/lib/mkinit/state -action="$1" -service="${2/-*}" +action=$1 +service=`{echo $2 | sed s/-.*//} -[ "$action" -a "$service" ] || usage +if(~ $action '' || ~ $service '') + usage -# echo "$0 $@" - -case "$1" in - -u|--is-up ) test -e "$STATE/$service" ;; - -d|--is-down ) test ! -e "$STATE/$service" ;; - -U|--set-up ) touch "$STATE/$service" ;; - -D|--set-down ) rm "$STATE/$service" ;; - -q|--query ) - echo -n "Service \`$service' is " - test -e "$STATE/$service" \ - && echo running \ - || echo stopped - ;; - * ) usage ;; -esac +switch($1){ +case -u --is-up + test -e $STATE/$service +case -d --is-down + test ! -e $STATE/$service +case -U --set-up + touch $STATE/$service +case -D --set-down + rm $STATE/$service +case -q --query + echo -n Service "$service" is + test -e $STATE/$service \ + && echo ' running' \ + || echo ' stopped' +case * + usage +}