tor.init 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. --chuid debian-tor:debian-tor \
  104. --pidfile $TORPID \
  105. $NICE \
  106. --exec $DAEMON -- $ARGS
  107. echo "done."
  108. ;;
  109. stop)
  110. echo -n "Stopping $DESC: "
  111. pid=`cat $TORPID 2>/dev/null` || true
  112. if test ! -f $TORPID -o -z "$pid"; then
  113. echo "not running (there is no $TORPID)."
  114. exit 0
  115. fi
  116. if start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON; then
  117. wait_for_deaddaemon $pid
  118. echo "$NAME."
  119. elif kill -0 $pid 2>/dev/null
  120. then
  121. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  122. else
  123. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  124. fi
  125. ;;
  126. reload|force-reload)
  127. echo -n "Reloading $DESC configuration: "
  128. pid=`cat $TORPID 2>/dev/null` || true
  129. if test ! -f $TORPID -o -z "$pid"; then
  130. echo "not running (there is no $TORPID)."
  131. exit 0
  132. fi
  133. if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
  134. echo "ABORTED: Tor configuration invalid:" >&2
  135. su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
  136. exit 1
  137. fi
  138. if start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  139. then
  140. echo "$NAME."
  141. elif kill -0 $pid 2>/dev/null
  142. then
  143. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  144. else
  145. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  146. fi
  147. ;;
  148. restart)
  149. if ! su -s /bin/sh -c "$DAEMON --verify-config" debian-tor > /dev/null; then
  150. echo "Restarting Tor ABORTED: Tor configuration invalid:" >&2
  151. su -s /bin/sh -c "$DAEMON --verify-config" debian-tor >&2
  152. exit 1
  153. fi
  154. $0 stop
  155. sleep 1
  156. $0 start
  157. ;;
  158. *)
  159. echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
  160. exit 1
  161. ;;
  162. esac
  163. exit 0