dahdi_waitfor_span_assignments 1.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#! /bin/sh

usage() {
	echo >&2 "Usage: $0 {assigned|unassigned}"
	echo >&2 "# wait until all spans known are assigned/unassigned"
	exit 1
}

TIMEOUT=5	# How much time to wait for spans

if [ "$#" -lt 1 ]; then
	usage
fi
wanted_event="$1"
shift

case "$wanted_event" in
assigned)
	;;
unassigned)
	;;
*)
	usage
	;;
esac

devbase='/sys/bus/dahdi_devices/devices'

spans_of() {
	dev="$1"
	wc -l < "$dev/spantype"
}

assigned_spans_of() {
	dev="$1"
	ls -d "$dev/span-"* 2>/dev/null | wc -l
}


waitfor_span_assignments() {
	wanted_state="$1"
	device_list=`ls -d "$devbase/"* 2> /dev/null`
	finished=''
	count="$TIMEOUT"
	echo -n "Waiting for spans to become $wanted_state: "
	while [ "$count" -gt 0 ]; do
		finished='yes'
		for dev in $device_list
		do
			spans=`spans_of "$dev"`
			assigned_spans=`assigned_spans_of "$dev"`
			if [ "$wanted_state" = 'assigned' -a "$assigned_spans" -ne "$spans" ]; then
				finished='no'
			elif [ "$wanted_state" = 'unassigned' -a "$assigned_spans" -ne 0 ]; then
				finished='no'
			fi
		done
		if [ "$finished" = 'yes' ]; then
			break
		else
			sleep 1
			echo -n "."
		fi
		count=`expr "$count" - 1`
	done
	if [ "$finished" = 'yes' ]; then
		echo "done"
	else
		echo "timeout"
	fi
}

waitfor_span_assignments "$wanted_event"