#!/bin/sh # # run init scripts action=$1 direction=$(test "$1" = "start" -o "$1" = "resume" && echo "start" || echo "stop") name=$2 path=/var/spool/apm.init process() { test -e $path/pre/$action/$name && call $path/pre/$action/$name if [ -n "$nobg" ]; then /etc/init.d/$name $action else echo -e "$( (/etc/init.d/$name $action) 2>&1 )" fi rm -f $path/pid/$(echo -e "start stop\nstop start\nresume halt\nhalt resume" | grep "^$action" | cut -d " " -f2)/$name } call() ( names="" for name in $(cat $1); do if [ -e /etc/init.d/$name -a ! -e $path/pid/$action/$name ]; then names="$names $name" touch $path/pid/$action/$name fi done for name in $names; do echo -ne "$name: \r" if [ -n "$nobg" ]; then process else process & echo $! > $path/pid/$action/$name fi done for name in $(cat $1); do i=0 test -s $path/pid/$action/$name && while [ $i -lt 0${wait-60} -a -e /proc/$(cat $path/pid/$action/$name 2>/dev/null) ]; do i=$(($i+1)); sleep 1 "#waiting for /etc/init.d/$name"; done; if [ $i -eq 0${wait-60} ]; then echo "$name seems to hang! continue now..." echo -n > $path/pid/$action/$name fi done ) init() ( rm -rf $path/pre $path/order if [ -z "$name" ]; then rm -rf $path/pid/halt $path/pid/resume test "$action" = "start" && rm -rf $path/pid fi for a in start stop resume halt; do mkdir -p $path/order/$a $path/pre/$a $path/pid/$a; done find /etc/init.d -maxdepth 1 -type f ! -name "rc.*" -printf "%f\n" | while read name; do ( . /etc/init.d/$name init if [ -n "$start" ]; then test -n "$before" && echo $name | tee $(echo "$before" | sed "s#\(\S\+\)#-a $path/pre/start/\1#g") >/dev/null test -n "$after" && echo $after >> $path/pre/start/$name echo $name >> $path/order/start/${priority:-0} fi if [ -n "$resume" ]; then test -n "$before" && echo $name | tee $(echo "$before" | sed "s#\(\S\+\)#-a $path/pre/resume/\1#g") >/dev/null test -n "$after" && echo $after >> $path/pre/resume/$name echo $name >> $path/order/resume/${priority:-0} fi if [ -n "$stop" ]; then test -n "$before" && echo $before >> $path/pre/stop/$name test -n "$after" && echo $name | tee $(echo "$after" | sed "s#\(\S\+\)#-a $path/pre/stop/\1#g") >/dev/null echo $name >> $path/order/stop/${priority:-0} fi if [ -n "$halt" ]; then test -n "$before" && echo $before >> $path/pre/halt/$name test -n "$after" && echo $name | tee $(echo "$after" | sed "s#\(\S\+\)#-a $path/pre/halt/\1#g") >/dev/null echo $name >> $path/order/halt/${priority:-0} fi ) done ) init if [ -z "$name" ]; then export init=1 ls -1 $path/order/$action | sort $(test "$direction" = "start" && echo "-r") | while read priority; do call $path/order/$action/$priority done else rm -f $path/pid/$action/$name echo "$name" > $path/call export nobg=1 call $path/call fi