tor.init 3.3 KB

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