tor.init 4.1 KB

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