From mboxrd@z Thu Jan 1 00:00:00 1970 From: INAKOSHI Hiroya Subject: [PATCH] SIGTERM and SIGINT handler to flush xentop -b outputs Date: Tue, 02 Oct 2007 20:10:49 +0900 Message-ID: <47022739.6070109@jp.fujitsu.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org Flush stdout when xentop -b gets SIGINT and SIGTERM. It is useful when you stop xentop -b by keyboard interrupt or by other programs such as killall from a batch script. You would have missed the bottom part of xentop outputs without this patch. This second patch calls no unsafe function in a signal handler. The main loop breaks and xentop exits normally when a flag is set by the signal handler. Stdout is flushed accordingly. Signed-off-by: INAKOSHI Hiroya diff -r 83239b289072 tools/xenstat/xentop/xentop.c --- a/tools/xenstat/xentop/xentop.c Thu Sep 27 16:29:43 2007 -0600 +++ b/tools/xenstat/xentop/xentop.c Tue Oct 02 16:53:10 2007 +0900 @@ -28,6 +28,7 @@ #include #include #include +#include #if defined(__linux__) #include #endif @@ -1011,6 +1012,12 @@ static void top(void) free(domains); } +static volatile int sigout_flag = 0; + +void a_sig_handler(int sig) { + sigout_flag = 1; +} + int main(int argc, char **argv) { int opt, optind = 0; @@ -1102,6 +1109,14 @@ int main(int argc, char **argv) ch = getch(); } while (handle_key(ch)); } else { + struct sigaction sa = { + .sa_handler = a_sig_handler, + .sa_flags = 0 + }; + sigemptyset(&sa.sa_mask); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + do { gettimeofday(&curtime, NULL); top(); @@ -1109,7 +1124,7 @@ int main(int argc, char **argv) if ((!loop) && !(--iterations)) break; sleep(delay); - } while (1); + } while (sigout_flag == 0); } /* Cleanup occurs in cleanup(), so no work to do here. */