tor.init 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. DEFAULTSFILE=/etc/default/$NAME
  23. WAITFORDAEMON=60
  24. ARGS=""
  25. # Let's try to figure our some sane defaults:
  26. if [ -r /proc/sys/fs/file-max ]; then
  27. system_max=`cat /proc/sys/fs/file-max`
  28. if [ "$system_max" -gt "80000" ] ; then
  29. MAX_FILEDESCRIPTORS=32768
  30. elif [ "$system_max" -gt "40000" ] ; then
  31. MAX_FILEDESCRIPTORS=16384
  32. elif [ "$system_max" -gt "10000" ] ; then
  33. MAX_FILEDESCRIPTORS=8192
  34. else
  35. MAX_FILEDESCRIPTORS=1024
  36. cat << EOF
  37. Warning: Your system has very few filedescriptors available in total.
  38. Maybe you should try raising that by adding 'fs.file-max=100000' to your
  39. /etc/sysctl.conf file. Feel free to pick any number that you deem appropriate.
  40. Then run 'sysctl -p'. See /proc/sys/fs/file-max for the current value, and
  41. file-nr in the same directory for how many of those are used at the moment.
  42. EOF
  43. fi
  44. else
  45. MAX_FILEDESCRIPTORS=8192
  46. fi
  47. NICE=""
  48. test -x $DAEMON || exit 0
  49. # Include tor defaults if available
  50. if [ -f $DEFAULTSFILE ] ; then
  51. . $DEFAULTSFILE
  52. fi
  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
  63. do
  64. cnt=`expr $cnt + 1`
  65. if [ $cnt -gt $WAITFORDAEMON ]
  66. then
  67. echo " FAILED."
  68. return 1
  69. fi
  70. sleep 1
  71. echo -n "."
  72. done
  73. fi
  74. fi
  75. return 0
  76. }
  77. check_torpiddir () {
  78. if test ! -d $TORPIDDIR; then
  79. #echo "There is no $TORPIDDIR directory. Creating one for you."
  80. mkdir -m 02700 "$TORPIDDIR"
  81. chown debian-tor:debian-tor "$TORPIDDIR"
  82. fi
  83. if test ! -x $TORPIDDIR; then
  84. echo "Cannot access $TORPIDDIR directory, are you root?" >&2
  85. exit 1
  86. fi
  87. }
  88. check_config () {
  89. if ! $DAEMON --verify-config > /dev/null; then
  90. echo "ABORTED: Tor configuration invalid:" >&2
  91. $DAEMON --verify-config >&2
  92. exit 1
  93. fi
  94. }
  95. case "$1" in
  96. start)
  97. if [ "$RUN_DAEMON" != "yes" ]; then
  98. echo "Not starting $DESC (Disabled in $DEFAULTSFILE)."
  99. exit 0
  100. fi
  101. if [ -n "$MAX_FILEDESCRIPTORS" ]; then
  102. echo -n "Raising maximum number of filedescriptors (ulimit -n) to $MAX_FILEDESCRIPTORS"
  103. if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
  104. echo "."
  105. else
  106. echo ": FAILED."
  107. fi
  108. fi
  109. check_torpiddir
  110. echo "Starting $DESC: $NAME..."
  111. check_config
  112. start-stop-daemon --start --quiet --oknodo \
  113. --pidfile $TORPID \
  114. $NICE \
  115. --exec $DAEMON -- $ARGS
  116. echo "done."
  117. ;;
  118. stop)
  119. echo -n "Stopping $DESC: "
  120. pid=`cat $TORPID 2>/dev/null` || true
  121. if test ! -f $TORPID -o -z "$pid"; then
  122. echo "not running (there is no $TORPID)."
  123. exit 0
  124. fi
  125. if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
  126. wait_for_deaddaemon $pid
  127. echo "$NAME."
  128. elif kill -0 $pid 2>/dev/null
  129. then
  130. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  131. else
  132. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  133. fi
  134. ;;
  135. reload|force-reload)
  136. echo -n "Reloading $DESC configuration: "
  137. pid=`cat $TORPID 2>/dev/null` || true
  138. if test ! -f $TORPID -o -z "$pid"; then
  139. echo "not running (there is no $TORPID)."
  140. exit 0
  141. fi
  142. check_config
  143. if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  144. then
  145. echo "$NAME."
  146. elif kill -0 $pid 2>/dev/null
  147. then
  148. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  149. else
  150. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  151. fi
  152. ;;
  153. restart)
  154. check_config
  155. $0 stop
  156. sleep 1
  157. $0 start
  158. ;;
  159. *)
  160. echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
  161. exit 1
  162. ;;
  163. esac
  164. exit 0