From 3a94d8c357c776c215627f6d07f1baac725e591b Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 31 Jan 2018 20:55:11 +0100 Subject: [PATCH] Now when killed with (SIGINT, SIGTERM, SIGHUP), all will shut down (bound input/output + daemons ...) --- master | 40 ++++++++++++++----- pool | 124 +++++++++++++++++++++++++++++---------------------------- 2 files changed, 95 insertions(+), 69 deletions(-) diff --git a/master b/master index 0c3f0cf..25c77c2 100755 --- a/master +++ b/master @@ -51,26 +51,28 @@ on_exit(){ for logger_name in "${!OUTPUT_PID[@]}"; do # kill each logger OUTPUT - kill -9 ${OUTPUT_PID[$logger_name]} 2>/dev/null; + echo " > killing output@$logger_name"; + kill -INT ${OUTPUT_PID[$logger_name]} 2>/dev/null; done; for logger_name in "${!LOGGER_PID[@]}"; do # kill each logger OUTPUT - kill -9 ${LOGGER_PID[$logger_name]} 2>/dev/null; + echo " > killing logger@$logger_name"; + kill -INT ${LOGGER_PID[$logger_name]} 2>/dev/null; done; echo "killing 'master' output socket"; - kill -9 $MASTER_PID 2>/dev/null; + kill -INT $MASTER_PID 2>/dev/null; echo "killing 'master' loop"; - # kill -9 $LOOP_PID 2>/dev/null; + # kill -INT $LOOP_PID 2>/dev/null; } -trap "on_exit;" INT KILL; +trap "on_exit;" HUP INT KILL TERM; @@ -80,7 +82,7 @@ trap "on_exit;" INT KILL; #--------------------------------------------------------# # (1) Bind-output # echo "(.) connect to $POOL_HOST:$POOL_PORT"; -$ROOT/bind-output master $POOL_HOST $POOL_PORT& +( $ROOT/bind-output master $POOL_HOST $POOL_PORT )& MASTER_PID=$?; @@ -90,6 +92,7 @@ MASTER_PID=$?; PORT="$MIN_PORT"; for logger_name in "${LOGGERS[@]}"; do + echo "(1)"; # Find an available port while true; do @@ -102,28 +105,35 @@ for logger_name in "${LOGGERS[@]}"; do done; # send logger port request + echo "(2)"; echo " (.) Sending logger@$logger_name request"; $ROOT/write master "$POOL_KEY:$logger_name:$LOCAL_IP:$PORT"; + echo "(3)"; # check pool response - RESP="`nc -w 5 -lp $PORT`"; + RESP="`nc -w 2 -lp $PORT`"; + echo "(4)"; # empty response -> ignore test -z "$RESP" && continue; + echo "(5)"; # not a number -> ignore echo -n "$RESP" | grep -P '^\d+$' >/dev/null || continue; + echo "(6)"; # Create output bound echo " (.) Connecting '$logger_name' to $POOL_HOST:$RESP"; - $ROOT/bind-output master_$logger_name $POOL_HOST $RESP& + ( $ROOT/bind-output master_$logger_name $POOL_HOST $RESP )& OUTPUT_PID[$logger_name]=$!; + echo "(7)"; echo " (.) launching logger@$logger_name"; # launch logger bound to output ( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )& LOGGER_PID[$logger_name]=$!; + echo "(8)"; @@ -134,4 +144,16 @@ done; # When all loggers are bound, destroy pool connection -kill -9 $MASTER_PID 2>/dev/null; \ No newline at end of file +# kill -INT $MASTER_PID 2>/dev/null; + +echo "waiting for ${#LOGGER_PID[@]} + ${#OUTPUT_PID[@]} .."; +for logger_name in "${!LOGGER_PID[@]}"; do + + echo "waiting for $logger_name"; + wait ${LOGGER_PID[$logger_name]}; + echo "done with $logger_name"; + +done; + + +# on_exit; \ No newline at end of file diff --git a/pool b/pool index dd9fd8b..e676c82 100755 --- a/pool +++ b/pool @@ -21,44 +21,39 @@ POOL_PORT="$2"; # (5) init logger variables # declare -A INPUT_PID; # will contain each bind-input pid - - - -# (2) Set exit management -#--------------------------------------------------------# -# (1) On-Exit routine # +# (6) Define on-exit routine # +LOOP_PID=""; on_exit(){ - echo "killing 'bind-input' sub processes"; + echo "killing 'pool' loop"; + kill -INT $LOOP_PID 2>/dev/null; + + echo "killing ${#INPUT_PID[@]} background 'bind-input'"; for logger_name in "${!INPUT_PID[@]}"; do # kill each logger input - kill -9 ${INPUT_PID[$logger_name]} 2>/dev/null; - $INPUT_PID[$logger_name]=""; + echo ".killing input@$logger_name (${INPUT_PID[$logger_name]})"; + test ! -z "${INPUT_PID[$logger_name]}" && kill -INT ${INPUT_PID[$logger_name]} 2>/dev/null; done; - echo "killing 'pool' input listener"; - kill -9 $POOL_PID 2>/dev/null; + echo "killing 'pool' input listener ($POOL_PID)"; + kill -INT $POOL_PID 2>/dev/null; - echo "killing 'pool' loop"; - kill -9 $LOOP_PID 2>/dev/null; + exit 1; } -trap "on_exit;" INT KILL; - - -# (3) Launch pool listener +# (2) Launch pool listener #--------------------------------------------------------# # (1) Bind-input # echo "(.) listen $POOL_PORT"; -$ROOT/bind-input pool $POOL_PORT& -POOL_PID=$?; +( $ROOT/bind-input pool $POOL_PORT )& +POOL_PID=$!; @@ -66,58 +61,67 @@ POOL_PID=$?; #--------------------------------------------------------# # infinite listener PORT="$MIN_PORT"; -while sleep 1; do +( - # Listen on port @POOL_PORT - MSG=`$ROOT/read pool`; - - # Do nothing if empty msg or invalid format - test -z "$MSG" && continue; - echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && continue; - - # Extract ID - PORT - KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`"; - LOGGER_NAME="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\2/'`"; - LOGGER_HOST="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\3/'`"; - LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`"; - - # if ID does not match - test "$KEY" != "$POOL_KEY" && echo ' /!\ wrong id' && continue; - - # create new listening socket - echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)"; - - # Find an available port - PORT="`expr $PORT + 1`"; + trap "on_exit;" HUP INT KILL TERM; while true; do - # ignore pool port - test "$PORT" = "$POOL_PORT" && PORT="`expr $PORT + 1`" && continue; + # Listen on port @POOL_PORT + MSG=`$ROOT/read pool`; - # if port not in use -> use it - ss -tl4 "( sport = :$PORT )" | grep "$PORT" >/dev/null 2>&1 || break; + # Do nothing if empty msg or invalid format + test -z "$MSG" && sleep .5 && continue; + echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && sleep .5 && continue; - # else try next port (+1) + # Extract ID - PORT + KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`"; + LOGGER_NAME="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\2/'`"; + LOGGER_HOST="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\3/'`"; + LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`"; + + # if ID does not match + test "$KEY" != "$POOL_KEY" && echo ' /!\ wrong id' && sleep .5 && continue; + + # create new listening socket + echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)"; + + # Find an available port PORT="`expr $PORT + 1`"; + while true; do + + # ignore pool port + test "$PORT" = "$POOL_PORT" && PORT="`expr $PORT + 1`" && continue; + + # if port not in use -> use it + ss -tl4 "( sport = :$PORT )" | grep "$PORT" >/dev/null 2>&1 || break; + + # else try next port (+1) + PORT="`expr $PORT + 1`"; + + done; + + # if already an input for this logger -> kill it + test ! -z "${INPUT_PID[$LOGGER_NAME]}" && kill -INT ${INPUT_PID[$LOGGER_NAME]}; + + # Bind-input for the logger at the free port found + ( $ROOT/bind-input $LOGGER_NAME $PORT )& + + # Store bind-input PID + INPUT_PID["$LOGGER_NAME"]=$!; + + # Notify that input is active to MASTER + bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;"; + done; - # Bind-input for the logger at the free port found - $ROOT/bind-input $LOGGER_NAME $PORT& - - # if already an input for this logger -> kill it - test ! -z "${INPUT_PID[$LOGGER_NAME]}" && kill -9 ${INPUT_PID[$LOGGER_NAME]}; - - # Store bind-input PID - INPUT_PID[LOGGER_NAME]=$!; - - # Notify that input is active to MASTER - bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;"; - -done& - +)& LOOP_PID=$!; +# (3) Set exit management +#--------------------------------------------------------# +trap "kill -INT $LOOP_PID;" HUP INT KILL TERM; + wait $LOOP_PID;