Guest User

Untitled

a guest
Jun 2nd, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #!/bin/sh /etc/rc.common
  2.  
  3. # SVN: $Id: cmh-ra 13196 2014-11-28 12:58:54Z florin $
  4.  
  5. # Copyright (C) 2008 Mi Casa Verde, Inc., a Nevada Corporation
  6. # www.micasaverde.com
  7. # 1 - 702 - 4879770 / 866 - 966 - casa
  8. # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License.
  9. # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10. # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11.  
  12. START=997
  13. STOP=43
  14.  
  15. #set -x
  16.  
  17. INIT_SCRIPT=0
  18.  
  19. init_script()
  20. {
  21. [ $INIT_SCRIPT -eq 1 ] && return
  22.  
  23. # Skip it if we're on batteries.
  24. if [ -f /tmp/.running_on_batteries ]; then
  25. echo "Running on batteries. Skipping..."
  26. exit 0
  27. fi
  28.  
  29. MIOS_CONSTANTS_FILE="$(dirname $0)/../../mios_constants.sh"
  30. [ -f "$MIOS_CONSTANTS_FILE" ] && source "$MIOS_CONSTANTS_FILE" || echo "FILE-NOT-FOUND: mios_constants.sh"
  31.  
  32. log_to="both"; log_file="$MIOS_LOGS_PATH/log.cmh-ra"; log_as="cmh-ra"
  33. [ -f "$MIOS_FUNCTIONS_FILE" ] && source "$MIOS_FUNCTIONS_FILE" || echo "FILE-NOT-FOUND: functions.sh"
  34.  
  35. export Ppid=$PPID
  36. export PPpid=$(awk '/^PPid:/ {print $2}' /proc/$Ppid/status)
  37. get_app_name_from_pid() {
  38. [ -n "$1" ] && cat /proc/$1/cmdline | tr '\0' ' '
  39. }
  40.  
  41. log_START "$log_as $* from $(get_app_name_from_pid $Ppid)[$Ppid] with parent $(get_app_name_from_pid $PPpid)[$PPpid]"
  42.  
  43. get_lock -t 1 "cmh-ra" || exit # Don't wait for the other cmh-ra process to stop, exit (almost) immediately.
  44.  
  45. FACTORY_TEST_REQUIRED_FILE="$MIOS_TMP_PATH/.factory_test_required"
  46. [ -f "$FACTORY_TEST_REQUIRED_FILE" ] && log_DIE "Unit has not passed factory testing."
  47.  
  48. INIT_SCRIPT=1
  49. }
  50.  
  51.  
  52. get_ra_port()
  53. {
  54. [ -f "$MIOS_FUNCTIONS_REST_FILE" ] && source "$MIOS_FUNCTIONS_REST_FILE" || echo "FILE-NOT-FOUND: functions_rest.sh"
  55.  
  56. log "Getting cmh-ra port"
  57. local restOutput
  58. restOutput=$(MiOSRestApi.sh GetRaPort -p Type=$PORT_TYPE_CMH_RA -p CheckRAPortSession=1 -rs -rk)
  59. local restExitCode=$?
  60. local restResponseFile=$(echo $restOutput | awk '{ print $1 }')
  61.  
  62. if [ -s "$restResponseFile" ]; then
  63. local restResponse=$(cat "$restResponseFile" 2>/dev/null)
  64. rm -f $restResponseFile
  65. fi
  66.  
  67. if [ $restExitCode -ne 0 ]; then
  68. log_DIE "Failed to get cmh-ra port, reason: $restResponse, MiOSRestApi exit code: $restExitCode"
  69. fi
  70.  
  71. RelayServer=$(echo $restOutput | awk '{ print $2 }')
  72. [ -z "$RelayServer" ] && log_DIE "Server not found in output: $restOutput."
  73. MmsSession=$(echo $restOutput | awk '{ print $3 }')
  74. [ -z "$MmsSession" ] && log_DIE "Session key not found in output: $restOutput."
  75.  
  76. RaPort=$(rest_get_param_json Port "$restResponse")
  77. if [ -z "$RaPort" ] || [ -n "${RaPort//[0-9]/}" ] || [ $RaPort -gt 65535 ] || [ $RaPort -lt 1 ]; then
  78. log_DIE "Got invalid port: $restResponse"
  79. fi
  80. log_SUCCESS "Got port: $RaPort"
  81. }
  82.  
  83.  
  84. get_ssh_pids()
  85. {
  86. local pids
  87. local sshPids=$(ps ax | grep -v "grep" | grep "ssh" | awk '{ print $1 }')
  88. log "Found running ssh pids: $sshPids"
  89. local sshPpid
  90. for sshPid in $sshPids; do
  91. sshPpid=$(cat /proc/$sshPid/status 2>/dev/null | awk '/PPid/ { print $2 }')
  92. if cat /proc/$sshPpid/cmdline 2>/dev/null | grep -q "cmh-ra-daemon"; then
  93. log "Found cmh-ra tunnel with pid: $sshPid, and parent pid: $sshPpid"
  94. pids="$pids $sshPid"
  95. fi
  96. done
  97. echo "$pids"
  98. }
  99.  
  100.  
  101. get_cmh_ra_daemon_pids()
  102. {
  103. local dPids=$(ps ax | grep -v "grep" | grep "cmh-ra-daemon.sh" | awk '{ print $1 }')
  104. log "Found running cmh-ra-daemon pids: $dPids"
  105. echo "$dPids"
  106. }
  107.  
  108.  
  109. start()
  110. {
  111. init_script
  112.  
  113. # Check RA permission.
  114. [ -f "$MIOS_SERVICES_FILE" ] && source "$MIOS_SERVICES_FILE" || log "FILE-NOT-FOUND: services.conf"
  115. if [ "$Permissions_Relay" == 0 ]; then
  116. log "Permissions_Relay is $Permissions_Relay. We won't start the cmh-ra tunnel"
  117. exit 1
  118. fi
  119.  
  120. get_ra_port
  121.  
  122. log "Starting cmh-ra-daemon.sh"
  123. # RelayServer, RaPort and MmsSession are defined in get_ra_port
  124. cmh-ra-daemon.sh "127.0.0.1" 80 "$RelayServer" "$RaPort" "$MmsSession" 1>/dev/null 2>&1 &
  125. }
  126.  
  127.  
  128. stop()
  129. {
  130. init_script
  131.  
  132. local daemonPids=$(get_cmh_ra_daemon_pids)
  133. local sshPids=$(get_ssh_pids)
  134.  
  135. if [ -n "$daemonPids" ]; then
  136. log "Stopping cmh-ra-daemon pids: $daemonPids"
  137. kill $daemonPids
  138. sleep 2
  139. daemonPids=$(get_cmh_ra_daemon_pids)
  140. if [ -n "$daemonPids" ]; then
  141. log "Force stopping cmh-ra-daemon pids: $daemonPids"
  142. kill -9 $daemonPids
  143. fi
  144. fi
  145.  
  146. if [ -n "$sshPids" ]; then
  147. log "Stopping cmh-ra tunnels with pids: $sshPids"
  148. kill $sshPids 2>>$log_file
  149. sleep 2
  150. sshPids=$(get_ssh_pids)
  151. if [ -n "$sshPids" ]; then
  152. log "Force stopping cmh-ra tunnels with pids: $sshPids"
  153. kill -9 $sshPids
  154. fi
  155. fi
  156.  
  157. local unitModel=$(get_unit_model)
  158. case $unitModel in
  159. "Sercomm NA930"|"Sercomm NA301")
  160. set_led.sh off service
  161. ;;
  162. esac
  163. }
  164.  
  165.  
  166. restart()
  167. {
  168. stop
  169. sleep 2
  170. start
  171. }
Add Comment
Please, Sign In to add comment