From 5c01b973456d3898b4cfbea06195f5e4796cdc9e Mon Sep 17 00:00:00 2001 From: Greg Alexander Date: Tue, 16 Dec 2014 18:42:37 -0500 Subject: [PATCH] when creating the Service, we check for a pid file for an orphaned daemon process, and if it exists, we kill it. --- .../galexander/sshd/SimpleSSHDService.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/org/galexander/sshd/SimpleSSHDService.java b/src/org/galexander/sshd/SimpleSSHDService.java index e6be8a7..7fadfdb 100644 --- a/src/org/galexander/sshd/SimpleSSHDService.java +++ b/src/org/galexander/sshd/SimpleSSHDService.java @@ -4,6 +4,9 @@ import android.app.Service; import android.content.Intent; import android.content.Context; import android.os.IBinder; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; public class SimpleSSHDService extends Service { public static int sshd_pid = 0; @@ -11,7 +14,15 @@ public class SimpleSSHDService extends Service { public void onCreate() { super.onCreate(); + Prefs.init(this); + + read_pidfile(); + if (is_started()) { + /* would prefer to restart the daemon process rather + * than leave the stale one around.. */ + stop_sshd(); + } } public int onStartCommand(Intent intent, int flags, int startId) { @@ -81,6 +92,22 @@ public class SimpleSSHDService extends Service { } } + private static void read_pidfile() { + try { + File f = new File(Prefs.get_path(), "dropbear.pid"); + if (f.exists()) { + BufferedReader r = new BufferedReader( + new FileReader(f)); + try { + sshd_pid = + Integer.valueOf(r.readLine()); + } finally { + r.close(); + } + } + } catch (Exception e) { /* *shrug* */ } + } + private native void start_sshd(int port, String path, String shell, String home); private native void stop_sshd();