freeswitch-stable: import 1.8.2 from master
[feed/telephony.git] / net / freeswitch-stable / files / freeswitch.hotplug
1 #!/bin/sh
2
3 NAME=freeswitch
4 COMMAND=/etc/init.d/$NAME
5
6 LOGGER="/usr/bin/logger -t $NAME-hotplug"
7 LOG_ERR="$LOGGER -p user.err --"
8 LOG_NOTICE="$LOGGER -p user.notice --"
9
10 [ "$ACTION" = ifup ] || exit 0
11
12 . /lib/functions.sh
13 config_load $NAME
14
15 config_get interface hotplug interface
16
17 [ "$INTERFACE" = "$interface" ] || exit 0
18
19 pidof $NAME &> /dev/null
20 if [ $? -eq 0 ]; then
21 $LOG_NOTICE stopping $NAME
22 $COMMAND stop &> /dev/null
23 fi
24
25 config_get timeout hotplug timeout 60
26
27 [ "$timeout" -gt 0 ] 2> /dev/null || unset timeout
28 timeout="${timeout:-60}"
29
30 config_get mount_point hotplug mount_point
31
32 # Mount condition, idea lifted from OpenWrt Wiki
33 [ -n "$mount_point" ] && {
34
35 if ! [ -d "$mount_point" ]; then
36 $LOG_ERR "$mount_point" not a valid mount point
37 exit 1
38 fi
39
40 mnt="$mount_point"
41 notReady=start
42 tmp_timeout=$timeout
43
44 while [ -n "$notReady" -a $tmp_timeout -gt 0 ]; do
45 if [ "$notReady" != start ]; then
46 $LOG_NOTICE "$mnt" not yet mounted, timeout in $tmp_timeout s
47 sleep 5
48 tmp_timeout=$(($tmp_timeout-5))
49 fi
50
51 notReady=
52
53 result=$(cat /proc/mounts | awk '{print $2}' | grep "^$mnt\$")
54 if [ -z "$result" ]; then
55 notReady="$mnt not ready yet"
56 fi
57 done
58
59 if [ -n "$notReady" ]; then
60 $LOG_ERR "$mnt" still not mounted
61 $LOG_ERR not starting $NAME
62 exit 1
63 else
64 $LOG_NOTICE "$mnt" mounted
65 fi
66
67 }
68
69 config_get_bool ntpd hotplug ntpd 0
70
71 # ntpd condition
72 [ $ntpd -eq 1 ] && {
73
74 type ntpq &> /dev/null
75 [ $? -eq 0 ] || {
76 $LOG_ERR ntpq utility not found
77 exit 1
78 }
79
80 pidof ntpd &> /dev/null || {
81 $LOG_ERR ntpd not running
82 exit 1
83 }
84
85 notReady=start
86 tmp_timeout=$timeout
87
88 while [ -n "$notReady" -a $tmp_timeout -gt 0 ]; do
89 if [ "$notReady" != start ]; then
90 $LOG_NOTICE system time not in sync yet, timeout in $tmp_timeout s
91 sleep 5
92 tmp_timeout=$(($tmp_timeout-5))
93 fi
94
95 notReady=
96
97 result=$(ntpq -c 'timeout 300' -c 'rv 0 stratum' 2> /dev/null | \
98 awk -F '=' '{print $2}' | grep -o -E '^[0-9]+')
99 if [ -z $result ]; then
100 $LOG_ERR failed to extract stratum from ntpd
101 notReady="unable to extract stratum"
102 else
103 $LOG_NOTICE ntpd stratum $result
104 if [ $result -lt 16 ] 2> /dev/null; then
105 result=$(ntpq -c 'timeout 300' -c 'rv 0 offset' 2> /dev/null \
106 | awk -F '=' '{print $2}' | grep -o -E '^-?[0-9]+')
107 if [ -z $result ]; then
108 $LOG_ERR failed to extract offset from ntpd
109 notReady="unable to extract offset"
110 else
111 # "-0" looks stupid, so remove "-"
112 result=$(echo $result | grep -o '[0-9]*')
113 $LOG_NOTICE ntpd to system time offset \+\/\- $result ms
114 # If offset < 100 ms consider system time in sync
115 [ $result -lt 100 ] || notReady="system time not in sync yet"
116 fi
117 else
118 notReady="ntpd not in sync yet"
119 fi
120 fi
121 done
122
123 if [ -n "$notReady" ]; then
124 $LOG_ERR system time still not in sync
125 $LOG_ERR not starting $NAME
126 exit 1
127 else
128 $LOG_NOTICE system time in sync
129 fi
130
131 }
132
133 $COMMAND start &> /dev/null
134 sleep 1
135 pidof $NAME &>/dev/null
136 if [ $? -eq 0 ]; then
137 $LOG_NOTICE started $NAME due to \"ifup "$INTERFACE"\" event
138 else
139 $LOG_ERR start of $NAME due to \"ifup "$INTERFACE"\" event failed
140 exit 1
141 fi