Sophie

Sophie

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

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: TorService.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>TorService.cpp</h1><a href="_tor_service_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 TorService.cpp</span>
<a name="l00013"></a>00013 <span class="comment">** \version $Id: TorService.cpp 3735 2009-04-28 20:28:01Z edmanm $</span>
<a name="l00014"></a>00014 <span class="comment">** \brief Starts, stops, installs, and uninstalls a Tor service (Win32).</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_service_8h.html">TorService.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 &lt;QLibrary&gt;</span>
<a name="l00021"></a>00021 <span class="comment"></span>
<a name="l00022"></a>00022 <span class="comment">/** Returned by TorService::exitCode() when we are unable to determine the</span>
<a name="l00023"></a>00023 <span class="comment"> * actual exit code of the service (unless, of course, Tor returns -999999). */</span>
<a name="l00024"></a><a class="code" href="_tor_service_8cpp.html#a8d8df7ca12398fbe97f9db863c2c3c03">00024</a> <span class="preprocessor">#define UNKNOWN_EXIT_CODE     -999999</span>
<a name="l00025"></a>00025 <span class="preprocessor"></span><span class="comment"></span>
<a name="l00026"></a>00026 <span class="comment">/** List of dynamically loaded NT service functions. */</span>
<a name="l00027"></a>00027 <a class="code" href="struct_service_functions.html">ServiceFunctions</a> <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">TorService::_service_fns</a> = 
<a name="l00028"></a>00028   { <span class="keyword">false</span>,
<a name="l00029"></a>00029     NULL, NULL, NULL, NULL, NULL,
<a name="l00030"></a>00030     NULL, NULL, NULL, NULL, NULL
<a name="l00031"></a>00031   };
<a name="l00032"></a>00032 
<a name="l00033"></a>00033 <span class="comment"></span>
<a name="l00034"></a>00034 <span class="comment">/** Default ctor. */</span>
<a name="l00035"></a><a class="code" href="class_tor_service.html#a6a14fe7307b348093ae38e16d4275785">00035</a> <a class="code" href="class_tor_service.html#a6a14fe7307b348093ae38e16d4275785">TorService::TorService</a>(QObject *parent)
<a name="l00036"></a>00036   : QObject(parent)
<a name="l00037"></a>00037 {
<a name="l00038"></a>00038   <a class="code" href="class_tor_service.html#a46e51a37fe5f1b464c011e870ec206c8">_scm</a> = <a class="code" href="class_tor_service.html#a9bdc336db137705c75c4596818664ff3">openSCM</a>();
<a name="l00039"></a>00039 }
<a name="l00040"></a>00040 <span class="comment"></span>
<a name="l00041"></a>00041 <span class="comment">/** Default dtor. */</span>
<a name="l00042"></a><a class="code" href="class_tor_service.html#ab87e0a2c1f43c062caebf7a10c11ffce">00042</a> <a class="code" href="class_tor_service.html#ab87e0a2c1f43c062caebf7a10c11ffce">TorService::~TorService</a>()
<a name="l00043"></a>00043 {
<a name="l00044"></a>00044   <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(<a class="code" href="class_tor_service.html#a46e51a37fe5f1b464c011e870ec206c8">_scm</a>);
<a name="l00045"></a>00045 }
<a name="l00046"></a>00046 <span class="comment"></span>
<a name="l00047"></a>00047 <span class="comment">/** Returns true if services are supported. */</span>
<a name="l00048"></a>00048 <span class="keywordtype">bool</span>
<a name="l00049"></a><a class="code" href="class_tor_service.html#ad9f8db8e338a5393cdad6315e95bcbf9">00049</a> <a class="code" href="class_tor_service.html#ad9f8db8e338a5393cdad6315e95bcbf9">TorService::isSupported</a>()
<a name="l00050"></a>00050 {
<a name="l00051"></a>00051   <span class="keywordflow">return</span> (QSysInfo::WindowsVersion &amp; QSysInfo::WV_NT_based);
<a name="l00052"></a>00052 }
<a name="l00053"></a>00053 <span class="comment"></span>
<a name="l00054"></a>00054 <span class="comment">/** Dyanmically loads NT service related functions from advapi32.dll. This</span>
<a name="l00055"></a>00055 <span class="comment"> * function is adapted from Tor&#39;s nt_service_load_library() function. See</span>
<a name="l00056"></a>00056 <span class="comment"> * LICENSE for details on Tor&#39;s license. */</span>
<a name="l00057"></a>00057 <span class="keywordtype">bool</span>
<a name="l00058"></a><a class="code" href="class_tor_service.html#ae3fe23ef6342a03f0cab05ffe7f4a7a3">00058</a> <a class="code" href="class_tor_service.html#ae3fe23ef6342a03f0cab05ffe7f4a7a3">TorService::loadServiceFunctions</a>()
<a name="l00059"></a>00059 {
<a name="l00060"></a>00060 <span class="preprocessor">#define LOAD_SERVICE_FN(f) do {                                         \</span>
<a name="l00061"></a>00061 <span class="preprocessor">  void *fn;                                                             \</span>
<a name="l00062"></a>00062 <span class="preprocessor">  if (!((fn = QLibrary::resolve(&quot;advapi32&quot;, #f)))) {                    \</span>
<a name="l00063"></a>00063 <span class="preprocessor">      return false;                                                     \</span>
<a name="l00064"></a>00064 <span class="preprocessor">    } else {                                                            \</span>
<a name="l00065"></a>00065 <span class="preprocessor">      _service_fns.f = (f ## _fn) fn;                                   \</span>
<a name="l00066"></a>00066 <span class="preprocessor">    }                                                                   \</span>
<a name="l00067"></a>00067 <span class="preprocessor">  } while (0)</span>
<a name="l00068"></a>00068 <span class="preprocessor"></span>
<a name="l00069"></a>00069   <span class="keywordflow">if</span> (!<a class="code" href="class_tor_service.html#ad9f8db8e338a5393cdad6315e95bcbf9">isSupported</a>()) {
<a name="l00070"></a>00070     <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#aeb944441051ce74d1e5ed02d06206647">loaded</a> = <span class="keyword">false</span>;
<a name="l00071"></a>00071   } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!<a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#aeb944441051ce74d1e5ed02d06206647">loaded</a>) {
<a name="l00072"></a>00072     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(ChangeServiceConfig2A);
<a name="l00073"></a>00073     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(CloseServiceHandle);
<a name="l00074"></a>00074     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(ControlService);
<a name="l00075"></a>00075     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(CreateServiceA);
<a name="l00076"></a>00076     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(DeleteService);
<a name="l00077"></a>00077     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(OpenSCManagerA);
<a name="l00078"></a>00078     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(OpenServiceA);
<a name="l00079"></a>00079     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(QueryServiceStatus);
<a name="l00080"></a>00080     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(SetServiceStatus);
<a name="l00081"></a>00081     <a class="code" href="_tor_service_8cpp.html#a90ef31ec96737af8199f420ec1a81b30">LOAD_SERVICE_FN</a>(StartServiceA);
<a name="l00082"></a>00082     <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#aeb944441051ce74d1e5ed02d06206647">loaded</a> = <span class="keyword">true</span>;
<a name="l00083"></a>00083   }
<a name="l00084"></a>00084   <span class="keywordflow">return</span> <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#aeb944441051ce74d1e5ed02d06206647">loaded</a>;
<a name="l00085"></a>00085 }
<a name="l00086"></a>00086 <span class="comment"></span>
<a name="l00087"></a>00087 <span class="comment">/** Opens a handle to the Tor service. Returns NULL on error. */</span>
<a name="l00088"></a>00088 SC_HANDLE
<a name="l00089"></a><a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">00089</a> <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">TorService::openService</a>()
<a name="l00090"></a>00090 {
<a name="l00091"></a>00091   <span class="keywordflow">if</span> (!<a class="code" href="class_tor_service.html#ae3fe23ef6342a03f0cab05ffe7f4a7a3">loadServiceFunctions</a>())
<a name="l00092"></a>00092     <span class="keywordflow">return</span> NULL;
<a name="l00093"></a>00093   <span class="keywordflow">if</span> (!<a class="code" href="class_tor_service.html#a46e51a37fe5f1b464c011e870ec206c8">_scm</a>)
<a name="l00094"></a>00094     <span class="keywordflow">return</span> NULL;
<a name="l00095"></a>00095   <span class="keywordflow">return</span> <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a600797ca876ee54242da39cf682f6613">OpenServiceA</a>(<a class="code" href="class_tor_service.html#a46e51a37fe5f1b464c011e870ec206c8">_scm</a>, 
<a name="l00096"></a>00096                                    (LPCTSTR)<a class="code" href="_tor_service_8h.html#ab94ebc0021b96d5498ff2484fc7b7853">TOR_SERVICE_NAME</a>, 
<a name="l00097"></a>00097                                    <a class="code" href="_tor_service_8h.html#ae36be8de454f1eda19eeaded49424277">TOR_SERVICE_ACCESS</a>);
<a name="l00098"></a>00098 }
<a name="l00099"></a>00099 <span class="comment"></span>
<a name="l00100"></a>00100 <span class="comment">/** Opens a handle to the service control manager. Returns NULL on error. */</span>
<a name="l00101"></a>00101 SC_HANDLE
<a name="l00102"></a><a class="code" href="class_tor_service.html#a9bdc336db137705c75c4596818664ff3">00102</a> <a class="code" href="class_tor_service.html#a9bdc336db137705c75c4596818664ff3">TorService::openSCM</a>()
<a name="l00103"></a>00103 {
<a name="l00104"></a>00104   <span class="keywordflow">if</span> (!<a class="code" href="class_tor_service.html#ae3fe23ef6342a03f0cab05ffe7f4a7a3">loadServiceFunctions</a>())
<a name="l00105"></a>00105     <span class="keywordflow">return</span> NULL;
<a name="l00106"></a>00106   <span class="keywordflow">return</span> <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a13dc542abc96167318d2fbe9f8be6a80">OpenSCManagerA</a>(NULL, NULL, SC_MANAGER_ALL_ACCESS);
<a name="l00107"></a>00107 }
<a name="l00108"></a>00108 <span class="comment"></span>
<a name="l00109"></a>00109 <span class="comment">/** Closes the service &lt;b&gt;handle&lt;/b&gt;. */</span>
<a name="l00110"></a>00110 <span class="keywordtype">void</span>
<a name="l00111"></a><a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">00111</a> <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">TorService::closeHandle</a>(SC_HANDLE handle)
<a name="l00112"></a>00112 {
<a name="l00113"></a>00113   <span class="keywordflow">if</span> (!<a class="code" href="class_tor_service.html#ae3fe23ef6342a03f0cab05ffe7f4a7a3">loadServiceFunctions</a>())
<a name="l00114"></a>00114     <span class="keywordflow">return</span>;
<a name="l00115"></a>00115   <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a4b7862a13bccedc7100a18da68fab91a">CloseServiceHandle</a>(handle);
<a name="l00116"></a>00116 }
<a name="l00117"></a>00117 <span class="comment"></span>
<a name="l00118"></a>00118 <span class="comment">/** Returns true if the Tor service is installed. */</span>
<a name="l00119"></a>00119 <span class="keywordtype">bool</span>
<a name="l00120"></a><a class="code" href="class_tor_service.html#a7e54797c5499b30153df9290c0ad62df">00120</a> <a class="code" href="class_tor_service.html#a7e54797c5499b30153df9290c0ad62df">TorService::isInstalled</a>()
<a name="l00121"></a>00121 {
<a name="l00122"></a>00122   <span class="keywordtype">bool</span> installed;
<a name="l00123"></a>00123   SC_HANDLE service = <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">openService</a>();
<a name="l00124"></a>00124   installed = (service != NULL);
<a name="l00125"></a>00125   <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(service);
<a name="l00126"></a>00126   <span class="keywordflow">return</span> installed;
<a name="l00127"></a>00127 }
<a name="l00128"></a>00128 <span class="comment"></span>
<a name="l00129"></a>00129 <span class="comment">/** Returns true if the Tor service is running. */</span>
<a name="l00130"></a>00130 <span class="keywordtype">bool</span>
<a name="l00131"></a><a class="code" href="class_tor_service.html#a403393278b9f1d2076a76289aed4be54">00131</a> <a class="code" href="class_tor_service.html#a403393278b9f1d2076a76289aed4be54">TorService::isRunning</a>()
<a name="l00132"></a>00132 {
<a name="l00133"></a>00133   <span class="keywordflow">return</span> (<a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">status</a>() == SERVICE_RUNNING);
<a name="l00134"></a>00134 }
<a name="l00135"></a>00135 <span class="comment"></span>
<a name="l00136"></a>00136 <span class="comment">/** Starts Tor service. */</span>
<a name="l00137"></a>00137 <span class="keywordtype">void</span>
<a name="l00138"></a><a class="code" href="class_tor_service.html#ad0b2bbbae2215166229c8dca9a83341a">00138</a> <a class="code" href="class_tor_service.html#ad0b2bbbae2215166229c8dca9a83341a">TorService::start</a>()
<a name="l00139"></a>00139 {
<a name="l00140"></a>00140   SC_HANDLE service = <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">openService</a>();
<a name="l00141"></a>00141 
<a name="l00142"></a>00142   <span class="keywordflow">if</span> (!service) {
<a name="l00143"></a>00143     <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;Bug: We tried to start the Tor service, but it is not installed.&quot;</span>);
<a name="l00144"></a>00144     emit <a class="code" href="class_tor_service.html#ab15ec5607a014c611809f968f75e4d0b">startFailed</a>(tr(<span class="stringliteral">&quot;The Tor service is not installed.&quot;</span>));
<a name="l00145"></a>00145     <span class="keywordflow">return</span>;
<a name="l00146"></a>00146   }
<a name="l00147"></a>00147 
<a name="l00148"></a>00148   <span class="comment">/* Starting a service can take up to 30 seconds! */</span>
<a name="l00149"></a>00149   <span class="keywordflow">if</span> (<a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">status</a>() != SERVICE_RUNNING) {
<a name="l00150"></a>00150     <span class="keywordtype">int</span> tries = 0;
<a name="l00151"></a>00151     <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">&quot;Starting the Tor service.&quot;</span>);
<a name="l00152"></a>00152     <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a24bf9428aaebf63a78366b63a63a20c6">StartServiceA</a>(service, 0, NULL);
<a name="l00153"></a>00153 
<a name="l00154"></a>00154     <span class="keywordflow">while</span> ((<a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">status</a>() != SERVICE_RUNNING) &amp;&amp; ++tries &lt;= 5)
<a name="l00155"></a>00155       Sleep(1000);
<a name="l00156"></a>00156   }
<a name="l00157"></a>00157 
<a name="l00158"></a>00158   <span class="keywordflow">if</span> (<a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">status</a>() == SERVICE_RUNNING) {
<a name="l00159"></a>00159     emit <a class="code" href="class_tor_service.html#a8b382075df50e0e128c63d7eadb8eefd">started</a>();
<a name="l00160"></a>00160   } <span class="keywordflow">else</span> {
<a name="l00161"></a>00161     <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;Unable to start the Tor service.&quot;</span>);
<a name="l00162"></a>00162     emit <a class="code" href="class_tor_service.html#ab15ec5607a014c611809f968f75e4d0b">startFailed</a>(tr(<span class="stringliteral">&quot;Unable to start the Tor service.&quot;</span>));
<a name="l00163"></a>00163   }
<a name="l00164"></a>00164   <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(service);
<a name="l00165"></a>00165 }
<a name="l00166"></a>00166 <span class="comment"></span>
<a name="l00167"></a>00167 <span class="comment">/** Stops Tor service. */</span>
<a name="l00168"></a>00168 <span class="keywordtype">bool</span>
<a name="l00169"></a><a class="code" href="class_tor_service.html#a3dd3a77a3cc5b988477d0ae09825c26a">00169</a> <a class="code" href="class_tor_service.html#a3dd3a77a3cc5b988477d0ae09825c26a">TorService::stop</a>()
<a name="l00170"></a>00170 {
<a name="l00171"></a>00171   SC_HANDLE service = <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">openService</a>();
<a name="l00172"></a>00172 
<a name="l00173"></a>00173   <span class="keywordflow">if</span> (!service)
<a name="l00174"></a>00174     <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00175"></a>00175 
<a name="l00176"></a>00176   <span class="keywordflow">if</span> (<a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">status</a>() != SERVICE_STOPPED) {
<a name="l00177"></a>00177     SERVICE_STATUS stat;
<a name="l00178"></a>00178     stat.dwCurrentState = SERVICE_RUNNING;
<a name="l00179"></a>00179     <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">&quot;Stopping the Tor service.&quot;</span>);
<a name="l00180"></a>00180     <span class="keywordflow">if</span> (<a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a7870cabdbc5928b0d6f1b14d81af19de">ControlService</a>(service, SERVICE_CONTROL_STOP, &amp;stat)) {
<a name="l00181"></a>00181       <span class="comment">/* XXX Five seconds isn&#39;t long enough to wait when we&#39;re stopping a Tor</span>
<a name="l00182"></a>00182 <span class="comment">       * that is running as a server, but we don&#39;t want to block for 30</span>
<a name="l00183"></a>00183 <span class="comment">       * seconds. It would be nice if we could get an async notification when</span>
<a name="l00184"></a>00184 <span class="comment">       * the service stops or fails to stop. */</span>
<a name="l00185"></a>00185       <span class="keywordtype">int</span> tries = 0;
<a name="l00186"></a>00186       <span class="keywordflow">while</span> ((<a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">status</a>() != SERVICE_STOPPED) &amp;&amp; (++tries &lt;= 5))
<a name="l00187"></a>00187         Sleep(1000);
<a name="l00188"></a>00188     }
<a name="l00189"></a>00189   }
<a name="l00190"></a>00190   <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(service);
<a name="l00191"></a>00191 
<a name="l00192"></a>00192   <span class="comment">/* Find out if the service really stopped and return the result */</span>
<a name="l00193"></a>00193   <span class="keywordflow">if</span> (<a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">status</a>() == SERVICE_STOPPED) {
<a name="l00194"></a>00194     emit <a class="code" href="class_tor_service.html#a39988777b4993c2e5eeeb6b92332e187">finished</a>(<a class="code" href="class_tor_service.html#a43385dfaffdf336b2d3b3cb9c5e4454e">exitCode</a>(), <a class="code" href="class_tor_service.html#a86ae694a35592aeeda0bf33891843ce5">exitStatus</a>());
<a name="l00195"></a>00195     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00196"></a>00196   }
<a name="l00197"></a>00197   <span class="comment">/* XXX This needs an actual reason message. */</span>
<a name="l00198"></a>00198   <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;Unable to stop the Tor service.&quot;</span>);
<a name="l00199"></a>00199   <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00200"></a>00200 }
<a name="l00201"></a>00201 <span class="comment"></span>
<a name="l00202"></a>00202 <span class="comment">/** Returns the exit code of the last Tor service that finished. */</span>
<a name="l00203"></a>00203 <span class="keywordtype">int</span>
<a name="l00204"></a><a class="code" href="class_tor_service.html#a43385dfaffdf336b2d3b3cb9c5e4454e">00204</a> <a class="code" href="class_tor_service.html#a43385dfaffdf336b2d3b3cb9c5e4454e">TorService::exitCode</a>()
<a name="l00205"></a>00205 {
<a name="l00206"></a>00206   SC_HANDLE service;
<a name="l00207"></a>00207   <span class="keywordtype">int</span> <a class="code" href="class_tor_service.html#a43385dfaffdf336b2d3b3cb9c5e4454e">exitCode</a> = <a class="code" href="_tor_service_8cpp.html#a8d8df7ca12398fbe97f9db863c2c3c03">UNKNOWN_EXIT_CODE</a>;
<a name="l00208"></a>00208   
<a name="l00209"></a>00209   service = <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">openService</a>();
<a name="l00210"></a>00210   <span class="keywordflow">if</span> (service) {
<a name="l00211"></a>00211     SERVICE_STATUS s;
<a name="l00212"></a>00212     <span class="keywordflow">if</span> (<a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a3b40037d4921e8d3d95dbb6c6f8046f7">QueryServiceStatus</a>(service, &amp;s)) {
<a name="l00213"></a>00213       <span class="comment">/* Services return one exit code, but it could be in one of two</span>
<a name="l00214"></a>00214 <span class="comment">       * variables. Fun. */</span>
<a name="l00215"></a>00215       exitCode = (int)(s.dwWin32ExitCode == ERROR_SERVICE_SPECIFIC_ERROR
<a name="l00216"></a>00216                                             ? s.dwServiceSpecificExitCode
<a name="l00217"></a>00217                                             : s.dwWin32ExitCode);
<a name="l00218"></a>00218     }
<a name="l00219"></a>00219     <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(service);
<a name="l00220"></a>00220   }
<a name="l00221"></a>00221   <span class="keywordflow">return</span> exitCode;
<a name="l00222"></a>00222 }
<a name="l00223"></a>00223 <span class="comment"></span>
<a name="l00224"></a>00224 <span class="comment">/** Returns the exit status of the last Tor service that finished. */</span>
<a name="l00225"></a>00225 QProcess::ExitStatus
<a name="l00226"></a><a class="code" href="class_tor_service.html#a86ae694a35592aeeda0bf33891843ce5">00226</a> <a class="code" href="class_tor_service.html#a86ae694a35592aeeda0bf33891843ce5">TorService::exitStatus</a>()
<a name="l00227"></a>00227 {
<a name="l00228"></a>00228   <span class="comment">/* NT services don&#39;t really have an equivalent to QProcess::CrashExit, so </span>
<a name="l00229"></a>00229 <span class="comment">   * this just returns QProcess::NormalExit. Tor _could_ set</span>
<a name="l00230"></a>00230 <span class="comment">   * dwServiceSpecificExitCode to some magic value when it starts and then</span>
<a name="l00231"></a>00231 <span class="comment">   * set it to the real exit code when Tor exits. Then we would know if the</span>
<a name="l00232"></a>00232 <span class="comment">   * service crashed when dwServiceSpecificExitCode is still the magic value.</span>
<a name="l00233"></a>00233 <span class="comment">   * However, I don&#39;t care and it doesn&#39;t really matter anyway. */</span>
<a name="l00234"></a>00234   <span class="keywordflow">return</span> QProcess::NormalExit;
<a name="l00235"></a>00235 }
<a name="l00236"></a>00236 <span class="comment"></span>
<a name="l00237"></a>00237 <span class="comment">/** Installs the Tor service. Returns true if the service was successfully</span>
<a name="l00238"></a>00238 <span class="comment"> * installed or already exists. */</span>
<a name="l00239"></a>00239 <span class="keywordtype">bool</span>
<a name="l00240"></a><a class="code" href="class_tor_service.html#ad56bdbd81c64764dfaf5675411e7f049">00240</a> <a class="code" href="class_tor_service.html#ad56bdbd81c64764dfaf5675411e7f049">TorService::install</a>(<span class="keyword">const</span> QString &amp;torPath, <span class="keyword">const</span> QString &amp;torrc,
<a name="l00241"></a>00241                     quint16 controlPort)
<a name="l00242"></a>00242 {
<a name="l00243"></a>00243   SC_HANDLE service;
<a name="l00244"></a>00244   
<a name="l00245"></a>00245   <span class="keywordflow">if</span> (!<a class="code" href="class_tor_service.html#a46e51a37fe5f1b464c011e870ec206c8">_scm</a>)
<a name="l00246"></a>00246     <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00247"></a>00247  
<a name="l00248"></a>00248   service = <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">openService</a>();
<a name="l00249"></a>00249   <span class="keywordflow">if</span> (!service) {
<a name="l00250"></a>00250     QString command = QString(<span class="stringliteral">&quot;\&quot;%1\&quot; --nt-service -f \&quot;%2\&quot; ControlPort %3&quot;</span>)
<a name="l00251"></a>00251                                                  .arg(torPath)
<a name="l00252"></a>00252                                                  .arg(torrc)
<a name="l00253"></a>00253                                                  .arg(controlPort);
<a name="l00254"></a>00254 
<a name="l00255"></a>00255     <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">&quot;Installing the Tor service using the command line &#39;%1&#39;&quot;</span>)
<a name="l00256"></a>00256                                                           .arg(command);
<a name="l00257"></a>00257     service = <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#aac4a7d128a08928acb5d6c44ac3990c8">CreateServiceA</a>(<a class="code" href="class_tor_service.html#a46e51a37fe5f1b464c011e870ec206c8">_scm</a>, 
<a name="l00258"></a>00258                               (LPCTSTR)<a class="code" href="_tor_service_8h.html#ab94ebc0021b96d5498ff2484fc7b7853">TOR_SERVICE_NAME</a>, (LPCTSTR)<a class="code" href="_tor_service_8h.html#a10b57df9c21825e2e6c705afd948b787">TOR_SERVICE_DISP</a>,
<a name="l00259"></a>00259                               <a class="code" href="_tor_service_8h.html#ae36be8de454f1eda19eeaded49424277">TOR_SERVICE_ACCESS</a>, SERVICE_WIN32_OWN_PROCESS,
<a name="l00260"></a>00260                               SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
<a name="l00261"></a>00261                               (LPCTSTR)command.toAscii().data(), NULL, NULL, NULL, 
<a name="l00262"></a>00262                               NULL, NULL);
<a name="l00263"></a>00263     <span class="keywordflow">if</span> (!service) {
<a name="l00264"></a>00264       <span class="comment">/* XXX This needs an actual reason message. */</span>
<a name="l00265"></a>00265       <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;Failed to install the Tor service.&quot;</span>);
<a name="l00266"></a>00266       <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00267"></a>00267     }
<a name="l00268"></a>00268 
<a name="l00269"></a>00269     SERVICE_DESCRIPTION desc;
<a name="l00270"></a>00270     desc.lpDescription = <a class="code" href="_tor_service_8h.html#a23d0458cc91579cdfd4a0bbdfb1ece36">TOR_SERVICE_DESC</a>;
<a name="l00271"></a>00271     <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a8cf5473280da29e50211666d001b3c23">ChangeServiceConfig2A</a>(service, 
<a name="l00272"></a>00272                                        SERVICE_CONFIG_DESCRIPTION, &amp;desc);
<a name="l00273"></a>00273     <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(service);
<a name="l00274"></a>00274   }
<a name="l00275"></a>00275   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00276"></a>00276 }
<a name="l00277"></a>00277 <span class="comment"></span>
<a name="l00278"></a>00278 <span class="comment">/** Removes the Tor service. Returns true if the service was removed</span>
<a name="l00279"></a>00279 <span class="comment"> * successfully or does not exist. */</span>
<a name="l00280"></a>00280 <span class="keywordtype">bool</span>
<a name="l00281"></a><a class="code" href="class_tor_service.html#a4235fd8923b2eee0a33dc4ea24fd26c1">00281</a> <a class="code" href="class_tor_service.html#a4235fd8923b2eee0a33dc4ea24fd26c1">TorService::remove</a>()
<a name="l00282"></a>00282 {
<a name="l00283"></a>00283   <span class="keywordtype">bool</span> removed = <span class="keyword">true</span>;
<a name="l00284"></a>00284   SC_HANDLE service = <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">openService</a>();
<a name="l00285"></a>00285 
<a name="l00286"></a>00286   <span class="keywordflow">if</span> (service) {
<a name="l00287"></a>00287     <a class="code" href="class_tor_service.html#a3dd3a77a3cc5b988477d0ae09825c26a">stop</a>();
<a name="l00288"></a>00288     <a class="code" href="namespacetc.html#aee5c749751dca91b96d6d024b1c4a076">tc::debug</a>(<span class="stringliteral">&quot;Removing the Tor service.&quot;</span>);
<a name="l00289"></a>00289     removed = <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#ace5e247e6646727738b1ef7e879fe12d">DeleteService</a>(service);
<a name="l00290"></a>00290     <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(service);
<a name="l00291"></a>00291   }
<a name="l00292"></a>00292   <span class="keywordflow">if</span> (!removed) {
<a name="l00293"></a>00293     <span class="comment">/* XXX This needs an actual reason message. */</span>
<a name="l00294"></a>00294     <a class="code" href="namespacetc.html#aa7353218fd7baef0876bbb422ce818af">tc::error</a>(<span class="stringliteral">&quot;Failed to remove the Tor service.&quot;</span>);
<a name="l00295"></a>00295   }
<a name="l00296"></a>00296   <span class="keywordflow">return</span> removed;
<a name="l00297"></a>00297 }
<a name="l00298"></a>00298 <span class="comment"></span>
<a name="l00299"></a>00299 <span class="comment">/** Gets the status of the Tor service. */</span>
<a name="l00300"></a>00300 DWORD
<a name="l00301"></a><a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">00301</a> <a class="code" href="class_tor_service.html#a6f610fd655180d69a19eb2a46c102aaa">TorService::status</a>()
<a name="l00302"></a>00302 {
<a name="l00303"></a>00303   SC_HANDLE service;
<a name="l00304"></a>00304   SERVICE_STATUS s;
<a name="l00305"></a>00305   DWORD stat = <a class="code" href="_tor_service_8h.html#a53a2dc80fa450eb12e1c76edb4ede017">SERVICE_ERROR</a>;
<a name="l00306"></a>00306   
<a name="l00307"></a>00307   service = <a class="code" href="class_tor_service.html#a20f67c721cd24a889594906646422d14">openService</a>();
<a name="l00308"></a>00308   <span class="keywordflow">if</span> (service &amp;&amp; <a class="code" href="class_tor_service.html#a4a930d2d1fc5d7c4c887ba92f242d46c">_service_fns</a>.<a class="code" href="struct_service_functions.html#a3b40037d4921e8d3d95dbb6c6f8046f7">QueryServiceStatus</a>(service, &amp;s))
<a name="l00309"></a>00309     stat = s.dwCurrentState;
<a name="l00310"></a>00310   <a class="code" href="class_tor_service.html#a6003b562b733c382e07e8e2f90f5c04d">closeHandle</a>(service);
<a name="l00311"></a>00311   <span class="keywordflow">return</span> stat;
<a name="l00312"></a>00312 }
<a name="l00313"></a>00313 
</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>