tor.init 3.8 KB

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