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

[tor-commits] [orbot/master] fixes for getting process id of Tor process



commit 3413b341fca28c51aaff03ee2975ca3d0806fcbf
Author: Nathan Freitas <nathan@xxxxxxxxxxx>
Date:   Wed Apr 9 22:53:08 2014 -0400

    fixes for getting process id of Tor process
---
 src/org/torproject/android/service/TorService.java |   70 ++++++++++----------
 .../android/service/TorServiceConstants.java       |    2 +-
 .../android/service/TorServiceUtils.java           |   40 +++--------
 3 files changed, 45 insertions(+), 67 deletions(-)

diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java
index 1d2c727..52e4061 100644
--- a/src/org/torproject/android/service/TorService.java
+++ b/src/org/torproject/android/service/TorService.java
@@ -631,6 +631,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		if (mHasRoot && mEnableTransparentProxy)
 			enableTransparentProxy(mTransProxyAll, mTransProxyTethering);
 		
+		//checkAddressAndCountry();
     }
     
     /*
@@ -730,47 +731,27 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		
 		
 		int procId = -1;
-		int attempts = 0;
 
-		int torRetryWaitTimeMS = 2000;
+		int torRetryWaitTimeMS = 1000;
 		
 		ArrayList<String> alEnv = new ArrayList<String>();
 		alEnv.add("HOME=" + appBinHome.getAbsolutePath());
 		
+		sendCallbackStatusMessage(getString(R.string.status_starting_up));
+		
 		Shell shell = Shell.startShell(alEnv,appBinHome.getAbsolutePath());
 		SimpleCommand cmdTor = new SimpleCommand(fileTor.getAbsolutePath() + " DataDirectory " + appCacheHome.getAbsolutePath() + " -f " + torrcPath + "&");
 		shell.add(cmdTor);
-		
-		while (procId == -1 && attempts < MAX_START_TRIES)
-		{
-			
-			sendCallbackStatusMessage(getString(R.string.status_starting_up));
-			
-			shell.add(cmdTor);
-		
-			Thread.sleep(torRetryWaitTimeMS);
-			
-			procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath());
-			
-			if (procId == -1)
-			{
-				Thread.sleep(torRetryWaitTimeMS);
-				procId = TorServiceUtils.findProcessId(fileTor.getAbsolutePath());
-				attempts++;
-			}
-			else
-			{
-				logNotice("got tor proc id: " + procId);
-				
-			}
-		}
-		
+		Thread.sleep(torRetryWaitTimeMS);
+
+		procId = initControlConnection ();
+
 		shell.close();
 		
 		if (procId == -1)
 		{
-
-			logNotice(cmdTor.getExitCode() + ": " + cmdTor.getOutput());
+			
+			logNotice(getString(R.string.couldn_t_start_tor_process_) + "; exit=" + cmdTor.getExitCode() + ": " + cmdTor.getOutput());
 			sendCallbackStatusMessage(getString(R.string.couldn_t_start_tor_process_));
 			
 			throw new Exception ("Unable to start Tor");
@@ -778,10 +759,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		else
 		{
 		
-			logNotice("Tor process id=" + procId);
+			logNotice("Tor started; process id=" + procId);
 			
-			initControlConnection ();
-
 			processSettingsImpl();
 	    }
     }
@@ -840,7 +819,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 		return null;
 	}*/
 	
-	private void initControlConnection () throws Exception, RuntimeException
+	private int initControlConnection () throws Exception, RuntimeException
 	{
 			while (conn == null)
 			{
@@ -872,7 +851,9 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 				        
 			        }
 			        
-			        break; //don't need to retry
+			        String torProcId = conn.getInfo("process/pid");
+			        
+			        return Integer.parseInt(torProcId);
 				}
 				catch (Exception ce)
 				{
@@ -887,9 +868,24 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 			}
 		
 		
+			return -1;
 
 	}
 	
