summaryrefslogtreecommitdiff
path: root/xpp/astribank_hook
blob: 074ea310cef7509fdfefc56e6019b25f173acd69 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /bin/sh

me=`basename $0`
dir=`dirname $0`
LOGGER="logger -i -t '$me'"

# Always redirect stderr somewhere, otherwise the shell script will die
# when it tries to do I/O related stuff on closed file descriptor.
# Our default is to throw it down the bit-bucket.
#exec 2> /dev/console
## If you wish to trace this script:
#exec 2> "/tmp/${me}_$XBUS_NAME" 1>&2

# Our directory in the beginning, so we can use local lab setup
PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"

set -e

[ -r /etc/dahdi/init.conf ] && . /etc/dahdi/init.conf

# For lab testing
liveconf="$dir/liveconf/dahdi"

if [ -d "$liveconf" ]; then
	dahdi_conf="$liveconf"
else
	dahdi_conf="/etc/dahdi"
fi

if [ "$XPP_HOTPLUG_DAHDI" != yes ]; then
	exit 0
fi

export XPPORDER_CONF="$dahdi_conf/xpp_order"
if [ ! -r "$XPPORDER_CONF" ]; then
	(
		echo "Skip($ACTION): No '$XPPORDER_CONF'"
		echo "Removing uneeded startup semaphore"
		astribank_is_starting -v -r 2>&1
	) 2>&1 | $LOGGER
	exit 0
fi
export DAHDI_CFG_CMD="dahdi_cfg -c $dahdi_conf/system.conf"
export CALLED_FROM_ATRIBANK_HOOK=yes

clean_lines() {
	sed -e 's/#.*//' -e 'y/\t/ /' -e 's/^ *//' -e 's/ *$//' -e '$s/$/\n/' "$XPPORDER_CONF"
}

matched_devices() {
	ready=`grep -H READY /sys/bus/astribanks/devices/*/xbus_state | sed 's,/xbus_state.*,,'`
	for dev in $ready
	do
		label=`cat "$dev/label"`
		connector=`cat "$dev/connector"`
		xbus=`echo "$dev" | sed 's,.*/,,'`
		lineno=`clean_lines | egrep -n "^${label}$|^@${connector}$" | cut -d: -f1`
		if [ "$lineno" != "" ]; then
			#echo "$xbus: $XPPORDER_CONF:$lineno -- Match ${label} @${connector}" | $LOGGER
			printf "${xbus}\t${label}\n"
		else
			echo "${xbus}: ${label} @${connector} not found in $XPPORDER_CONF: Ignore($ACTION)" | $LOGGER
		fi
	done
}

NUM_WANTED=`clean_lines | sed '/^$/d' | wc -l`
NUM_GOOD=`matched_devices | wc -l`
LOCK="/var/lock/twinstar_startup"

start_dahdi() {
	script=/etc/init.d/dahdi
	echo "Starting $script."
	"$script" start | logger -i -t "$script"
	status=$?
	echo "Status($script): $status"
	if [ -x "$dir/twinstar_hook" ]; then
		"$dir/twinstar_hook"
	fi
	# Finished astribanks
	echo "Removing semaphore"
	astribank_is_starting -v -r
	rm -f "$LOCK"
}

#echo "$0: $ACTION($XBUS_NAME)" | $LOGGER
case "$ACTION" in
add)
	;;
remove)
	;;
online)
	echo "$ACTION($XBUS_NAME): $NUM_GOOD/$NUM_WANTED from $XPPORDER_CONF" | $LOGGER
	if [ "$NUM_GOOD" -eq "$NUM_WANTED" ]; then
		(
			# Delay the initialization of the Astribank until the filesystem
			# is mounted read-write:
			test_file="/var/lock/astribank_test_file"
			for i in `seq 1 20`; do
				if touch $test_file 2> /dev/null; then
					rm -f $test_file
					break
				else
					echo "$0: [$i] - Failed writing '$test_file'...waiting" | $LOGGER
					sleep 1;
				fi
		        done

			if ln -s "$XBUS_NAME" "$LOCK"; then
				echo "START-DAHDI: Total $NUM_GOOD online." | $LOGGER
				# Fork services
				start_dahdi < /dev/null 2>&1 | $LOGGER
			else
				echo "$0: Was started: $(ls -l $LOCK)" | $LOGGER
			fi
		) < /dev/null 2>&1 | $LOGGER &
	fi
	;;
offline)
	echo "$ACTION($XBUS_NAME): $NUM_GOOD/$NUM_WANTED from $XPPORDER_CONF" | $LOGGER
	if [ "$NUM_GOOD" -eq 0 ]; then
		echo "All Astribanks offline" | $LOGGER
		if [ -x "$dir/twinstar_hook" ]; then
			"$dir/twinstar_hook" || :
		fi
		rm -f "$LOCK"
	fi
	;;
*)
	echo "$0: Unknown ACTION='$ACTION'" | $LOGGER
	echo "$0: ARGS='$*'" | $LOGGER
	echo "$0: ENV:" | $LOGGER
	env | $LOGGER
	exit 1
esac