From 8fea022982214f5ed91d15fcde483217a264cc89 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Thu, 23 Aug 2018 09:45:54 +0200 Subject: [PATCH] Fix exit code 9 handling in SyncthingRunnable (#25) Display syncthing native exit code on crash --- .../service/NotificationHandler.java | 4 +- .../service/SyncthingRunnable.java | 112 +++++++++++------- .../service/SyncthingService.java | 19 ++- app/src/main/res/values-ca-rES/strings.xml | 2 +- app/src/main/res/values-cs/strings.xml | 2 +- app/src/main/res/values-da/strings.xml | 2 +- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-el/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-fi/strings.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values-hu/strings.xml | 2 +- app/src/main/res/values-it/strings.xml | 2 +- app/src/main/res/values-ja/strings.xml | 2 +- app/src/main/res/values-ko/strings.xml | 2 +- app/src/main/res/values-nl/strings.xml | 2 +- app/src/main/res/values-pl/strings.xml | 2 +- app/src/main/res/values-pt-rBR/strings.xml | 2 +- app/src/main/res/values-ro/strings.xml | 4 +- app/src/main/res/values-ru/strings.xml | 2 +- app/src/main/res/values-sv/strings.xml | 2 +- app/src/main/res/values-uk/strings.xml | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 25 files changed, 110 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java b/app/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java index 7782f778..f939b6cf 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/NotificationHandler.java @@ -168,10 +168,10 @@ public class NotificationHandler { mNotificationManager.cancel(ID_PERSISTENT_WAITING); } - public void showCrashedNotification(@StringRes int title) { + public void showCrashedNotification(@StringRes int title, String extraInfo) { Intent intent = new Intent(mContext, LogActivity.class); Notification n = getNotificationBuilder(mInfoChannel) - .setContentTitle(mContext.getString(title)) + .setContentTitle(mContext.getString(title, extraInfo)) .setContentText(mContext.getString(R.string.notification_crash_text)) .setSmallIcon(R.drawable.ic_stat_notify) .setContentIntent(PendingIntent.getActivity(mContext, 0, intent, 0)) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java index fc1aa1f2..be92933f 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java @@ -109,9 +109,14 @@ public class SyncthingRunnable implements Runnable { @SuppressLint("WakelockTimeout") public String run(boolean returnStdOut) { - trimLogFile(); - int ret; + Boolean sendStopToService = false; + Boolean restartSyncthingNative = false; + int exitCode; String capturedStdOut = ""; + + // Trim Syncthing log. + trimLogFile(); + // Make sure Syncthing is executable try { ProcessBuilder pb = new ProcessBuilder("chmod", "500", mSyncthingBinary.getPath()); @@ -161,31 +166,52 @@ public class SyncthingRunnable implements Runnable { niceSyncthing(); - ret = process.waitFor(); - Log.i(TAG, "Syncthing exited with code " + ret); + exitCode = process.waitFor(); + Log.i(TAG, "Syncthing exited with code " + exitCode); mSyncthing.set(null); - if (lInfo != null) + if (lInfo != null) { lInfo.join(); - if (lWarn != null) + } + if (lWarn != null) { lWarn.join(); + } - switch (ret) { + switch (exitCode) { case 0: case 137: - // Syncthing was shut down (via API or SIGKILL), do nothing. + Log.i(TAG, "Syncthing was shut down normally via API or SIGKILL. Exit code = " + exitCode); break; case 1: - Log.w(TAG, "Another Syncthing instance is already running, requesting restart via SyncthingService intent"); - //fallthrough + Log.w(TAG, "exit reason = exitError. Another Syncthing instance may be already running."); + mNotificationHandler.showCrashedNotification(R.string.notification_crash_title, Integer.toString(exitCode)); + sendStopToService = true; + break; + case 2: + // This should not happen as STNOUPGRADE is set. + Log.w(TAG, "exit reason = exitNoUpgradeAvailable. Another Syncthing instance may be already running."); + mNotificationHandler.showCrashedNotification(R.string.notification_crash_title, Integer.toString(exitCode)); + sendStopToService = true; + break; case 3: // Restart was requested via Rest API call. - Log.i(TAG, "Restarting syncthing"); - mContext.startService(new Intent(mContext, SyncthingService.class) - .setAction(SyncthingService.ACTION_RESTART)); + Log.i(TAG, "exit reason = exitRestarting. Restarting syncthing."); + restartSyncthingNative = true; + break; + case 9: + // Native was force killed. + Log.w(TAG, "exit reason = exitForceKill."); + mNotificationHandler.showCrashedNotification(R.string.notification_crash_title, Integer.toString(exitCode)); + sendStopToService = true; + break; + case 64: + Log.w(TAG, "exit reason = exitInvalidCommandLine."); + mNotificationHandler.showCrashedNotification(R.string.notification_crash_title, Integer.toString(exitCode)); + sendStopToService = true; break; default: - Log.w(TAG, "Syncthing has crashed (exit code " + ret + ")"); - mNotificationHandler.showCrashedNotification(R.string.notification_crash_title); + Log.w(TAG, "Syncthing exited unexpectedly. Exit code = " + exitCode); + mNotificationHandler.showCrashedNotification(R.string.notification_crash_title, Integer.toString(exitCode)); + sendStopToService = true; } } catch (IOException | InterruptedException e) { Log.e(TAG, "Failed to execute syncthing binary or read output", e); @@ -195,6 +221,20 @@ public class SyncthingRunnable implements Runnable { if (process != null) process.destroy(); } + + // Restart syncthing if it exited unexpectedly while running on a separate thread. + if (!returnStdOut && restartSyncthingNative) { + mContext.startService(new Intent(mContext, SyncthingService.class) + .setAction(SyncthingService.ACTION_RESTART)); + } + + // Notify {@link SyncthingService} that service state State.ACTIVE is no longer valid. + if (!returnStdOut && sendStopToService) { + mContext.startService(new Intent(mContext, SyncthingService.class) + .setAction(SyncthingService.ACTION_STOP)); + } + + // Return captured command line output. return capturedStdOut; } @@ -299,37 +339,23 @@ public class SyncthingRunnable implements Runnable { } } - public interface OnSyncthingKilled { - void onKilled(); - } /** - * Look for running libsyncthing.so processes and kill them. - * Try a SIGINT first, then try again with SIGKILL. + * Look for running libsyncthing.so processes and end them gracefully. */ public void killSyncthing() { - for (int i = 0; i < 2; i++) { - List syncthingPIDs = getSyncthingPIDs(); - if (syncthingPIDs.isEmpty()) { - Log.d(TAG, "killSyncthing: Found no more running instances of " + Constants.FILENAME_SYNCTHING_BINARY); - break; - } - - int exitCode; - for (String syncthingPID : syncthingPIDs) { - if (i > 0) { - // Force termination of the process by sending SIGKILL. - SystemClock.sleep(3000); - exitCode = Util.runShellCommand("kill -SIGKILL " + syncthingPID + "\n", mUseRoot); - } else { - exitCode = Util.runShellCommand("kill -SIGINT " + syncthingPID + "\n", mUseRoot); - SystemClock.sleep(1000); - } - if (exitCode == 0) { - Log.d(TAG, "Killed Syncthing process " + syncthingPID); - } else { - Log.w(TAG, "Failed to kill Syncthing process " + syncthingPID + - " exit code " + Integer.toString(exitCode)); - } + int exitCode; + List syncthingPIDs = getSyncthingPIDs(); + if (syncthingPIDs.isEmpty()) { + Log.d(TAG, "killSyncthing: Found no more running instances of " + Constants.FILENAME_SYNCTHING_BINARY); + return; + } + for (String syncthingPID : syncthingPIDs) { + exitCode = Util.runShellCommand("kill -SIGINT " + syncthingPID + "\n", mUseRoot); + if (exitCode == 0) { + Log.d(TAG, "Sent kill SIGINT to process " + syncthingPID); + } else { + Log.w(TAG, "Failed to send kill SIGINT to process " + syncthingPID + + " exit code " + Integer.toString(exitCode)); } } } diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 3cf03707..98e8efd7 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -44,6 +44,12 @@ public class SyncthingService extends Service { public static final String ACTION_RESTART = "com.nutomic.syncthingandroid.service.SyncthingService.RESTART"; + /** + * Intent action to perform a Syncthing stop. + */ + public static final String ACTION_STOP = + "com.nutomic.syncthingandroid.service.SyncthingService.STOP"; + /** * Intent action to reset Syncthing's database. */ @@ -95,6 +101,10 @@ public class SyncthingService extends Service { public static final String EXTRA_FOLDER_ID = "com.nutomic.syncthingandroid.service.SyncthingService.EXTRA_FOLDER_ID"; + public interface OnSyncthingKilled { + void onKilled(); + } + public interface OnServiceStateChangeListener { void onServiceStateChange(State currentState); } @@ -221,11 +231,14 @@ public class SyncthingService extends Service { } mNotificationHandler.updatePersistentNotification(this); - if (intent == null) + if (intent == null) { return START_STICKY; + } if (ACTION_RESTART.equals(intent.getAction()) && mCurrentState == State.ACTIVE) { shutdown(State.INIT, () -> launchStartupTask()); + } else if (ACTION_STOP.equals(intent.getAction()) && mCurrentState == State.ACTIVE) { + shutdown(State.DISABLED, () -> {}); } else if (ACTION_RESET_DATABASE.equals(intent.getAction())) { shutdown(State.INIT, () -> { new SyncthingRunnable(this, SyncthingRunnable.Command.resetdatabase).run(); @@ -337,7 +350,7 @@ public class SyncthingService extends Service { syncthingService.mConfig = new ConfigXml(syncthingService); syncthingService.mConfig.updateIfNeeded(); } catch (ConfigXml.OpenConfigException e) { - syncthingService.mNotificationHandler.showCrashedNotification(R.string.config_read_failed); + syncthingService.mNotificationHandler.showCrashedNotification(R.string.config_read_failed, "ConfigXml.OpenConfigException"); synchronized (syncthingService.mStateLock) { syncthingService.onServiceStateChange(State.ERROR); } @@ -473,7 +486,7 @@ public class SyncthingService extends Service { * * Sets {@link #mCurrentState} to newState, and calls onKilledListener once Syncthing is killed. */ - private void shutdown(State newState, SyncthingRunnable.OnSyncthingKilled onKilledListener) { + private void shutdown(State newState, OnSyncthingKilled onKilledListener) { Log.i(TAG, "Shutting down background service"); synchronized(mStateLock) { onServiceStateChange(newState); diff --git a/app/src/main/res/values-ca-rES/strings.xml b/app/src/main/res/values-ca-rES/strings.xml index 3aa36a6c..8bd67645 100644 --- a/app/src/main/res/values-ca-rES/strings.xml +++ b/app/src/main/res/values-ca-rES/strings.xml @@ -349,7 +349,7 @@ Ens podeu informar dels problemes que trobeu a través de Github. Càmera %1$s-fotos - El Syncthing s\'ha bloquejat + El Syncthing s\'ha bloquejat %1$s Feu clic per veure els registres El Syncthing està actiu Vigilant les condicions d\'execució diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 9822bc7b..11030df4 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -327,7 +327,7 @@ Všechny zaznamenané chyby prosím hlašte přes Github. Kamera %1$s-foto - Syncthing spadl + Syncthing spadl %1$s Pro prohlížení logů klikněte Ostatní upozornění diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index 303a4607..093facb9 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -314,7 +314,7 @@ Vær venlig at rapportere ethvert problem, du støder på, via Github. Kamera %1$s-fotos - Syncthing crashede + Syncthing crashede %1$s Klik for at se logs diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 94fe2678..398aa24d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -366,7 +366,7 @@ Bitte melden Sie auftretende Probleme via GitHub. Kamera %1$s-Bilder - Syncthing ist abgestürzt + Syncthing ist abgestürzt (Exit-Code %1$s) Klicken um Logs anzuzeigen Syncthing läuft Laufkonditionen werden überwacht diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 99b30bd1..83f963a8 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -318,7 +318,7 @@ Φωτογραφική μηχανή %1$s-photos - Το Syncthing κατέρρευσε + Το Syncthing κατέρρευσε %1$s Πατήστε για να δείτε την καταγραφή συμβάντων Άλλες ειδοποιήσεις diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 92ee7396..727e42ba 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -292,7 +292,7 @@ Cámara %1$s-photos - Syncthing se ha estrellado + Syncthing se ha estrellado %1$s Haga clic para ver los registros diff --git a/app/src/main/res/values-fi/strings.xml b/app/src/main/res/values-fi/strings.xml index ffd8fc04..af4472bf 100644 --- a/app/src/main/res/values-fi/strings.xml +++ b/app/src/main/res/values-fi/strings.xml @@ -288,7 +288,7 @@ Ilmoitathan ystävällisesti kaikista havaitsemistasi ongelmista Githubin kautta Kamera %1$s-kuvat - Syncthing kaatui + Syncthing kaatui %1$s Uudelleenkäynnistys tarvitaan diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 495e5e35..cda78bc8 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -350,7 +350,7 @@ S\'il vous plaît, soumettez les problèmes que vous rencontrez via Github.Caméra %1$s-photos - Syncthing s\'est planté + Syncthing s\'est planté %1$s Cliquer pour voir les journaux Syncthing en marche Surveillance des conditions de fonctionnement diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index ced4151b..86ce07b6 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -352,7 +352,7 @@ VIGYÁZAT! Más alkalmazások kiolvashatják a backupból a titkos kulcsot, és Kamera %1$s-kepek - A Syncthing összeomlott + A Syncthing összeomlott %1$s Koppints a naplók megtekintéséhez Syncthing aktív Futási feltételek monitorozása diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 2a6267bd..6f36177e 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -350,7 +350,7 @@ Si prega di segnalare eventuali problemi che si incontrano via Github. Camera %1$s-photos - Syncthing è andato in crash + Syncthing è andato in crash %1$s Clicca per visualizzare i log Syncthing attivo Monitoraggio delle condizioni di esecuzione diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 2bd467e7..c106c246 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -315,7 +315,7 @@ カメラ %1$s-写真 - Syncthing がクラッシュしました + Syncthing がクラッシュしました %1$s クリックしてログを表示 他の通知 diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 157aab0e..de522e46 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -314,7 +314,7 @@ 카메라 %1$s-사진 - Syncthing이 충돌하였습니다. + Syncthing이 충돌하였습니다. %1$s 클릭해서 로그 보기 기타 알림 diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 377c44b6..ff14cc29 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -350,7 +350,7 @@ Als je problemen tegenkomt, meld ze dan via GitHub. Camera %1$s-foto\'s - Syncthing is gecrasht + Syncthing is gecrasht %1$s Tik om logboeken te bekijken Syncthing is actief Uitvoervoorwaarden controleren diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index d1943494..9c99fa62 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -322,7 +322,7 @@ Proszę zgłaszać napotkane błędy programu za pośrednictwem serwisu Github.< Kamera %1$s-zdjęcia - Syncthing spadło z rowerka :( + Syncthing spadło z rowerka %1$s Dotknij, by zobaczyć logi Inne powiadomienia diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index d3740b2d..56ba541c 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -340,7 +340,7 @@ Por favor, nos avise sobre quaisquer problemas que você encontrar via Github.Câmera %1$s-fotos - O Syncthing fechou inesperadamente + O Syncthing fechou inesperadamente %1$s Clique para ver os logs Syncthing ativado Monitorando condições de execução diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index 3f3d3eef..251035bc 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -373,7 +373,7 @@ Vă rugăm să raportați orice problemă întâlniți, prin intermediul GitHub. Camera %1$s-fotografii - Syncthing s-a oprit neașteptat + Syncthing s-a oprit neașteptat %1$s Atingeți pentru a vedea jurnalele de erori Syncthing este activ Monitorizare condiții rulare @@ -445,7 +445,7 @@ Vă rugăm să raportați orice problemă întâlniți, prin intermediul GitHub. Numărul de versiuni vechi ale fișierelor ce vor fi păstrate, pentru fiecare fișier. Fișierele sunt mutate și redenumite după dată în directorul .stversions atunci când sunt înlocuite sau șterse de către Syncthing. Fișierele sunt mutate în directorul .stversions atunci când sunt înlocuite sau șterse de către Syncthing. - Fișierele sunt mutate și redenumite după dată în directorul .stversions + Fișierele sunt mutate și redenumite după dată în directorul .stversions atunci când sunt înlocuite sau șterse de către Syncthing. Versiunile sunt șterse automat dacă sunt mai vechi decât vârsta maximă sau dacă au depășit numărul de fișiere permise într-un interval.\n\nUrmătoarele intervale sunt folosite: în prima oră o versiune este păstrată pentru fiecare 30 de secunde, în prima zi o versiune este păstrată pentru fiecare oră, în primele 30 de zile o versiune este păstrată pentru fiecare zi, până la vârsta maximă o versiune este păstrată pentru fiecare săptămână. Intervalul de timp maxim de păstrat o versiune (în zile, setați la 0 pentru a păstra versiunile pentru totdeauna) Calea unde versiunile vor fi stocate (lăsați gol pentru a folosi directorul implicit .stversions din directorul partajat) diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 8f5ae5ca..66254759 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -355,7 +355,7 @@ Камера %1$s фото - Syncthing завершился из-за сбоя + Syncthing завершился из-за сбоя %1$s Нажмите для просмотра логов Syncthing работает Отслеживание условий работы diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index 34d67d5c..02305f9a 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -368,7 +368,7 @@ Vänligen rapportera eventuella problem du stöter på via Github. Kamera %1$s foton - Syncthing har kraschat + Syncthing har kraschat %1$s Klicka för att visa loggar Syncthing är aktiv Övervakar körvillkor diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index b678f335..13fdb69b 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -243,7 +243,7 @@ Камера - Роботу Syncthing аварійно завершено  + Роботу Syncthing аварійно завершено %1$s Необхідний перезапуск diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 2c0cca7d..a47c305f 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -317,7 +317,7 @@ 摄像机 %1$s-照片 - Syncthing 已崩溃 + Syncthing 已崩溃 %1$s 点击查看日志 其他通知 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 689c1e9e..2c0322b9 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -314,7 +314,7 @@ 相機 %1$s-photos - Syncthing 已經當機 + Syncthing 已經當機 %1$s 觸碰檢視日誌 其他通知 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7e806593..ae6019db 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -663,7 +663,7 @@ Please report any problems you encounter via Github. %1$s-photos - Syncthing has crashed + Syncthing has crashed (exit code %1$s) Click to view logs