[or-cvs] Apply RPM-related cleanups from John Bashinski

Nick Mathewson nickm at seul.org
Sat Dec 24 04:03:41 UTC 2005


Update of /home/or/cvsroot/tor/contrib
In directory moria:/tmp/cvs-serv20806/contrib

Modified Files:
	tor.sh.in torctl.in 
Log Message:
Apply RPM-related cleanups from John Bashinski

Index: tor.sh.in
===================================================================
RCS file: /home/or/cvsroot/tor/contrib/tor.sh.in,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -p -d -r1.9 -r1.10
--- tor.sh.in	5 Jun 2005 14:44:27 -0000	1.9
+++ tor.sh.in	24 Dec 2005 04:03:39 -0000	1.10
@@ -1,28 +1,35 @@
 #!/bin/sh
 #
-#tor    The Onion Router
+# tor    The Onion Router
+#
+# Startup/shutdown script for tor. This is a wrapper around torctl;
+# torctl does the actual work in a relatively system-independent, or at least
+# distribution-independent, way, and this script deals with fitting the
+# whole thing into the conventions of the particular system at hand.
+# This particular script is written for Red Hat/Fedora Linux, and may
+# also work on Mandrake, but not SuSE.
+#
+# These next couple of lines "declare" tor for the "chkconfig" program,
+# originally from SGI, used on Red Hat/Fedora and probably elsewhere.
 #
 # chkconfig: 2345 90 10
-# description: Onion Router
-
-TORUSER=
-TORGROUP=
-TORBIN=@BINDIR@/tor
-TORPID=@LOCALSTATEDIR@/run/tor/tor.pid
-TORLOG=@LOCALSTATEDIR@/log/tor/tor.log
-TORDATA=@LOCALSTATEDIR@/lib/tor
+# description: Onion Router - A low-latency anonymous proxy
+#
 
-TORCONF=@CONFDIR@/torrc
-# Strictly speaking, we don't need to su if we have --user and --group.
-# "Belt and suspenders," says jbash.
-TORARGS="--pidfile $TORPID --log \"notice file $TORLOG \" --runasdaemon 1 --datadirectory $TORDATA"
-if [ "x$TORUSER" != "x" ]; then
-    TORARGS="$TORARGS --user $TORUSER"
-fi
-if [ "x$TORGROUP" != "x" ]; then
-    TORARGS="$TORARGS --group $TORGROUP"
+# Library functions
+if [ -f /etc/rc.d/init.d/functions ]; then
+   . /etc/rc.d/init.d/functions
+elif [ -f /etc/init.d/functions ]; then
+   . /etc/init.d/functions
 fi
-RETVAL=0
+
+TORCTL=@BINDIR@/torctl
+
+# torctl will use these environment variables
+TORUSER=@TORUSER@
+export TORUSER
+TORGROUP=@TORGROUP@
+export TORGROUP
 
 if [ -x /bin/su ] ; then
     SUPROG=/bin/su
@@ -39,87 +46,33 @@ fi
 case "$1" in
 
     start)
-    if [ -f $TORPID ]; then
-        echo "tor appears to be already running (pid file exists)"
-        echo "Maybe you should run: $0 restart ?"
-        RETVAL=1
-    else
-        echo -n "Starting tor..."
-        if  [ "x$TORUSER" = "x" ]; then
-            $TORBIN -f $TORCONF $TORARGS
-        else
-            $SUPROG -c "$TORBIN -f $TORCONF $TORARGS" $TORUSER
-        fi
-        RETVAL=$?
-        if [ $RETVAL -eq 0 ]; then
-            echo " ok"
-        else
-            echo " ERROR!"
-        fi
-    fi
+    action $"Starting tor:" $TORCTL start
+    RETVAL=$?
     ;;
 
     stop)
-    if [ -f $TORPID ]; then
-        echo -n "Killing tor..."
-        kill `cat $TORPID`
-        RETVAL=$?
-        if [ $RETVAL -eq 0 ]; then
-            echo " ok"
-        else
-            echo " ERROR!"
-        fi
-    else
-        echo "Unable to kill tor: $TORPID does not exist. Assuming already dead."
-        RETVAL=0
-    fi
-    ;;
-
-    reload)
-    if [ -f $TORPID ]; then
-        echo -n "Sending HUP to tor..."
-        kill -HUP `cat $TORPID`
-        RETVAL=$?
-        if [ $RETVAL -eq 0 ]; then
-            echo " ok"
-        else
-            echo " ERROR!"
-        fi
-    else
-        echo "Unable to kill tor: $TORPID does not exist"
-        RETVAL=1
-    fi
+    action $"Stopping tor:" $TORCTL stop
+    RETVAL=$?
     ;;
 
     restart)