+	private void checkAddressAndCountry () throws IOException
+	{
+
+        if (TorService.ENABLE_DEBUG_LOG)
+        {
+        	String torExternalAddress = conn.getInfo("address");
+        	String torCountry = conn.getInfo("ip-to-country/" + torExternalAddress);
+        
+        	Log.d(TAG,"external address=" + torExternalAddress);
+        	Log.d(TAG,"external country=" + torCountry);
+        	
+        }
+        
+	}
 	
 	/*
 	private void getTorStatus () throws IOException
@@ -1356,7 +1352,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
 	        	{
 	        		public void run ()
 	        		{
-	        			try { conn.signal("NEWNYM"); }
+	        			try { conn.signal("NEWNYM"); 
+	        			
+	        			//checkAddressAndCountry();
+	        			
+	        			}
 	        			catch (IOException ioe){
 	        				logMessage("error requesting newny: " + ioe.getLocalizedMessage());
 	        			}
diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java
index f11315d..567d933 100644
--- a/src/org/torproject/android/service/TorServiceConstants.java
+++ b/src/org/torproject/android/service/TorServiceConstants.java
@@ -38,7 +38,7 @@ public interface TorServiceConstants {
 	public final static String SHELL_CMD_KILL = "kill -9";
 	public final static String SHELL_CMD_RM = "rm";
 	public final static String SHELL_CMD_PS = "ps";
-	public final static String SHELL_CMD_PIDOF = "pidof";
+	//public final static String SHELL_CMD_PIDOF = "pidof";
 	public final static String SHELL_CMD_LINK = "ln -s";
 	public final static String SHELL_CMD_CP = "cp";
 	
diff --git a/src/org/torproject/android/service/TorServiceUtils.java b/src/org/torproject/android/service/TorServiceUtils.java
index 96a2c8a..7b6df59 100644
--- a/src/org/torproject/android/service/TorServiceUtils.java
+++ b/src/org/torproject/android/service/TorServiceUtils.java
@@ -17,33 +17,14 @@ public class TorServiceUtils implements TorServiceConstants {
 
 	
 	
-	public static int findProcessId(String command) 
+	public static int findProcessId(String command) throws IOException 
 	{
-		int procId = -1;
-		
-		try
-		{
-			procId = findProcessIdWithPidOf(command);
-			
-			if (procId == -1)
-				procId = findProcessIdWithPS(command);
-		}
-		catch (Exception e)
-		{
-			try
-			{
-				procId = findProcessIdWithPS(command);
-			}
-			catch (Exception e2)
-			{
-				Log.w(TorConstants.TAG,"Unable to get proc id for: " + command,e2);
-			}
-		}
-		
+		int procId = findProcessIdWithPS(command);
 		return procId;
 	}
 	
 	//use 'pidof' command
+	/**
 	public static int findProcessIdWithPidOf(String command) throws Exception
 	{
 		
@@ -80,9 +61,10 @@ public class TorServiceUtils implements TorServiceConstants {
         return procId;
 
 	}
+	 * @throws IOException */
 	
 	//use 'ps' command
-	public static int findProcessIdWithPS(String command) throws Exception
+	public static int findProcessIdWithPS(String command) throws IOException 
 	{
 		
 		int procId = -1;
@@ -91,7 +73,7 @@ public class TorServiceUtils implements TorServiceConstants {
 		    	
 		Process procPs = null;
 		
-		String baseName = new File(command).getName();
+		String processKey = '/' + new File(command).getName();
 		
         procPs = r.exec(SHELL_CMD_PS);
             
@@ -100,20 +82,16 @@ public class TorServiceUtils implements TorServiceConstants {
         
         while ((line = reader.readLine())!=null)
         {
-        	if (line.indexOf('/' + baseName)!=-1)
+        	if (line.contains(processKey))
         	{
+        		String[] lineParts = line.split("\\s+");
         		
-        		StringTokenizer st = new StringTokenizer(line," ");
-        		st.nextToken(); //proc owner
-        		
-        		procId = Integer.parseInt(st.nextToken().trim());
+        		procId = Integer.parseInt(lineParts[1]);
         		
         		break;
         	}
         }
-        
        
-        
         return procId;
 
 	}



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits