From c7cebe1aeb54b7f2d500b130cde8dd10074c6ac1 Mon Sep 17 00:00:00 2001 From: Johns Date: Tue, 24 Jan 2012 22:20:17 +0100 Subject: [PATCH] Wait for X11 exit and kill it, if not. --- softhddev.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/softhddev.c b/softhddev.c index b998b7d..7a9272d 100644 --- a/softhddev.c +++ b/softhddev.c @@ -1216,8 +1216,34 @@ void SoftHdDeviceExit(void) Debug(3, "x-setup: Stop x11 server\n"); if (X11ServerPid) { + int waittime; + int timeout; + pid_t wpid; + int status; + kill(X11ServerPid, SIGTERM); - // FIXME: wait for x11 finishing + waittime = 0; + timeout = 500; // 0.5s + // wait for x11 finishing, with timeout + do { + wpid = waitpid(X11ServerPid, &status, WNOHANG); + if (wpid) { + break; + } + if (waittime++ < timeout) { + usleep(1 * 1000); + continue; + } + kill(X11ServerPid, SIGKILL); + } while (waittime < timeout); + if (wpid && WIFEXITED(status)) { + Debug(3, "x-setup: x11 server exited (%d)\n", + WEXITSTATUS(status)); + } + if (wpid && WIFSIGNALED(status)) { + Debug(3, "x-setup: x11 server killed (%d)\n", + WTERMSIG(status)); + } } }