From patchwork Mon May 14 18:25:20 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 159108 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0239BB7041 for ; Tue, 15 May 2012 04:25:24 +1000 (EST) Received: from localhost ([::1]:54536 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STzxQ-0005Rn-TJ for incoming@patchwork.ozlabs.org; Mon, 14 May 2012 14:25:20 -0400 Received: from eggs.gnu.org ([208.118.235.92]:46719) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STzxI-0005R8-OW for qemu-devel@nongnu.org; Mon, 14 May 2012 14:25:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1STzxF-0006pR-Tk for qemu-devel@nongnu.org; Mon, 14 May 2012 14:25:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29101) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1STzxF-0006p1-KK for qemu-devel@nongnu.org; Mon, 14 May 2012 14:25:09 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q4EIP4WL016552 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 14 May 2012 14:25:04 -0400 Received: from doriath.home (ovpn-116-82.ams2.redhat.com [10.36.116.82]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q4EIP1t2007487; Mon, 14 May 2012 14:25:02 -0400 Date: Mon, 14 May 2012 15:25:20 -0300 From: Luiz Capitulino To: qemu-devel Message-ID: <20120514152520.69ce699a@doriath.home> Organization: Red Hat Mime-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Eric Blake , mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH v2] qemu-ga: guest-shutdown: use only async-signal-safe functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- o v2 - fix doc typo - drop 'ret' and use EXIT_FAILURE instead qapi-schema-guest.json | 3 +-- qga/commands-posix.c | 19 ++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/qapi-schema-guest.json b/qapi-schema-guest.json index 1c949ff..bd2256d 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 9a59276..15ce928 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; }