Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > media > contrib-backports > by-pkgid > 9601c7beb4ff23e834bfa171795ed560 > files > 343

vidalia-0.2.9-1mdv2010.1.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Vidalia: TorProcess.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.3 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
    </ul>
  </div>
  <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a>&nbsp;&raquo;&nbsp;<a class="el" href="dir_67a2ce25601334632bdc5ef4a72c0f17.html">torcontrol</a>
  </div>
</div>
<div class="contents">
<h1>TorProcess.cpp</h1><a href="_tor_process_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment">**  This file is part of Vidalia, and is subject to the license terms in the</span>
<a name="l00003"></a>00003 <span class="comment">**  LICENSE file, found in the top level directory of this distribution. If </span>
<a name="l00004"></a>00004 <span class="comment">**  you did not receive the LICENSE file with this file, you may obtain it</span>
<a name="l00005"></a>00005 <span class="comment">**  from the Vidalia source package distributed by the Vidalia Project at</span>
<a name="l00006"></a>00006 <span class="comment">**  http://www.vidalia-project.net/. No part of Vidalia, including this file,</span>
<a name="l00007"></a>00007 <span class="comment">**  may be copied, modified, propagated, or distributed except according to</span>
<a name="l00008"></a>00008 <span class="comment">**  the terms described in the LICENSE file.</span>
<a name="l00009"></a>00009 <span class="comment">*/</span>
<a name="l00010"></a>00010 
<a name="l00011"></a>00011 <span class="comment">/* </span>
<a name="l00012"></a>00012 <span class="comment">** \file TorProcess.cpp</span>
<a name="l00013"></a>00013 <span class="comment">** \version $Id: TorProcess.cpp 3735 2009-04-28 20:28:01Z edmanm $</span>
<a name="l00014"></a>00014 <span class="comment">** \brief Starts and stops a Tor process</span>
<a name="l00015"></a>00015 <span class="comment">*/</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="preprocessor">#include &quot;<a class="code" href="_tor_process_8h.html">TorProcess.h</a>&quot;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;<a class="code" href="tcglobal_8h.html">tcglobal.h</a>&quot;</span>
<a name="l00019"></a>00019 
<a name="l00020"></a>00020 <span class="preprocessor">#include &quot;<a class="code" href="stringutil_8h.html">stringutil.h</a>&quot;</span>
<a name="l00021"></a>00021 
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;QString&gt;</span>
<a name="l00023"></a>00023 
<a name="l00024"></a>00024 <span class="comment">/* Needed for _PROCESS_INFORMATION so that pid() works on Win32 */</span>
<a name="l00025"></a>00025 <span class="preprocessor">#if defined (Q_OS_WIN32)</span>
<a name="l00026"></a>00026 <span class="preprocessor"></span><span class="preprocessor">#include &lt;windows.h&gt;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#endif</span>
<a name="l00028"></a>00028 <span class="preprocessor"></span>
<a name="l00029"></a>00029 <span class="comment"></span>
<a name="l00030"></a>00030 <span class="comment">/** Default constructor */</span>
<a name="l00031"></a><a class="code" href="class_tor_process.html#a49a18f443ed7d77e73944ff3465bd4fa">00031</a> <a class="code" href="class_tor_process.html#a49a18f443ed7d77e73944ff3465bd4fa">TorProcess::TorProcess</a>(QObject *parent)
<a name="l00032"></a>00032 : QProcess(parent)
<a name="l00033"></a>00033 {
<a name="l00034"></a>00034   <a class="code" href="class_tor_process.html#a4a6956ca20e13905543ce638c8ce1c93">openStdout</a>();
<a name="l00035"></a>00035   connect(<span class="keyword">this</span>, SIGNAL(readyReadStandardOutput()), 
<a name="l00036"></a>00036           <span class="keyword">this</span>,   SLOT(<a class="code" href="class_tor_process.html#a167afd9d6dfd04f7d3d211fa2ea9ad04">onReadyRead</a>()));
<a name="l00037"></a>00037   connect(<span class="keyword">this</span>, SIGNAL(<a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">error</a>(QProcess::ProcessError)), 
<a name="l00038"></a>00038           <span class="keyword">this</span>,   SLOT(<a class="code" href="class_tor_process.html#a5d95d00bf190facac9e635a6a3d8fc59">onError</a>(QProcess::ProcessError)));
<a name="l00039"></a>00039 }
<a name="l00040"></a>00040 <span class="comment"></span>
<a name="l00041"></a>00041 <span class="comment">/** Formats the Tor process arguments for logging. */</span>
<a name="l00042"></a>00042 QString
<a name="l00043"></a><a class="code" href="class_tor_process.html#a9689950480eddec3add41ca9672a56a8">00043</a> <a class="code" href="class_tor_process.html#a9689950480eddec3add41ca9672a56a8">TorProcess::formatArguments</a>(<span class="keyword">const</span> QStringList &amp;args)
<a name="l00044"></a>00044 {
<a name="l00045"></a>00045   QStringList out;
<a name="l00046"></a>00046   <span class="keywordflow">foreach</span> (QString arg, args) {
<a name="l00047"></a>00047     out &lt;&lt; (arg.contains(<span class="stringliteral">&quot; &quot;</span>) || arg.isEmpty() ? <a class="code" href="stringutil_8cpp.html#a2323f625d9f361167e0455447c3fc83a">string_escape</a>(arg) : arg);
<a name="l00048"></a>00048   }
<a name="l00049"></a>00049   <span class="keywordflow">return</span> out.join(<span class="stringliteral">&quot; &quot;</span>);
<a name="l00050"></a>00050 }
<a name="l00051"></a>00051 <span class="comment"></span>
<a name="l00052"></a>00052 <span class="comment">/** Attempts to start the Tor process using the location, executable, and</span>
<a name="l00053"></a>00053 <span class="comment"> * command-line arguments specified in Vidalia&#39;s settings. If Tor starts, the</span>
<a name="l00054"></a>00054 <span class="comment"> * signal started() will be emitted. If Tor fails to start,</span>
<a name="l00055"></a>00055 <span class="comment"> * startFailed(errmsg) will be emitted, with an appropriate error message. */</span>
<a name="l00056"></a>00056 <span class="keywordtype">void</span>
<a name="l00057"></a><a class="code" href="class_tor_process.html#a2960d800606ab1b4843756519d3d5cbb">00057</a> <a class="code" href="class_tor_process.html#a2960d800606ab1b4843756519d3d5cbb">TorProcess::start</a>(<span class="keyword">const</span> QString &amp;app, <span class="keyword">const</span> QStringList &amp;args) 
<a name="l00058"></a>00058 {
<a name="l00059"></a>00059   QString exe = app;
<a name="l00060"></a>00060 <span class="preprocessor">#if defined(Q_OS_WIN32)</span>
<a name="l00061"></a>00061 <span class="preprocessor"></span>  <span class="comment">/* If we&#39;re on Windows, QProcess::start requires that paths with spaces are</span>
<a name="l00062"></a>00062 <span class="comment">   * quoted before being passed to it. */</span>
<a name="l00063"></a>00063   exe = <span class="stringliteral">&quot;\&quot;&quot;</span> + exe + <span class="stringliteral">&quot;\&quot;&quot;</span>;
<a name="l00064"></a>00064 <span class="preprocessor">#endif</span>
<a name="l00065"></a>00065 <span class="preprocessor"></span>  
<a name="l00066"></a>00066   <span class="comment">/* Attempt to start Tor with the given command-line arguments */</span>
<a name="l00067"></a>00067   QStringList env = QProcess::systemEnvironment();
<a name="l00068"></a>00068 <span class="preprocessor">#if !defined(Q_OS_WIN32)</span>
<a name="l00069"></a>00069 <span class="preprocessor"></span>  <span class="comment">/* Add &quot;/usr/sbin&quot; to an existing $PATH</span>
<a name="l00070"></a>00070 <span class="comment">   * XXX What if they have no path? Would always just making one with </span>
<a name="l00071"></a>00071 <span class="comment">   *     &quot;/usr/sbin&quot; smart? Should we add anything else? */</span>
<a name="l00072"></a>00072   <span class="keywordflow">for</span> (<span class="keywordtype">int</span> <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> = 0; <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> &lt; env.size(); <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a>++) {
<a name="l00073"></a>00073     QString envVar = env.at(<a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a>);
<a name="l00074"></a>00074     <span class="keywordflow">if</span> (envVar.startsWith(<span class="stringliteral">&quot;PATH=&quot;</span>))
<a name="l00075"></a>00075       env.replace(<a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a>, envVar += <span class="stringliteral">&quot;:/usr/sbin&quot;</span>);
<a name="l00076"></a>00076   }
<a name="l00077"></a>00077 <span class="preprocessor">#endif</span>
<a name="l00078"></a>00078 <span class="preprocessor"></span>  setEnvironment(env);
<a name="l00079"></a>00079 
<a name="l00080"></a>00080   <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">&quot;Starting Tor using &#39;%1 %2&#39;&quot;</span>).arg(app).arg(<a class="code" href="class_tor_process.html#a9689950480eddec3add41ca9672a56a8">formatArguments</a>(args));
<a name="l00081"></a>00081   <a class="code" href="class_tor_process.html#a2960d800606ab1b4843756519d3d5cbb">QProcess::start</a>(exe, args, QIODevice::ReadOnly | QIODevice::Text);
<a name="l00082"></a>00082 }
<a name="l00083"></a>00083 <span class="comment"></span>
<a name="l00084"></a>00084 <span class="comment">/** Stops the Tor process */</span>
<a name="l00085"></a>00085 <span class="keywordtype">bool</span>
<a name="l00086"></a><a class="code" href="class_tor_process.html#ab326cc43224cd475efa4b210531e4f6e">00086</a> <a class="code" href="class_tor_process.html#ab326cc43224cd475efa4b210531e4f6e">TorProcess::stop</a>(QString *errmsg)
<a name="l00087"></a>00087 {
<a name="l00088"></a>00088   <span class="comment">/* First, check if the process is already stopped before closing it</span>
<a name="l00089"></a>00089 <span class="comment">   * forcefully. */</span>
<a name="l00090"></a>00090   <span class="keywordflow">if</span> (state() == QProcess::NotRunning) {
<a name="l00091"></a>00091     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00092"></a>00092   }
<a name="l00093"></a>00093 
<a name="l00094"></a>00094   <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">&quot;Stopping the Tor process.&quot;</span>);
<a name="l00095"></a>00095   <span class="comment">/* Tell the process to stop */</span>
<a name="l00096"></a>00096 <span class="preprocessor">#if defined(Q_OS_WIN32)</span>
<a name="l00097"></a>00097 <span class="preprocessor"></span>  <span class="comment">/* Tor on Windows doesn&#39;t understand a WM_CLOSE message (which is what </span>
<a name="l00098"></a>00098 <span class="comment">   * QProcess::terminate() sends it), so we have to kill it harshly. */</span>
<a name="l00099"></a>00099   kill();
<a name="l00100"></a>00100 <span class="preprocessor">#else</span>
<a name="l00101"></a>00101 <span class="preprocessor"></span>  terminate();
<a name="l00102"></a>00102 
<a name="l00103"></a>00103   <span class="comment">/* Wait for it to complete */</span>
<a name="l00104"></a>00104   <span class="keywordflow">if</span> (!waitForFinished(5000)) {
<a name="l00105"></a>00105     <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;Tor failed to stop: %1&quot;</span>).arg(errorString());
<a name="l00106"></a>00106     <span class="keywordflow">if</span> (errmsg) {
<a name="l00107"></a>00107       *errmsg = 
<a name="l00108"></a>00108         tr(<span class="stringliteral">&quot;Process %1 failed to stop. [%2]&quot;</span>).arg(<a class="code" href="class_tor_process.html#a9f770ce83a1dc5640ba39815291ce291">pid</a>()).arg(errorString());
<a name="l00109"></a>00109     }
<a name="l00110"></a>00110     <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00111"></a>00111   }
<a name="l00112"></a>00112 <span class="preprocessor">#endif</span>
<a name="l00113"></a>00113 <span class="preprocessor"></span>  <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 <span class="comment"></span>
<a name="l00116"></a>00116 <span class="comment">/** Return the process ID for the current process. */</span>
<a name="l00117"></a>00117 quint64
<a name="l00118"></a><a class="code" href="class_tor_process.html#a9f770ce83a1dc5640ba39815291ce291">00118</a> <a class="code" href="class_tor_process.html#a9f770ce83a1dc5640ba39815291ce291">TorProcess::pid</a>()
<a name="l00119"></a>00119 {
<a name="l00120"></a>00120 <span class="preprocessor">#if defined(Q_OS_WIN32)</span>
<a name="l00121"></a>00121 <span class="preprocessor"></span>  <span class="keywordflow">return</span> (quint64)((<a class="code" href="class_tor_process.html#a9f770ce83a1dc5640ba39815291ce291">QProcess::pid</a>())-&gt;dwProcessId);
<a name="l00122"></a>00122 <span class="preprocessor">#else</span>
<a name="l00123"></a>00123 <span class="preprocessor"></span>  <span class="keywordflow">return</span> <a class="code" href="class_tor_process.html#a9f770ce83a1dc5640ba39815291ce291">QProcess::pid</a>();
<a name="l00124"></a>00124 <span class="preprocessor">#endif</span>
<a name="l00125"></a>00125 <span class="preprocessor"></span>}
<a name="l00126"></a>00126 <span class="comment"></span>
<a name="l00127"></a>00127 <span class="comment">/** Opens logging on stdout. When this is open, the log() signal will be</span>
<a name="l00128"></a>00128 <span class="comment"> * emitted when Tor prints a message to stdout. */</span>
<a name="l00129"></a>00129 <span class="keywordtype">void</span>
<a name="l00130"></a><a class="code" href="class_tor_process.html#a4a6956ca20e13905543ce638c8ce1c93">00130</a> <a class="code" href="class_tor_process.html#a4a6956ca20e13905543ce638c8ce1c93">TorProcess::openStdout</a>()
<a name="l00131"></a>00131 {
<a name="l00132"></a>00132   setReadChannelMode(QProcess::MergedChannels);
<a name="l00133"></a>00133   setReadChannel(QProcess::StandardOutput);
<a name="l00134"></a>00134 }
<a name="l00135"></a>00135 <span class="comment"></span>
<a name="l00136"></a>00136 <span class="comment">/** Closes logging on stdout. When this is closed, the log() signal will not</span>
<a name="l00137"></a>00137 <span class="comment"> * be emitted when Tor prints a message to stdout. */</span>
<a name="l00138"></a>00138 <span class="keywordtype">void</span>
<a name="l00139"></a><a class="code" href="class_tor_process.html#aee62d3364ab83002863c1f43ed0d8613">00139</a> <a class="code" href="class_tor_process.html#aee62d3364ab83002863c1f43ed0d8613">TorProcess::closeStdout</a>()
<a name="l00140"></a>00140 {
<a name="l00141"></a>00141   <span class="comment">/* Close the stdout channel */</span>
<a name="l00142"></a>00142   closeReadChannel(QProcess::StandardOutput);
<a name="l00143"></a>00143   <span class="comment">/* Read anything left waiting on the buffer */</span>
<a name="l00144"></a>00144   <a class="code" href="class_tor_process.html#a167afd9d6dfd04f7d3d211fa2ea9ad04">onReadyRead</a>();
<a name="l00145"></a>00145 }
<a name="l00146"></a>00146 <span class="comment"></span>
<a name="l00147"></a>00147 <span class="comment">/** Called when there is data to be read from stdout */</span>
<a name="l00148"></a>00148 <span class="keywordtype">void</span>
<a name="l00149"></a><a class="code" href="class_tor_process.html#a167afd9d6dfd04f7d3d211fa2ea9ad04">00149</a> <a class="code" href="class_tor_process.html#a167afd9d6dfd04f7d3d211fa2ea9ad04">TorProcess::onReadyRead</a>()
<a name="l00150"></a>00150 {
<a name="l00151"></a>00151   <span class="keywordtype">int</span> <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a>, j;
<a name="l00152"></a>00152   QString line;
<a name="l00153"></a>00153   
<a name="l00154"></a>00154   <span class="keywordflow">while</span> (canReadLine()) {
<a name="l00155"></a>00155     line = readLine();
<a name="l00156"></a>00156     <span class="keywordflow">if</span> (!line.isEmpty()) {
<a name="l00157"></a>00157       <span class="comment">/* Parse the log message and emit log() */</span>
<a name="l00158"></a>00158       i = line.indexOf(<span class="stringliteral">&quot;[&quot;</span>);
<a name="l00159"></a>00159       j = line.indexOf(<span class="stringliteral">&quot;]&quot;</span>);
<a name="l00160"></a>00160       <span class="keywordflow">if</span> (i &gt; 0 &amp;&amp; j &gt; i &amp;&amp; line.length() &gt;= j+2) {
<a name="l00161"></a>00161         emit <a class="code" href="class_tor_process.html#a3ba9ae11f024932e460f1a5b29dcac46">log</a>(line.mid(i+1, j-i-1), line.mid(j+2));
<a name="l00162"></a>00162       }
<a name="l00163"></a>00163     }
<a name="l00164"></a>00164   }
<a name="l00165"></a>00165 }
<a name="l00166"></a>00166 <span class="comment"></span>
<a name="l00167"></a>00167 <span class="comment">/** Called when the process encounters an error. If the error tells us that</span>
<a name="l00168"></a>00168 <span class="comment"> * the process failed to start, then we will emit the startFailed() signal and</span>
<a name="l00169"></a>00169 <span class="comment"> * an error message indicating why. */</span>
<a name="l00170"></a>00170 <span class="keywordtype">void</span>
<a name="l00171"></a><a class="code" href="class_tor_process.html#a5d95d00bf190facac9e635a6a3d8fc59">00171</a> <a class="code" href="class_tor_process.html#a5d95d00bf190facac9e635a6a3d8fc59">TorProcess::onError</a>(QProcess::ProcessError <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">error</a>)
<a name="l00172"></a>00172 {
<a name="l00173"></a>00173   <span class="keywordflow">if</span> (error == QProcess::FailedToStart) {
<a name="l00174"></a>00174     <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;The Tor process failed to start: %1&quot;</span>).arg(errorString());
<a name="l00175"></a>00175     <span class="comment">/* Tor didn&#39;t start, so let everyone know why. */</span>
<a name="l00176"></a>00176     emit <a class="code" href="class_tor_process.html#af14ac85df9f1190dd816c2f07cfa928d">startFailed</a>(errorString());
<a name="l00177"></a>00177   } <span class="keywordflow">else</span> {
<a name="l00178"></a>00178     <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;Tor process error: %1&quot;</span>).arg(errorString());
<a name="l00179"></a>00179   }
<a name="l00180"></a>00180 }
<a name="l00181"></a>00181 <span class="comment"></span>
<a name="l00182"></a>00182 <span class="comment">/** Returns the version reported by the Tor executable specified in</span>
<a name="l00183"></a>00183 <span class="comment"> * &lt;b&gt;exe&lt;/b&gt;, or a default-constructed QString on failure. */</span>
<a name="l00184"></a>00184 QString
<a name="l00185"></a><a class="code" href="class_tor_process.html#ae23646918337ebda1ae51c449556a0b8">00185</a> <a class="code" href="class_tor_process.html#ae23646918337ebda1ae51c449556a0b8">TorProcess::version</a>(<span class="keyword">const</span> QString &amp;exe)
<a name="l00186"></a>00186 {
<a name="l00187"></a>00187   QProcess tor;
<a name="l00188"></a>00188 
<a name="l00189"></a>00189   tor.start(exe, QStringList() &lt;&lt; <span class="stringliteral">&quot;--version&quot;</span>);
<a name="l00190"></a>00190   <span class="keywordflow">if</span> (!tor.waitForStarted() || !tor.waitForFinished())
<a name="l00191"></a>00191     <span class="keywordflow">return</span> QString();
<a name="l00192"></a>00192 
<a name="l00193"></a>00193   <span class="keywordflow">while</span> (tor.canReadLine()) {
<a name="l00194"></a>00194     QString line = tor.readLine();
<a name="l00195"></a>00195     <span class="keywordflow">if</span> (line.startsWith(<span class="stringliteral">&quot;Tor version&quot;</span>, Qt::CaseInsensitive)) {
<a name="l00196"></a>00196       QStringList parts = line.split(<span class="stringliteral">&quot; &quot;</span>);
<a name="l00197"></a>00197       <span class="keywordflow">if</span> (parts.size() &gt;= 3)
<a name="l00198"></a>00198         <span class="keywordflow">return</span> parts.at(2);
<a name="l00199"></a>00199     }
<a name="l00200"></a>00200   }
<a name="l00201"></a>00201   <span class="keywordflow">return</span> QString();
<a name="l00202"></a>00202 }
<a name="l00203"></a>00203 
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:55 2010 for Vidalia by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>