tor.init 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. case "$1" in
  82. start)
  83. if [ "$RUN_DAEMON" != "yes" ]; then
  84. echo "Not starting $DESC (Disabled in $DEFAULTSFILE)."
  85. exit 0
  86. fi
  87. if [ -n "$MAX_FILEDESCRIPTORS" ]; then
  88. echo -n "Raising maximum number of filedescriptors (ulimit -n) to $MAX_FILEDESCRIPTORS"
  89. if ulimit -n "$MAX_FILEDESCRIPTORS" ; then
  90. echo "."
  91. else
  92. echo ": FAILED."
  93. fi
  94. fi
  95. check_torpiddir
  96. echo "Starting $DESC: $NAME..."
  97. if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
  98. echo "ABORTED: Tor configuration invalid:" >&2
  99. su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
  100. exit 1
  101. fi
  102. start-stop-daemon --start --quiet --oknodo \
  103. --pidfile $TORPID \
  104. $NICE \
  105. --exec $DAEMON -- $ARGS
  106. echo "done."
  107. ;;
  108. stop)
  109. echo -n "Stopping $DESC: "
  110. pid=`cat $TORPID 2>/dev/null` || true
  111. if test ! -f $TORPID -o -z "$pid"; then
  112. echo "not running (there is no $TORPID)."
  113. exit 0
  114. fi
  115. if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
  116. wait_for_deaddaemon $pid
  117. echo "$NAME."
  118. elif kill -0 $pid 2>/dev/null
  119. then
  120. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  121. else
  122. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  123. fi
  124. ;;
  125. reload|force-reload)
  126. echo -n "Reloading $DESC configuration: "
  127. 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
  131. fi
  132. if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
  133. echo "ABORTED: Tor configuration invalid:" >&2
  134. su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
  135. exit 1
  136. fi
  137. if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  138. then
  139. echo "$NAME."
  140. elif kill -0 $pid 2>/dev/null
  141. then
  142. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  143. else
  144. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  145. fi
  146. ;;
  147. restart)
  148. if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
  149. echo "Restarting Tor ABORTED: Tor configuration invalid:" >&2
  150. su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
  151. exit 1
  152. fi
  153. $0 stop
  154. sleep 1
  155. $0 start
  156. ;;
  157. *)
  158. echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
  159. exit 1
  160. ;;
  161. esac
  162. exit 0