2004-11-14 10:05:28 +01:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# TOR control script designed to allow an easy command line interface
|
|
|
|
# to controlling The Onion Router
|
|
|
|
#
|
|
|
|
# The exit codes returned are:
|
2005-12-24 05:03:39 +01:00
|
|
|
# 0 - operation completed successfully. For "status", tor running.
|
|
|
|
# 1 - For "status", tor not running.
|
|
|
|
# 2 - Command not supported
|
|
|
|
# 3 - Could not be started or reloaded
|
|
|
|
# 4 - Could not be stopped
|
|
|
|
# 5 -
|
|
|
|
# 6 -
|
|
|
|
# 7 -
|
|
|
|
# 8 -
|
2004-11-14 10:05:28 +01:00
|
|
|
#
|
|
|
|
# When multiple arguments are given, only the error from the _last_
|
|
|
|
# one is reported.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# |||||||||||||||||||| START CONFIGURATION SECTION ||||||||||||||||||||
|
|
|
|
# -------------------- --------------------
|
|
|
|
# Name of the executable
|
|
|
|
EXEC=tor
|
|
|
|
#
|
|
|
|
# the path to your binary, including options if necessary
|
|
|
|
TORBIN="@BINDIR@/$EXEC"
|
|
|
|
#
|
|
|
|
# the path to the configuration file
|
2005-12-24 05:03:39 +01:00
|
|
|
TORCONF="@CONFDIR@/torrc"
|
2004-11-14 10:05:28 +01:00
|
|
|
#
|
|
|
|
# the path to your PID file
|
2005-12-24 05:03:39 +01:00
|
|
|
PIDFILE="@LOCALSTATEDIR@/run/tor/tor.pid"
|
2004-11-14 10:05:28 +01:00
|
|
|
#
|
|
|
|
# The path to the log file
|
2005-12-24 05:03:39 +01:00
|
|
|
LOGFILE="@LOCALSTATEDIR@/log/tor/tor.log"
|
2004-11-14 10:05:28 +01:00
|
|
|
#
|
|
|
|
# The path to the datadirectory
|
2005-12-24 05:03:39 +01:00
|
|
|
TORDATA="@LOCALSTATEDIR@/lib/tor"
|
2004-11-14 10:05:28 +01:00
|
|
|
#
|
2005-12-24 05:03:39 +01:00
|
|
|
TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE\" --runasdaemon 1"
|
2004-11-14 10:05:28 +01:00
|
|
|
TORARGS="$TORARGS --datadirectory $TORDATA"
|
|
|
|
|
2008-11-10 16:40:15 +01:00
|
|
|
# If user name is set in the environment, then use it;
|
2005-12-24 05:03:39 +01:00
|
|
|
# otherwise run as the invoking user (or whatever user the config
|
|
|
|
# file says)... unless the invoking user is root. The idea here is to
|
|
|
|
# let an unprivileged user run tor for her own use using this script,
|
|
|
|
# while still providing for it to be used as a system daemon.
|
|
|
|
if [ "x`id -u`" = "x0" ]; then
|
|
|
|
TORUSER=@TORUSER@
|
|
|
|
fi
|
|
|
|
|
2004-11-14 10:05:28 +01:00
|
|
|
if [ "x$TORUSER" != "x" ]; then
|
|
|
|
TORARGS="$TORARGS --user $TORUSER"
|
|
|
|
fi
|
|
|
|
|
2005-12-24 05:03:39 +01:00
|
|
|
# We no longer wrap the Tor daemon startup in an su when running as
|
|
|
|
# root, because it's too painful to make the use of su portable.
|
|
|
|
# Just let the daemon set the UID and GID.
|
|
|
|
START="$TORBIN -f $TORCONF $TORARGS"
|
2004-11-14 10:05:28 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# -------------------- --------------------
|
|
|
|
# |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
|
|
|
|
|
|
|
|
ERROR=0
|
|
|
|
ARGV="$@"
|
|
|
|
if [ "x$ARGV" = "x" ] ; then
|
|
|
|
ARGS="help"
|
|
|
|
fi
|
|
|
|
|
|
|
|
checkIfRunning ( ) {
|
|
|
|
# check for pidfile
|
|
|
|
PID=unknown
|
2005-12-24 05:03:39 +01:00
|
|
|
if [ -f $PIDFILE ] ; then
|
2004-11-14 10:05:28 +01:00
|
|
|
PID=`/bin/cat $PIDFILE`
|
|
|
|
if [ "x$PID" != "x" ] ; then
|
2005-12-24 05:03:39 +01:00
|
|
|
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
|
2004-11-14 10:05:28 +01:00
|
|
|
else
|
|
|
|
STATUS="$EXEC (pid $PID?) not running"
|
|
|
|
RUNNING=0
|
|
|
|
fi
|
|
|
|
else
|
2005-12-24 05:03:39 +01:00
|
|
|
STATUS="$EXEC apparently not running (no pid file)"
|
|
|
|
RUNNING=0
|
2004-11-14 10:05:28 +01:00
|
|
|
fi
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for ARG in $@ $ARGS
|
|
|
|
do
|
|
|
|
checkIfRunning
|
|
|
|
|
|
|
|
case $ARG in
|
|
|
|
start)
|
|
|
|
if [ $RUNNING -eq 1 ]; then
|
|
|
|
echo "$0 $ARG: $EXEC (pid $PID) already running"
|
|
|
|
continue
|
|
|
|
fi
|
2005-12-24 05:03:39 +01:00
|
|
|
if eval "$START" ; then
|
2004-11-14 10:05:28 +01:00
|
|
|
echo "$0 $ARG: $EXEC started"
|
2005-12-24 05:03:39 +01:00
|
|
|
# Make sure it stayed up!
|
|
|
|
/bin/sleep 1
|
|
|
|
checkIfRunning
|
|
|
|
if [ $RUNNING -eq 0 ]; then
|
|
|
|
echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
|
|
|
|
fi
|
2004-11-14 10:05:28 +01:00
|
|
|
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
|
2005-12-24 05:03:39 +01:00
|
|
|
/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
|
2004-11-14 10:05:28 +01:00
|
|
|
fi
|
|
|
|
# Make sure it really died!
|
|
|
|
/bin/sleep 1
|
|
|
|
checkIfRunning
|
|
|
|
if [ $RUNNING -eq 1 ]; then
|
|
|
|
echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
|
2005-12-24 05:03:39 +01:00
|
|
|
ERROR=4
|
2004-11-14 10:05:28 +01:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
$0 stop start
|
|
|
|
;;
|
2005-12-24 05:03:39 +01:00
|
|
|
reload)
|
|
|
|
if [ $RUNNING -eq 0 ]; then
|
|
|
|
echo "$0 $ARG: $STATUS"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if kill -1 $PID; then
|
|
|
|
/bin/sleep 1
|
|
|
|
echo "$EXEC (PID $PID) reloaded"
|
|
|
|
else
|
|
|
|
echo "Can't reload $EXEC"
|
|
|
|
ERROR=3
|
|
|
|
fi
|
|
|
|
;;
|
2004-11-14 10:05:28 +01:00
|
|
|
status)
|
|
|
|
echo $STATUS
|
2005-12-24 05:03:39 +01:00
|
|
|
if [ $RUNNING -eq 1 ]; then
|
|
|
|
ERROR=0
|
|
|
|
else
|
|
|
|
ERROR=1
|
|
|
|
fi
|
2004-11-14 10:05:28 +01:00
|
|
|
;;
|
2005-12-24 05:03:39 +01:00
|
|
|
log)
|
|
|
|
cat $LOGFILE
|
|
|
|
;;
|
|
|
|
help)
|
2004-11-14 10:05:28 +01:00
|
|
|
echo "usage: $0 (start|stop|restart|status|help)"
|
|
|
|
/bin/cat <<EOF
|
|
|
|
|
|
|
|
start - start $EXEC
|
|
|
|
stop - stop $EXEC
|
|
|
|
restart - stop and restart $EXEC if running or start if not running
|
2005-12-24 05:03:39 +01:00
|
|
|
reload - cause the running process to reinitialize itself
|
2004-11-14 10:05:28 +01:00
|
|
|
status - tell whether $EXEC is running or not
|
2005-12-24 05:03:39 +01:00
|
|
|
log - display the contents of the log file
|
2004-11-14 10:05:28 +01:00
|
|
|
help - this text
|
|
|
|
|
|
|
|
EOF
|
2005-12-24 05:03:39 +01:00
|
|
|
ERROR=0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
$0 help
|
2004-11-14 10:05:28 +01:00
|
|
|
ERROR=2
|
2005-12-24 05:03:39 +01:00
|
|
|
;;
|
2004-11-14 10:05:28 +01:00
|
|
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
exit $ERROR
|
|
|
|
|