From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:53061) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUJ4R-0001CI-4C for qemu-devel@nongnu.org; Tue, 15 May 2012 10:49:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SUJ4O-00032Y-Qo for qemu-devel@nongnu.org; Tue, 15 May 2012 10:49:50 -0400 Received: from mail-yx0-f173.google.com ([209.85.213.173]:43493) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUJ4O-0002vv-KX for qemu-devel@nongnu.org; Tue, 15 May 2012 10:49:48 -0400 Received: by mail-yx0-f173.google.com with SMTP id m4so6939514yen.4 for ; Tue, 15 May 2012 07:49:47 -0700 (PDT) Sender: fluxion From: Michael Roth Date: Tue, 15 May 2012 09:48:57 -0500 Message-Id: <1337093339-10786-12-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1337093339-10786-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1337093339-10786-1-git-send-email-mdroth@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 11/13] qemu-ga: guest-shutdown: use only async-signal-safe functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: mprivozn@redhat.com, aliguori@us.ibm.com, eblake@redhat.com, jcody@redhat.com, lcapitulino@redhat.com From: Luiz Capitulino POSIX mandates[1] that a child process of a multi-thread program uses only async-signal-safe functions before exec(). We consider qemu-ga to be multi-thread, because it uses glib. However, qmp_guest_shutdown() uses functions that are not async-signal-safe. Fix it the following way: - fclose() -> reopen_fd_to_null() - execl() -> execle() - exit() -> _exit() - drop slog() usage (which is not safe) [1] http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html Signed-off-by: Luiz Capitulino Reviewed-by: Eric Blake Signed-off-by: Michael Roth --- qapi-schema-guest.json | 3 +-- qga/commands-posix.c | 17 +++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/qapi-schema-guest.json b/qapi-schema-guest.json index 7c6cb21..d4055d2 100644 --- a/qapi-schema-guest.json +++ b/qapi-schema-guest.json @@ -126,8 +126,7 @@ # @guest-shutdown: # # Initiate guest-activated shutdown. Note: this is an asynchronous -# shutdown request, with no guaruntee of successful shutdown. Errors -# will be logged to guest's syslog. +# shutdown request, with no guarantee of successful shutdown. # # @mode: #optional "halt", "powerdown" (default), or "reboot" # diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 68da71f..7664be1 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -37,8 +37,8 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) { const char *shutdown_flag; - int ret, status; pid_t rpid, pid; + int status; slog("guest-shutdown called, mode: %s", mode); if (!has_mode || strcmp(mode, "powerdown") == 0) { @@ -57,16 +57,13 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err) if (pid == 0) { /* child, start the shutdown */ setsid(); - fclose(stdin); - fclose(stdout); - fclose(stderr); - - ret = execl("/sbin/shutdown", "shutdown", shutdown_flag, "+0", - "hypervisor initiated shutdown", (char*)NULL); - if (ret) { - slog("guest-shutdown failed: %s", strerror(errno)); - } - exit(!!ret); + reopen_fd_to_null(0); + reopen_fd_to_null(1); + reopen_fd_to_null(2); + + execle("/sbin/shutdown", "shutdown", shutdown_flag, "+0", + "hypervisor initiated shutdown", (char*)NULL, environ); + _exit(EXIT_FAILURE); } else if (pid < 0) { goto exit_err; } -- 1.7.4.1