tor.init 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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=4096
  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. echo "Starting $DESC: $NAME..."
  69. ulimit -n $MAX_FILEDESCRIPTORS || echo "Warn: Could not set ulimit for number of file descriptors." >&2
  70. start-stop-daemon --start --quiet --oknodo \
  71. --chuid debian-tor:debian-tor \
  72. --pidfile $TORPID \
  73. $NICE \
  74. --exec $DAEMON -- $ARGS
  75. echo "done."
  76. fi
  77. fi
  78. ;;
  79. stop)
  80. echo -n "Stopping $DESC: "
  81. pid=`cat $TORPID 2>/dev/null` || true
  82. if test ! -d $TORPIDDIR; then echo "There is no $TORPIDDIR directory." >&2; exit 1
  83. elif test ! -x $TORPIDDIR; then echo "Cannot access $TORPIDDIR directory, are you root?" >&2; exit 1;
  84. elif test ! -f $TORPID -o -z "$pid"
  85. then
  86. echo "not running (there is no $TORPID)."
  87. elif start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON
  88. then
  89. wait_for_deaddaemon $pid
  90. echo "$NAME."
  91. elif kill -0 $pid 2>/dev/null
  92. then
  93. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  94. else
  95. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  96. fi
  97. ;;
  98. reload|force-reload)
  99. echo -n "Reloading $DESC configuration: "
  100. pid=`cat $TORPID 2>/dev/null` || true
  101. if test ! -d $TORPIDDIR; then echo "There is no $TORPIDDIR directory." >&2; exit 1
  102. elif test ! -x $TORPIDDIR; then echo "Cannot access $TORPIDDIR directory, are you root?" >&2; exit 1;
  103. elif test ! -f $TORPID -o -z "$pid"
  104. then
  105. echo "not running (there is no $TORPID)."
  106. elif start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  107. then
  108. echo "$NAME."
  109. elif kill -0 $pid 2>/dev/null
  110. then
  111. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  112. else
  113. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  114. fi
  115. ;;
  116. restart)
  117. $0 stop
  118. sleep 1
  119. $0 start
  120. ;;
  121. *)
  122. echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
  123. exit 1
  124. ;;
  125. esac
  126. exit 0