1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-07 10:42:07 +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,42 +289,26 @@ public class SyncthingRunnable implements Runnable {
*/
private List<String> getSyncthingPIDs(Boolean enableLog) {
List<String> syncthingPIDs = new ArrayList<String>();
Process ps = null;
DataOutputStream psOut = null;
BufferedReader br = null;
try {
ps = Runtime.getRuntime().exec((mUseRoot) ? "su" : "sh");
psOut = new DataOutputStream(ps.getOutputStream());
psOut.writeBytes("ps\n");
psOut.writeBytes("exit\n");
psOut.flush();
ps.waitFor();
br = new BufferedReader(new InputStreamReader(ps.getInputStream(), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
if (line.contains(Constants.FILENAME_SYNCTHING_BINARY)) {
String syncthingPID = line.trim().split("\\s+")[1];
if (enableLog) {
Log.v(TAG, "getSyncthingPIDs: Found process PID [" + syncthingPID + "]");
}
syncthingPIDs.add(syncthingPID);
String output = Util.runShellCommandGetOutput("ps\n", mUseRoot);
if (TextUtils.isEmpty(output)) {
Log.w(TAG, "Failed to list SyncthingNative processes. ps command returned empty.");
return syncthingPIDs;
}
String lines[] = output.split("\n");
if (lines.length == 0) {
Log.w(TAG, "Failed to list SyncthingNative processes. ps command returned no rows.");
return syncthingPIDs;
}
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if (line.contains(Constants.FILENAME_SYNCTHING_BINARY)) {
String syncthingPID = line.trim().split("\\s+")[1];
if (enableLog) {
Log.v(TAG, "getSyncthingPIDs: Found process PID [" + 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();
syncthingPIDs.add(syncthingPID);
}
}
return syncthingPIDs;