-    $0 stop
-    if [ -f $TORPID ]; then
-            rm -f $TORPID
-    fi
-    $0 start
+    action $"Restarting tor:" $TORCTL restart
+    RETVAL=$?
     ;;
 
-    status)
-    PID=`cat $TORPID 2>/dev/null`
-    if [ "$PID" != "" ]; then
-        torstat=`ps -p $PID | grep -c "^$PID"`
-        if [ $torstat ]; then
-            echo "tor is running ($PID)"
-        else
-            echo "tor is not running (looks like it crashed, look for core?  $PID)"
-        fi
-    else
-        echo "tor is not running (exited gracefully)"
-    fi
+    reload)
+    action $"Reloading tor:" $TORCTL reload
+    RETVAL=$?
     ;;
 
-    log)
-    cat $TORLOG
+    status)
+    $TORCTL status
+    RETVAL=$?
     ;;
 
     *)
-    echo "Usage: $0 (start|stop|restart|status|log)"
-    exit 1
+    echo "Usage: $0 (start|stop|restart|reload|status)"
+    RETVAL=1
 esac
 
 exit $RETVAL

Index: torctl.in
===================================================================
RCS file: /home/or/cvsroot/tor/contrib/torctl.in,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -p -d -r1.3 -r1.4
--- torctl.in	5 Jun 2005 14:44:27 -0000	1.3
+++ torctl.in	24 Dec 2005 04:03:39 -0000	1.4
@@ -4,15 +4,15 @@
 # to controlling The Onion Router
 #
 # The exit codes returned are:
-#	0 - operation completed successfully
-#	1 -
-#	2 - Command not supported
-#	3 - Could not be started
-#	4 - Could not be stopped
-#	5 -
-#	6 -
-#	7 -
-#	8 -
+#       0 - operation completed successfully. For "status", tor running.
+#       1 - For "status", tor not running.
+#       2 - Command not supported
+#       3 - Could not be started or reloaded
+#       4 - Could not be stopped
+#       5 -
+#       6 -
+#       7 -
+#       8 -
 #
 # When multiple arguments are given, only the error from the _last_
 # one is reported.
@@ -27,26 +27,30 @@ EXEC=tor
 TORBIN="@BINDIR@/$EXEC"
 #
 # the path to the configuration file
-TORCONF=@CONFDIR@/torrc
+TORCONF="@CONFDIR@/torrc"
 #
 # the path to your PID file
-PIDFILE=@LOCALSTATEDIR@/run/tor/tor.pid
+PIDFILE="@LOCALSTATEDIR@/run/tor/tor.pid"
 #
 # The path to the log file
-LOGFILE=@LOCALSTATEDIR@/log/tor/tor.log
+LOGFILE="@LOCALSTATEDIR@/log/tor/tor.log"
 #
 # The path to the datadirectory
-TORDATA=@LOCALSTATEDIR@/lib/tor
+TORDATA="@LOCALSTATEDIR@/lib/tor"
 #
-# The USER and GROUP names:
-# TORUSER and TORGROUP if defined in the environment, else LOGNAME and GROUP
-# respectively.
-TORUSER=
-TORGROUP=
-
-TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE \" --runasdaemon 1"
+TORARGS="--pidfile $PIDFILE --log \"notice file $LOGFILE\" --runasdaemon 1"
 TORARGS="$TORARGS --datadirectory $TORDATA"
 
+# If user and group names are set in the environment, then use them;
+# otherwise run as the invoking user (or whatever user the config
+# file says)... unless the invoking user is root. The idea here is to
+# let an unprivileged user run tor for her own use using this script,
+# while still providing for it to be used as a system daemon.
+if [ "x`id -u`" = "x0" ]; then
+    TORUSER=@TORUSER@
+    TORGROUP=@TORGROUP@
+fi
+
 if [ "x$TORUSER" != "x" ]; then
     TORARGS="$TORARGS --user $TORUSER"
 fi
@@ -54,24 +58,10 @@ if [ "x$TORGROUP" != "x" ]; then
     TORARGS="$TORARGS --group $TORGROUP"
 fi
 
