On Thu, Oct 09, 2025 at 05:12:46PM +0100, Richard W.M. Jones wrote:
> Libguestfs wants to use qemu to run a captive appliance. When the
> program linked to libguestfs exits, we want qemu to be cleaned up.
> Libguestfs goes to great lengths to do this at the moment: it either
> forks a separate process to ensure clean-up is done, or it asks
> libvirt to clean up the qemu process. However this is complicated and
> not totally reliable.
>
> On Linux, FreeBSD and macOS, there are mechanisms to ensure a signal
> or message is delivered to a process when its parent process goes
> away. The qemu test suite even uses this mechanism on Linux (see
> PR_SET_PDEATHSIG in tests/qtest/libqtest.c).
>
> In nbdkit we have long had the concept of running nbdkit captively,
> and we have the nbdkit --exit-with-parent flag to help
> (https://libguestfs.org/nbdkit-captive.1.html#EXIT-WITH-PARENT)
>
> This commit adds the same mechanism. The syntax is:
>
> qemu -run-with exit-with-parent=on [...]
>
> This is not a feature that most typical users of qemu (for running
> general purpose, long-lived VMs) should use, so it defaults to off.
>
> The exit-with-parent.[ch] files are copied from nbdkit, where they
> have a 3-clause BSD license which is compatible with qemu:
>
> https://gitlab.com/nbdkit/nbdkit/-/tree/master/common/utils?ref_type=heads
>
> Thanks: Daniel P. Berrangé <[email protected]>
> Signed-off-by: Richard W.M. Jones <[email protected]>
> ---
> include/qemu/exit-with-parent.h | 57 +++++++++++++
> qemu-options.hx | 13 ++-
> system/exit-with-parent.c | 140 ++++++++++++++++++++++++++++++++
> system/meson.build | 1 +
> system/vl.c | 13 +++
> 5 files changed, 222 insertions(+), 2 deletions(-)
>
> diff --git a/include/qemu/exit-with-parent.h b/include/qemu/exit-with-parent.h
> new file mode 100644
> index 0000000000..c00b863fe9
> --- /dev/null
> +++ b/include/qemu/exit-with-parent.h
> @@ -0,0 +1,57 @@
snip
> +
> +#ifndef NBDKIT_EXIT_WITH_PARENT_H
> +#define NBDKIT_EXIT_WITH_PARENT_H
I'd suggest s/NBDKIT/QEMU/ here & at the end
> +
> +/* Test if the feature is available on the platform. */
> +static inline bool can_exit_with_parent(void)
> +{
> +#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
> + return true;
> +#else
> + return false;
> +#endif
> +}
> +
> +/*
> + * --exit-with-parent: kill the current process if the parent exits.
> + * This may return -1 on error.
> + *
> + * Note this will abort on platforms where can_exit_with_parent()
> + * returned false.
> + */
> +extern int set_exit_with_parent(void);
> +
> +#endif /* NBDKIT_EXIT_WITH_PARENT_H */
With that minor change
Reviewed-by: Daniel P. Berrangé <[email protected]>
With regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|