From c86525c1d4bcfce6e3af927f0e7eac9aab49b83f Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Fri, 9 May 2014 17:51:54 +0100
Subject: [PATCH] Kill zombie Tor processes more reliably.

---
 .../briarproject/plugins/tor/TorPlugin.java   | 23 ++++++++++++++-----
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java b/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java
index 5b3a339b56..a7e3186add 100644
--- a/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java
+++ b/briar-android/src/org/briarproject/plugins/tor/TorPlugin.java
@@ -376,9 +376,11 @@ class TorPlugin implements DuplexPlugin, EventHandler {
 	 * killed. ActivityManager.killBackgroundProcesses() doesn't seem to work
 	 * in this case, so we must parse the output of ps to get the PID.
 	 * <p>
-	 * On all tested devices, the output consists of a header line followed by
-	 * one line per process. The second column is the PID and the last column
-	 * is the process name, which includes the app's package name.
+	 * On all devices we've tested, the output consists of a header line
+	 * followed by one line per process. The second column is the PID and the
+	 * last column is the process name, which includes the app's package name.
+	 * On some devices tested by the Guardian Project, the first column is the
+	 * PID.
 	 */
 	private void killZombieProcess() {
 		String packageName = "/" + appContext.getPackageName() + "/";
@@ -388,12 +390,21 @@ class TorPlugin implements DuplexPlugin, EventHandler {
 			Scanner scanner = new Scanner(ps.getInputStream());
 			// Discard the header line
 			if(scanner.hasNextLine()) scanner.nextLine();
-			// Look for a Tor process with our package name
+			// Look for any Tor processes with our package name
 			boolean found = false;
 			while(scanner.hasNextLine()) {
 				String[] columns = scanner.nextLine().split("\\s+");
-				if(columns.length < 3) break;
-				int pid = Integer.parseInt(columns[1]);
+				if(columns.length < 3) continue;
+				int pid;
+				try {
+					pid = Integer.parseInt(columns[1]);
+				} catch(NumberFormatException e) {
+					try {
+						pid = Integer.parseInt(columns[0]);
+					} catch(NumberFormatException e1) {
+						continue;
+					}
+				}
 				String name = columns[columns.length - 1];
 				if(name.contains(packageName) && name.endsWith("/tor")) {
 					if(LOG.isLoggable(INFO))
-- 
GitLab