aboutsummaryrefslogtreecommitdiff
path: root/contrib/torctl.in
blob: 4faa8f0a0b4ad5bc18008795959bf6cfb811ef25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/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 executable
EXEC=tor
#
# the path to your binary, including options if necessary
TORBIN="@BINDIR@/$EXEC"
#
# the path to the configuration file
TORCONF=@CONFDIR@/torrc
#
# the path to your PID file
PIDFILE=@LOCALSTATEDIR@/run/tor/tor.pid
#
# The path to the log file
LOGFILE=@LOCALSTATEDIR@/log/tor/tor.log
#
# The path to the datadirectory
TORDATA=@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"
fi
if [ "x$TORGROUP" != "x" ]; then
    TORARGS="$TORARGS --group $TORGROUP"
fi

if [ -x /bin/su ] ; then
    SUPROG=/bin/su
elif [ -x /sbin/su ] ; then
    SUPROG=/sbin/su
elif [ -x /usr/bin/su ] ; then
    SUPROG=/usr/bin/su
elif [ -x /usr/sbin/su ] ; then
    SUPROG=/usr/sbin/su
else
    SUPROG=/bin/su
fi

# the command used to start
if  [ "x$TORUSER" = "x" ]; then
    START="$TORBIN -f $TORCONF $TORARGS"
else
    START="$SUPROG -c \\"$TORBIN -f $TORCONF $TORARGS\\" $TORUSER"
fi

#
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

ERROR=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then
    ARGS="help"
fi

checkIfRunning ( ) {
    # 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 $@ $ARGS
do
    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 <<EOF

start      - start $EXEC
stop       - stop $EXEC
restart    - stop and restart $EXEC if running or start if not running
status     - tell whether $EXEC is running or not
help       - this text

EOF
        ERROR=2
    ;;

    esac

done

exit $ERROR