tor.init 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #! /bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: tor
  4. # Required-Start: $local_fs $remote_fs $network $named $time
  5. # Required-Stop: $local_fs $remote_fs $network $named $time
  6. # Should-Start: $syslog
  7. # Should-Stop: $syslog
  8. # Default-Start: 2 3 4 5
  9. # Default-Stop: 0 1 6
  10. # Short-Description: Starts The Onion Router daemon processes
  11. # Description: Start The Onion Router, a TCP overlay
  12. # network client that provides anonymous
  13. # transport.
  14. ### END INIT INFO
  15. set -e
  16. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  17. DAEMON=/usr/sbin/tor
  18. NAME=tor
  19. DESC="tor daemon"
  20. TORPIDDIR=/var/run/tor
  21. TORPID=$TORPIDDIR/tor.pid
  22. DAEMON_USER=debian-tor
  23. DAEMON_NAME=tor
  24. DEFAULTSFILE=/etc/default/$NAME
  25. WAITFORDAEMON=60
  26. ARGS=""
  27. # Let's try to figure our some sane defaults:
  28. if [ -r /proc/sys/fs/file-max ]; then
  29. system_max=`cat /proc/sys/fs/file-max`
  30. if [ "$system_max" -gt "80000" ] ; then
  31. MAX_FILEDESCRIPTORS=32768
  32. elif [ "$system_max" -gt "40000" ] ; then
  33. MAX_FILEDESCRIPTORS=16384
  34. elif [ "$system_max" -gt "10000" ] ; then
  35. MAX_FILEDESCRIPTORS=8192
  36. else
  37. MAX_FILEDESCRIPTORS=1024
  38. cat << EOF
  39. Warning: Your system has very few filedescriptors available in total.
  40. Maybe you should try raising that by adding 'fs.file-max=100000' to your
  41. /etc/sysctl.conf file. Feel free to pick any number that you deem appropriate.
  42. Then run 'sysctl -p'. See /proc/sys/fs/file-max for the current value, and
  43. file-nr in the same directory for how many of those are used at the moment.
  44. EOF
  45. fi
  46. else
  47. MAX_FILEDESCRIPTORS=8192
  48. fi
  49. NICE=""
  50. test -x $DAEMON || exit 0
  51. # Include tor defaults if available
  52. if [ -f $DEFAULTSFILE ] ; then
  53. . $DEFAULTSFILE
  54. fi
  55. wait_for_deaddaemon () {
  56. pid=$1
  57. sleep 1
  58. if test -n "$pid"
  59. then
  60. if kill -0 $pid 2>/dev/null
  61. then
  62. echo -n "."
  63. cnt=0
  64. while kill -0 $pid 2>/dev/null
  65. do
  66. cnt=`expr $cnt + 1`
  67. if [ $cnt -gt $WAITFORDAEMON ]
  68. then
  69. echo " FAILED."
  70. return 1
  71. fi
  72. sleep 1
  73. echo -n "."
  74. done
  75. fi
  76. fi
  77. return 0
  78. }
  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"
  84. fi
  85. if test ! -x $TORPIDDIR; then
  86. echo "Cannot access $TORPIDDIR directory, are you root?" >&2
  87. exit 1
  88. fi
  89. }
  90. check_config () {
  91. if ! $DAEMON --verify-config > /dev/null; then
  92. echo "ABORTED: Tor configuration invalid:" >&2
  93. $DAEMON --verify-config >&2
  94. exit 1
  95. fi
  96. }
  97. case "$1" in
  98. start)
  99. if [ "$RUN_DAEMON" != "yes" ]; then
  100. echo "Not starting $DESC (Disabled in $DEFAULTSFILE)."
  101. exit 0
  102. fi
  103. if [ -n "$MAX_FILEDESCRIPTORS" ]; then
  104. echo -n "Raising maximum number of filedescriptors (ulimit -n) to $MAX_FILEDESCRIPTORS"
  105. if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
  106. echo "."
  107. else
  108. echo ": FAILED."
  109. fi
  110. fi
  111. check_torpiddir
  112. echo "Starting $DESC: $NAME..."
  113. check_config
  114. start-stop-daemon --start --quiet --oknodo \
  115. --pidfile $TORPID \
  116. $NICE \
  117. --exec $DAEMON -- $ARGS
  118. echo "done."
  119. ;;
  120. stop)
  121. echo -n "Stopping $DESC: "
  122. pid=`cat $TORPID 2>/dev/null` || true
  123. if test ! -f $TORPID -o -z "$pid"; then
  124. echo "not running (there is no $TORPID)."
  125. exit 0
  126. fi
  127. if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --name $DAEMON_NAME --user $DAEMON_USER; then
  128. wait_for_deaddaemon $pid
  129. echo "$NAME."
  130. elif kill -0 $pid 2>/dev/null
  131. then
  132. echo "FAILED (Is $pid not $DAEMON_NAME or not running as $DAEMON_USER?)."
  133. else
  134. echo "FAILED ($DAEMON_NAME died: process $pid not running; or permission denied)."
  135. fi
  136. ;;
  137. reload|force-reload)
  138. echo -n "Reloading $DESC configuration: "
  139. pid=`cat $TORPID 2>/dev/null` || true
  140. if test ! -f $TORPID -o -z "$pid"; then
  141. echo "not running (there is no $TORPID)."
  142. exit 0
  143. fi
  144. check_config
  145. if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --name $DAEMON_NAME --user $DAEMON_USER
  146. then
  147. echo "$NAME."
  148. elif kill -0 $pid 2>/dev/null
  149. then
  150. echo "FAILED (Is $pid not $DAEMON_NAME or not running as $DAEMON_USER?)."
  151. else
  152. echo "FAILED ($DAEMON_NAME died: process $pid not running; or permission denied)."
  153. fi
  154. ;;
  155. restart)
  156. check_config
  157. $0 stop
  158. sleep 1
  159. $0 start
  160. ;;
  161. *)
  162. echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
  163. exit 1
  164. ;;
  165. esac
  166. exit 0