Handle exit code 137 caused by SIGKILL.

Also don't restart Syncthing twice (via loop and via intent).
This commit is contained in:
Felix Ableitner 2016-04-04 22:43:50 +02:00
parent 88abd80c66
commit 7f090e3653
1 changed files with 38 additions and 30 deletions

View File

@ -108,46 +108,54 @@ public class SyncthingRunnable implements Runnable {
try { try {
if (wakeLock != null) if (wakeLock != null)
wakeLock.acquire(); wakeLock.acquire();
// Loop to handle Syncthing restarts (these always have an error code of 3). ProcessBuilder pb = (useRoot())
do { ? new ProcessBuilder("su", "-c", TextUtils.join(" ", mCommand))
ProcessBuilder pb = (useRoot()) : new ProcessBuilder(mCommand);
? new ProcessBuilder("su", "-c", TextUtils.join(" ", mCommand))
: new ProcessBuilder(mCommand);
Map<String, String> env = pb.environment(); Map<String, String> env = pb.environment();
// Set home directory to data folder for web GUI folder picker. // Set home directory to data folder for web GUI folder picker.
env.put("HOME", Environment.getExternalStorageDirectory().getAbsolutePath()); env.put("HOME", Environment.getExternalStorageDirectory().getAbsolutePath());
env.put("STTRACE", sp.getString("sttrace", "")); env.put("STTRACE", sp.getString("sttrace", ""));
env.put("STNORESTART", "1"); env.put("STNORESTART", "1");
env.put("STNOUPGRADE", "1"); env.put("STNOUPGRADE", "1");
env.put("STGUIAUTH", sp.getString("gui_user", "") + ":" + env.put("STGUIAUTH", sp.getString("gui_user", "") + ":" +
sp.getString("gui_password", "")); sp.getString("gui_password", ""));
process = pb.start(); process = pb.start();
mSyncthing.set(process); mSyncthing.set(process);
mErrorLog = ""; mErrorLog = "";
Thread lInfo = log(process.getInputStream(), Log.INFO, true); Thread lInfo = log(process.getInputStream(), Log.INFO, true);
Thread lWarn = log(process.getErrorStream(), Log.WARN, true); Thread lWarn = log(process.getErrorStream(), Log.WARN, true);
niceSyncthing(); niceSyncthing();
ret = process.waitFor(); ret = process.waitFor();
mSyncthing.set(null); mSyncthing.set(null);
lInfo.join(); lInfo.join();
lWarn.join(); lWarn.join();
// Restart if that was requested via Rest API call. switch (ret) {
if (ret == 3) { case 0:
case 2:
case 4:
// Valid exit codes, ignored.
break;
case 3:
// Restart if that was requested via Rest API call.
Log.i(TAG, "Restarting syncthing"); Log.i(TAG, "Restarting syncthing");
mContext.startService(new Intent(mContext, SyncthingService.class) mContext.startService(new Intent(mContext, SyncthingService.class)
.setAction(SyncthingService.ACTION_RESTART)); .setAction(SyncthingService.ACTION_RESTART));
} break;
// Force crash if Syncthing exits with an error. case 137:
else if (ret == 1 || ret > 4) { // Ignore SIGKILL that we use to stop Syncthing.
break;
case 1:
// fallthrough
default:
// Force crash if Syncthing exits with an error.
throw new RuntimeException("Syncthing binary crashed with error code " + throw new RuntimeException("Syncthing binary crashed with error code " +
Integer.toString(ret) + ", output:\n" + mErrorLog); Integer.toString(ret) + ", output:\n" + mErrorLog);
} }
} while (ret == 3);
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
Log.e(TAG, "Failed to execute syncthing binary or read output", e); Log.e(TAG, "Failed to execute syncthing binary or read output", e);
} finally { } finally {