Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh /etc/rc.common
- # SVN: $Id: cmh-ra 13196 2014-11-28 12:58:54Z florin $
- # Copyright (C) 2008 Mi Casa Verde, Inc., a Nevada Corporation
- # www.micasaverde.com
- # 1 - 702 - 4879770 / 866 - 966 - casa
- # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License.
- # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- START=997
- STOP=43
- #set -x
- INIT_SCRIPT=0
- init_script()
- {
- [ $INIT_SCRIPT -eq 1 ] && return
- # Skip it if we're on batteries.
- if [ -f /tmp/.running_on_batteries ]; then
- echo "Running on batteries. Skipping..."
- exit 0
- fi
- MIOS_CONSTANTS_FILE="$(dirname $0)/../../mios_constants.sh"
- [ -f "$MIOS_CONSTANTS_FILE" ] && source "$MIOS_CONSTANTS_FILE" || echo "FILE-NOT-FOUND: mios_constants.sh"
- log_to="both"; log_file="$MIOS_LOGS_PATH/log.cmh-ra"; log_as="cmh-ra"
- [ -f "$MIOS_FUNCTIONS_FILE" ] && source "$MIOS_FUNCTIONS_FILE" || echo "FILE-NOT-FOUND: functions.sh"
- export Ppid=$PPID
- export PPpid=$(awk '/^PPid:/ {print $2}' /proc/$Ppid/status)
- get_app_name_from_pid() {
- [ -n "$1" ] && cat /proc/$1/cmdline | tr '\0' ' '
- }
- log_START "$log_as $* from $(get_app_name_from_pid $Ppid)[$Ppid] with parent $(get_app_name_from_pid $PPpid)[$PPpid]"
- get_lock -t 1 "cmh-ra" || exit # Don't wait for the other cmh-ra process to stop, exit (almost) immediately.
- FACTORY_TEST_REQUIRED_FILE="$MIOS_TMP_PATH/.factory_test_required"
- [ -f "$FACTORY_TEST_REQUIRED_FILE" ] && log_DIE "Unit has not passed factory testing."
- INIT_SCRIPT=1
- }
- get_ra_port()
- {
- [ -f "$MIOS_FUNCTIONS_REST_FILE" ] && source "$MIOS_FUNCTIONS_REST_FILE" || echo "FILE-NOT-FOUND: functions_rest.sh"
- log "Getting cmh-ra port"
- local restOutput
- restOutput=$(MiOSRestApi.sh GetRaPort -p Type=$PORT_TYPE_CMH_RA -p CheckRAPortSession=1 -rs -rk)
- local restExitCode=$?
- local restResponseFile=$(echo $restOutput | awk '{ print $1 }')
- if [ -s "$restResponseFile" ]; then
- local restResponse=$(cat "$restResponseFile" 2>/dev/null)
- rm -f $restResponseFile
- fi
- if [ $restExitCode -ne 0 ]; then
- log_DIE "Failed to get cmh-ra port, reason: $restResponse, MiOSRestApi exit code: $restExitCode"
- fi
- RelayServer=$(echo $restOutput | awk '{ print $2 }')
- [ -z "$RelayServer" ] && log_DIE "Server not found in output: $restOutput."
- MmsSession=$(echo $restOutput | awk '{ print $3 }')
- [ -z "$MmsSession" ] && log_DIE "Session key not found in output: $restOutput."
- RaPort=$(rest_get_param_json Port "$restResponse")
- if [ -z "$RaPort" ] || [ -n "${RaPort//[0-9]/}" ] || [ $RaPort -gt 65535 ] || [ $RaPort -lt 1 ]; then
- log_DIE "Got invalid port: $restResponse"
- fi
- log_SUCCESS "Got port: $RaPort"
- }
- get_ssh_pids()
- {
- local pids
- local sshPids=$(ps ax | grep -v "grep" | grep "ssh" | awk '{ print $1 }')
- log "Found running ssh pids: $sshPids"
- local sshPpid
- for sshPid in $sshPids; do
- sshPpid=$(cat /proc/$sshPid/status 2>/dev/null | awk '/PPid/ { print $2 }')
- if cat /proc/$sshPpid/cmdline 2>/dev/null | grep -q "cmh-ra-daemon"; then
- log "Found cmh-ra tunnel with pid: $sshPid, and parent pid: $sshPpid"
- pids="$pids $sshPid"
- fi
- done
- echo "$pids"
- }
- get_cmh_ra_daemon_pids()
- {
- local dPids=$(ps ax | grep -v "grep" | grep "cmh-ra-daemon.sh" | awk '{ print $1 }')
- log "Found running cmh-ra-daemon pids: $dPids"
- echo "$dPids"
- }
- start()
- {
- init_script
- # Check RA permission.
- [ -f "$MIOS_SERVICES_FILE" ] && source "$MIOS_SERVICES_FILE" || log "FILE-NOT-FOUND: services.conf"
- if [ "$Permissions_Relay" == 0 ]; then
- log "Permissions_Relay is $Permissions_Relay. We won't start the cmh-ra tunnel"
- exit 1
- fi
- get_ra_port
- log "Starting cmh-ra-daemon.sh"
- # RelayServer, RaPort and MmsSession are defined in get_ra_port
- cmh-ra-daemon.sh "127.0.0.1" 80 "$RelayServer" "$RaPort" "$MmsSession" 1>/dev/null 2>&1 &
- }
- stop()
- {
- init_script
- local daemonPids=$(get_cmh_ra_daemon_pids)
- local sshPids=$(get_ssh_pids)
- if [ -n "$daemonPids" ]; then
- log "Stopping cmh-ra-daemon pids: $daemonPids"
- kill $daemonPids
- sleep 2
- daemonPids=$(get_cmh_ra_daemon_pids)
- if [ -n "$daemonPids" ]; then
- log "Force stopping cmh-ra-daemon pids: $daemonPids"
- kill -9 $daemonPids
- fi
- fi
- if [ -n "$sshPids" ]; then
- log "Stopping cmh-ra tunnels with pids: $sshPids"
- kill $sshPids 2>>$log_file
- sleep 2
- sshPids=$(get_ssh_pids)
- if [ -n "$sshPids" ]; then
- log "Force stopping cmh-ra tunnels with pids: $sshPids"
- kill -9 $sshPids
- fi
- fi
- local unitModel=$(get_unit_model)
- case $unitModel in
- "Sercomm NA930"|"Sercomm NA301")
- set_led.sh off service
- ;;
- esac
- }
- restart()
- {
- stop
- sleep 2
- start
- }
Add Comment
Please, Sign In to add comment