summaryrefslogtreecommitdiffstats
path: root/net/freeswitch/files/freeswitch.hotplug
blob: 93b5a20eec9e65a5bdd37cd1db3c8fa8cb524703 (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
137
138
139
140
141
142
#!/bin/sh

NAME=freeswitch
COMMAND=/etc/init.d/$NAME

LOGGER="/usr/bin/logger -t $NAME-hotplug"
LOG_ERR="$LOGGER -p daemon.err --"
LOG_INFO="$LOGGER -p daemon.info --"
LOG_WARN="$LOGGER -p daemon.warn --"

[ "$ACTION" = ifup ] || exit 0

. /lib/functions.sh
config_load $NAME

config_get interface hotplug interface

[ "$INTERFACE" = "$interface" ] || exit 0

pidof $NAME &> /dev/null
if [ $? -eq 0 ]; then
  $LOG_INFO stopping $NAME
  $COMMAND stop &> /dev/null
fi

config_get timeout hotplug timeout 60

[ "$timeout" -gt 0 ] 2> /dev/null || unset timeout
timeout="${timeout:-60}"

config_get mount_point hotplug mount_point

# Mount condition, idea lifted from OpenWrt Wiki
[ -n "$mount_point" ] && {

  if ! [ -d "$mount_point" ]; then
    $LOG_ERR "$mount_point" not a valid mount point
    exit 1
  fi

  mnt="$mount_point"
  notReady=start
  tmp_timeout=$timeout

  while [ -n "$notReady" -a $tmp_timeout -gt 0 ]; do
    if [ "$notReady" != start ]; then
      $LOG_INFO "$mnt" not yet mounted, timeout in $tmp_timeout s
      sleep 5
      tmp_timeout=$(($tmp_timeout-5))
    fi

    notReady=

    result=$(cat /proc/mounts | awk '{print $2}' | grep "^$mnt\$")
    if [ -z "$result" ]; then
      notReady="$mnt not ready yet"
    fi
  done

  if [ -n "$notReady" ]; then
    $LOG_ERR "$mnt" still not mounted
    $LOG_ERR not starting $NAME
    exit 1
  else
    $LOG_INFO "$mnt" mounted
  fi

}

config_get_bool ntpd hotplug ntpd 0

# ntpd condition
[ $ntpd -eq 1 ] && {

  type ntpq &> /dev/null
  [ $? -eq 0 ] || {
    $LOG_ERR ntpq utility not found
    exit 1
  }

  pidof ntpd &> /dev/null || {
    $LOG_ERR ntpd not running
    exit 1
  }

  notReady=start
  tmp_timeout=$timeout

  while [ -n "$notReady" -a $tmp_timeout -gt 0 ]; do
    if [ "$notReady" != start ]; then
      $LOG_INFO system time not in sync yet, timeout in $tmp_timeout s
      sleep 5
      tmp_timeout=$(($tmp_timeout-5))
    fi

    notReady=

    result=$(ntpq -c 'timeout 300' -c 'rv 0 stratum' 2> /dev/null | \
                    awk -F '=' '{print $2}' | grep -o -E '^[0-9]+')
    if [ -z $result ]; then
      $LOG_WARN failed to extract stratum from ntpd
      notReady="unable to extract stratum"
    else
      $LOG_INFO ntpd stratum $result
      if [ $result -lt 16 ] 2> /dev/null; then
        result=$(ntpq -c 'timeout 300' -c 'rv 0 offset' 2> /dev/null \
                 | awk -F '=' '{print $2}' | grep -o -E '^[-+]?[0-9]+')
        if [ -z $result ]; then
          $LOG_WARN failed to extract offset from ntpd
          notReady="unable to extract offset"
        else
          # "-0" looks stupid, so remove "-"
          result=$(echo $result | grep -o '[0-9]*')
          $LOG_INFO ntpd to system time offset \+\/\- $result ms
          # If offset < 100 ms consider system time in sync
          [ $result -lt 100 ] || notReady="system time not in sync yet"
        fi
      else
        notReady="ntpd not in sync yet"
      fi
    fi
  done

  if [ -n "$notReady" ]; then
    $LOG_ERR system time still not in sync
    $LOG_ERR not starting $NAME
    exit 1
  else
    $LOG_INFO system time in sync
  fi

}

$COMMAND start &> /dev/null
sleep 1
pidof $NAME &>/dev/null
if [ $? -eq 0 ]; then
  $LOG_INFO started $NAME due to \"ifup "$INTERFACE"\" event
else
  $LOG_ERR start of $NAME due to \"ifup "$INTERFACE"\" event failed
  exit 1
fi