torctl.in 5.0 KB

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