From 58eefb02b9b9d6dd9168add32d46c1cf386dc91c Mon Sep 17 00:00:00 2001 From: Greg Alexander Date: Tue, 16 Dec 2014 17:07:26 -0500 Subject: [PATCH] add something to wait for the forked process to die, so it doesn't become a zombie --- jni/interface.c | 13 +++++ .../galexander/sshd/SimpleSSHDService.java | 52 ++++++++++++++----- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/jni/interface.c b/jni/interface.c index 8224948..4800859 100644 --- a/jni/interface.c +++ b/jni/interface.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -110,3 +111,15 @@ Java_org_galexander_sshd_SimpleSSHDService_stop_1sshd(JNIEnv *env_, jobject this kill(pid, SIGKILL); (*env)->SetStaticIntField(env, cl_simplesshdservice, fid_sss_sshd_pid, 0); } + +JNIEXPORT int JNICALL +Java_org_galexander_sshd_SimpleSSHDService_waitpid(JNIEnv *env_, jclass cl, + jint pid) +{ + int status; + waitpid(pid, &status, 0); + if (WIFEXITED(status)) { + return WEXITSTATUS(status); + } + return 0; +} diff --git a/src/org/galexander/sshd/SimpleSSHDService.java b/src/org/galexander/sshd/SimpleSSHDService.java index 3626427..9aacf10 100644 --- a/src/org/galexander/sshd/SimpleSSHDService.java +++ b/src/org/galexander/sshd/SimpleSSHDService.java @@ -14,25 +14,13 @@ public class SimpleSSHDService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { if ((intent == null) || (!intent.getBooleanExtra("stop", false))) { - SharedPreferences p = PreferenceManager. - getDefaultSharedPreferences(this); - if (is_started()) { - stop_sshd(); - } - start_sshd(SimpleSSHD.get_port(p), - SimpleSSHD.get_path(p), SimpleSSHD.get_shell(p), - SimpleSSHD.get_home(p)); - if (activity != null) { - activity.update_startstop(); - } + do_start(); /* XXX - maybe we should call startForeground(), but then we'd have to make a * bogus notification... */ return START_STICKY; } else { stop_sshd(); - if (activity != null) { - activity.update_startstop(); - } + update_activity(); stopSelf(); /* XXX - need stopForeground() too ? */ return START_NOT_STICKY; @@ -46,9 +34,45 @@ public class SimpleSSHDService extends Service { return (sshd_pid != 0); } + private void do_start() { + SharedPreferences p = PreferenceManager. + getDefaultSharedPreferences(this); + if (is_started()) { + stop_sshd(); + } + start_sshd(SimpleSSHD.get_port(p), + SimpleSSHD.get_path(p), SimpleSSHD.get_shell(p), + SimpleSSHD.get_home(p)); + + if (sshd_pid != 0) { + final int pid = sshd_pid; + (new Thread() { + public void run() { + waitpid(pid); + if (sshd_pid == pid) { + sshd_pid = 0; + } + update_activity(); + } + }).start(); + } + update_activity(); + } + + private static void update_activity() { + if (activity != null) { + activity.runOnUiThread(new Runnable() { + public void run() { + activity.update_startstop(); + } + }); + } + } + private native void start_sshd(int port, String path, String shell, String home); private native void stop_sshd(); + private static native int waitpid(int pid); static { System.loadLibrary("simplesshd-jni"); }