From 91f9231501872f14eee67cfad0559ccd1f66f2ee Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 11 Mar 2016 23:00:23 +0100 Subject: [PATCH 1/3] Don't use Process#exit() to avoid exit code 9. Process#exit() seems to cause syncthing to return exit code 9 all the time. Using SIGINT instead always gives a clean exit (at least on my Android 6 device). --- .../syncthing/SyncthingRunnable.java | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java index 6a5e65bc..ca7c9cc9 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java @@ -217,21 +217,9 @@ public class SyncthingRunnable implements Runnable { /** * Look for running libsyncthing.so processes and kill them. - * Try a SIGTERM once, then try again (twice) with SIGKILL. + * Try a SIGINT first, then try again with SIGKILL. */ public void killSyncthing() { - final Process p = mSyncthing.get(); - if (p != null) { - mSyncthing.set(null); - p.destroy(); - try { - p.waitFor(); - } catch (InterruptedException e) { - Log.w(TAG_KILL, "Failed to kill Syncthing's process", e); - } - } - - // Ensure kill for (int i = 0; i < 2; i++) { Process ps = null; DataOutputStream psOut = null; @@ -269,18 +257,18 @@ public class SyncthingRunnable implements Runnable { * * @param force Whether to use a SIGKILL. */ - private static void killProcessId(String id, boolean force) { + private void killProcessId(String id, boolean force) { Process kill = null; DataOutputStream killOut = null; try { - kill = Runtime.getRuntime().exec("sh"); + kill = Runtime.getRuntime().exec((useRoot()) ? "su" : "sh"); killOut = new DataOutputStream(kill.getOutputStream()); if (!force) { - killOut.writeBytes("kill " + id + "\n"); + killOut.writeBytes("kill -SIGINT " + id + "\n"); killOut.writeBytes("sleep 1\n"); } else { killOut.writeBytes("sleep 3\n"); - killOut.writeBytes("kill -9 " + id + "\n"); + killOut.writeBytes("kill -SIGKILL " + id + "\n"); } killOut.writeBytes("exit\n"); killOut.flush(); @@ -322,8 +310,6 @@ public class SyncthingRunnable implements Runnable { mErrorLog += line + "\n"; } } catch (IOException e) { - // NOTE: This is sometimes called on shutdown, as - // Process.destroy() closes the stream. Log.w(TAG, "Failed to read Syncthing's command line output", e); } } From fc5827a1df8a3c7d194c127eac435df73c95d104 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 12 Mar 2016 19:20:41 +0100 Subject: [PATCH 2/3] Actually use PID to stop Syncthing. --- .../nutomic/syncthingandroid/syncthing/SyncthingRunnable.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java index ca7c9cc9..d891ef37 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java @@ -234,6 +234,7 @@ public class SyncthingRunnable implements Runnable { BufferedReader br = new BufferedReader(isr); String id; while ((id = br.readLine()) != null) { + id = id.trim().split("\\s+")[1]; killProcessId(id, i > 0); } } catch (IOException | InterruptedException e) { From b9c76ff062c4166549ee54e723e5a384d2b5624e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 12 Mar 2016 19:30:38 +0100 Subject: [PATCH 3/3] Shutdown syncthing before importing a config (fixes #570). --- .../nutomic/syncthingandroid/fragments/SettingsFragment.java | 3 ++- .../nutomic/syncthingandroid/syncthing/SyncthingRunnable.java | 2 +- .../nutomic/syncthingandroid/syncthing/SyncthingService.java | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java index ab3e7945..e71d03bd 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/SettingsFragment.java @@ -381,7 +381,8 @@ public class SettingsFragment extends PreferenceFragment Toast.makeText(getActivity(), getString(R.string.config_imported_successful), Toast.LENGTH_SHORT).show(); - mSyncthingService.getApi().requireRestart(getActivity()); + // No need to restart, as we shutdown to import the config, and + // then have to start Syncthing again. } else { Toast.makeText(getActivity(), getString(R.string.config_import_failed, diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java index d891ef37..eabc2745 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingRunnable.java @@ -136,7 +136,7 @@ public class SyncthingRunnable implements Runnable { lInfo.join(); lWarn.join(); - // Restart if that was requested. + // Restart if that was requested via Rest API call. if (ret == 3) { Log.i(TAG, "Restarting syncthing"); mContext.startService(new Intent(mContext, SyncthingService.class) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index 808d2959..0cfd8a16 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -606,6 +606,8 @@ public class SyncthingService extends Service implements * @return True if the import was successful, false otherwise (eg if files aren't found). */ public boolean importConfig() { + mCurrentState = State.DISABLED; + shutdown(); File config = new File(EXPORT_PATH, ConfigXml.CONFIG_FILE); File privateKey = new File(EXPORT_PATH, PRIVATE_KEY_FILE); File publicKey = new File(EXPORT_PATH, PUBLIC_KEY_FILE); @@ -615,6 +617,8 @@ public class SyncthingService extends Service implements copyFile(config, new File(getFilesDir(), ConfigXml.CONFIG_FILE)); copyFile(privateKey, new File(getFilesDir(), PRIVATE_KEY_FILE)); copyFile(publicKey, new File(getFilesDir(), PUBLIC_KEY_FILE)); + mCurrentState = State.INIT; + updateState(); return true; }