From patchwork Mon Mar 7 20:10:38 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 85812 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BB38EB6F0D for ; Tue, 8 Mar 2011 07:48:45 +1100 (EST) Received: from localhost ([127.0.0.1]:55994 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pwh2x-0000cu-Ey for incoming@patchwork.ozlabs.org; Mon, 07 Mar 2011 15:28:51 -0500 Received: from [140.186.70.92] (port=41527 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pwgm3-0001iE-Fj for qemu-devel@nongnu.org; Mon, 07 Mar 2011 15:11:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Pwgm1-0006cc-Ne for qemu-devel@nongnu.org; Mon, 07 Mar 2011 15:11:22 -0500 Received: from e6.ny.us.ibm.com ([32.97.182.146]:53017) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Pwgm1-0006cY-Kv for qemu-devel@nongnu.org; Mon, 07 Mar 2011 15:11:21 -0500 Received: from d01dlp02.pok.ibm.com (d01dlp02.pok.ibm.com [9.56.224.85]) by e6.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p27Jl4x9030766 for ; Mon, 7 Mar 2011 14:47:04 -0500 Received: from d01relay07.pok.ibm.com (d01relay07.pok.ibm.com [9.56.227.147]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id 363196E8044 for ; Mon, 7 Mar 2011 15:11:21 -0500 (EST) Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay07.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p27KBFln1208346 for ; Mon, 7 Mar 2011 15:11:15 -0500 Received: from d01av01.pok.ibm.com (loopback [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p27KBFM9012843 for ; Mon, 7 Mar 2011 15:11:15 -0500 Received: from localhost.localdomain (sig-9-76-30-5.mts.ibm.com [9.76.30.5]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p27KAi8M010005; Mon, 7 Mar 2011 15:11:14 -0500 From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 7 Mar 2011 14:10:38 -0600 Message-Id: <1299528642-23631-13-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1299528642-23631-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1299528642-23631-1-git-send-email-mdroth@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.182.146 Cc: agl@linux.vnet.ibm.com, stefanha@linux.vnet.ibm.com, Jes.Sorensen@redhat.com, mdroth@linux.vnet.ibm.com, markus_mueller@de.ibm.com, aliguori@linux.vnet.ibm.com, abeekhof@redhat.com Subject: [Qemu-devel] [RFC][PATCH v7 12/16] virtagent: add "shutdown" RPC to server X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Michael Roth --- virtagent-server.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 58 insertions(+), 0 deletions(-) diff --git a/virtagent-server.c b/virtagent-server.c index b0fc0c4..3c8c805 100644 --- a/virtagent-server.c +++ b/virtagent-server.c @@ -113,11 +113,69 @@ static QDict *va_ping(const QDict *params) return va_server_format_response(NULL, 0, NULL); } +/* va_shutdown(): initiate guest shutdown + * params/response qdict format: + * params{shutdown_mode}: "reboot"|"powerdown"|"shutdown" + * response{error}: + * response{errstr}: + */ +static QDict *va_shutdown(const QDict *params) +{ + int ret; + const char *shutdown_mode, *shutdown_flag; + + shutdown_mode = qdict_get_try_str(params, "shutdown_mode"); + SLOG("va_shutdown(), shutdown_mode:%s", shutdown_mode); + + if (!shutdown_mode) { + ret = -EINVAL; + LOG("missing shutdown argument"); + goto out; + } else if (strcmp(shutdown_mode, "halt") == 0) { + shutdown_flag = "-H"; + } else if (strcmp(shutdown_mode, "powerdown") == 0) { + shutdown_flag = "-P"; + } else if (strcmp(shutdown_mode, "reboot") == 0) { + shutdown_flag = "-r"; + } else { + ret = -EINVAL; + LOG("invalid shutdown argument"); + goto out; + } + + ret = fork(); + if (ret == 0) { + /* child, start the shutdown */ + setsid(); + fclose(stdin); + fclose(stdout); + fclose(stderr); + + sleep(5); + ret = execl("/sbin/shutdown", "shutdown", shutdown_flag, "+0", + "hypervisor initiated shutdown", (char*)NULL); + if (ret < 0) { + LOG("execl() failed: %s", strerror(errno)); + exit(1); + } + exit(0); + } else if (ret < 0) { + LOG("fork() failed: %s", strerror(errno)); + } else { + ret = 0; + } + +out: + return va_server_format_response(NULL, ret, strerror(errno)); +} + static VARPCFunction guest_functions[] = { { .func = va_capabilities, .func_name = "capabilities" }, { .func = va_ping, .func_name = "ping" }, + { .func = va_shutdown, + .func_name = "shutdown" }, { NULL, NULL } };