From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:57072) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMKBK-0007vg-Mc for qemu-devel@nongnu.org; Thu, 11 Oct 2012 10:56:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TMKBD-0007G1-5p for qemu-devel@nongnu.org; Thu, 11 Oct 2012 10:56:14 -0400 Received: from e37.co.us.ibm.com ([32.97.110.158]:34325) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TMKBC-0007Fp-Tz for qemu-devel@nongnu.org; Thu, 11 Oct 2012 10:56:07 -0400 Received: from /spool/local by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 11 Oct 2012 08:56:05 -0600 Received: from d03relay05.boulder.ibm.com (d03relay05.boulder.ibm.com [9.17.195.107]) by d03dlp01.boulder.ibm.com (Postfix) with ESMTP id 4742D1FF0437 for ; Thu, 11 Oct 2012 08:45:18 -0600 (MDT) Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay05.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q9BEj318140788 for ; Thu, 11 Oct 2012 08:45:07 -0600 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q9BEj2Q3028469 for ; Thu, 11 Oct 2012 08:45:02 -0600 Message-ID: <5076DB6C.7040003@linux.vnet.ibm.com> Date: Thu, 11 Oct 2012 10:45:00 -0400 From: Corey Bryant MIME-Version: 1.0 References: <1349878805-16352-1-git-send-email-coreyb@linux.vnet.ibm.com> <1349878805-16352-4-git-send-email-coreyb@linux.vnet.ibm.com> <5075F727.1060608@redhat.com> In-Reply-To: <5075F727.1060608@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2 3/3] qemu-config: Add new -add-fd command line option List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: kwolf@redhat.com, libvir-list@redhat.com, qemu-devel@nongnu.org On 10/10/2012 06:31 PM, Eric Blake wrote: > 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. I agree. I'll try this approach. > > 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. > I don't see this validation check for duplicate use of fd's being necessary. Like you say below, in the QMP add-fd case we can add the same fd multiple times. So we should be able to add the same fd multiple times via the command line. The only difference between QMP and command line in this case is that the QMP fd is a dup and therefore a different number and the command line fd will be the same fd. I'd prefer to leave this alone unless there's a compelling reason to block adding of the same fd. > > > 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. > -- Regards, Corey Bryant