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

[vidalia-svn] r1244: Snapshot the list of running processes on Win32 when we're c (trunk/src/util)



Author: edmanm
Date: 2006-09-26 22:32:47 -0400 (Tue, 26 Sep 2006)
New Revision: 1244

Modified:
   trunk/src/util/process.cpp
   trunk/src/util/process.h
   trunk/src/util/win32.cpp
   trunk/src/util/win32.h
Log:
Snapshot the list of running processes on Win32 when we're checking for an existing Vidalia and compare exe filename in addition to PID, instead of relying on OpenProcess and GetExitCodeProcess. This should reduce the number of false positives we've heard about on Windows. (Ticket #128)

Modified: trunk/src/util/process.cpp
===================================================================
--- trunk/src/util/process.cpp	2006-09-26 01:35:34 UTC (rev 1243)
+++ trunk/src/util/process.cpp	2006-09-27 02:32:47 UTC (rev 1244)
@@ -27,7 +27,9 @@
 
 #include <QDir>
 #include <QFile>
+#include <QFileInfo>
 #include <QTextStream>
+#include <vidalia.h>
 
 #include "string.h"
 #include "process.h"
@@ -49,26 +51,14 @@
 is_process_running(qint64 pid)
 {
 #if defined(Q_OS_WIN)
-  BOOL rc;
-  DWORD exitCode;
-  
-  /* Try to open the process to see if it exists */
-  HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, (DWORD)pid);
-  if (hProcess == NULL) {
-    return false;
+  QHash<quint64, QString> procList = win32_process_list();
+  if (procList.contains(pid)) {
+    /* A process with this ID exists. Check if it's Vidalia. */
+    QString exeFile = procList.value(pid);
+    QString vidaliaExe = QFileInfo(Vidalia::applicationFilePath()).fileName();
+    return (exeFile.toLower() == vidaliaExe.toLower());
   }
-  
-  /* It exists, so see if it's still active or if it terminated already */
-  rc = GetExitCodeProcess(hProcess, &exitCode);
-  CloseHandle(hProcess);
-  if (!rc) {
-    /* Error. Assume it doesn't exist (is this a bad assumption?) */
-    return false;
-  }
-  /* If GetExitCodeProcess() returns a non-zero value, and the process is
-   * still running, exitCode should equal STILL_ACTIVE. Otherwise, this means
-   * the process has terminated. */
-  return (exitCode == STILL_ACTIVE);
+  return false;
 #else
   /* Send the "null" signal to check if a process exists */
   if (kill((pid_t)pid, 0) < 0) {

Modified: trunk/src/util/process.h
===================================================================
--- trunk/src/util/process.h	2006-09-26 01:35:34 UTC (rev 1243)
+++ trunk/src/util/process.h	2006-09-27 02:32:47 UTC (rev 1244)
@@ -31,8 +31,7 @@
 #include <QString>
 
 #if defined(Q_OS_WIN)
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
+#include "win32.h"
 #else
 #include <sys/types.h>
 #include <unistd.h>

Modified: trunk/src/util/win32.cpp
===================================================================
--- trunk/src/util/win32.cpp	2006-09-26 01:35:34 UTC (rev 1243)
+++ trunk/src/util/win32.cpp	2006-09-27 02:32:47 UTC (rev 1244)
@@ -25,10 +25,10 @@
  * \brief Win32-specific functions
  */
 
-#include <windows.h>
+#include "win32.h"
+#include <tlhelp32.h>
 #include <shlobj.h>
 #include <QDir>
-#include "win32.h"
 
 
 /** Finds the location of the "special" Windows folder using the given CSIDL
@@ -144,3 +144,38 @@
   RegCloseKey(key);
 }
 
+/** Returns a list of all currently active processes, including their pid
+ * and exe filename. */
+QHash<quint64, QString>
+win32_process_list()
+{
+  QHash<quint64, QString> procList;
+  HANDLE hSnapshot;
+  PROCESSENTRY32 proc;
+  QString exeFile;
+  quint64 pid;
+ 
+  /* Create a snapshot of all active processes */
+  hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+  if (hSnapshot != INVALID_HANDLE_VALUE) {
+    proc.dwSize = sizeof(PROCESSENTRY32);
+    
+    /* Iterate through all the processes in the snapshot */
+    if (!Process32First(hSnapshot, &proc)) {
+      return procList;
+    }
+    do {
+      /* Extract the PID and exe filename from the process record */
+      pid = (quint64)proc.th32ProcessID;
+      QT_WA(
+        exeFile = QString::fromUtf16((const ushort *)proc.szExeFile);,
+        exeFile = QString::fromAscii((const char *)proc.szExeFile);
+      )
+      /* Add this process to our list */
+      procList.insert(pid, exeFile);
+    } while (Process32Next(hSnapshot, &proc));
+    CloseHandle(hSnapshot);
+  }
+  return procList;
+}
+

Modified: trunk/src/util/win32.h
===================================================================
--- trunk/src/util/win32.h	2006-09-26 01:35:34 UTC (rev 1243)
+++ trunk/src/util/win32.h	2006-09-27 02:32:47 UTC (rev 1244)
@@ -28,6 +28,9 @@
 #ifndef _WIN32_H
 #define _WIN32_H
 
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <QHash>
 #include <QString>
 
 /** Retrieves the location of the user's %PROGRAMFILES% folder. */
@@ -45,5 +48,9 @@
 /** Removes the key from the registry if it exists */
 void win32_registry_remove_key(QString keyLocation, QString keyName);
 
+/** Returns a list of all currently active processes, including their pid
+ * and exe filename. */
+QHash<quint64, QString> win32_process_list();
+
 #endif