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)
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);
Thread.sleep(torRetryWaitTimeMS);
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);
}
}
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,9 +759,7 @@ public class TorService extends Service implements TorServiceConstants, TorConst
else
{
logNotice("Tor process id=" + procId);
initControlConnection ();
logNotice("Tor started; process id=" + procId);
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());
}

View File

@ -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";

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;
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;
}