~ubuntu-branches/ubuntu/hoary/tor/hoary-backports

« back to all changes in this revision

Viewing changes to contrib/torctl.in

  • Committer: Bazaar Package Importer
  • Author(s): Peter Palfrader
  • Date: 2005-01-04 11:14:03 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050104111403-ukxghb7fddw8vx01
Tags: 0.0.9.2-1
* New upstream version.
* Update debian/copyright (it's 2005).
* Add sharedscripts tor logrotate.d/tor.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# TOR control script designed to allow an easy command line interface
 
4
# to controlling The Onion Router
 
5
#
 
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 -
 
16
#
 
17
# When multiple arguments are given, only the error from the _last_
 
18
# one is reported.
 
19
#
 
20
#
 
21
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
 
22
# --------------------                              --------------------
 
23
# Name of the executable
 
24
EXEC=tor
 
25
#
 
26
# the path to your binary, including options if necessary
 
27
TORBIN="@BINDIR@/$EXEC"
 
28
#
 
29
# the path to the configuration file
 
30
TORCONF=@CONFDIR@/torrc
 
31
#
 
32
# the path to your PID file
 
33
PIDFILE=@LOCALSTATEDIR@/run/tor/tor.pid
 
34
#
 
35
# The path to the log file
 
36
LOGFILE=@LOCALSTATEDIR@/log/tor/tor.log
 
37
#
 
38
# The path to the datadirectory
 
39
TORDATA=@LOCALSTATEDIR@/lib/tor
 
40
#
 
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=
 
46
 
 
47
TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE \" --runasdaemon 1"
 
48
TORARGS="$TORARGS --datadirectory $TORDATA"
 
49
 
 
50
if [ "x$TORUSER" != "x" ]; then
 
51
    TORARGS="$TORARGS --user $TORUSER"
 
52
fi
 
53
if [ "x$TORGROUP" != "x" ]; then
 
54
    TORARGS="$TORARGS --group $TORGROUP"
 
55
fi
 
56
 
 
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"
 
62
fi
 
63
 
 
64
#
 
65
# --------------------                              --------------------
 
66
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||
 
67
 
 
68
ERROR=0
 
69
ARGV="$@"
 
70
if [ "x$ARGV" = "x" ] ; then
 
71
    ARGS="help"
 
72
fi
 
73
 
 
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
 
86
                                fi
 
87
        else
 
88
            STATUS="$EXEC (pid $PID?) not running"
 
89
            RUNNING=0
 
90
        fi
 
91
    else
 
92
        STATUS="$EXEC apparently not running (no pid file)"
 
93
        RUNNING=0
 
94
    fi
 
95
    return
 
96
}
 
97
 
 
98
for ARG in $@ $ARGS
 
99
do
 
100
    checkIfRunning
 
101
 
 
102
    case $ARG in
 
103
    start)
 
104
        if [ $RUNNING -eq 1 ]; then
 
105
            echo "$0 $ARG: $EXEC (pid $PID) already running"
 
106
            continue
 
107
        fi
 
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"
 
115
                                fi
 
116
        else
 
117
            echo "$0 $ARG: $EXEC could not be started"
 
118
            ERROR=3
 
119
        fi
 
120
        ;;
 
121
    stop)
 
122
        if [ $RUNNING -eq 0 ]; then
 
123
            echo "$0 $ARG: $STATUS"
 
124
            continue
 
125
        fi
 
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
 
135
          fi
 
136
        fi
 
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"
 
142
        fi
 
143
        ;;
 
144
    restart)
 
145
        $0 stop start
 
146
        ;;
 
147
    status)
 
148
        echo $STATUS
 
149
        ;;
 
150
    *)
 
151
        echo "usage: $0 (start|stop|restart|status|help)"
 
152
        /bin/cat <<EOF
 
153
 
 
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
 
159
 
 
160
EOF
 
161
        ERROR=2
 
162
    ;;
 
163
 
 
164
    esac
 
165
 
 
166
done
 
167
 
 
168
exit $ERROR
 
169