[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1993: Create a single TorProcess object used to start and stop Tor (in trunk: . src/control)



Author: edmanm
Date: 2007-10-10 21:27:06 -0400 (Wed, 10 Oct 2007)
New Revision: 1993

Modified:
   trunk/
   trunk/src/control/torcontrol.cpp
   trunk/src/control/torcontrol.h
Log:
 r2049@lysithea:  edmanm | 2007-10-10 21:26:45 -0400
 Create a single TorProcess object used to start and stop Tor, instead of
 creating a new one each time we start Tor and cleaning it up when Tor stops.
 Fixes ticket #294, but could use testing on Qt 4.1 if anyone is still running
 that.



Property changes on: trunk
___________________________________________________________________
 svk:merge ticket from /local/vidalia/trunk [r2049] on dc66be73-d13e-47ba-a267-8dc7cda68c65

Modified: trunk/src/control/torcontrol.cpp
===================================================================
--- trunk/src/control/torcontrol.cpp	2007-10-11 01:26:50 UTC (rev 1992)
+++ trunk/src/control/torcontrol.cpp	2007-10-11 01:27:06 UTC (rev 1993)
@@ -33,18 +33,9 @@
 /** Default constructor */
 TorControl::TorControl()
 {
-  /* For reasons currently unknown to me, QProcess saves some state
-   * information between executions of a process. Consequently, if we started
-   * Tor, it crashed, and then we tried to restart it, Vidalia would crash in
-   * the QProcess code. So, we create a new TorProcess object each time we
-   * start Tor and then destroy it when it stops. */
-  _torProcess = 0;
-
-  /** Create an instance of a connection to Tor's control interface and give
+  /* Create an instance of a connection to Tor's control interface and give
    * it an object to use to handle asynchronous events. */
   _controlConn = new ControlConnection(&_torEvents);
-
-  /* Plumb the appropriate socket signals */
   QObject::connect(_controlConn, SIGNAL(connected()),
                    this, SLOT(onConnected()));
   QObject::connect(_controlConn, SIGNAL(connectFailed(QString)),
@@ -52,6 +43,17 @@
   QObject::connect(_controlConn, SIGNAL(disconnected()),
                    this, SLOT(onDisconnected()));
 
+  /* Create an object used to start and stop a Tor process. */
+  _torProcess = new TorProcess(this);
+  QObject::connect(_torProcess, SIGNAL(started()),
+                   this, SLOT(onStarted()));
+  QObject::connect(_torProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
+                   this, SLOT(onStopped(int, QProcess::ExitStatus)));
+  QObject::connect(_torProcess, SIGNAL(startFailed(QString)),
+                   this, SLOT(onStartFailed(QString)));
+  QObject::connect(_torProcess, SIGNAL(log(QString, QString)),
+                   this, SLOT(onLogStdout(QString, QString)));
+
 #if defined(Q_OS_WIN32)
   _torService = new TorService(this);
   QObject::connect(_torService, SIGNAL(started()), this, SLOT(onStarted()));
@@ -85,27 +87,15 @@
     emit started();
   } else {
 #if defined(Q_OS_WIN32)
-    if (TorService::isSupported() && _torService->isInstalled()) {
+    /* If the Tor service is installed, run that. Otherwise, start a new
+     * Tor process. */
+    if (TorService::isSupported() && _torService->isInstalled())
       _torService->start();
-      
-    } else {
-#endif
-      _torProcess = new TorProcess;
-  
-      /* Plumb the process signals */
-      QObject::connect(_torProcess, SIGNAL(started()),
-                       this, SLOT(onStarted()));
-      QObject::connect(_torProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
-                       this, SLOT(onStopped(int, QProcess::ExitStatus)));
-      QObject::connect(_torProcess, SIGNAL(startFailed(QString)),
-                       this, SLOT(onStartFailed(QString)));
-      QObject::connect(_torProcess, SIGNAL(log(QString, QString)),
-                       this, SLOT(onLogStdout(QString, QString)));
-  
-      /* Kick off the Tor process. */
+    else
       _torProcess->start(expand_filename(tor), args);
-#if defined(Q_OS_WIN32)
-    }
+#else
+    /* Start a new Tor process */
+    _torProcess->start(expand_filename(tor), args);
 #endif
   }
 }
@@ -122,9 +112,6 @@
 void
 TorControl::onStartFailed(QString errmsg)
 {
-  /* If an error occurs we need to clean up the tor process */
-  closeTorProcess();
-
   emit startFailed(errmsg);
 }
 
@@ -149,8 +136,6 @@
 void
 TorControl::onStopped(int exitCode, QProcess::ExitStatus exitStatus)
 {
-  closeTorProcess();
-  
   if (_controlConn->status() == ControlConnection::Connecting)
     _controlConn->cancelConnect();
   
@@ -158,36 +143,20 @@
   emit stopped(exitCode, exitStatus);
 }
 
-/** Disconnect signals from _torProcess and clean up after it. */
-void
-TorControl::closeTorProcess()
-{
-  if (_torProcess) {
-    QObject::disconnect(_torProcess, 0, 0, 0);
-    delete _torProcess;
-    _torProcess = 0;
-  }
-}
-
 /** Detects if the Tor process is running under Vidalia. Returns true if
  * Vidalia owns the Tor process, or false if it was an independent Tor. */
 bool
 TorControl::isVidaliaRunningTor()
 {
-  if (_torProcess) {
-    return (_torProcess->pid() != 0);
-  }
-  return false;
+  return (_torProcess->state() != QProcess::NotRunning);
 }
 
 /** Detect if the Tor process is running. */
 bool
 TorControl::isRunning()
 {
-  if (_torProcess) {
-    return (_torProcess->pid() != 0);
-  }
-  return _controlConn->isConnected();
+  return (_torProcess->state() != QProcess::NotRunning
+            || _controlConn->isConnected());
 }
 
 /** Called when Tor has printed a log message to stdout. */

Modified: trunk/src/control/torcontrol.h
===================================================================
--- trunk/src/control/torcontrol.h	2007-10-11 01:26:50 UTC (rev 1992)
+++ trunk/src/control/torcontrol.h	2007-10-11 01:27:06 UTC (rev 1993)
@@ -205,8 +205,6 @@
   bool send(ControlCommand cmd, ControlReply &reply, QString *errmsg = 0);
   /** Send a message to Tor and discard the response */
   bool send(ControlCommand cmd, QString *errmsg = 0);
-  /** Disconnects signals from the TorProcess and frees its memory. */
-  void closeTorProcess();
   
 /* The slots below simply relay signals from the appropriate member objects */
 private slots: