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