Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > by-pkgid > 9601c7beb4ff23e834bfa171795ed560 > files > 898

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: procutil.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_3459d97d61361c9d41d467fba45f9c61.html">common</a>
  </div>
</div>
<div class="contents">
<h1>procutil.cpp</h1><a href="procutil_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 you</span>
<a name="l00004"></a>00004 <span class="comment">**  did not receive the LICENSE file with this file, you may obtain it from the</span>
<a name="l00005"></a>00005 <span class="comment">**  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 the</span>
<a name="l00008"></a>00008 <span class="comment">**  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 procutil.cpp</span>
<a name="l00013"></a>00013 <span class="comment">** \version $Id: procutil.cpp 3751 2009-05-01 03:26:38Z edmanm $</span>
<a name="l00014"></a>00014 <span class="comment">** \brief Process information and pidfile functions </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="procutil_8h.html">procutil.h</a>&quot;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;<a class="code" href="stringutil_8h.html">stringutil.h</a>&quot;</span>
<a name="l00019"></a>00019 
<a name="l00020"></a>00020 <span class="preprocessor">#include &lt;QDir&gt;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;QFile&gt;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;QFileInfo&gt;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;QTextStream&gt;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;QApplication&gt;</span>
<a name="l00025"></a>00025 
<a name="l00026"></a>00026 <span class="comment"></span>
<a name="l00027"></a>00027 <span class="comment">/** Returns the PID of the current process. */</span>
<a name="l00028"></a>00028 qint64
<a name="l00029"></a><a class="code" href="procutil_8h.html#a9815ed5d861a404567963345f845594d">00029</a> <a class="code" href="procutil_8cpp.html#a9815ed5d861a404567963345f845594d">get_pid</a>()
<a name="l00030"></a>00030 {
<a name="l00031"></a>00031 <span class="preprocessor">#if defined(Q_OS_WIN)</span>
<a name="l00032"></a>00032 <span class="preprocessor"></span>  <span class="keywordflow">return</span> (qint64)GetCurrentProcessId();
<a name="l00033"></a>00033 <span class="preprocessor">#else</span>
<a name="l00034"></a>00034 <span class="preprocessor"></span>  <span class="keywordflow">return</span> (qint64)getpid();
<a name="l00035"></a>00035 <span class="preprocessor">#endif</span>
<a name="l00036"></a>00036 <span class="preprocessor"></span>}
<a name="l00037"></a>00037 <span class="comment"></span>
<a name="l00038"></a>00038 <span class="comment">/** Returns true if a process with the given PID is running. */</span>
<a name="l00039"></a>00039 <span class="keywordtype">bool</span>
<a name="l00040"></a><a class="code" href="procutil_8h.html#af5b803a9d74a53d365aa058283187202">00040</a> <a class="code" href="procutil_8cpp.html#af5b803a9d74a53d365aa058283187202">is_process_running</a>(qint64 pid)
<a name="l00041"></a>00041 {
<a name="l00042"></a>00042 <span class="preprocessor">#if defined(Q_OS_WIN)</span>
<a name="l00043"></a>00043 <span class="preprocessor"></span>  QHash&lt;qint64, QString&gt; procList = <a class="code" href="win32_8cpp.html#a8af39b060612b54ccf604ccbf9c4c415">win32_process_list</a>();
<a name="l00044"></a>00044   <span class="keywordflow">if</span> (procList.contains(pid)) {
<a name="l00045"></a>00045     <span class="comment">/* A process with this ID exists. Check if it&#39;s the same as this process. */</span>
<a name="l00046"></a>00046     QString exeFile = procList.value(pid);
<a name="l00047"></a>00047     QString thisExe = QFileInfo(QApplication::applicationFilePath()).fileName();
<a name="l00048"></a>00048     <span class="keywordflow">return</span> (exeFile.toLower() == thisExe.toLower());
<a name="l00049"></a>00049   }
<a name="l00050"></a>00050   <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00051"></a>00051 <span class="preprocessor">#else</span>
<a name="l00052"></a>00052 <span class="preprocessor"></span>  <span class="comment">/* Send the &quot;null&quot; signal to check if a process exists */</span>
<a name="l00053"></a>00053   <span class="keywordflow">if</span> (kill((pid_t)pid, 0) &lt; 0) {
<a name="l00054"></a>00054     <span class="keywordflow">return</span> (errno != ESRCH);
<a name="l00055"></a>00055   }
<a name="l00056"></a>00056   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00057"></a>00057 <span class="preprocessor">#endif</span>
<a name="l00058"></a>00058 <span class="preprocessor"></span>}
<a name="l00059"></a>00059 <span class="comment"></span>
<a name="l00060"></a>00060 <span class="comment">/** Writes the given file to disk containing the current process&#39;s PID. */</span>
<a name="l00061"></a>00061 <span class="keywordtype">bool</span>
<a name="l00062"></a><a class="code" href="procutil_8h.html#a6ae9de0ac8703cb747f24861847fe28f">00062</a> <a class="code" href="procutil_8cpp.html#a6100070763002248ce43a0a185619010">write_pidfile</a>(<span class="keyword">const</span> QString &amp;pidFileName, QString *errmsg)
<a name="l00063"></a>00063 {
<a name="l00064"></a>00064   <span class="comment">/* Make sure the directory exists */</span>
<a name="l00065"></a>00065   QDir pidFileDir = QFileInfo(pidFileName).absoluteDir();
<a name="l00066"></a>00066   <span class="keywordflow">if</span> (!pidFileDir.exists()) {
<a name="l00067"></a>00067     pidFileDir.mkpath(QDir::convertSeparators(pidFileDir.absolutePath()));
<a name="l00068"></a>00068   }
<a name="l00069"></a>00069 
<a name="l00070"></a>00070   <span class="comment">/* Try to open (and create if it doesn&#39;t exist) the pidfile */</span>
<a name="l00071"></a>00071   QFile pidfile(pidFileName);
<a name="l00072"></a>00072   <span class="keywordflow">if</span> (!pidfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
<a name="l00073"></a>00073     <span class="keywordflow">return</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>(errmsg, pidfile.errorString());
<a name="l00074"></a>00074   }
<a name="l00075"></a>00075 
<a name="l00076"></a>00076   <span class="comment">/* Write our current PID to it */</span>
<a name="l00077"></a>00077   QTextStream pidstream(&amp;pidfile);
<a name="l00078"></a>00078   pidstream &lt;&lt; <a class="code" href="procutil_8cpp.html#a9815ed5d861a404567963345f845594d">get_pid</a>();
<a name="l00079"></a>00079   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00080"></a>00080 }
<a name="l00081"></a>00081 <span class="comment"></span>
<a name="l00082"></a>00082 <span class="comment">/** Reads the given pidfile and returns the value contained in it. If the file</span>
<a name="l00083"></a>00083 <span class="comment"> * does not exist 0 is returned. Returns -1 if an error occurs. */</span>
<a name="l00084"></a>00084 qint64 
<a name="l00085"></a><a class="code" href="procutil_8h.html#a0c1947b6c742ab642d15e25b7fbbb9fa">00085</a> <a class="code" href="procutil_8cpp.html#a57687226d89c6ea7588b7faf64b38aa0">read_pidfile</a>(<span class="keyword">const</span> QString &amp;pidFileName, QString *errmsg)
<a name="l00086"></a>00086 {
<a name="l00087"></a>00087   qint64 pid;
<a name="l00088"></a>00088   
<a name="l00089"></a>00089   <span class="comment">/* Open the pidfile, if it exists */</span>
<a name="l00090"></a>00090   QFile pidfile(pidFileName);
<a name="l00091"></a>00091   <span class="keywordflow">if</span> (!pidfile.exists()) {
<a name="l00092"></a>00092     <span class="keywordflow">return</span> 0;
<a name="l00093"></a>00093   }
<a name="l00094"></a>00094   <span class="keywordflow">if</span> (!pidfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
<a name="l00095"></a>00095     <span class="keywordflow">if</span> (errmsg) {
<a name="l00096"></a>00096       *errmsg = pidfile.errorString();
<a name="l00097"></a>00097     }
<a name="l00098"></a>00098     <span class="keywordflow">return</span> -1; 
<a name="l00099"></a>00099   }
<a name="l00100"></a>00100   
<a name="l00101"></a>00101   <span class="comment">/* Read the PID in from the file */</span>
<a name="l00102"></a>00102   QTextStream pidstream(&amp;pidfile);
<a name="l00103"></a>00103   pidstream &gt;&gt; pid;
<a name="l00104"></a>00104   <span class="keywordflow">return</span> pid;
<a name="l00105"></a>00105 }
<a name="l00106"></a>00106 
<a name="l00107"></a>00107 QHash&lt;qint64, QString&gt;
<a name="l00108"></a><a class="code" href="procutil_8h.html#ab7841f8a13c52978844e7ac73fffce3e">00108</a> <a class="code" href="procutil_8cpp.html#ab7841f8a13c52978844e7ac73fffce3e">process_list</a>()
<a name="l00109"></a>00109 {
<a name="l00110"></a>00110 <span class="preprocessor">#if defined(Q_OS_WIN32)</span>
<a name="l00111"></a>00111 <span class="preprocessor"></span>  <span class="keywordflow">return</span> <a class="code" href="win32_8cpp.html#a8af39b060612b54ccf604ccbf9c4c415">win32_process_list</a>();
<a name="l00112"></a>00112 <span class="preprocessor">#else</span>
<a name="l00113"></a>00113 <span class="preprocessor"></span>  <span class="keywordflow">return</span> QHash&lt;qint64, QString&gt;();
<a name="l00114"></a>00114 <span class="preprocessor">#endif</span>
<a name="l00115"></a>00115 <span class="preprocessor"></span>}
<a name="l00116"></a>00116 
<a name="l00117"></a>00117 <span class="keywordtype">bool</span>
<a name="l00118"></a><a class="code" href="procutil_8h.html#adaca84b3676f00dc1318fb38a5f8d376">00118</a> <a class="code" href="procutil_8cpp.html#adaca84b3676f00dc1318fb38a5f8d376">process_kill</a>(qint64 pid)
<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>  HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE,
<a name="l00122"></a>00122                                 static_cast&lt;DWORD&gt;(pid));
<a name="l00123"></a>00123   <span class="keywordflow">if</span> (hProcess == NULL)
<a name="l00124"></a>00124     <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00125"></a>00125 
<a name="l00126"></a>00126   BOOL ret = TerminateProcess(hProcess, 0);
<a name="l00127"></a>00127   CloseHandle(hProcess);
<a name="l00128"></a>00128 
<a name="l00129"></a>00129   <span class="keywordflow">return</span> (ret != FALSE);
<a name="l00130"></a>00130 <span class="preprocessor">#else</span>
<a name="l00131"></a>00131 <span class="preprocessor"></span>  <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00132"></a>00132 <span class="preprocessor">#endif</span>
<a name="l00133"></a>00133 <span class="preprocessor"></span>}
<a name="l00134"></a>00134 
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:54 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>