torctl.in 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/sh
  2. #
  3. # TOR control script designed to allow an easy command line interface
  4. # to controlling The Onion Router
  5. #
  6. # The exit codes returned are:
  7. # 0 - operation completed successfully
  8. # 1 -
  9. # 2 - Command not supported
  10. # 3 - Could not be started
  11. # 4 - Could not be stopped
  12. # 5 -
  13. # 6 -
  14. # 7 -
  15. # 8 -
  16. #
  17. # When multiple arguments are given, only the error from the _last_
  18. # one is reported.
  19. #
  20. #
  21. # |||||||||||||||||||| START CONFIGURATION SECTION ||||||||||||||||||||
  22. # -------------------- --------------------
  23. # Name of the executable
  24. EXEC=tor
  25. #
  26. # the path to your binary, including options if necessary
  27. TORBIN="@BINDIR@/$EXEC"
  28. #
  29. # the path to the configuration file
  30. TORCONF=@CONFDIR@/torrc
  31. #
  32. # the path to your PID file
  33. PIDFILE=@LOCALSTATEDIR@/run/tor/tor.pid
  34. #
  35. # The path to the log file
  36. LOGFILE=@LOCALSTATEDIR@/log/tor/tor.log
  37. #
  38. # The path to the datadirectory
  39. TORDATA=@LOCALSTATEDIR@/lib/tor
  40. #
  41. # The USER and GROUP names:
  42. # TORUSER and TORGROUP if defined in the environment, else LOGNAME and GROUP
  43. # respectively.
  44. TORUSER=
  45. TORGROUP=
  46. TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE \" --runasdaemon 1"
  47. TORARGS="$TORARGS --datadirectory $TORDATA"
  48. if [ "x$TORUSER" != "x" ]; then
  49. TORARGS="$TORARGS --user $TORUSER"
  50. fi
  51. if [ "x$TORGROUP" != "x" ]; then
  52. TORARGS="$TORARGS --group $TORGROUP"
  53. fi
  54. # the command used to start
  55. if [ "x$TORUSER" = "x" ]; then
  56. START="$TORBIN -f $TORCONF $TORARGS"
  57. else
  58. START="/bin/su -c \\"$TORBIN -f $TORCONF $TORARGS\\" $TORUSER"
  59. fi
  60. #
  61. # -------------------- --------------------
  62. # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
  63. ERROR=0
  64. ARGV="$@"
  65. if [ "x$ARGV" = "x" ] ; then
  66. ARGS="help"
  67. fi
  68. checkIfRunning ( ) {
  69. # check for pidfile
  70. PID=unknown
  71. if [ -f $PIDFILE ] ; then
  72. PID=`/bin/cat $PIDFILE`
  73. if [ "x$PID" != "x" ] ; then
  74. if kill -0 $PID 2>/dev/null ; then
  75. STATUS="$EXEC (pid $PID) running"
  76. RUNNING=1
  77. else
  78. STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
  79. RUNNING=0
  80. fi
  81. else
  82. STATUS="$EXEC (pid $PID?) not running"
  83. RUNNING=0
  84. fi
  85. else
  86. STATUS="$EXEC apparently not running (no pid file)"
  87. RUNNING=0
  88. fi
  89. return
  90. }
  91. for ARG in $@ $ARGS
  92. do
  93. checkIfRunning
  94. case $ARG in
  95. start)
  96. if [ $RUNNING -eq 1 ]; then
  97. echo "$0 $ARG: $EXEC (pid $PID) already running"
  98. continue
  99. fi
  100. if $START ; then
  101. echo "$0 $ARG: $EXEC started"
  102. # Make sure it stayed up!
  103. /bin/sleep 1
  104. checkIfRunning
  105. if [ $RUNNING -eq 0 ]; then
  106. echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
  107. fi
  108. else
  109. echo "$0 $ARG: $EXEC could not be started"
  110. ERROR=3
  111. fi
  112. ;;
  113. stop)
  114. if [ $RUNNING -eq 0 ]; then
  115. echo "$0 $ARG: $STATUS"
  116. continue
  117. fi
  118. if kill -15 $PID ; then
  119. echo "$0 $ARG: $EXEC stopped"
  120. else
  121. /bin/sleep 1
  122. if kill -9 $PID ; then
  123. echo "$0 $ARG: $EXEC stopped"
  124. else
  125. echo "$0 $ARG: $EXEC could not be stopped"
  126. ERROR=4
  127. fi
  128. fi
  129. # Make sure it really died!
  130. /bin/sleep 1
  131. checkIfRunning
  132. if [ $RUNNING -eq 1 ]; then
  133. echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
  134. fi
  135. ;;
  136. restart)
  137. $0 stop start
  138. ;;
  139. status)
  140. echo $STATUS
  141. ;;
  142. *)
  143. echo "usage: $0 (start|stop|restart|status|help)"
  144. /bin/cat <<EOF
  145. start - start $EXEC
  146. stop - stop $EXEC
  147. restart - stop and restart $EXEC if running or start if not running
  148. status - tell whether $EXEC is running or not
  149. help - this text
  150. EOF
  151. ERROR=2
  152. ;;
  153. esac
  154. done
  155. exit $ERROR