-if [ -x /bin/su ] ; then
-    SUPROG=/bin/su
-elif [ -x /sbin/su ] ; then
-    SUPROG=/sbin/su
-elif [ -x /usr/bin/su ] ; then
-    SUPROG=/usr/bin/su
-elif [ -x /usr/sbin/su ] ; then
-    SUPROG=/usr/sbin/su
-else
-    SUPROG=/bin/su
-fi
-
-# the command used to start
-if  [ "x$TORUSER" = "x" ]; then
-    START="$TORBIN -f $TORCONF $TORARGS"
-else
-    START="$SUPROG -c \\"$TORBIN -f $TORCONF $TORARGS\\" $TORUSER"
-fi
+# We no longer wrap the Tor daemon startup in an su when running as
+# root, because it's too painful to make the use of su portable.
+# Just let the daemon set the UID and GID.
+START="$TORBIN -f $TORCONF $TORARGS"
 
 #
 # --------------------                              --------------------
@@ -86,23 +76,23 @@ fi
 checkIfRunning ( ) {
     # check for pidfile
     PID=unknown
-	 if [ -f $PIDFILE ] ; then
+    if [ -f $PIDFILE ] ; then
         PID=`/bin/cat $PIDFILE`
         if [ "x$PID" != "x" ] ; then
-		      if kill -0 $PID 2>/dev/null ; then
-                STATUS="$EXEC (pid $PID) running"
-                RUNNING=1
-		      else
-            	 STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
-            	 RUNNING=0
-				fi
+            if kill -0 $PID 2>/dev/null ; then
+               STATUS="$EXEC (pid $PID) running"
+               RUNNING=1
+            else
+                STATUS="PID file ($PIDFILE) present, but $EXEC ($PID) not running"
+                RUNNING=0
+            fi
         else
             STATUS="$EXEC (pid $PID?) not running"
             RUNNING=0
         fi
     else
-        STATUS="$EXEC apparently not running (no pid file)"
-        RUNNING=0
+       STATUS="$EXEC apparently not running (no pid file)"
+       RUNNING=0
     fi
     return
 }
@@ -117,14 +107,14 @@ do
             echo "$0 $ARG: $EXEC (pid $PID) already running"
             continue
         fi
-        if $START ; then
+        if eval "$START" ; then
             echo "$0 $ARG: $EXEC started"
-				# Make sure it stayed up!
-				/bin/sleep 1
-				checkIfRunning
-				if [ $RUNNING -eq 0 ]; then
-   				 echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
-				fi
+            # Make sure it stayed up!
+            /bin/sleep 1
+            checkIfRunning
+            if [ $RUNNING -eq 0 ]; then
+                echo "$0 $ARG: $EXEC (pid $PID) quit unexpectedly"
+            fi
         else
             echo "$0 $ARG: $EXEC could not be started"
             ERROR=3
@@ -138,40 +128,68 @@ do
         if kill -15 $PID ; then
             echo "$0 $ARG: $EXEC stopped"
         else
-      	  /bin/sleep 1
-      	  if kill -9 $PID ; then
-            	echo "$0 $ARG: $EXEC stopped"
-      	  else
-            	echo "$0 $ARG: $EXEC could not be stopped"
-            	ERROR=4
-      	  fi
+            /bin/sleep 1
+            if kill -9 $PID ; then
+                echo "$0 $ARG: $EXEC stopped"
+            else
+                echo "$0 $ARG: $EXEC could not be stopped"
+                ERROR=4
+            fi
         fi
         # Make sure it really died!
         /bin/sleep 1
         checkIfRunning
         if [ $RUNNING -eq 1 ]; then
             echo "$0 $ARG: $EXEC (pid $PID) unexpectedly still running"
+            ERROR=4
         fi
         ;;
     restart)
         $0 stop start
         ;;
+    reload)
+        if [ $RUNNING -eq 0 ]; then
+            echo "$0 $ARG: $STATUS"
+            continue
+        fi
+        if kill -1 $PID; then
+            /bin/sleep 1
+            echo "$EXEC (PID $PID) reloaded"
+        else
+            echo "Can't reload $EXEC"
+            ERROR=3
+        fi
+        ;;
     status)
         echo $STATUS
+        if [ $RUNNING -eq 1 ]; then
+            ERROR=0
+        else
+            ERROR=1
+        fi
         ;;
-    *)
+    log)
+        cat $LOGFILE
+        ;;
+    help)
         echo "usage: $0 (start|stop|restart|status|help)"
         /bin/cat <<EOF
 
 start      - start $EXEC
 stop       - stop $EXEC
 restart    - stop and restart $EXEC if running or start if not running
+reload     - cause the running process to reinitialize itself
 status     - tell whether $EXEC is running or not
+log        - display the contents of the log file
 help       - this text
 
 EOF
+        ERROR=0
+        ;;
+    *)
+        $0 help
         ERROR=2
-    ;;
+        ;;
 
     esac
 



More information about the tor-commits mailing list