mirror of
https://github.com/syncthing/syncthing-android.git
synced 2025-01-11 04:25:53 +00:00
Merge pull request #581 from syncthing/change-exit
Don't use Process#exit() to avoid exit code 9.
This commit is contained in:
commit
567e332651
3 changed files with 13 additions and 21 deletions
|
@ -381,7 +381,8 @@ public class SettingsFragment extends PreferenceFragment
|
||||||
Toast.makeText(getActivity(),
|
Toast.makeText(getActivity(),
|
||||||
getString(R.string.config_imported_successful),
|
getString(R.string.config_imported_successful),
|
||||||
Toast.LENGTH_SHORT).show();
|
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 {
|
} else {
|
||||||
Toast.makeText(getActivity(),
|
Toast.makeText(getActivity(),
|
||||||
getString(R.string.config_import_failed,
|
getString(R.string.config_import_failed,
|
||||||
|
|
|
@ -136,7 +136,7 @@ public class SyncthingRunnable implements Runnable {
|
||||||
lInfo.join();
|
lInfo.join();
|
||||||
lWarn.join();
|
lWarn.join();
|
||||||
|
|
||||||
// Restart if that was requested.
|
// Restart if that was requested via Rest API call.
|
||||||
if (ret == 3) {
|
if (ret == 3) {
|
||||||
Log.i(TAG, "Restarting syncthing");
|
Log.i(TAG, "Restarting syncthing");
|
||||||
mContext.startService(new Intent(mContext, SyncthingService.class)
|
mContext.startService(new Intent(mContext, SyncthingService.class)
|
||||||
|
@ -217,21 +217,9 @@ public class SyncthingRunnable implements Runnable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Look for running libsyncthing.so processes and kill them.
|
* 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() {
|
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++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
Process ps = null;
|
Process ps = null;
|
||||||
DataOutputStream psOut = null;
|
DataOutputStream psOut = null;
|
||||||
|
@ -246,6 +234,7 @@ public class SyncthingRunnable implements Runnable {
|
||||||
BufferedReader br = new BufferedReader(isr);
|
BufferedReader br = new BufferedReader(isr);
|
||||||
String id;
|
String id;
|
||||||
while ((id = br.readLine()) != null) {
|
while ((id = br.readLine()) != null) {
|
||||||
|
id = id.trim().split("\\s+")[1];
|
||||||
killProcessId(id, i > 0);
|
killProcessId(id, i > 0);
|
||||||
}
|
}
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
|
@ -269,18 +258,18 @@ public class SyncthingRunnable implements Runnable {
|
||||||
*
|
*
|
||||||
* @param force Whether to use a SIGKILL.
|
* @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;
|
Process kill = null;
|
||||||
DataOutputStream killOut = null;
|
DataOutputStream killOut = null;
|
||||||
try {
|
try {
|
||||||
kill = Runtime.getRuntime().exec("sh");
|
kill = Runtime.getRuntime().exec((useRoot()) ? "su" : "sh");
|
||||||
killOut = new DataOutputStream(kill.getOutputStream());
|
killOut = new DataOutputStream(kill.getOutputStream());
|
||||||
if (!force) {
|
if (!force) {
|
||||||
killOut.writeBytes("kill " + id + "\n");
|
killOut.writeBytes("kill -SIGINT " + id + "\n");
|
||||||
killOut.writeBytes("sleep 1\n");
|
killOut.writeBytes("sleep 1\n");
|
||||||
} else {
|
} else {
|
||||||
killOut.writeBytes("sleep 3\n");
|
killOut.writeBytes("sleep 3\n");
|
||||||
killOut.writeBytes("kill -9 " + id + "\n");
|
killOut.writeBytes("kill -SIGKILL " + id + "\n");
|
||||||
}
|
}
|
||||||
killOut.writeBytes("exit\n");
|
killOut.writeBytes("exit\n");
|
||||||
killOut.flush();
|
killOut.flush();
|
||||||
|
@ -322,8 +311,6 @@ public class SyncthingRunnable implements Runnable {
|
||||||
mErrorLog += line + "\n";
|
mErrorLog += line + "\n";
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} 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);
|
Log.w(TAG, "Failed to read Syncthing's command line output", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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).
|
* @return True if the import was successful, false otherwise (eg if files aren't found).
|
||||||
*/
|
*/
|
||||||
public boolean importConfig() {
|
public boolean importConfig() {
|
||||||
|
mCurrentState = State.DISABLED;
|
||||||
|
shutdown();
|
||||||
File config = new File(EXPORT_PATH, ConfigXml.CONFIG_FILE);
|
File config = new File(EXPORT_PATH, ConfigXml.CONFIG_FILE);
|
||||||
File privateKey = new File(EXPORT_PATH, PRIVATE_KEY_FILE);
|
File privateKey = new File(EXPORT_PATH, PRIVATE_KEY_FILE);
|
||||||
File publicKey = new File(EXPORT_PATH, PUBLIC_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(config, new File(getFilesDir(), ConfigXml.CONFIG_FILE));
|
||||||
copyFile(privateKey, new File(getFilesDir(), PRIVATE_KEY_FILE));
|
copyFile(privateKey, new File(getFilesDir(), PRIVATE_KEY_FILE));
|
||||||
copyFile(publicKey, new File(getFilesDir(), PUBLIC_KEY_FILE));
|
copyFile(publicKey, new File(getFilesDir(), PUBLIC_KEY_FILE));
|
||||||
|
mCurrentState = State.INIT;
|
||||||
|
updateState();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue