[libvirt] [PATCHv2] command: avoid fd leak on failure
Eric Blake
eblake at redhat.com
Sat Jul 16 03:32:53 UTC 2011
On 07/14/2011 11:04 AM, Eric Blake wrote:
> virCommandTransferFD promises that the fd is no longer owned by
> the caller. Normally, we want the fd to remain open until the
> child runs, but in error situations, we must close it earlier.
>
> To avoid any risks of double-close due to misunderstanding about
> this interface, change it to take a pointer which gets set to
> -1 in the caller to record that the transfer was successful.
>
> @@ -2109,8 +2115,8 @@ void virCommandRequireHandshake(virCommandPtr cmd)
>
> VIR_DEBUG("Transfer handshake wait=%d notify=%d",
> cmd->handshakeWait[1], cmd->handshakeNotify[0]);
> - virCommandTransferFD(cmd, cmd->handshakeWait[1]);
> - virCommandTransferFD(cmd, cmd->handshakeNotify[0]);
> + virCommandTransferFD(cmd,&cmd->handshakeWait[1]);
> + virCommandTransferFD(cmd,&cmd->handshakeNotify[0]);
Unfortunately, resetting the caller's fd to -1 breaks handshake; here,
cmd->handshakeWait[1] need to be closed in the parent, but not set to -1
until after the child has been forked (setting to -1 here was too soon).
Squash this in:
diff --git i/src/util/command.c w/src/util/command.c
index 25494cf..3fda2cd 100644
--- i/src/util/command.c
+++ w/src/util/command.c
@@ -2292,6 +2292,8 @@ virCommandAbort(virCommandPtr cmd ATTRIBUTE_UNUSED)
*/
void virCommandRequireHandshake(virCommandPtr cmd)
{
+ int tmp;
+
if (!cmd || cmd->has_error)
return;
@@ -2314,8 +2316,12 @@ void virCommandRequireHandshake(virCommandPtr cmd)
VIR_DEBUG("Transfer handshake wait=%d notify=%d",
cmd->handshakeWait[1], cmd->handshakeNotify[0]);
- virCommandTransferFD(cmd, &cmd->handshakeWait[1]);
- virCommandTransferFD(cmd, &cmd->handshakeNotify[0]);
+ /* Transfer the fds now, but don't change the handshake arrays,
+ * because the child must see what fd it inherited. */
+ tmp = cmd->handshakeWait[1];
+ virCommandTransferFD(cmd, &tmp);
+ tmp = cmd->handshakeNotify[0];
+ virCommandTransferFD(cmd, &tmp);
cmd->handshake = true;
}
--
Eric Blake eblake at redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
More information about the libvir-list
mailing list