| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 | #!/bin/sh## TOR control script designed to allow an easy command line interface# to controlling The Onion Router## The exit codes returned are:#	0 - operation completed successfully#	1 -#	2 - Command not supported#	3 - Could not be started#	4 - Could not be stopped#	5 -#	6 -#	7 -#	8 -## When multiple arguments are given, only the error from the _last_# one is reported.### |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||# --------------------                              --------------------# Name of the executableEXEC=tor## the path to your binary, including options if necessaryTORBIN="@BINDIR@/$EXEC"## the path to the configuration fileTORCONF=@CONFDIR@/torrc## the path to your PID filePIDFILE=@LOCALSTATEDIR@/run/tor/tor.pid## The path to the log fileLOGFILE=@LOCALSTATEDIR@/log/tor/tor.log## The path to the datadirectoryTORDATA=@LOCALSTATEDIR@/lib/tor## The USER and GROUP names:# TORUSER and TORGROUP if defined in the environment, else LOGNAME and GROUP# respectively.TORUSER=TORGROUP=TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE \" --runasdaemon 1"TORARGS="$TORARGS --datadirectory $TORDATA"if [ "x$TORUSER" != "x" ]; then    TORARGS="$TORARGS --user $TORUSER"fiif [ "x$TORGROUP" != "x" ]; then    TORARGS="$TORARGS --group $TORGROUP"fiif [ -x /bin/su ] ; then    SUPROG=/bin/suelif [ -x /sbin/su ] ; then    SUPROG=/sbin/suelif [ -x /usr/bin/su ] ; then    SUPROG=/usr/bin/suelif [ -x /usr/sbin/su ] ; then    SUPROG=/usr/sbin/suelse    SUPROG=/bin/sufi# the command used to startif  [ "x$TORUSER" = "x" ]; then    START="$TORBIN -f $TORCONF $TORARGS"else    START="$SUPROG -c \\"$TORBIN -f $TORCONF $TORARGS\\" $TORUSER"fi## --------------------                              --------------------# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||ERROR=0ARGV="$@"if [ "x$ARGV" = "x" ] ; then    ARGS="help"ficheckIfRunning ( ) {    # check for pidfile    PID=unknown	 if [ -f $PIDFILE ] ; then        PID=`/bin/cat $PIDFILE`        if [ "x$PID" != "x" ] ; then		      if kill -0 $PID 2>/dev/null ; then                STATUS="$EXEC (pid $PID) running"                RUNNING=1		      else            	 STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"            	 RUNNING=0				fi        else            STATUS="$EXEC (pid $PID?) not running"            RUNNING=0        fi    else        STATUS="$EXEC apparently not running (no pid file)"        RUNNING=0    fi    return}for ARG in $@ $ARGSdo    checkIfRunning    case $ARG in    start)        if [ $RUNNING -eq 1 ]; then            echo "$0 $ARG: $EXEC (pid $PID) already running"            continue        fi        if $START ; then            echo "$0 $ARG: $EXEC started"				# Make sure it stayed up!				/bin/sleep 1				checkIfRunning				if [ $RUNNING -eq 0 ]; then   				 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"				fi        else            echo "$0 $ARG: $EXEC could not be started"            ERROR=3        fi        ;;    stop)        if [ $RUNNING -eq 0 ]; then            echo "$0 $ARG: $STATUS"            continue        fi        if kill -15 $PID ; then            echo "$0 $ARG: $EXEC stopped"        else      	  /bin/sleep 1      	  if kill -9 $PID ; then            	echo "$0 $ARG: $EXEC stopped"      	  else            	echo "$0 $ARG: $EXEC could not be stopped"            	ERROR=4      	  fi        fi        # Make sure it really died!        /bin/sleep 1        checkIfRunning        if [ $RUNNING -eq 1 ]; then            echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"        fi        ;;    restart)        $0 stop start        ;;    status)        echo $STATUS        ;;    *)        echo "usage: $0 (start|stop|restart|status|help)"        /bin/cat <<EOFstart      - start $EXECstop       - stop $EXECrestart    - stop and restart $EXEC if running or start if not runningstatus     - tell whether $EXEC is running or nothelp       - this textEOF        ERROR=2    ;;    esacdoneexit $ERROR
 |