Skip to contentSkip to conversation

SIGTERM and SIGINT handler to flush xentop -b outputs

PATCH In review INAKOSHI Hiroya 2 participants 3 messages Oct 2, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SIGTERM and SIGINT handler to flush xentop -b outputs

# HG changeset patch
# User [email protected]
# 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.

tools/xenstat/xentop/xentop.c +10 -0
diff -r 83239b289072 -r 5543e74774a8 tools/xenstat/xentop/xentop.c
@@ -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();
#1 lore raw

tools/xenstat/xentop/xentop.c static void top(void) · line 1020

Quoted 5 lines · INAKOSHI Hiroya
tools/xenstat/xentop/xentop.c +5 -0
+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.

tools/xenstat/xentop/xentop.c int main(int argc, char **argv) · line 1114

Quoted 3 lines
tools/xenstat/xentop/xentop.c +2 -0
 	} 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

#2 lore raw

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

Quoted 30 lines · John Levon

tools/xenstat/xentop/xentop.c static void top(void) · line 1020

Quoted 5 lines · INAKOSHI Hiroya
tools/xenstat/xentop/xentop.c +5 -0
+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.

tools/xenstat/xentop/xentop.c int main(int argc, char **argv) · line 1114

Quoted 3 lines
tools/xenstat/xentop/xentop.c +2 -0
 	} 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
[email protected]
http://lists.xensource.com/xen-devel