aboutsummaryrefslogtreecommitdiff
path: root/debian/tor.init
blob: 7a69ae621abba17163c4c88e1852f880e859f4a1 (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
#! /bin/bash

### BEGIN INIT INFO
# Provides:          tor
# Required-Start:    $local_fs, $remote_fs, $network, $named, $time
# Required-Stop:     $local_fs, $remote_fs, $network, $named, $time
# Should-Start:      $syslog
# Should-Stop:       $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts The Onion Router daemon processes
# Description:       Start The Onion Router, a TCP overlay
#                    network client that provides anonymous
#                    transport.
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/tor
NAME=tor
DESC="tor daemon"
TORPIDDIR=/var/run/tor
TORPID=$TORPIDDIR/tor.pid
DEFAULTSFILE=/etc/default/$NAME
WAITFORDAEMON=60
ARGS=""
MAX_FILEDESCRIPTORS=8192
NICE=""

test -x $DAEMON || exit 0

# Include tor defaults if available
if [ -f $DEFAULTSFILE ] ; then
	. $DEFAULTSFILE
fi

wait_for_deaddaemon () {
	pid=$1
	sleep 1
	if test -n "$pid"
	then
		if kill -0 $pid 2>/dev/null
		then
			echo -n "."
			cnt=0
			while kill -0 $pid 2>/dev/null
			do
				cnt=`expr $cnt + 1`
				if [ $cnt -gt $WAITFORDAEMON ]
				then
					echo " FAILED."
					return 1
				fi
				sleep 1
				echo -n "."
			done
		fi
	fi
	return 0
}


if test ! -d $TORPIDDIR; then
	echo "There is no $TORPIDDIR directory.  Creating one for you."
	mkdir -m 02700 "$TORPIDDIR"
	chown debian-tor:debian-tor "$TORPIDDIR"
fi

if test ! -x $TORPIDDIR; then
	echo "Cannot access $TORPIDDIR directory, are you root?" >&2
	exit 1
fi


case "$1" in
  start)
	if [ "$RUN_DAEMON" != "yes" ]; then
		echo "Not starting $DESC (Disabled in $DEFAULTSFILE)."
		exit 0
	fi

	if [ -n "$MAX_FILEDESCRIPTORS" ]; then
		echo -n "Raising maximum number of filedescriptors (ulimit -n) to $MAX_FILEDESCRIPTORS"
		if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
			echo "."
		else
			echo ": FAILED."
		fi
	fi

	echo "Starting $DESC: $NAME..."
	if ! su -c "$DAEMON --verify-config" debian-tor > /dev/null; then
		echo "ABORTED: Tor configuration invalid:" >&2
		su -c "$DAEMON --verify-config" debian-tor >&2
		exit 1
	fi

	start-stop-daemon --start --quiet --oknodo \
		--chuid debian-tor:debian-tor \
		--pidfile $TORPID \
		$NICE \
		--exec $DAEMON -- $ARGS
	echo "done."
	;;
  stop)
	echo -n "Stopping $DESC: "
	pid=`cat $TORPID 2>/dev/null` || true

	if test ! -f $TORPID -o -z "$pid"; then
		echo "not running (there is no $TORPID)."
		exit 0
	fi

	if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
		wait_for_deaddaemon $pid
		echo "$NAME."
	elif kill -0 $pid 2>/dev/null
	then
		echo "FAILED (Is $pid not $NAME?  Is $DAEMON a different binary now?)."
	else
		echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
	fi
	;;
  reload|force-reload)
	echo -n "Reloading $DESC configuration: "
	pid=`cat $TORPID 2>/dev/null` || true

	if test ! -f $TORPID -o -z "$pid"; then
		echo "not running (there is no $TORPID)."
		exit 0
	fi

	if ! su -c "$DAEMON --verify-config" debian-tor > /dev/null; then
		echo "ABORTED: Tor configuration invalid:" >&2
		su -c "$DAEMON --verify-config" debian-tor >&2
		exit 1
	fi

	if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
	then
		echo "$NAME."
	elif kill -0 $pid 2>/dev/null
	then
		echo "FAILED (Is $pid not $NAME?  Is $DAEMON a different binary now?)."
	else
		echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
	fi
	;;
  restart)
	if ! su -c "$DAEMON --verify-config" debian-tor > /dev/null; then
		echo "Restarting Tor ABORTED: Tor configuration invalid:" >&2
		su -c "$DAEMON --verify-config" debian-tor >&2
		exit 1
	fi

	$0 stop
	sleep 1
	$0 start
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0