tor.init 3.5 KB

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