2010-02-08 21:39:42 +01:00
|
|
|
/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
package org.torproject.android.service;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
2011-02-09 22:11:53 +01:00
|
|
|
import java.io.File;
|
2010-09-17 23:32:21 +02:00
|
|
|
import java.io.IOException;
|
2010-02-08 21:39:42 +01:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
2011-10-28 06:29:57 +02:00
|
|
|
import org.torproject.android.TorConstants;
|
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
public class TorServiceUtils implements TorServiceConstants {
|
|
|
|
|
2010-09-17 23:32:21 +02:00
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
|
|
|
|
public static int findProcessId(String command)
|
|
|
|
{
|
2010-07-20 00:34:15 +02:00
|
|
|
int procId = -1;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
procId = findProcessIdWithPidOf(command);
|
|
|
|
|
|
|
|
if (procId == -1)
|
|
|
|
procId = findProcessIdWithPS(command);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
procId = findProcessIdWithPS(command);
|
|
|
|
}
|
|
|
|
catch (Exception e2)
|
|
|
|
{
|
2011-10-28 06:29:57 +02:00
|
|
|
Log.w(TorConstants.TAG,"Unable to get proc id for: " + command,e2);
|
2010-07-20 00:34:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return procId;
|
|
|
|
}
|
|
|
|
|
|
|
|
//use 'pidof' command
|
|
|
|
public static int findProcessIdWithPidOf(String command) throws Exception
|
|
|
|
{
|
2010-03-06 15:56:39 +01:00
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
int procId = -1;
|
|
|
|
|
|
|
|
Runtime r = Runtime.getRuntime();
|
|
|
|
|
|
|
|
Process procPs = null;
|
|
|
|
|
2011-02-09 22:11:53 +01:00
|
|
|
String baseName = new File(command).getName();
|
|
|
|
//fix contributed my mikos on 2010.12.10
|
|
|
|
procPs = r.exec(new String[] {SHELL_CMD_PIDOF, baseName});
|
|
|
|
//procPs = r.exec(SHELL_CMD_PIDOF);
|
2010-02-08 21:39:42 +01:00
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
|
|
|
|
String line = null;
|
|
|
|
|
|
|
|
while ((line = reader.readLine())!=null)
|
|
|
|
{
|
2011-02-09 22:11:53 +01:00
|
|
|
|
|
|
|
try
|
2010-07-20 00:34:15 +02:00
|
|
|
{
|
|
|
|
//this line should just be the process id
|
|
|
|
procId = Integer.parseInt(line.trim());
|
|
|
|
break;
|
|
|
|
}
|
2011-02-09 22:11:53 +01:00
|
|
|
catch (NumberFormatException e)
|
|
|
|
{
|
2012-10-25 11:59:47 +02:00
|
|
|
Log.e("TorServiceUtils","unable to parse process pid: " + line,e);
|
2011-02-09 22:11:53 +01:00
|
|
|
}
|
2010-07-20 00:34:15 +02:00
|
|
|
}
|
2010-02-08 21:39:42 +01:00
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
|
|
|
|
return procId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//use 'ps' command
|
|
|
|
public static int findProcessIdWithPS(String command) throws Exception
|
|
|
|
{
|
|
|
|
|
|
|
|
int procId = -1;
|
|
|
|
|
|
|
|
Runtime r = Runtime.getRuntime();
|
|
|
|
|
|
|
|
Process procPs = null;
|
|
|
|
|
2014-02-23 01:16:47 +01:00
|
|
|
String baseName = new File(command).getName();
|
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
procPs = r.exec(SHELL_CMD_PS);
|
2010-02-08 21:39:42 +01:00
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
|
|
|
|
String line = null;
|
|
|
|
|
|
|
|
while ((line = reader.readLine())!=null)
|
|
|
|
{
|
2014-02-23 01:16:47 +01:00
|
|
|
if (line.indexOf('/' + baseName)!=-1)
|
2010-07-20 00:34:15 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
StringTokenizer st = new StringTokenizer(line," ");
|
|
|
|
st.nextToken(); //proc owner
|
|
|
|
|
|
|
|
procId = Integer.parseInt(st.nextToken().trim());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2010-02-08 21:39:42 +01:00
|
|
|
}
|
|
|
|
|
2010-07-20 00:34:15 +02:00
|
|
|
|
|
|
|
|
2010-02-08 21:39:42 +01:00
|
|
|
return procId;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|