Servers that don't know their own IP address should go to the
[tor.git] / debian / tor.init
blob38712ceb7a87d5378b67d8c3cbfc091585c3427d
1 #! /bin/bash
3 ### BEGIN INIT INFO
4 # Provides: tor
5 # Required-Start: $local_fs $remote_fs $network $named $time
6 # Required-Stop: $local_fs $remote_fs $network $named $time
7 # Should-Start: $syslog
8 # Should-Stop: $syslog
9 # Default-Start: 2 3 4 5
10 # Default-Stop: 0 1 6
11 # Short-Description: Starts The Onion Router daemon processes
12 # Description: Start The Onion Router, a TCP overlay
13 # network client that provides anonymous
14 # transport.
15 ### END INIT INFO
17 set -e
19 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
20 DAEMON=/usr/sbin/tor
21 NAME=tor
22 DESC="tor daemon"
23 TORPIDDIR=/var/run/tor
24 TORPID=$TORPIDDIR/tor.pid
25 DEFAULTSFILE=/etc/default/$NAME
26 WAITFORDAEMON=60
27 ARGS=""
28 # Let's try to figure our some sane defaults:
29 if [ -r /proc/sys/fs/file-max ]; then
30 system_max=`cat /proc/sys/fs/file-max`
31 if [ "$system_max" -gt "80000" ] ; then
32 MAX_FILEDESCRIPTORS=32768
33 elif [ "$system_max" -gt "40000" ] ; then
34 MAX_FILEDESCRIPTORS=16384
35 elif [ "$system_max" -gt "10000" ] ; then
36 MAX_FILEDESCRIPTORS=8192
37 else
38 MAX_FILEDESCRIPTORS=1024
40 else
41 MAX_FILEDESCRIPTORS=8192
44 NICE=""
46 test -x $DAEMON || exit 0
48 # Include tor defaults if available
49 if [ -f $DEFAULTSFILE ] ; then
50 . $DEFAULTSFILE
53 wait_for_deaddaemon () {
54 pid=$1
55 sleep 1
56 if test -n "$pid"
57 then
58 if kill -0 $pid 2>/dev/null
59 then
60 echo -n "."
61 cnt=0
62 while kill -0 $pid 2>/dev/null
64 cnt=`expr $cnt + 1`
65 if [ $cnt -gt $WAITFORDAEMON ]
66 then
67 echo " FAILED."
68 return 1
70 sleep 1
71 echo -n "."
72 done
75 return 0
79 check_torpiddir () {
80 if test ! -d $TORPIDDIR; then
81 echo "There is no $TORPIDDIR directory. Creating one for you."
82 mkdir -m 02700 "$TORPIDDIR"
83 chown debian-tor:debian-tor "$TORPIDDIR"
86 if test ! -x $TORPIDDIR; then
87 echo "Cannot access $TORPIDDIR directory, are you root?" >&2
88 exit 1
93 case "$1" in
94 start)
95 if [ "$RUN_DAEMON" != "yes" ]; then
96 echo "Not starting $DESC (Disabled in $DEFAULTSFILE)."
97 exit 0
100 if [ -n "$MAX_FILEDESCRIPTORS" ]; then
101 echo -n "Raising maximum number of filedescriptors (ulimit -n) to $MAX_FILEDESCRIPTORS"
102 if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
103 echo "."
104 else
105 echo ": FAILED."
109 check_torpiddir
111 echo "Starting $DESC: $NAME..."
112 if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
113 echo "ABORTED: Tor configuration invalid:" >&2
114 su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
115 exit 1
118 start-stop-daemon --start --quiet --oknodo \
119 --pidfile $TORPID \
120 $NICE \
121 --exec $DAEMON -- $ARGS
122 echo "done."
124 stop)
125 echo -n "Stopping $DESC: "
126 pid=`cat $TORPID 2>/dev/null` || true
128 if test ! -f $TORPID -o -z "$pid"; then
129 echo "not running (there is no $TORPID)."
130 exit 0
133 if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
134 wait_for_deaddaemon $pid
135 echo "$NAME."
136 elif kill -0 $pid 2>/dev/null
137 then
138 echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
139 else
140 echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
143 reload|force-reload)
144 echo -n "Reloading $DESC configuration: "
145 pid=`cat $TORPID 2>/dev/null` || true
147 if test ! -f $TORPID -o -z "$pid"; then
148 echo "not running (there is no $TORPID)."
149 exit 0
152 if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
153 echo "ABORTED: Tor configuration invalid:" >&2
154 su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
155 exit 1
158 if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
159 then
160 echo "$NAME."
161 elif kill -0 $pid 2>/dev/null
162 then
163 echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
164 else
165 echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
168 restart)
169 if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
170 echo "Restarting Tor ABORTED: Tor configuration invalid:" >&2
171 su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
172 exit 1
175 $0 stop
176 sleep 1
177 $0 start
180 echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
181 exit 1
183 esac
185 exit 0