From: Kevin Wolf <kwolf@redhat.com>
To: Corey Bryant <coreyb@linux.vnet.ibm.com>
Cc: libvir-list@redhat.com, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v4 4/4] qemu-config: Add new -add-fd command line option
Date: Fri, 19 Oct 2012 13:05:55 +0200 [thread overview]
Message-ID: <50813413.3010705@redhat.com> (raw)
In-Reply-To: <50807681.5060301@linux.vnet.ibm.com>
Am 18.10.2012 23:37, schrieb Corey Bryant:
>
>
> On 10/18/2012 04:43 PM, Eric Blake wrote:
>> On 10/18/2012 01:19 PM, Corey Bryant wrote:
>>> This option can be used for passing file descriptors on the
>>> command line. It mirrors the existing add-fd QMP command which
>>> allows an fd to be passed to QEMU via SCM_RIGHTS and added to an
>>> fd set.
>>>
>>> This can be combined with commands such as -drive to link file
>>> descriptors in an fd set to a drive:
>>>
>>> qemu-kvm -add-fd fd=3,set=2,opaque="rdwr:/path/to/file"
>>> -add-fd fd=4,set=2,opaque="rdonly:/path/to/file"
>>> -drive file=/dev/fdset/2,index=0,media=disk
>>>
>>> This example adds dups of fds 3 and 4, and the accompanying opaque
>>> strings to the fd set with ID=2. qemu_open() already knows how
>>> to handle a filename of this format. qemu_open() searches the
>>> corresponding fd set for an fd and when it finds a match, QEMU
>>> goes on to use a dup of that fd just like it would have used an
>>> fd that it opened itself.
>>>
>>> Signed-off-by: Corey Bryant <coreyb@linux.vnet.ibm.com>
>>
>>> +
>>> + if (fcntl(fd, F_GETFD) & FD_CLOEXEC) {
>>> + qerror_report(ERROR_CLASS_GENERIC_ERROR,
>>> + "fd is not valid or already in use");
>>> + return -1;
>>> + }
>>
>> Hmm, I was about to call you on the fact that you didn't check whether
>> fcntl() succeeded; but then realized that in the failure case it is
>> required by POSIX to return -1 which happens to include the FD_CLOEXEC
>> bit, so you actually ended up with a sneaky optimization that does the
>> right thing for both open and closed fds.
>
> Yep it works for both cases. I have to admit I stumbled into this at
> first and then decided to leave it this way since it worked. :)
I wouldn't be surprised to find such subtleties in Fabrice's code, but
I'm not sure if adding new instances is the best idea ever. :-)
>> Perhaps a comment in the code is warranted (after all, it is not
>> immediately apparent from reading just this if statement why it works);
>> maybe "/* All fds inherited across exec() necessarily have FD_CLOEXEC
>> clear, while qemu sets FD_CLOEXEC on all other fds opened from command
>> line arguments */". But I'm not going to require a v5 just for a
>> comment addition.
>
> I agree, a comment would be useful. Maybe Kevin can add if this series
> gets pushed?
I'll amend the following to this patch, hope you both agree with the change:
diff --git a/vl.c b/vl.c
index 47095a2..5fb40da 100644
--- a/vl.c
+++ b/vl.c
@@ -792,7 +792,7 @@ static int parse_sandbox(QemuOpts *opts, void *opaque)
static int parse_add_fd(QemuOpts *opts, void *opaque)
{
- int fd, dupfd;
+ int fd, dupfd, flags;
int64_t fdset_id;
const char *fd_opaque = NULL;
@@ -812,7 +812,12 @@ static int parse_add_fd(QemuOpts *opts, void *opaque)
return -1;
}
- if (fcntl(fd, F_GETFD) & FD_CLOEXEC) {
+ /*
+ * All fds inherited across exec() necessarily have FD_CLOEXEC
+ * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
+ */
+ flags = fcntl(fd, F_GETFD);
+ if (flags == -1 || (flags & FD_CLOEXEC)) {
qerror_report(ERROR_CLASS_GENERIC_ERROR,
"fd is not valid or already in use");
return -1;
next prev parent reply other threads:[~2012-10-19 11:07 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-18 19:19 [Qemu-devel] [PATCH v4 0/4] command line fd passing using fd sets Corey Bryant
2012-10-18 19:19 ` [Qemu-devel] [PATCH v4 1/4] monitor: Allow add-fd to any specified fd set Corey Bryant
2012-10-18 19:19 ` [Qemu-devel] [PATCH v4 2/4] monitor: Enable adding an inherited fd to an " Corey Bryant
2012-10-18 19:19 ` [Qemu-devel] [PATCH v4 3/4] monitor: Prevent removing fd from set during init Corey Bryant
2012-10-18 19:19 ` [Qemu-devel] [PATCH v4 4/4] qemu-config: Add new -add-fd command line option Corey Bryant
2012-10-18 20:43 ` Eric Blake
2012-10-18 21:37 ` Corey Bryant
2012-10-19 11:05 ` Kevin Wolf [this message]
2012-10-19 13:12 ` Corey Bryant
2012-10-18 22:09 ` Eric Blake
2012-10-22 14:36 ` [Qemu-devel] [PATCH v5] " Kevin Wolf
2012-10-22 15:06 ` Corey Bryant
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=50813413.3010705@redhat.com \
--to=kwolf@redhat.com \
--cc=coreyb@linux.vnet.ibm.com \
--cc=libvir-list@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).