torctl.in 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. if [ -x /bin/su ] ; then
  55. SUPROG=/bin/su
  56. elif [ -x /sbin/su ] ; then
  57. SUPROG=/sbin/su
  58. elif [ -x /usr/bin/su ] ; then
  59. SUPROG=/usr/bin/su
  60. elif [ -x /usr/sbin/su ] ; then
  61. SUPROG=/usr/sbin/su
  62. else
  63. SUPROG=/bin/su
  64. fi
  65. # the command used to start
  66. if [ "x$TORUSER" = "x" ]; then
  67. START="$TORBIN -f $TORCONF $TORARGS"
  68. else
  69. START="$SUPROG -c \\"$TORBIN -f $TORCONF $TORARGS\\" $TORUSER"
  70. fi
  71. #
  72. # -------------------- --------------------
  73. # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
  74. ERROR=0
  75. ARGV="$@"
  76. if [ "x$ARGV" = "x" ] ; then
  77. ARGS="help"
  78. fi
  79. checkIfRunning ( ) {
  80. # check for pidfile
  81. PID=unknown
  82. if [ -f $PIDFILE ] ; then
  83. PID=`/bin/cat $PIDFILE`
  84. if [ "x$PID" != "x" ] ; then
  85. if kill -0 $PID 2>/dev/null ; then
  86. STATUS="$EXEC (pid $PID) running"
  87. RUNNING=1
  88. else
  89. STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
  90. RUNNING=0
  91. fi
  92. else
  93. STATUS="$EXEC (pid $PID?) not running"
  94. RUNNING=0
  95. fi
  96. else
  97. STATUS="$EXEC apparently not running (no pid file)"
  98. RUNNING=0
  99. fi
  100. return
  101. }
  102. for ARG in $@ $ARGS
  103. do
  104. checkIfRunning
  105. case $ARG in
  106. start)
  107. if [ $RUNNING -eq 1 ]; then
  108. echo "$0 $ARG: $EXEC (pid $PID) already running"
  109. continue
  110. fi
  111. if $START ; then
  112. echo "$0 $ARG: $EXEC started"
  113. # Make sure it stayed up!
  114. /bin/sleep 1
  115. checkIfRunning
  116. if [ $RUNNING -eq 0 ]; then
  117. echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
  118. fi
  119. else
  120. echo "$0 $ARG: $EXEC could not be started"
  121. ERROR=3
  122. fi
  123. ;;
  124. stop)
  125. if [ $RUNNING -eq 0 ]; then
  126. echo "$0 $ARG: $STATUS"
  127. continue
  128. fi
  129. if kill -15 $PID ; then
  130. echo "$0 $ARG: $EXEC stopped"
  131. else
  132. /bin/sleep 1
  133. if kill -9 $PID ; then
  134. echo "$0 $ARG: $EXEC stopped"
  135. else
  136. echo "$0 $ARG: $EXEC could not be stopped"
  137. ERROR=4
  138. fi
  139. fi
  140. # Make sure it really died!
  141. /bin/sleep 1
  142. checkIfRunning
  143. if [ $RUNNING -eq 1 ]; then
  144. echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
  145. fi
  146. ;;
  147. restart)
  148. $0 stop start
  149. ;;
  150. status)
  151. echo $STATUS
  152. ;;
  153. *)
  154. echo "usage: $0 (start|stop|restart|status|help)"
  155. /bin/cat <<EOF
  156. start - start $EXEC
  157. stop - stop $EXEC
  158. restart - stop and restart $EXEC if running or start if not running
  159. status - tell whether $EXEC is running or not
  160. help - this text
  161. EOF
  162. ERROR=2
  163. ;;
  164. esac
  165. done
  166. exit $ERROR