mirror of
https://github.com/syncthing/syncthing-android.git
synced 2025-01-09 03:31:46 +00:00
* Use Util.runShellCommandGetOutput in SyncthingRunnable#getSyncthingPIDs (fixes #285)
This commit is contained in:
parent
9dcb9f2262
commit
37c11836e1
1 changed files with 19 additions and 35 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue