Skip to contentSkip to conversation

qemu-progress.c: printf isn't signal safe

PATCH Superseded [email protected] 2 participants 2 messages Apr 28, 2011

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-progress.c: printf isn't signal safe

From: Jes Sorensen <[email protected]>

Change the signal handling to indicate a signal is pending, rather then printing directly from the signal handler.

In addition make the signal prints go to stderr, rather than stdout.

---
 qemu-progress.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
qemu-progress.c +6 -1
index e1feb89..27a2310 100644
@@ -32,6 +32,7 @@ struct progress_state {
     float current;
     float last_print;
     float min_skip;
+    int print_pending;
     void (*print)(void);
     void (*end)(void);
 };
@@ -63,12 +64,16 @@ static void progress_simple_init(void)
 #ifdef CONFIG_POSIX
 static void sigusr_print(int signal)
 {
-    printf("    (%3.2f/100%%)\n", state.current);
+    state.print_pending = 1;
 }
 #endif
 
 static void progress_dummy_print(void)
 {
+    if (state.print_pending) {
+        fprintf(stderr, "    (%3.2f/100%%)\n", state.current);
+        state.print_pending = 0;
+    }
 }
 
 static void progress_dummy_end(void)
Signature · 1 line
1.7.4.4
#1 lore raw
Quoted 17 lines · [email protected] writes:

From: Jes Sorensen <[email protected]>

Change the signal handling to indicate a signal is pending, rather then printing directly from the signal handler.

In addition make the signal prints go to stderr, rather than stdout.

---
 qemu-progress.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
qemu-progress.c +0 -0
index e1feb89..27a2310 100644

qemu-progress.c struct progress_state { · line 35

Quoted 4 lines
qemu-progress.c +1 -0
@@ -32,6 +32,7 @@ struct progress_state {
     float current;
     float last_print;
     float min_skip;
+    int print_pending;

To be pedantically correct: volatile sig_atomic_t print_pending

qemu-progress.c struct progress_state { · line 38

Quoted 3 lines
qemu-progress.c +0 -0
     void (*print)(void);
     void (*end)(void);
 };

I don't care for wrapping a file's static global variables in a struct, but I guess that's water under the bridge now.

Quoted 18 lines
qemu-progress.c +5 -1
@@ -63,12 +64,16 @@ static void progress_simple_init(void)
 #ifdef CONFIG_POSIX
 static void sigusr_print(int signal)
 {
-    printf("    (%3.2f/100%%)\n", state.current);
+    state.print_pending = 1;
 }
 #endif
 
 static void progress_dummy_print(void)
 {
+    if (state.print_pending) {
+        fprintf(stderr, "    (%3.2f/100%%)\n", state.current);
+        state.print_pending = 0;
+    }
 }
 
 static void progress_dummy_end(void)