qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* Re: [Qemu-devel] [PATCH RFC] main loop: fix some accesses made in sighandler context
@ 2011-09-16 14:31 Eric Blake
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Blake @ 2011-09-16 14:31 UTC (permalink / raw)
  To: lersek, QEMU Developers

> +++ b/cpus.c
> @@ -289,9 +289,16 @@ static void qemu_event_increment(void)
>
>      /* EAGAIN is fine, a read must be pending.  */
>      if (ret < 0 && errno != EAGAIN) {
> -        fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
> -                strerror(errno));
> -        exit (1);
> +        int len;
> +        char buf[128];
> +
> +        /* Don't bother with strerror_[rl]. Make a single attempt to write. */
> +        len = snprintf(buf, sizeof buf,
> +                       "qemu_event_increment: write() failed: %d\n", errno);

snprintf is not async-signal safe (it can malloc, and depending on the 
implementation, tries to grab stdio locks, neither of which is safe).

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

^ permalink raw reply	[flat|nested] 7+ messages in thread
* [Qemu-devel] [PATCH RFC] main loop: fix some accesses made in sighandler context
@ 2011-09-15 17:22 Laszlo Ersek
  2011-09-15 19:16 ` Anthony Liguori
  2011-09-15 19:44 ` Peter Maydell
  0 siblings, 2 replies; 7+ messages in thread
From: Laszlo Ersek @ 2011-09-15 17:22 UTC (permalink / raw)
  To: qemu-devel, lersek

Make variables volatile ("sig_atomic_t" should cover "int" and "pid_t").

Also replace calls to functions that are not required to be async-signal-safe
[1]. (I haven't checked if any signal masks and/or previous suspension of the
interrupted thread keep the current calls safe.)

termsig_handler()
  -> qemu_system_killed(): shutdown_signal, shutdown_pid, no_shutdown [2]
    -> qemu_system_shutdown_request(): shutdown_requested
      -> qemu_notify_event()
        -> qemu_event_increment(): fprintf(), strerror(), exit()

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03_03
[2] http://lists.nongnu.org/archive/html/qemu-devel/2011-09/msg01757.html

"checkpatch.pl" warned four times about "volatile", and considered the
zero-initialization of "no_shutdown" (which has static storage duration) an
error.

Build tested only. Please CC me on any followup, I'm not subscribed. Thank you.

Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 cpus.c   |   13 ++++++++++---
 sysemu.h |    2 +-
 vl.c     |    6 +++---
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/cpus.c b/cpus.c
index 54c188c..ed51247 100644
--- a/cpus.c
+++ b/cpus.c
@@ -289,9 +289,16 @@ static void qemu_event_increment(void)
 
     /* EAGAIN is fine, a read must be pending.  */
     if (ret < 0 && errno != EAGAIN) {
-        fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
-                strerror(errno));
-        exit (1);
+        int len;
+        char buf[128];
+
+        /* Don't bother with strerror_[rl]. Make a single attempt to write. */
+        len = snprintf(buf, sizeof buf,
+                       "qemu_event_increment: write() failed: %d\n", errno);
+        if ((size_t)len < sizeof buf) {
+            ret = write(STDERR_FILENO, buf, len); /* shut up gcc */
+        }
+        _exit(1);
     }
 }
 
diff --git a/sysemu.h b/sysemu.h
index 9090457..52a71ef 100644
--- a/sysemu.h
+++ b/sysemu.h
@@ -119,7 +119,7 @@ extern int max_cpus;
 extern int cursor_hide;
 extern int graphic_rotate;
 extern int no_quit;
-extern int no_shutdown;
+extern volatile int no_shutdown;
 extern int semihosting_enabled;
 extern int old_param;
 extern int boot_menu;
diff --git a/vl.c b/vl.c
index b773d2f..21bc6b4 100644
--- a/vl.c
+++ b/vl.c
@@ -215,7 +215,7 @@ int acpi_enabled = 1;
 int no_hpet = 0;
 int fd_bootchk = 1;
 int no_reboot = 0;
-int no_shutdown = 0;
+volatile int no_shutdown = 0;
 int cursor_hide = 1;
 int graphic_rotate = 0;
 uint8_t irq0override = 1;
@@ -1178,8 +1178,8 @@ typedef struct QEMUResetEntry {
 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
     QTAILQ_HEAD_INITIALIZER(reset_handlers);
 static int reset_requested;
-static int shutdown_requested, shutdown_signal = -1;
-static pid_t shutdown_pid;
+static volatile int shutdown_requested, shutdown_signal = -1;
+static volatile pid_t shutdown_pid;
 static int powerdown_requested;
 static int debug_requested;
 static int vmstop_requested;
-- 
1.7.4.4

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

end of thread, other threads:[~2011-09-16 14:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-16 14:31 [Qemu-devel] [PATCH RFC] main loop: fix some accesses made in sighandler context Eric Blake
  -- strict thread matches above, loose matches on Subject: below --
2011-09-15 17:22 Laszlo Ersek
2011-09-15 19:16 ` Anthony Liguori
2011-09-16  8:08   ` Laszlo Ersek
2011-09-15 19:44 ` Peter Maydell
2011-09-16  7:58   ` Laszlo Ersek
2011-09-16  9:09     ` Markus Armbruster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).