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