1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-11-30 11:18:12 +00:00

add something to wait for the forked process to die, so it doesn't become

a zombie
This commit is contained in:
Greg Alexander 2014-12-16 17:07:26 -05:00
parent b88f4f1db0
commit 58eefb02b9
2 changed files with 51 additions and 14 deletions

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <jni.h> #include <jni.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -110,3 +111,15 @@ Java_org_galexander_sshd_SimpleSSHDService_stop_1sshd(JNIEnv *env_, jobject this
kill(pid, SIGKILL); kill(pid, SIGKILL);
(*env)->SetStaticIntField(env, cl_simplesshdservice, fid_sss_sshd_pid, 0); (*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;
}

View File

@ -14,25 +14,13 @@ public class SimpleSSHDService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if ((intent == null) || if ((intent == null) ||
(!intent.getBooleanExtra("stop", false))) { (!intent.getBooleanExtra("stop", false))) {
SharedPreferences p = PreferenceManager. do_start();
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();
}
/* XXX - maybe we should call startForeground(), but then we'd have to make a /* XXX - maybe we should call startForeground(), but then we'd have to make a
* bogus notification... */ * bogus notification... */
return START_STICKY; return START_STICKY;
} else { } else {
stop_sshd(); stop_sshd();
if (activity != null) { update_activity();
activity.update_startstop();
}
stopSelf(); stopSelf();
/* XXX - need stopForeground() too ? */ /* XXX - need stopForeground() too ? */
return START_NOT_STICKY; return START_NOT_STICKY;
@ -46,9 +34,45 @@ public class SimpleSSHDService extends Service {
return (sshd_pid != 0); 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, private native void start_sshd(int port, String path,
String shell, String home); String shell, String home);
private native void stop_sshd(); private native void stop_sshd();
private static native int waitpid(int pid);
static { static {
System.loadLibrary("simplesshd-jni"); System.loadLibrary("simplesshd-jni");
} }