#!/bin/bash # ################################################################ # # Should keep this very simple as we do not know what binaries # are available on the destination system. Many helper procs # are used to avoid relying on 'sed' and 'ps'. # # For example: # ps ax 2>/dev/null | grep '[ /]httpd-tt.tcl' # is replaced with: # grep -l '[ /]httpd-tt.tcl' /proc/[0-9]*/cmdline # ################################################################ ################################################################ # # The following could be changed from 2916352 to 3244032 to # help out with the memory issues some people are experiencing. # This will be automatically increased if a crash occurs due # to a BlockFailure - see the code in the Start() function. # ################################################################ export TIVOSH_POOLSIZE=2916352 shopt -s extglob dir=${0%%+([^/])} bin=${dir}httpd-tt.tcl mem=${dir}.dist/BlockFailure cct=${dir}.dist/CrashCount arc=`uname -m` int=`echo $- | grep -c i` [ -x ${dir}bin_$arc/setpri ] && ${dir}bin_$arc/setpri fifo 1 $$ ################################################################ # # This will return the value of a configuration parameter # ################################################################ get_config() { grep -i "^$1" $2 |\ while read lhs eq rhs; do echo "${rhs//\"/}" done } ################################################################ # # This is an alternative to using 'ps' # ################################################################ ps_httpd() { grep -l '[ /]httpd-tt.tcl' /proc/[0-9]*/cmdline 2>/dev/null } ################################################################ # # Return the status of the daemon # ################################################################ status() { if [ -n "`ps_httpd`" ] ; then echo "An httpd is running" else echo "An httpd is not running" fi } ################################################################ # # Start the http daemon # ################################################################ start() { if [ -n "`ps_httpd`" ] ; then echo "Warning: An http server was already running" fi # Check to see if we have experience a BlockFailure in the past if [ ! -s $mem ] ; then grep 'BlockFailure' /var/log/*tivoweb.log > $mem 2>/dev/null fi if [ -s $mem ] ; then export TIVOSH_POOLSIZE=3244032 fi if [ $int -eq 0 ]; then # Check to see if there was a crash ended=`cat /var/log/*tivoweb.log|grep -c '^[0-9].* - - .*".*"$'` if [ $ended -eq 0 ]; then begun=`cat /var/log/*tivoweb.log|grep -c 'TivoWebPlus'` if [ $begun -gt 0 ]; then if [ -f "$cct" ]; then cnt=`cat $cct` let cnt+=1 else cnt=1 fi echo "$cnt" > $cct if [ $cnt -gt 3 ]; then echo "Warning: The last few sessions did not complete successfully" echo " We may be in a reboot loop... abort" exit 1 fi fi else echo "0" > $cct fi fi if [ -f $bin ] ; then if [ ${1:-null} == "console" ] ; then tivosh $bin & else if [ -f /var/log/tivoweb.log ] ; then mv -f /var/log/tivoweb.log /var/log/Otivoweb.log fi tivosh $bin >/var/log/tivoweb.log 2>&1 & fi else echo "Error: could not locate httpd-tt.tcl in dir '${dir}'" exit 1 fi } ################################################################ # # Stop the http daemon # ################################################################ stop() { conf=${dir}tivoweb.cfg if [ -f $conf ] ; then pass=$(get_config Password $conf) user=$(get_config UserName $conf) port=$(get_config Port $conf) pref=$(get_config Prefix $conf) # Ensure the loopback interface is up ifconfig lo 127.0.0.1 up >/dev/null 2>&1 addr=127.0.0.1 if [ -n "$pref" ]; then pref="$pref/" fi if [ -n "$user" ]; then url="http://$user:$pass@$addr" else url="http://$addr" fi if [ -n "$port" ]; then url="${url}:$port" fi # Check to see if an http daemon is running ps_cnt=`ps_httpd | grep -c ""` if [ $ps_cnt -ge 1 ] ; then # Attempt to quit tivoweb/plus via a wget call wget --version >/dev/null 2>&1 if [ $? -ne 127 ]; then if wget --version >/dev/null 2>&1; then # We have a GNU wget binary wget --tries=2 --timeout=15 -O /dev/null -o /dev/null "$url/${pref}quit" & else # Assume a busybox wget binary wget -O- "$url/${pref}quit" >/dev/null 2>&1 & fi # Sleep (for up to 30 seconds) to allow TivoWebPlus to quit count=1 while [ $count -le 12 ] ; do count=$(($count+1)) sleep 3 new_ps_cnt=`ps_httpd | grep -c ""` if [ $new_ps_cnt -lt $ps_cnt ] ; then count=99 fi done if [ $count -ne 99 ] ; then echo "Warning: An http daemon is still running, quit may have failed." return fi else echo "ERROR: A wget binary could not be found, and is required" echo " to shutdown the application from the command line." fi fi else echo "Error: could not locate tivoweb.cfg in dir '${dir}'" exit 1 fi } ################################################################ # # Process the command line # ################################################################ case "$1" in stop) stop;; start) start;; status) status;; reload) stop; start;; restart) stop; start;; crashit) ############################################ # This will crash the TiVo... it's used to # # measure the amount of available memory. # ############################################ cp "${dir}.dist/{z}.itcl" "${dir}modules" export MemoryTest="crashit"; stop; start console;; console) start console;; *) start $*;; esac