fix compile complaints (and a bug)
[tor.git] / contrib / torctl.in
blob4faa8f0a0b4ad5bc18008795959bf6cfb811ef25
1 #!/bin/sh
3 # TOR control script designed to allow an easy command line interface
4 # to controlling The Onion Router
6 # The exit codes returned are:
7 # 0 - operation completed successfully
8 # 1 -
9 # 2 - Command not supported
10 # 3 - Could not be started
11 # 4 - Could not be stopped
12 # 5 -
13 # 6 -
14 # 7 -
15 # 8 -
17 # When multiple arguments are given, only the error from the _last_
18 # one is reported.
21 # |||||||||||||||||||| START CONFIGURATION SECTION ||||||||||||||||||||
22 # -------------------- --------------------
23 # Name of the executable
24 EXEC=tor
26 # the path to your binary, including options if necessary
27 TORBIN="@BINDIR@/$EXEC"
29 # the path to the configuration file
30 TORCONF=@CONFDIR@/torrc
32 # the path to your PID file
33 PIDFILE=@LOCALSTATEDIR@/run/tor/tor.pid
35 # The path to the log file
36 LOGFILE=@LOCALSTATEDIR@/log/tor/tor.log
38 # The path to the datadirectory
39 TORDATA=@LOCALSTATEDIR@/lib/tor
41 # The USER and GROUP names:
42 # TORUSER and TORGROUP if defined in the environment, else LOGNAME and GROUP
43 # respectively.
44 TORUSER=
45 TORGROUP=
47 TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE \" --runasdaemon 1"
48 TORARGS="$TORARGS --datadirectory $TORDATA"
50 if [ "x$TORUSER" != "x" ]; then
51 TORARGS="$TORARGS --user $TORUSER"
53 if [ "x$TORGROUP" != "x" ]; then
54 TORARGS="$TORARGS --group $TORGROUP"
57 if [ -x /bin/su ] ; then
58 SUPROG=/bin/su
59 elif [ -x /sbin/su ] ; then
60 SUPROG=/sbin/su
61 elif [ -x /usr/bin/su ] ; then
62 SUPROG=/usr/bin/su
63 elif [ -x /usr/sbin/su ] ; then
64 SUPROG=/usr/sbin/su
65 else
66 SUPROG=/bin/su
69 # the command used to start
70 if [ "x$TORUSER" = "x" ]; then
71 START="$TORBIN -f $TORCONF $TORARGS"
72 else
73 START="$SUPROG -c \\"$TORBIN -f $TORCONF $TORARGS\\" $TORUSER"
77 # -------------------- --------------------
78 # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
80 ERROR=0
81 ARGV="$@"
82 if [ "x$ARGV" = "x" ] ; then
83 ARGS="help"
86 checkIfRunning ( ) {
87 # check for pidfile
88 PID=unknown
89 if [ -f $PIDFILE ] ; then
90 PID=`/bin/cat $PIDFILE`
91 if [ "x$PID" != "x" ] ; then
92 if kill -0 $PID 2>/dev/null ; then
93 STATUS="$EXEC (pid $PID) running"
94 RUNNING=1
95 else
96 STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
97 RUNNING=0
99 else
100 STATUS="$EXEC (pid $PID?) not running"
101 RUNNING=0
103 else
104 STATUS="$EXEC apparently not running (no pid file)"
105 RUNNING=0
107 return
110 for ARG in $@ $ARGS
112 checkIfRunning
114 case $ARG in
115 start)
116 if [ $RUNNING -eq 1 ]; then
117 echo "$0 $ARG: $EXEC (pid $PID) already running"
118 continue
120 if $START ; then
121 echo "$0 $ARG: $EXEC started"
122 # Make sure it stayed up!
123 /bin/sleep 1
124 checkIfRunning
125 if [ $RUNNING -eq 0 ]; then
126 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
128 else
129 echo "$0 $ARG: $EXEC could not be started"
130 ERROR=3
133 stop)
134 if [ $RUNNING -eq 0 ]; then
135 echo "$0 $ARG: $STATUS"
136 continue
138 if kill -15 $PID ; then
139 echo "$0 $ARG: $EXEC stopped"
140 else
141 /bin/sleep 1
142 if kill -9 $PID ; then
143 echo "$0 $ARG: $EXEC stopped"
144 else
145 echo "$0 $ARG: $EXEC could not be stopped"
146 ERROR=4
149 # Make sure it really died!
150 /bin/sleep 1
151 checkIfRunning
152 if [ $RUNNING -eq 1 ]; then
153 echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
156 restart)
157 $0 stop start
159 status)
160 echo $STATUS
163 echo "usage: $0 (start|stop|restart|status|help)"
164 /bin/cat <<EOF
166 start - start $EXEC
167 stop - stop $EXEC
168 restart - stop and restart $EXEC if running or start if not running
169 status - tell whether $EXEC is running or not
170 help - this text
173 ERROR=2
176 esac
178 done
180 exit $ERROR