tor.init 3.8 KB

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