summaryrefslogblamecommitdiffstats
path: root/scripts/ldadp-launcher
blob: be73ec7e591a0326baeb1e5e82e02bc2236c5b2c (plain) (tree)





















































































                                                                                                                     
#!/bin/bash

if [ $# -lt 2 ]; then
	echo "Invalid parameter count: '$#'"
	exit 1
fi

BASE="$1"
shift

if [ -z "$BASE" ] || [ ! -r "$BASE" ] || [ ! -d "$BASE" ]; then
	echo "Basedir invalid: '$BASE'"
	exit 2
fi
cd "$BASE"

[ "x$1" == "x--" ] && shift

# At this point $@ should only be ldadp instance ids

inArray () {
	local e
	for e in "${@:2}"; do
		[[ "x$e" == "x$1" ]] && return 0
	done
	return 1
}

isInstance () {
	if [ $# -ne 1 ]; then
		echo "isInstance needs 1 param, got $#"
		return 0
	fi
	[ -L "/proc/$1/exe" ] && [ -r "/proc/$1/exe" ] && [[ "$(readlink -f "/proc/$1/exe")" == *ldadp ]] && return 0
	return 1
}

launch () {
	local CONFIG="${BASE}/configs/${1}.cfg"
	if [ ! -r "$CONFIG" ]; then
		echo "Told to start ldadp for module '${1}', but no config file found!"
		return 1
	fi
	echo "Launching #$1"
	"${BASE}/ldadp" -n "$CONFIG" &
	local P=$!
	sleep 1
	if ! kill -0 "$P" 2>/dev/null; then
		echo "...FAILED!"
		return 1
	fi
	echo -n "$P" > "${BASE}/pid/${1}"
	return 0
}

for FILE in $(find "$BASE/pid" -type f -regextype posix-extended -regex "(^|.*/)[0-9]+$"); do
	ID=$(basename $FILE)
	PID=$(<$FILE)
	if ! [[ "$PID" =~ ^[0-9]+$ ]]; then
		echo "Invalid PID file: '$FILE'"
		unlink "$FILE"
	fi
	inArray $ID "$@" && isInstance $PID && continue # Should be running, is running, nothing to do
	#
	unlink "$FILE"
	if inArray $ID; then
		# Should be running, is not running, launch!
		launch $ID
	elif isInstance $PID; then
		# Is running, should not, kill
		kill $PID
		sleep 1
		isInstance $PID && kill -9 $PID
	fi
done

while [ $# -gt 0 ]; do
	ID=$1
	shift
	FILE="${BASE}/pid/${ID}"
	[ -r "$FILE" ] && isInstance $(<$FILE) && continue
	launch $ID
done

exit 0