tor.init 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. fi
  37. else
  38. MAX_FILEDESCRIPTORS=8192
  39. fi
  40. NICE=""
  41. test -x $DAEMON || exit 0
  42. # Include tor defaults if available
  43. if [ -f $DEFAULTSFILE ] ; then
  44. . $DEFAULTSFILE
  45. fi
  46. wait_for_deaddaemon () {
  47. pid=$1
  48. sleep 1
  49. if test -n "$pid"
  50. then
  51. if kill -0 $pid 2>/dev/null
  52. then
  53. echo -n "."
  54. cnt=0
  55. while kill -0 $pid 2>/dev/null
  56. do
  57. cnt=`expr $cnt + 1`
  58. if [ $cnt -gt $WAITFORDAEMON ]
  59. then
  60. echo " FAILED."
  61. return 1
  62. fi
  63. sleep 1
  64. echo -n "."
  65. done
  66. fi
  67. fi
  68. return 0
  69. }
  70. check_torpiddir () {
  71. if test ! -d $TORPIDDIR; then
  72. echo "There is no $TORPIDDIR directory. Creating one for you."
  73. mkdir -m 02700 "$TORPIDDIR"
  74. chown debian-tor:debian-tor "$TORPIDDIR"
  75. fi
  76. if test ! -x $TORPIDDIR; then
  77. echo "Cannot access $TORPIDDIR directory, are you root?" >&2
  78. exit 1
  79. fi
  80. }
  81. check_config () {
  82. if ! $DAEMON --verify-config > /dev/null; then
  83. echo "ABORTED: Tor configuration invalid:" >&2
  84. $DAEMON --verify-config >&2
  85. exit 1
  86. fi
  87. }
  88. case "$1" in
  89. start)
  90. if [ "$RUN_DAEMON" != "yes" ]; then
  91. echo "Not starting $DESC (Disabled in $DEFAULTSFILE)."
  92. exit 0
  93. fi
  94. if [ -n "$MAX_FILEDESCRIPTORS" ]; then
  95. echo -n "Raising maximum number of filedescriptors (ulimit -n) to $MAX_FILEDESCRIPTORS"
  96. if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
  97. echo "."
  98. else
  99. echo ": FAILED."
  100. fi
  101. fi
  102. check_torpiddir
  103. echo "Starting $DESC: $NAME..."
  104. check_config
  105. start-stop-daemon --start --quiet --oknodo \
  106. --pidfile $TORPID \
  107. $NICE \
  108. --exec $DAEMON -- $ARGS
  109. echo "done."
  110. ;;
  111. stop)
  112. echo -n "Stopping $DESC: "
  113. pid=`cat $TORPID 2>/dev/null` || true
  114. if test ! -f $TORPID -o -z "$pid"; then
  115. echo "not running (there is no $TORPID)."
  116. exit 0
  117. fi
  118. if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
  119. wait_for_deaddaemon $pid
  120. echo "$NAME."
  121. elif kill -0 $pid 2>/dev/null
  122. then
  123. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  124. else
  125. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  126. fi
  127. ;;
  128. reload|force-reload)
  129. echo -n "Reloading $DESC configuration: "
  130. pid=`cat $TORPID 2>/dev/null` || true
  131. if test ! -f $TORPID -o -z "$pid"; then
  132. echo "not running (there is no $TORPID)."
  133. exit 0
  134. fi
  135. check_config
  136. if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  137. then
  138. echo "$NAME."
  139. elif kill -0 $pid 2>/dev/null
  140. then
  141. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  142. else
  143. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  144. fi
  145. ;;
  146. restart)
  147. check_config
  148. $0 stop
  149. sleep 1
  150. $0 start
  151. ;;
  152. *)
  153. echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
  154. exit 1
  155. ;;
  156. esac
  157. exit 0