All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] SIGTERM and SIGINT handler to flush xentop -b outputs
@ 2007-10-02  1:22 INAKOSHI Hiroya
  2007-10-02  1:29 ` John Levon
  0 siblings, 1 reply; 4+ messages in thread
From: INAKOSHI Hiroya @ 2007-10-02  1:22 UTC (permalink / raw)
  To: xen-devel

# HG changeset patch
# User inakoshi.hiroya@jp.fujitsu.com
# Date 1191287395 -28800
# Node ID 5543e74774a826b1781893982ed5052312b820fc
# Parent  83239b2890723e0c06bad507bb273a970784b18e
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.

Signed-off-by: INAKOSHI Hiroya <inakoshi.hiroya@jp.fujitsu.com>

diff -r 83239b289072 -r 5543e74774a8 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 09:09:55 2007 +0800
@@ -31,6 +31,7 @@
 #if defined(__linux__)
 #include <linux/kdev_t.h>
 #endif
+#include <signal.h>

 #include <xenstat.h>

@@ -1011,6 +1012,13 @@ static void top(void)
 	free(domains);
 }

+
+void a_sig_handler(int n)
+{
+	fflush(stdout);
+	exit(0);
+}
+
 int main(int argc, char **argv)
 {
 	int opt, optind = 0;
@@ -1102,6 +1110,8 @@ int main(int argc, char **argv)
 			ch = getch();
 		} while (handle_key(ch));
 	} else {
+			signal(SIGTERM, a_sig_handler);
+			signal(SIGINT, a_sig_handler);
 			do {
 				gettimeofday(&curtime, NULL);
 				top();

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] SIGTERM and SIGINT handler to flush xentop -b outputs
  2007-10-02  1:22 [PATCH] SIGTERM and SIGINT handler to flush xentop -b outputs INAKOSHI Hiroya
@ 2007-10-02  1:29 ` John Levon
  2007-10-02 10:57   ` INAKOSHI Hiroya
  0 siblings, 1 reply; 4+ messages in thread
From: John Levon @ 2007-10-02  1:29 UTC (permalink / raw)
  To: INAKOSHI Hiroya; +Cc: xen-devel

On Tue, Oct 02, 2007 at 10:22:16AM +0900, INAKOSHI Hiroya wrote:

> +void a_sig_handler(int n)
> +{
> +	fflush(stdout);
> +	exit(0);
> +}

Neither exit() nor fflush() are guaranteed signal safe:

http://opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03

so can't be used in signal handlers.

>  	} else {
> +			signal(SIGTERM, a_sig_handler);
> +			signal(SIGINT, a_sig_handler);

There's never a reason to use signal() these days, even if you made the
handler use _exit()

regards
john

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] SIGTERM and SIGINT handler to flush xentop -b outputs
  2007-10-02  1:29 ` John Levon
@ 2007-10-02 10:57   ` INAKOSHI Hiroya
  0 siblings, 0 replies; 4+ messages in thread
From: INAKOSHI Hiroya @ 2007-10-02 10:57 UTC (permalink / raw)
  To: John Levon; +Cc: xen-devel

John,

thanks for your comment.  I see your point.  I will post another patch 
in a separate e-mail.  It uses a signal handler in a safe manner.

Regards,
Hiroya

John Levon wrote:
> On Tue, Oct 02, 2007 at 10:22:16AM +0900, INAKOSHI Hiroya wrote:
> 
>> +void a_sig_handler(int n)
>> +{
>> +	fflush(stdout);
>> +	exit(0);
>> +}
> 
> Neither exit() nor fflush() are guaranteed signal safe:
> 
> http://opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03
> 
> so can't be used in signal handlers.
> 
>>  	} else {
>> +			signal(SIGTERM, a_sig_handler);
>> +			signal(SIGINT, a_sig_handler);
> 
> There's never a reason to use signal() these days, even if you made the
> handler use _exit()
> 
> regards
> john
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xensource.com
> http://lists.xensource.com/xen-devel
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] SIGTERM and SIGINT handler to flush xentop -b outputs
@ 2007-10-02 11:10 INAKOSHI Hiroya
  0 siblings, 0 replies; 4+ messages in thread
From: INAKOSHI Hiroya @ 2007-10-02 11:10 UTC (permalink / raw)
  To: xen-devel

 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 <inakoshi.hiroya@jp.fujitsu.com>


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 <sys/time.h>
 #include <time.h>
 #include <unistd.h>
+#include <signal.h>
 #if defined(__linux__)
 #include <linux/kdev_t.h>
 #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. */

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-10-02 11:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-02  1:22 [PATCH] SIGTERM and SIGINT handler to flush xentop -b outputs INAKOSHI Hiroya
2007-10-02  1:29 ` John Levon
2007-10-02 10:57   ` INAKOSHI Hiroya
  -- strict thread matches above, loose matches on Subject: below --
2007-10-02 11:10 INAKOSHI Hiroya

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.