tor.init 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #! /bin/sh
  2. set -e
  3. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  4. DAEMON=/usr/sbin/tor
  5. NAME=tor
  6. DESC="tor daemon"
  7. TORPIDDIR=/var/run/tor
  8. TORPID=$TORPIDDIR/tor.pid
  9. DEFAULTSFILE=/etc/default/$NAME
  10. WAITFORDAEMON=60
  11. ARGS=""
  12. MAX_FILEDESCRIPTORS=4096
  13. NICE=""
  14. test -x $DAEMON || exit 0
  15. # Include tor defaults if available
  16. if [ -f $DEFAULTSFILE ] ; then
  17. . $DEFAULTSFILE
  18. fi
  19. wait_for_deaddaemon () {
  20. pid=$1
  21. sleep 1
  22. if test -n "$pid"
  23. then
  24. if kill -0 $pid 2>/dev/null
  25. then
  26. echo -n "."
  27. cnt=0
  28. while kill -0 $pid 2>/dev/null
  29. do
  30. cnt=`expr $cnt + 1`
  31. if [ $cnt -gt $WAITFORDAEMON ]
  32. then
  33. echo " FAILED."
  34. return 1
  35. fi
  36. sleep 1
  37. echo -n "."
  38. done
  39. fi
  40. fi
  41. return 0
  42. }
  43. case "$1" in
  44. start)
  45. if [ "$RUN_DAEMON" != "yes" ]; then
  46. echo "Not starting $DESC (Disabled in $DEFAULTSFILE)."
  47. else
  48. if test ! -d $TORPIDDIR; then echo "There is no $TORPIDDIR directory." >&2; exit 1
  49. elif test ! -x $TORPIDDIR; then echo "Cannot access $TORPIDDIR directory, are you root?" >&2; exit 1;
  50. else
  51. echo "Starting $DESC: $NAME..."
  52. ulimit -n $MAX_FILEDESCRIPTORS || echo "Warn: Could not set ulimit for number of file descriptors." >&2
  53. start-stop-daemon --start --quiet --oknodo \
  54. --chuid debian-tor:debian-tor \
  55. --pidfile $TORPID \
  56. $NICE \
  57. --exec $DAEMON -- $ARGS
  58. echo "done."
  59. fi
  60. fi
  61. ;;
  62. stop)
  63. echo -n "Stopping $DESC: "
  64. pid=`cat $TORPID 2>/dev/null` || true
  65. if test ! -d $TORPIDDIR; then echo "There is no $TORPIDDIR directory." >&2; exit 1
  66. elif test ! -x $TORPIDDIR; then echo "Cannot access $TORPIDDIR directory, are you root?" >&2; exit 1;
  67. elif test ! -f $TORPID -o -z "$pid"
  68. then
  69. echo "not running (there is no $TORPID)."
  70. elif start-stop-daemon --stop --signal INT --quiet --pidfile $TORPID --exec $DAEMON
  71. then
  72. wait_for_deaddaemon $pid
  73. echo "$NAME."
  74. elif kill -0 $pid 2>/dev/null
  75. then
  76. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  77. else
  78. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  79. fi
  80. ;;
  81. reload|force-reload)
  82. echo -n "Reloading $DESC configuration: "
  83. pid=`cat $TORPID 2>/dev/null` || true
  84. if test ! -d $TORPIDDIR; then echo "There is no $TORPIDDIR directory." >&2; exit 1
  85. elif test ! -x $TORPIDDIR; then echo "Cannot access $TORPIDDIR directory, are you root?" >&2; exit 1;
  86. elif test ! -f $TORPID -o -z "$pid"
  87. then
  88. echo "not running (there is no $TORPID)."
  89. elif start-stop-daemon --stop --signal 1 --quiet --pidfile $TORPID --exec $DAEMON
  90. then
  91. echo "$NAME."
  92. elif kill -0 $pid 2>/dev/null
  93. then
  94. echo "FAILED (Is $pid not $NAME? Is $DAEMON a different binary now?)."
  95. else
  96. echo "FAILED ($DAEMON died: process $pid not running; or permission denied)."
  97. fi
  98. ;;
  99. restart)
  100. $0 stop
  101. sleep 1
  102. $0 start
  103. ;;
  104. *)
  105. echo "Usage: $0 {start|stop|restart|reload|force-reload}" >&2
  106. exit 1
  107. ;;
  108. esac
  109. exit 0