On Wed, May 13, 2026 at 12:14:55 +0200, Peter Krempa via Devel wrote:
> From: Peter Krempa <[email protected]>
>
> Extract the common code used to pass FDs from a virCommand to a child
> process into virCommandMassClose - a new wrapper - and create
> 'virCommandFDMassClose' which simply takes a bitmap of FDs to be kept
> and closes everything else.
>
> This will allow reuse of the algorithm in test files where we want to
> prevent FDs leaked from the environment from breakign the test.
>
> Signed-off-by: Peter Krempa <[email protected]>
> ---
> src/libvirt_private.syms | 1 +
> src/util/vircommand.c | 101 +++++++++++++++++++++------------------
> src/util/vircommand.h | 3 ++
> 3 files changed, 59 insertions(+), 46 deletions(-)
>
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index f22b5895db..744932acd8 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -2182,6 +2182,7 @@ virCommandDoAsyncIO;
> virCommandDryRunTokenFree;
> virCommandDryRunTokenNew;
> virCommandExec;
> +virCommandFDMassClose;
[1]
> virCommandFree;
> virCommandGetArgList;
> virCommandGetBinaryPath;
> diff --git a/src/util/vircommand.c b/src/util/vircommand.c
> index e871d572a6..07e7040ef8 100644
> --- a/src/util/vircommand.c
> +++ b/src/util/vircommand.c
> +/**
> + * virCommandFDMassClose:
> + * @keep: bitmap of FDs to be kept open in the child process
> + *
> + * Closes all open FDs (in the current process) except those represented by
> + * set bits in @keep.
> + *
> + * Returns 0 on success, -1 on error and reports libvirt errors.
> + */
> +int
> +virCommandFDMassClose(virBitmap *keep)
> +{
> + if (virCloseRangeIsSupported())
> + return virCommandMassCloseRange(keep);
> +
> + return virCommandMassCloseFrom(keep);
> +}
I didn't notice that most of this file is in a #ifndef WIN32 block, so
compiler on mingw isn't happy with [1]. I'll need to add an override.