fixes for getting process id of Tor process

This commit is contained in:
Nathan Freitas 2014-04-09 22:53:08 -04:00
parent e9d0feade0
commit 3413b341fc
3 changed files with 45 additions and 67 deletions

View File

@ -631,6 +631,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
if (mHasRoot && mEnableTransparentProxy) if (mHasRoot && mEnableTransparentProxy)
enableTransparentProxy(mTransProxyAll, mTransProxyTethering); enableTransparentProxy(mTransProxyAll, mTransProxyTethering);
//checkAddressAndCountry();
} }
/* /*
@ -730,47 +731,27 @@ public class TorService extends Service implements TorServiceConstants, TorConst
int procId = -1; int procId = -1;
int attempts = 0;
int torRetryWaitTimeMS = 2000; int torRetryWaitTimeMS = 1000;
ArrayList<String> alEnv = new ArrayList<String>(); ArrayList<String> alEnv = new ArrayList<String>();
alEnv.add("HOME=" + appBinHome.getAbsolutePath()); alEnv.add("HOME=" + appBinHome.getAbsolutePath());
sendCallbackStatusMessage(getString(R.string.status_starting_up));
Shell shell = Shell.startShell(alEnv,appBinHome.getAbsolutePath()); Shell shell = Shell.startShell(alEnv,appBinHome.getAbsolutePath());
SimpleCommand cmdTor = new SimpleCommand(fileTor.getAbsolutePath() + " DataDirectory " + appCacheHome.getAbsolutePath() + " -f " + torrcPath + "&"); SimpleCommand cmdTor = new SimpleCommand(fileTor.getAbsolutePath() + " DataDirectory " + appCacheHome.getAbsolutePath() + " -f " + torrcPath + "&");
shell.add(cmdTor); shell.add(cmdTor);
Thread.sleep(torRetryWaitTimeMS);
while (procId == -1 && attempts < MAX_START_TRIES) procId = initControlConnection ();
{
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);
}
}
shell.close(); shell.close();
if (procId == -1) 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_)); sendCallbackStatusMessage(getString(R.string.couldn_t_start_tor_process_));
throw new Exception ("Unable to start Tor"); throw new Exception ("Unable to start Tor");
@ -778,9 +759,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
else else
{ {
logNotice("Tor process id=" + procId); logNotice("Tor started; process id=" + procId);
initControlConnection ();
processSettingsImpl(); processSettingsImpl();
} }
@ -840,7 +819,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
return null; return null;
}*/ }*/
private void initControlConnection () throws Exception, RuntimeException private int initControlConnection () throws Exception, RuntimeException
{ {
while (conn == null) 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) 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 private void getTorStatus () throws IOException
@ -1356,7 +1352,11 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
public void run () public void run ()
{ {
try { conn.signal("NEWNYM"); } try { conn.signal("NEWNYM");
//checkAddressAndCountry();
}
catch (IOException ioe){ catch (IOException ioe){
logMessage("error requesting newny: " + ioe.getLocalizedMessage()); logMessage("error requesting newny: " + ioe.getLocalizedMessage());
} }

View File

@ -38,7 +38,7 @@ public interface TorServiceConstants {
public final static String SHELL_CMD_KILL = "kill -9"; public final static String SHELL_CMD_KILL = "kill -9";
public final static String SHELL_CMD_RM = "rm"; public final static String SHELL_CMD_RM = "rm";
public final static String SHELL_CMD_PS = "ps"; 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_LINK = "ln -s";
public final static String SHELL_CMD_CP = "cp"; public final static String SHELL_CMD_CP = "cp";

View File

@ -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; int procId = findProcessIdWithPS(command);
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);
}
}
return procId; return procId;
} }
//use 'pidof' command //use 'pidof' command
/**
public static int findProcessIdWithPidOf(String command) throws Exception public static int findProcessIdWithPidOf(String command) throws Exception
{ {
@ -80,9 +61,10 @@ public class TorServiceUtils implements TorServiceConstants {
return procId; return procId;
} }
* @throws IOException */
//use 'ps' command //use 'ps' command
public static int findProcessIdWithPS(String command) throws Exception public static int findProcessIdWithPS(String command) throws IOException
{ {
int procId = -1; int procId = -1;
@ -91,7 +73,7 @@ public class TorServiceUtils implements TorServiceConstants {
Process procPs = null; Process procPs = null;
String baseName = new File(command).getName(); String processKey = '/' + new File(command).getName();
procPs = r.exec(SHELL_CMD_PS); procPs = r.exec(SHELL_CMD_PS);
@ -100,20 +82,16 @@ public class TorServiceUtils implements TorServiceConstants {
while ((line = reader.readLine())!=null) while ((line = reader.readLine())!=null)
{ {
if (line.indexOf('/' + baseName)!=-1) if (line.contains(processKey))
{ {
String[] lineParts = line.split("\\s+");
StringTokenizer st = new StringTokenizer(line," "); procId = Integer.parseInt(lineParts[1]);
st.nextToken(); //proc owner
procId = Integer.parseInt(st.nextToken().trim());
break; break;
} }
} }
return procId; return procId;
} }