torctl.in 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. For "status", tor running.
  8. # 1 - For "status", tor not running.
  9. # 2 - Command not supported
  10. # 3 - Could not be started or reloaded
  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. TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE\" --runasdaemon 1"
  42. TORARGS="$TORARGS --datadirectory $TORDATA"
  43. # If user name is set in the environment, then use it;
  44. # otherwise run as the invoking user (or whatever user the config
  45. # file says)... unless the invoking user is root. The idea here is to
  46. # let an unprivileged user run tor for her own use using this script,
  47. # while still providing for it to be used as a system daemon.
  48. if [ "x`id -u`" = "x0" ]; then
  49. TORUSER=@TORUSER@
  50. fi
  51. if [ "x$TORUSER" != "x" ]; then
  52. TORARGS="$TORARGS --user $TORUSER"
  53. fi
  54. # We no longer wrap the Tor daemon startup in an su when running as
  55. # root, because it's too painful to make the use of su portable.
  56. # Just let the daemon set the UID and GID.
  57. START="$TORBIN -f $TORCONF $TORARGS"
  58. #
  59. # -------------------- --------------------
  60. # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||
  61. ERROR=0
  62. ARGV="$@"
  63. if [ "x$ARGV" = "x" ] ; then
  64. ARGS="help"
  65. fi
  66. checkIfRunning ( ) {
  67. # check for pidfile
  68. PID=unknown
  69. if [ -f $PIDFILE ] ; then
  70. PID=`/bin/cat $PIDFILE`
  71. if [ "x$PID" != "x" ] ; then
  72. if kill -0 $PID 2>/dev/null ; then
  73. STATUS="$EXEC (pid $PID) running"
  74. RUNNING=1
  75. else
  76. STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
  77. RUNNING=0
  78. fi
  79. else
  80. STATUS="$EXEC (pid $PID?) not running"
  81. RUNNING=0
  82. fi
  83. else
  84. STATUS="$EXEC apparently not running (no pid file)"
  85. RUNNING=0
  86. fi
  87. return
  88. }
  89. for ARG in $@ $ARGS
  90. do
  91. checkIfRunning
  92. case $ARG in
  93. start)
  94. if [ $RUNNING -eq 1 ]; then
  95. echo "$0 $ARG: $EXEC (pid $PID) already running"
  96. continue
  97. fi
  98. if eval "$START" ; then
  99. echo "$0 $ARG: $EXEC started"
  100. # Make sure it stayed up!
  101. /bin/sleep 1
  102. checkIfRunning
  103. if [ $RUNNING -eq 0 ]; then
  104. echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
  105. fi
  106. else
  107. echo "$0 $ARG: $EXEC could not be started"
  108. ERROR=3
  109. fi
  110. ;;
  111. stop)
  112. if [ $RUNNING -eq 0 ]; then
  113. echo "$0 $ARG: $STATUS"
  114. continue
  115. fi
  116. if kill -15 $PID ; then
  117. echo "$0 $ARG: $EXEC stopped"
  118. else
  119. /bin/sleep 1
  120. if kill -9 $PID ; then
  121. echo "$0 $ARG: $EXEC stopped"
  122. else
  123. echo "$0 $ARG: $EXEC could not be stopped"
  124. ERROR=4
  125. fi
  126. fi
  127. # Make sure it really died!
  128. /bin/sleep 1
  129. checkIfRunning
  130. if [ $RUNNING -eq 1 ]; then
  131. echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
  132. ERROR=4
  133. fi
  134. ;;
  135. restart)
  136. $0 stop start
  137. ;;
  138. reload)
  139. if [ $RUNNING -eq 0 ]; then
  140. echo "$0 $ARG: $STATUS"
  141. continue
  142. fi
  143. if kill -1 $PID; then
  144. /bin/sleep 1
  145. echo "$EXEC (PID $PID) reloaded"
  146. else
  147. echo "Can't reload $EXEC"
  148. ERROR=3
  149. fi
  150. ;;
  151. status)
  152. echo $STATUS
  153. if [ $RUNNING -eq 1 ]; then
  154. ERROR=0
  155. else
  156. ERROR=1
  157. fi
  158. ;;
  159. log)
  160. cat $LOGFILE
  161. ;;
  162. help)
  163. echo "usage: $0 (start|stop|restart|status|help)"
  164. /bin/cat <<EOF
  165. start - start $EXEC
  166. stop - stop $EXEC
  167. restart - stop and restart $EXEC if running or start if not running
  168. reload - cause the running process to reinitialize itself
  169. status - tell whether $EXEC is running or not
  170. log - display the contents of the log file
  171. help - this text
  172. EOF
  173. ERROR=0
  174. ;;
  175. *)
  176. $0 help
  177. ERROR=2
  178. ;;
  179. esac
  180. done
  181. exit $ERROR