Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure TorService properly shuts down whenever the tor thread stops #65

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 47 additions & 51 deletions tor-android-binary/src/main/java/org/torproject/jni/TorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
*/
public class TorService extends Service {

// TODO orbotserver and/or tor-android-server are the borders to use, strip out Prefs and VPN
// grep -rF org.torproject.android.service app/src/main/java/ ~/code/guardianproject/orbot/app/src/main/java

public static final String TAG = "TorService";

public static final String VERSION_NAME = org.torproject.jni.BuildConfig.VERSION_NAME;
Expand Down Expand Up @@ -200,6 +197,7 @@ public TorService getService() {

@Override
public IBinder onBind(Intent intent) {
// TODO send broadcastStatus() here?
return binder;
}

Expand Down Expand Up @@ -327,62 +325,60 @@ public void run() {
}
runLock.lock();
Log.i(TAG, "Acquired lock");
try {
createTorConfiguration();
ArrayList<String> lines = new ArrayList<>(Arrays.asList("tor", "--verify-config", // must always be here
"--RunAsDaemon", "0",
"-f", getTorrc(context).getAbsolutePath(),
"--defaults-torrc", getDefaultsTorrc(context).getAbsolutePath(),
"--ignore-missing-torrc",
"--SyslogIdentityTag", TAG,
"--CacheDirectory", new File(getCacheDir(), TAG).getAbsolutePath(),
"--DataDirectory", getAppTorServiceDataDir(context).getAbsolutePath(),
"--ControlSocket", getControlSocket(context).getAbsolutePath(),
"--CookieAuthentication", "0",
"--SOCKSPort", socksPort,
"--HTTPTunnelPort", httpTunnelPort,

// can be moved to ControlPort messages
"--LogMessageDomains", "1",
"--TruncateLogFile", "1"
));
String[] verifyLines = lines.toArray(new String[0]);
if (!mainConfigurationSetCommandLine(verifyLines)) {
throw new IllegalArgumentException("Setting command line failed: " + Arrays.toString(verifyLines));
}
int result = runMain(); // run verify
if (result != 0) {
throw new IllegalArgumentException("Bad command flags: " + Arrays.toString(verifyLines));
}
createTorConfiguration();
ArrayList<String> lines = new ArrayList<>(Arrays.asList("tor", "--verify-config", // must always be here
"--RunAsDaemon", "0",
"-f", getTorrc(context).getAbsolutePath(),
"--defaults-torrc", getDefaultsTorrc(context).getAbsolutePath(),
"--ignore-missing-torrc",
"--SyslogIdentityTag", TAG,
"--CacheDirectory", new File(getCacheDir(), TAG).getAbsolutePath(),
"--DataDirectory", getAppTorServiceDataDir(context).getAbsolutePath(),
"--ControlSocket", getControlSocket(context).getAbsolutePath(),
"--CookieAuthentication", "0",
"--SOCKSPort", socksPort,
"--HTTPTunnelPort", httpTunnelPort,

// can be moved to ControlPort messages
"--LogMessageDomains", "1",
"--TruncateLogFile", "1"
));
String[] verifyLines = lines.toArray(new String[0]);
if (!mainConfigurationSetCommandLine(verifyLines)) {
throw new IllegalArgumentException("Setting command line failed: " + Arrays.toString(verifyLines));
}
int result = runMain(); // run verify
if (result != 0) {
throw new IllegalArgumentException("Bad command flags: " + Arrays.toString(verifyLines));
}

controlPortThreadStarted = new CountDownLatch(1);
controlPortThread.start();
controlPortThreadStarted.await();
controlPortThreadStarted = new CountDownLatch(1);
controlPortThread.start();
controlPortThreadStarted.await();

String[] runLines = new String[lines.size() - 1];
runLines[0] = "tor";
for (int i = 2; i < lines.size(); i++) {
runLines[i - 1] = lines.get(i);
}
if (!mainConfigurationSetCommandLine(runLines)) {
throw new IllegalArgumentException("Setting command line failed: " + Arrays.toString(runLines));
}
if (!mainConfigurationSetupControlSocket()) {
throw new IllegalStateException("Setting up ControlPort failed!");
}
if (runMain() != 0) {
throw new IllegalStateException("Tor could not start!");
}
} finally {
mainConfigurationFree();
Log.i(TAG, "Releasing lock");
runLock.unlock();
String[] runLines = new String[lines.size() - 1];
runLines[0] = "tor";
for (int i = 2; i < lines.size(); i++) {
runLines[i - 1] = lines.get(i);
}
if (!mainConfigurationSetCommandLine(runLines)) {
throw new IllegalArgumentException("Setting command line failed: " + Arrays.toString(runLines));
}
if (!mainConfigurationSetupControlSocket()) {
throw new IllegalStateException("Setting up ControlPort failed!");
}
if (runMain() != 0) {
throw new IllegalStateException("Tor could not start!");
}

} catch (IllegalStateException | IllegalArgumentException | InterruptedException e) {
e.printStackTrace();
broadcastError(context, e);
} finally {
broadcastStatus(context, STATUS_STOPPING);
mainConfigurationFree();
Log.i(TAG, "Releasing lock");
runLock.unlock();
eighthave marked this conversation as resolved.
Show resolved Hide resolved
TorService.this.stopSelf();
}
}
Expand Down