From: Dominik Csapak <d.csapak@proxmox.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, sw@weilnetz.de, jasowang@redhat.com,
thuth@redhat.com, philmd@redhat.com
Subject: [Qemu-devel] [PATCH v2 1/3] osdep: add qemu_launch_script for executing scripts
Date: Thu, 4 Oct 2018 13:43:10 +0200 [thread overview]
Message-ID: <20181004114312.27346-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20181004114312.27346-1-d.csapak@proxmox.com>
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 <d.csapak@proxmox.com>
---
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);
+/**
+ * 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 = fork();
+ if (pid < 0) {
+ error_setg_errno(errp, errno, "could not launch script %s",
+ script);
+ return;
+ }
+ if (pid == 0) {
+ int open_max = sysconf(_SC_OPEN_MAX), i;
+
+ for (i = 3; i < open_max; i++) {
+ if (i != fd) {
+ close(i);
+ }
+ }
+ execv(script, args);
+ _exit(1);
+ } else {
+ while (waitpid(pid, &status, 0) != pid) {
+ /* loop */
+ }
+
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 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 = ENOSYS;
+ error_setg_errno(errp, errno,
+ "cannot fork child process");
+}
--
2.11.0
next prev parent reply other threads:[~2018-10-04 11:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-04 11:43 [Qemu-devel] [PATCH v2 0/3] add exit-script option to qemu Dominik Csapak
2018-10-04 11:43 ` Dominik Csapak [this message]
2018-10-04 11:43 ` [Qemu-devel] [PATCH v2 2/3] tap: use qemu_launch_script instead of launch_script Dominik Csapak
2018-10-04 11:43 ` [Qemu-devel] [PATCH v2 3/3] vl.c: call optional script when exiting Dominik Csapak
2018-10-08 3:28 ` [Qemu-devel] [PATCH v2 0/3] add exit-script option to qemu Jason Wang
2018-10-08 7:39 ` Dominik Csapak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181004114312.27346-2-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=jasowang@redhat.com \
--cc=pbonzini@redhat.com \
--cc=philmd@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=sw@weilnetz.de \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).