From b3a318886407c98b3e91df9173c2d9c2cf195de3 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 20 Sep 2011 15:09:19 +0200 Subject: //hyper spawn: use well known //proc dir this commit also spawns the command in the current $PWD so spawn ls & co. are viable commands. this commit also sets up a trap for TERM, so dash behaves nicely, again. also we now've got more documentation and debug output [if called with sh -x]. --- hyper/process/spawn | 58 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 13 deletions(-) (limited to 'hyper') diff --git a/hyper/process/spawn b/hyper/process/spawn index c2b829ca..95854c7d 100755 --- a/hyper/process/spawn +++ b/hyper/process/spawn @@ -1,34 +1,61 @@ #! /bin/sh # -# spawn [command [argument ...]] +# [sh -x] spawn [command [argument ...]] +# +# export id to create&destroy or reuse the working directory //proc/$id/. +# this feature is for debug only and marked as deprecated, so don't rely +# on it too hard. # spawn() { set -euf - # create and change working directory - wd=`mktemp -d` - defer rmdir $wd + # establish working subdirectory in //proc. we're mking only + # transient dirs, i.e. if we mkdir, then we also defer rmdir. + if test -n "${id-}"; then + : "using id=$id from env" + wd=$pd/$id + if ! test -d $wd; then + : "make transient $wd/" + mkdir $wd + defer rmdir $wd + elif ! test `ls $wd | wc -l` = 0; then + : "$wd/ is not empty!" + exit 23 + else + : "reuse existing $wd/" + fi + else + id=`cd $pd && mktemp -d XXXXXXXXXXXXXXXX` + wd=$pd/$id + defer rmdir $wd + : "made transient $wd/" + fi + + # change to //proc working directory + cwd="$PWD" cd $wd + #defer cd $cwd # no need for this, b/c at that time we're aldeady dead # create named pipes for the child process's stdio mkfifo 0 1 2 defer rm 0 1 2 # spawn child process - (exec 0>&- 1>&- 2>&- 0<>0 1<>1 2<>2 "$@") & + ( : "in $PWD/ spawn ${*-"nothing"}" + exec 0>&- 1>&- 2>&- 0<>0 1<>1 2<>2 + cd "$cwd" + exec "$@") & pid=$! # setup a trap to kill the child process if this (parent) process dies defer kill $pid - # write child process's pid + # store misc. info. + ln -snf $cwd cwd + echo $id >id + echo $$ >ppid echo $pid >pid - defer rm pid - - # create dummy directory for easier debugging - mkdir -vp /tmp/dummy - ln -vsnf $wd/0 $wd/1 $wd/2 $wd/pid /tmp/dummy/ - defer rm -v /tmp/dummy/0 /tmp/dummy/1 /tmp/dummy/2 /tmp/dummy/pid + defer rm cwd id pid ppid # wait for the child process's set +e @@ -65,6 +92,11 @@ cancel() { defer="`echo "$defer" | grep -Fxv "$*"`" } +# setup //proc directory +pd=/tmp/krebs/proc +mkdir -p $pd +test -w $pd + # setup deferred execution and spawn command -trap 'eval "${defer-}"; defer=' EXIT INT +trap 'eval "${defer-}"; defer=' EXIT INT TERM spawn "$@" -- cgit v1.2.3