1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-09 11:41:29 +00:00

Fix ANR in SyncthingRunnable#getSyncthingPIDs (fixes #285) (#288)

* Use Util.runShellCommandGetOutput in SyncthingRunnable#getSyncthingPIDs (fixes #285)
This commit is contained in:
Catfriend1 2019-01-28 09:03:01 +01:00 committed by GitHub
parent 9dcb9f2262
commit 37c11836e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -289,19 +289,20 @@ public class SyncthingRunnable implements Runnable {
*/ */
private List<String> getSyncthingPIDs(Boolean enableLog) { private List<String> getSyncthingPIDs(Boolean enableLog) {
List<String> syncthingPIDs = new ArrayList<String>(); List<String> syncthingPIDs = new ArrayList<String>();
Process ps = null; String output = Util.runShellCommandGetOutput("ps\n", mUseRoot);
DataOutputStream psOut = null; if (TextUtils.isEmpty(output)) {
BufferedReader br = null; Log.w(TAG, "Failed to list SyncthingNative processes. ps command returned empty.");
try { return syncthingPIDs;
ps = Runtime.getRuntime().exec((mUseRoot) ? "su" : "sh"); }
psOut = new DataOutputStream(ps.getOutputStream());
psOut.writeBytes("ps\n"); String lines[] = output.split("\n");
psOut.writeBytes("exit\n"); if (lines.length == 0) {
psOut.flush(); Log.w(TAG, "Failed to list SyncthingNative processes. ps command returned no rows.");
ps.waitFor(); return syncthingPIDs;
br = new BufferedReader(new InputStreamReader(ps.getInputStream(), "UTF-8")); }
String line;
while ((line = br.readLine()) != null) { for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if (line.contains(Constants.FILENAME_SYNCTHING_BINARY)) { if (line.contains(Constants.FILENAME_SYNCTHING_BINARY)) {
String syncthingPID = line.trim().split("\\s+")[1]; String syncthingPID = line.trim().split("\\s+")[1];
if (enableLog) { if (enableLog) {
@ -310,23 +311,6 @@ public class SyncthingRunnable implements Runnable {
syncthingPIDs.add(syncthingPID); syncthingPIDs.add(syncthingPID);
} }
} }
} catch (IOException | InterruptedException e) {
Log.w(TAG, "Failed to list Syncthing processes", e);
} finally {
try {
if (br != null) {
br.close();
}
if (psOut != null) {
psOut.close();
}
} catch (IOException e) {
Log.w(TAG, "Failed to close psOut stream", e);
}
if (ps != null) {
ps.destroy();
}
}
return syncthingPIDs; return syncthingPIDs;
} }