a note for nick about other memory not freed at exit
[tor.git] / contrib / torctl.in
blobee865844ba909d660db8534c2f0886bdd5f8eb7e
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 # the command used to start
58 if [ "x$TORUSER" = "x" ]; then
59 START="$TORBIN -f $TORCONF $TORARGS"
60 else
61 START="/bin/su -c \\"$TORBIN -f $TORCONF $TORARGS\\" $TORUSER"
65 # -------------------- --------------------
66 # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
68 ERROR=0
69 ARGV="$@"
70 if [ "x$ARGV" = "x" ] ; then
71 ARGS="help"
74 checkIfRunning ( ) {
75 # check for pidfile
76 PID=unknown
77 if [ -f $PIDFILE ] ; then
78 PID=`/bin/cat $PIDFILE`
79 if [ "x$PID" != "x" ] ; then
80 if kill -0 $PID 2>/dev/null ; then
81 STATUS="$EXEC (pid $PID) running"
82 RUNNING=1
83 else
84 STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
85 RUNNING=0
87 else
88 STATUS="$EXEC (pid $PID?) not running"
89 RUNNING=0
91 else
92 STATUS="$EXEC apparently not running (no pid file)"
93 RUNNING=0
95 return
98 for ARG in $@ $ARGS
100 checkIfRunning
102 case $ARG in
103 start)
104 if [ $RUNNING -eq 1 ]; then
105 echo "$0 $ARG: $EXEC (pid $PID) already running"
106 continue
108 if $START ; then
109 echo "$0 $ARG: $EXEC started"
110 # Make sure it stayed up!
111 /bin/sleep 1
112 checkIfRunning
113 if [ $RUNNING -eq 0 ]; then
114 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
116 else
117 echo "$0 $ARG: $EXEC could not be started"
118 ERROR=3
121 stop)
122 if [ $RUNNING -eq 0 ]; then
123 echo "$0 $ARG: $STATUS"
124 continue
126 if kill -15 $PID ; then
127 echo "$0 $ARG: $EXEC stopped"
128 else
129 /bin/sleep 1
130 if kill -9 $PID ; then
131 echo "$0 $ARG: $EXEC stopped"
132 else
133 echo "$0 $ARG: $EXEC could not be stopped"
134 ERROR=4
137 # Make sure it really died!
138 /bin/sleep 1
139 checkIfRunning
140 if [ $RUNNING -eq 1 ]; then
141 echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
144 restart)
145 $0 stop start
147 status)
148 echo $STATUS
151 echo "usage: $0 (start|stop|restart|status|help)"
152 /bin/cat <<EOF
154 start - start $EXEC
155 stop - stop $EXEC
156 restart - stop and restart $EXEC if running or start if not running
157 status - tell whether $EXEC is running or not
158 help - this text
161 ERROR=2
164 esac
166 done
168 exit $ERROR