From 86eb638a46cd7188cfd2aeb7bf8515174540528d Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 31 Oct 2013 14:56:35 -0700 Subject: [PATCH] android: Monitor child process termination --- android/system-emulator.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/android/system-emulator.c b/android/system-emulator.c index 3e44e74fa..8ca4fe1d5 100644 --- a/android/system-emulator.c +++ b/android/system-emulator.c @@ -29,9 +29,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -63,6 +65,8 @@ static void ctl_start(void) exit(0); } + printf("New process %d created\n", pid); + daemon_pid = pid; } @@ -88,15 +92,49 @@ static void system_socket_callback(int fd, uint32_t events, void *user_data) ctl_start(); } +static void signal_callback(int signum, void *user_data) +{ + switch (signum) { + case SIGINT: + case SIGTERM: + mainloop_quit(); + break; + case SIGCHLD: + while (1) { + pid_t pid; + int status; + + pid = waitpid(WAIT_ANY, &status, WNOHANG); + if (pid < 0 || pid == 0) + break; + + printf("Process %d terminated with status=%d\n", + pid, status); + + if (pid == daemon_pid) + daemon_pid = -1; + } + break; + } +} + int main(int argc, char *argv[]) { static const char SYSTEM_SOCKET_PATH[] = "\0android_system"; + sigset_t mask; struct sockaddr_un addr; int fd; mainloop_init(); + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + sigaddset(&mask, SIGTERM); + sigaddset(&mask, SIGCHLD); + + mainloop_set_signal(&mask, signal_callback, NULL, NULL); + printf("Android system emulator ver %s\n", VERSION); snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0]));