From nobody Fri May 3 04:52:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538653931503143.17373330125247; Thu, 4 Oct 2018 04:52:11 -0700 (PDT) Received: from localhost ([::1]:55216 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g82Ak-0002CT-6f for importer@patchew.org; Thu, 04 Oct 2018 07:52:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38599) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g822Y-0004WY-02 for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g822T-0005wN-DV for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:33 -0400 Received: from proxmox-new.maurer-it.com ([212.186.127.180]:55945) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g822T-0005SM-21 for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:29 -0400 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 0FFC64428D; Thu, 4 Oct 2018 13:43:13 +0200 (CEST) From: Dominik Csapak To: qemu-devel@nongnu.org Date: Thu, 4 Oct 2018 13:43:10 +0200 Message-Id: <20181004114312.27346-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181004114312.27346-1-d.csapak@proxmox.com> References: <20181004114312.27346-1-d.csapak@proxmox.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.186.127.180 Subject: [Qemu-devel] [PATCH v2 1/3] osdep: add qemu_launch_script for executing scripts X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, jasowang@redhat.com, philmd@redhat.com, thuth@redhat.com, sw@weilnetz.de Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" this adds a small function for launching an external script via fork/exec (not available for windows) and is intended for replacing 'launch_script' in net/tap.c and to provide a general way to launch scripts Signed-off-by: Dominik Csapak --- i modeled the windows error after the qemu_fork implementation, since it does not exist for the same reason changes since v1: * new in v2 include/qemu/osdep.h | 12 ++++++++++++ util/oslib-posix.c | 34 ++++++++++++++++++++++++++++++++++ util/oslib-win32.c | 8 ++++++++ 3 files changed, 54 insertions(+) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 4f8559e550..26c1f01d55 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -564,6 +564,18 @@ char *qemu_get_pid_name(pid_t pid); */ pid_t qemu_fork(Error **errp); =20 +/** + * qemu_launch_script: + * @script: the path to the script + * @args: the arguments for the script + * @fd: a file descriptor that should not be closed + * + * fork and exec a script with the given arguments + * closes all file descriptors > 2 except the one given with fd + */ +void qemu_launch_script(const char *script, char *const args[], int fd, + Error **errp); + /* Using intptr_t ensures that qemu_*_page_mask is sign-extended even * when intptr_t is 32-bit and we are aligning a long long. */ diff --git a/util/oslib-posix.c b/util/oslib-posix.c index fbd0dc8c57..123b142591 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -698,3 +698,37 @@ void sigaction_invoke(struct sigaction *action, } action->sa_sigaction(info->ssi_signo, &si, NULL); } + +void qemu_launch_script(const char *script, char *const args[], int fd, + Error **errp) +{ + int pid, status; + + pid =3D fork(); + if (pid < 0) { + error_setg_errno(errp, errno, "could not launch script %s", + script); + return; + } + if (pid =3D=3D 0) { + int open_max =3D sysconf(_SC_OPEN_MAX), i; + + for (i =3D 3; i < open_max; i++) { + if (i !=3D fd) { + close(i); + } + } + execv(script, args); + _exit(1); + } else { + while (waitpid(pid, &status, 0) !=3D pid) { + /* loop */ + } + + if (WIFEXITED(status) && WEXITSTATUS(status) =3D=3D 0) { + return; + } + error_setg(errp, "script %s failed with status %d", + script, status); + } +} diff --git a/util/oslib-win32.c b/util/oslib-win32.c index b4c17f5dfa..5e14c89dc7 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -803,3 +803,11 @@ bool qemu_write_pidfile(const char *filename, Error **= errp) } return true; } + +void qemu_launch_script(const char *script, char *const args[], int fd, + Error **errp) +{ + errno =3D ENOSYS; + error_setg_errno(errp, errno, + "cannot fork child process"); +} --=20 2.11.0 From nobody Fri May 3 04:52:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538653998285894.9936737730139; Thu, 4 Oct 2018 04:53:18 -0700 (PDT) Received: from localhost ([::1]:55222 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g82Bx-0002sx-76 for importer@patchew.org; Thu, 04 Oct 2018 07:53:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38596) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g822X-0004WV-V5 for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g822T-0005wk-HZ for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:33 -0400 Received: from proxmox-new.maurer-it.com ([212.186.127.180]:18317) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g822T-0005U9-5h for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:29 -0400 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 42DC84429A; Thu, 4 Oct 2018 13:43:16 +0200 (CEST) From: Dominik Csapak To: qemu-devel@nongnu.org Date: Thu, 4 Oct 2018 13:43:11 +0200 Message-Id: <20181004114312.27346-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181004114312.27346-1-d.csapak@proxmox.com> References: <20181004114312.27346-1-d.csapak@proxmox.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.186.127.180 Subject: [Qemu-devel] [PATCH v2 2/3] tap: use qemu_launch_script instead of launch_script X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, jasowang@redhat.com, philmd@redhat.com, thuth@redhat.com, sw@weilnetz.de Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Dominik Csapak --- changes since v1: * new in v2 net/tap.c | 56 ++++++++++---------------------------------------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/net/tap.c b/net/tap.c index cc8525f154..15a0dd65b8 100644 --- a/net/tap.c +++ b/net/tap.c @@ -62,9 +62,6 @@ typedef struct TAPState { Notifier exit; } TAPState; =20 -static void launch_script(const char *setup_script, const char *ifname, - int fd, Error **errp); - static void tap_send(void *opaque); static void tap_writable(void *opaque); =20 @@ -300,7 +297,11 @@ static void tap_exit_notify(Notifier *notifier, void *= data) Error *err =3D NULL; =20 if (s->down_script[0]) { - launch_script(s->down_script, s->down_script_arg, s->fd, &err); + char *args[3]; + args[0] =3D (char *)(s->down_script); + args[1] =3D (char *)(s->down_script_arg); + args[2] =3D NULL; + qemu_launch_script(s->down_script, args, s->fd, &err); if (err) { error_report_err(err); } @@ -397,47 +398,6 @@ static TAPState *net_tap_fd_init(NetClientState *peer, return s; } =20 -static void launch_script(const char *setup_script, const char *ifname, - int fd, Error **errp) -{ - int pid, status; - char *args[3]; - char **parg; - - /* try to launch network script */ - pid =3D fork(); - if (pid < 0) { - error_setg_errno(errp, errno, "could not launch network script %s", - setup_script); - return; - } - if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; - - for (i =3D 3; i < open_max; i++) { - if (i !=3D fd) { - close(i); - } - } - parg =3D args; - *parg++ =3D (char *)setup_script; - *parg++ =3D (char *)ifname; - *parg =3D NULL; - execv(setup_script, args); - _exit(1); - } else { - while (waitpid(pid, &status, 0) !=3D pid) { - /* loop */ - } - - if (WIFEXITED(status) && WEXITSTATUS(status) =3D=3D 0) { - return; - } - error_setg(errp, "network script %s failed with status %d", - setup_script, status); - } -} - static int recv_fd(int c) { int fd; @@ -626,7 +586,11 @@ static int net_tap_init(const NetdevTapOptions *tap, i= nt *vnet_hdr, if (setup_script && setup_script[0] !=3D '\0' && strcmp(setup_script, "no") !=3D 0) { - launch_script(setup_script, ifname, fd, &err); + char *args[3]; + args[0] =3D (char *)setup_script; + args[1] =3D (char *)ifname; + args[2] =3D NULL; + qemu_launch_script(setup_script, args, fd, &err); if (err) { error_propagate(errp, err); close(fd); --=20 2.11.0 From nobody Fri May 3 04:52:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1538653543482626.5589071213502; Thu, 4 Oct 2018 04:45:43 -0700 (PDT) Received: from localhost ([::1]:55176 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g824X-0005xJ-4F for importer@patchew.org; Thu, 04 Oct 2018 07:45:37 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38593) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g822X-0004WL-O4 for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g822S-0005ug-Rj for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:33 -0400 Received: from proxmox-new.maurer-it.com ([212.186.127.180]:4578) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g822Q-0005SN-LO for qemu-devel@nongnu.org; Thu, 04 Oct 2018 07:43:27 -0400 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 25CA340561; Thu, 4 Oct 2018 13:43:13 +0200 (CEST) From: Dominik Csapak To: qemu-devel@nongnu.org Date: Thu, 4 Oct 2018 13:43:12 +0200 Message-Id: <20181004114312.27346-4-d.csapak@proxmox.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181004114312.27346-1-d.csapak@proxmox.com> References: <20181004114312.27346-1-d.csapak@proxmox.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.186.127.180 Subject: [Qemu-devel] [PATCH v2 3/3] vl.c: call optional script when exiting X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, jasowang@redhat.com, philmd@redhat.com, thuth@redhat.com, sw@weilnetz.de Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" some users might want to call a script when qemu exits, without listening to a qmp monitor for events when running with --daemonize this option is only available on non windows systems, since there is no fork there and no --daemonize option this can be used for things like external cleanups Signed-off-by: Dominik Csapak --- changes since v1: * only for non WIN32 * use qemu_launch_script * fix error in option description (@var{file} instead of $var{file}) qemu-options.hx | 20 ++++++++++++++++++++ vl.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/qemu-options.hx b/qemu-options.hx index f139459e80..6a67ec76fe 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3421,6 +3421,26 @@ This allows for instance switching to monitor to com= mit changes to the disk image. ETEXI =20 +#ifndef _WIN32 +DEF("exit-script", HAS_ARG, QEMU_OPTION_exit_script, \ + "-exit-script \n" + " Execute the script in file on exit.\n" + " The arguments are:\n" + " - the shutdown reason,\n" + " - if it was really a reset (if -no-reboot was = set)\n" + " - the name of the guest", + QEMU_ARCH_ALL) +STEXI +@item -exit-script @var{file} +@findex -exit-script +Execute the script @var{file} on exit. +The arguments are: + - the shutdown reason + - if it was really a reset (if -no-reboot was set) + - the name of the guest +ETEXI +#endif + DEF("loadvm", HAS_ARG, QEMU_OPTION_loadvm, \ "-loadvm [tag|id]\n" \ " start right away with a saved state (loadvm in monito= r)\n", diff --git a/vl.c b/vl.c index 0388852deb..5adf7c80f7 100644 --- a/vl.c +++ b/vl.c @@ -1522,6 +1522,9 @@ void vm_state_notify(int running, RunState state) } } =20 +static ShutdownCause shutdown_reason; +static bool shutdown_was_reset; + static ShutdownCause reset_requested; static ShutdownCause shutdown_requested; static int shutdown_signal; @@ -1681,6 +1684,7 @@ void qemu_system_guest_panicked(GuestPanicInformation= *info) void qemu_system_reset_request(ShutdownCause reason) { if (no_reboot && reason !=3D SHUTDOWN_CAUSE_SUBSYSTEM_RESET) { + shutdown_was_reset =3D true; shutdown_requested =3D reason; } else { reset_requested =3D reason; @@ -1811,6 +1815,7 @@ static bool main_loop_should_exit(void) if (no_shutdown) { vm_stop(RUN_STATE_SHUTDOWN); } else { + shutdown_reason =3D request; return true; } } @@ -2908,6 +2913,7 @@ int main(int argc, char **argv, char **envp) Error *err =3D NULL; bool list_data_dirs =3D false; char *dir, **dirs; + const char *exit_script =3D NULL; typedef struct BlockdevOptions_queue { BlockdevOptions *bdo; Location loc; @@ -3596,6 +3602,11 @@ int main(int argc, char **argv, char **envp) case QEMU_OPTION_no_shutdown: no_shutdown =3D 1; break; +#ifndef _WIN32 + case QEMU_OPTION_exit_script: + exit_script =3D optarg; + break; +#endif case QEMU_OPTION_show_cursor: cursor_hide =3D 0; break; @@ -4590,5 +4601,23 @@ int main(int argc, char **argv, char **envp) migration_object_finalize(); /* TODO: unref root container, check all devices are ok */ =20 + if (exit_script) { + char *args[5]; + Error *local_err =3D NULL; + args[0] =3D (char *)exit_script; + args[1] =3D g_strdup_printf("%d", shutdown_reason); + args[2] =3D g_strdup_printf("%d", shutdown_was_reset); + args[3] =3D (char *)qemu_get_vm_name(); + args[4] =3D NULL; + + qemu_launch_script(exit_script, args, -1, &local_err); + g_free(args[1]); + g_free(args[2]); + if (local_err) { + error_report_err(local_err); + exit(1); + } + } + return 0; } --=20 2.11.0