From: Eric Blake <eblake@redhat.com>
To: Corey Bryant <coreyb@linux.vnet.ibm.com>
Cc: kwolf@redhat.com, libvir-list@redhat.com, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v2 3/3] qemu-config: Add new -add-fd command line option
Date: Wed, 10 Oct 2012 16:31:03 -0600 [thread overview]
Message-ID: <5075F727.1060608@redhat.com> (raw)
In-Reply-To: <1349878805-16352-4-git-send-email-coreyb@linux.vnet.ibm.com>
[-- Attachment #1: Type: text/plain, Size: 4161 bytes --]
On 10/10/2012 08:20 AM, 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=4,set=2,opaque="rdwr:/path/to/file"
> -add-fd fd=5,set=2,opaque="rdonly:/path/to/file"
> -drive file=/dev/fdset/2,index=0,media=disk
>
> This example adds fds 4 and 5, 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.
Missing some argument validation. Earlier, I complained about set
validation, now I'm going to complain about fd validation.
> +static int parse_add_fd(QemuOpts *opts, void *opaque)
> +{
> + int fd;
> + int64_t fdset_id;
> + const char *fd_opaque = NULL;
> +
> + fd = qemu_opt_get_number(opts, "fd", -1);
> + fdset_id = qemu_opt_get_number(opts, "set", -1);
> + fd_opaque = qemu_opt_get(opts, "opaque");
If I call 'qemu -add-fd fd=-2,set=1', that had better fail because fd -2
does not exist. Likewise if I call 'qemu -add-fd fd=10000000,set=1'
(here, picking an fd that I know can't be opened). More subtly, 'qemu
-add-fd fd=4,set=1 4<&-' should fail, since fd 4 was not inherited down
to the qemu process. Fuzzier is whether 'qemu -add-fd fd=0,set=1' makes
sense, as that would then make stdin be competing for multiple uses;
this would be a situation that the monitor command can't duplicate, so
you have introduced new ways to possibly break things from the command
line. I'm thinking it's safest to reject fd of 0, 1, or 2 for now, and
only relax it later IF we can prove that it would be both useful and safe.
So it sounds like you need something like fcntl(fd,F_GETFL) to see that
an the fd was actually inherited, as well as validating that fd >
STDERR_FILENO.
Another missing validation check is for duplicate use. With the monitor
command, you ALWAYS have a unique fd (thanks to SCM_RIGHTS). But with
the command line, I can type 'qemu -add-fd fd=4,set=1 -add-fd
fd=4,set=2'. Oops - I've now corrupted your set layout, unless you
validate that every fd requested in -add-fd does not already reside in
any existing set.
Thinking aloud:
On the other hand, being able to pass in one fd to multiple sets MIGHT
be useful; in the SCM_RIGHTS monitor command case, I can pass the same
fd from the management perspective into multiple sets, even though in
qemu's perspective, there will be multiple fds created (one per call).
Perhaps instead of directly adding the inherited fd to a set, and having
to then sweep all sets to check for duplicates, it might make sense to
add dup(fd) to a set, so that if I call:
qemu -add-fd fd=4,set=1 -add-fd fd=4,set=2 -add-fd fd=5,set=2
what REALLY happens is that qemu adds dup(4)==6 to set 1, dup(4)==7 to
set 2, and dup(5)==8 to set 3. Then, after all ALL -add-fd have been
processed, qemu then does another pass through them calling close(4) and
close(5) (to avoid holding the original fds open indefinitely if the
corresponding sets are discarded). Hmm, notice that if you dup() before
adding to a set, then that the dup() resolves my first complaint of
validating that the inherited fd exists; it also changes the problem of
dealing with fd 0 (it would now be valid to add fd 0 to any number of
sets; but a final cleanup loop had better be careful to not call
close(0) unintentionally).
Personally, though, I'm not sure the complexity of using dup() buys us
anything, so I'm happy with the simpler solution of using fd as-is,
coupled with a check for no reuse of an fd already in a set.
--
Eric Blake eblake@redhat.com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 617 bytes --]
next prev parent reply other threads:[~2012-10-10 22:31 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-10 14:20 [Qemu-devel] [PATCH v2 0/3] command line fd passing using fd sets Corey Bryant
2012-10-10 14:20 ` [Qemu-devel] [PATCH v2 1/3] monitor: Allow add-fd to any specified fd set Corey Bryant
2012-10-10 21:49 ` Eric Blake
2012-10-11 14:29 ` Corey Bryant
2012-10-10 14:20 ` [Qemu-devel] [PATCH v2 2/3] monitor: Enable adding an inherited fd to an " Corey Bryant
2012-10-10 22:01 ` Eric Blake
2012-10-11 14:30 ` Corey Bryant
2012-10-11 11:25 ` Kevin Wolf
2012-10-11 15:04 ` Corey Bryant
2012-10-12 8:30 ` Kevin Wolf
2012-10-10 14:20 ` [Qemu-devel] [PATCH v2 3/3] qemu-config: Add new -add-fd command line option Corey Bryant
2012-10-10 22:31 ` Eric Blake [this message]
2012-10-11 14:45 ` Corey Bryant
2012-10-11 15:55 ` Eric Blake
2012-10-11 17:36 ` Corey Bryant
2012-10-11 16:04 ` [Qemu-devel] [libvirt] " Eric Blake
2012-10-11 16:11 ` Eric Blake
2012-10-11 17:49 ` 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=5075F727.1060608@redhat.com \
--to=eblake@redhat.com \
--cc=coreyb@linux.vnet.ibm.com \
--cc=kwolf@redhat.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).