[or-cvs] r9193: torctl: Apply patch from Karsten Loesing to fix several bugs (in torctl/trunk: . java/net/freehaven/tor/control java/net/freehaven/tor/control/examples)

nickm at seul.org nickm at seul.org
Mon Dec 25 04:37:57 UTC 2006


Author: nickm
Date: 2006-12-24 23:37:53 -0500 (Sun, 24 Dec 2006)
New Revision: 9193

Modified:
   torctl/trunk/ChangeLog
   torctl/trunk/java/net/freehaven/tor/control/TorControlConnection.java
   torctl/trunk/java/net/freehaven/tor/control/TorControlConnection0.java
   torctl/trunk/java/net/freehaven/tor/control/TorControlConnection1.java
   torctl/trunk/java/net/freehaven/tor/control/examples/Main.java
Log:
torctl: Apply patch from Karsten Loesing to fix several bugs when sending a signal that closes tor, or when tor shuts down on its own.

Modified: torctl/trunk/ChangeLog
===================================================================
--- torctl/trunk/ChangeLog	2006-12-25 03:55:37 UTC (rev 9192)
+++ torctl/trunk/ChangeLog	2006-12-25 04:37:53 UTC (rev 9193)
@@ -6,3 +6,6 @@
   - Fix behavior of signal in TorControlConnection0.java (from Oliver Rau)
   - Throw TorControlError, not Error.
   - Implement resetConf method in controllers.
+  - Fix bug when sending a signal that kills Tor; avoid a
+    NullPointerException when Tor exits. (java; from Karsten Loesing)
+  

Modified: torctl/trunk/java/net/freehaven/tor/control/TorControlConnection.java
===================================================================
--- torctl/trunk/java/net/freehaven/tor/control/TorControlConnection.java	2006-12-25 03:55:37 UTC (rev 9192)
+++ torctl/trunk/java/net/freehaven/tor/control/TorControlConnection.java	2006-12-25 04:37:53 UTC (rev 9193)
@@ -165,6 +165,10 @@
     /** Send a signal to the Tor process. */
     public abstract void signal(String signal) throws IOException;
 
+    /** Send a signal to the Tor process to shut it down or halt it.
+     * Does not wait for a response. */
+        public abstract void shutdownTor(String signal) throws IOException;
+
     /** Tell Tor to replace incoming addresses with those as listed in 'kvLines'.
      */
     public abstract Map mapAddresses(Collection kvLines) throws IOException;

Modified: torctl/trunk/java/net/freehaven/tor/control/TorControlConnection0.java
===================================================================
--- torctl/trunk/java/net/freehaven/tor/control/TorControlConnection0.java	2006-12-25 03:55:37 UTC (rev 9192)
+++ torctl/trunk/java/net/freehaven/tor/control/TorControlConnection0.java	2006-12-25 04:37:53 UTC (rev 9193)
@@ -327,6 +327,12 @@
         byte[] ba = { (byte)sig };
         sendAndWaitForResponse(CMD_SIGNAL, ba);
     }
+    
+    /** Send a signal to the Tor process to shut it down or halt it.
+     * Does not wait for a response. */
+        public void shutdownTor(String signal) throws IOException {
+                throw new RuntimeException("Method is not implemented!");
+        }
 
     public Map mapAddresses(Collection kvLines) throws IOException {
         StringBuffer sb = new StringBuffer();

Modified: torctl/trunk/java/net/freehaven/tor/control/TorControlConnection1.java
===================================================================
--- torctl/trunk/java/net/freehaven/tor/control/TorControlConnection1.java	2006-12-25 03:55:37 UTC (rev 9192)
+++ torctl/trunk/java/net/freehaven/tor/control/TorControlConnection1.java	2006-12-25 04:37:53 UTC (rev 9193)
@@ -103,6 +103,18 @@
         char c;
         do {
             String line = input.readLine();
+            if (line == null) {
+                // if line is null, the end of the stream has been reached, i.e.
+                // the connection to Tor has been closed!
+                if (reply.isEmpty()) {
+                        // nothing received so far, can exit cleanly
+                        return reply;
+                } else {
+                        // received half of a reply before the connection broke down
+                        throw new TorControlSyntaxError("Connection to Tor " +
+                                        " broke down while receiving reply!");
+                }
+            }
             if (debugOutput != null)
                 debugOutput.println("<< "+line);
             if (line.length() < 4)
@@ -135,6 +147,10 @@
     protected void react() throws IOException {
         while (true) {
             ArrayList lst = readReply();
+            if (lst.isEmpty()) {
+                // connection has been closed remotely! end the loop!
+                return;
+            }
             if (((ReplyLine)lst.get(0)).status.startsWith("6"))
                 handleEvent(lst);
             else {
@@ -383,6 +399,19 @@
         String cmd = "SIGNAL " + signal + "\r\n";
         sendAndWaitForResponse(cmd, null);
     }
+    
+    /** Send a signal to the Tor process to shut it down or halt it.
+     * Does not wait for a response. */
+        public void shutdownTor(String signal) throws IOException {
+                String s = "SIGNAL " + signal + "\r\n";
+        if (debugOutput != null)
+            debugOutput.print(">> "+s);
+        synchronized (waiters) {
+            output.write(s);
+            output.flush();
+        }        
+        }
+    
     /** Tells the Tor server that future SOCKS requests for connections to a set of original
     * addresses should be replaced with connections to the specified replacement
     * addresses.  Each element of <b>kvLines</b> is a String of the form

Modified: torctl/trunk/java/net/freehaven/tor/control/examples/Main.java
===================================================================
--- torctl/trunk/java/net/freehaven/tor/control/examples/Main.java	2006-12-25 03:55:37 UTC (rev 9192)
+++ torctl/trunk/java/net/freehaven/tor/control/examples/Main.java	2006-12-25 04:37:53 UTC (rev 9193)
@@ -121,7 +121,13 @@
     public static void signal(String[] args) throws IOException {
         // Usage signal [reload|shutdown|dump|debug|halt]
         TorControlConnection conn = getConnection(args, false);
-        conn.signal(args[1].toUpperCase());
+        // distinguish shutdown signal from other signals
+        if ("SHUTDOWN".equalsIgnoreCase(args[1])
+        		|| "HALT".equalsIgnoreCase(args[1])) {
+        	conn.shutdownTor(args[1].toUpperCase());
+        } else {
+        	conn.signal(args[1].toUpperCase());
+        }
     }
 
     public static void authDemo(String[] args) throws IOException {



More information about the tor-commits mailing list