Re: [libvirt] [PATCH v3 39/48] remote: handle autoprobing of driver within virtproxyd

2019-07-30 Thread Andrea Bolognani
On Mon, 2019-07-29 at 18:11 +0100, Daniel P. Berrangé wrote:
> @@ -2093,6 +2094,130 @@ void *remoteClientNew(virNetServerClientPtr client,
> +static int
> +remoteDispatchProbeURI(bool readonly, char **probeduri)

One argument per line.

Reviewed-by: Andrea Bolognani 

-- 
Andrea Bolognani / Red Hat / Virtualization

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v3 39/48] remote: handle autoprobing of driver within virtproxyd

2019-07-29 Thread Daniel P . Berrangé
The virtproxyd daemon is merely responsible for forwarding RPC calls to
one of the other per-driver daemons. As such, it does not have any
drivers loaded and so regular auto-probing logic will not work. We need
it to be able to handle NULL URIs though, so must implement some kind of
alternative probing logic.

When running as root this is quite crude. If a per-driver daemon is
running, its UNIX socket will exist and we can assume it will accept
connections. If the per-driver daemon is not running, but socket
autostart is enabled, we again just assume it will accept connections.

The is not great, however, because a default install may well have
all sockets available for activation. IOW, the virtxend socket may
exist, despite the fact that the libxl driver will not actually work.

When running as non-root this is slightly easier as we only have two
drivers, QEMU and VirtualBox. These daemons will likely not be running
and socket activation won't be used either, as libvirt spawns the
daemon on demand. So we just check whether the daemon actually is
installed.

Signed-off-by: Daniel P. Berrangé 
---
 src/remote/Makefile.inc.am  |   1 +
 src/remote/remote_daemon_dispatch.c | 137 
 2 files changed, 138 insertions(+)

diff --git a/src/remote/Makefile.inc.am b/src/remote/Makefile.inc.am
index 344f19311a..d100922a8d 100644
--- a/src/remote/Makefile.inc.am
+++ b/src/remote/Makefile.inc.am
@@ -235,6 +235,7 @@ virtproxyd_CFLAGS = \
-DSOCK_PREFIX="\"libvirt\"" \
-DDAEMON_NAME="\"virtproxyd\"" \
-DENABLE_IP \
+   -DVIRTPROXYD \
$(NULL)
 virtproxyd_LDFLAGS = $(REMOTE_DAEMON_LD_FLAGS)
 virtproxyd_LDADD = $(REMOTE_DAEMON_LD_ADD)
diff --git a/src/remote/remote_daemon_dispatch.c 
b/src/remote/remote_daemon_dispatch.c
index 8c11bb3f6c..9ef76daa55 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -50,6 +50,7 @@
 #include "viraccessapicheckqemu.h"
 #include "virpolkit.h"
 #include "virthreadjob.h"
+#include "configmake.h"
 
 #define VIR_FROM_THIS VIR_FROM_RPC
 
@@ -2093,6 +2094,130 @@ void *remoteClientNew(virNetServerClientPtr client,
 
 /*- Functions. -*/
 
+#ifdef VIRTPROXYD
+/*
+ * When running in virtproxyd regular auto-probing of drivers
+ * does not work as we don't have any drivers present (except
+ * stateless ones inside libvirt.so). All the interesting
+ * drivers are in separate daemons. Thus when we get a NULL
+ * URI we need to simulate probing that virConnectOpen would
+ * previously do. We use the existance of the UNIX domain
+ * socket as our hook for probing.
+ *
+ * This assumes no stale sockets left over from a now dead
+ * daemon, but that's reasonable since libvirtd unlinks
+ * sockets it creates on shutdown, or uses systemd activation
+ *
+ * We only try to probe for primary hypervisor drivers,
+ * not the secondary drivers.
+ */
+static int
+remoteDispatchProbeURI(bool readonly, char **probeduri)
+{
+*probeduri = NULL;
+VIR_DEBUG("Probing for driver daemon sockets");
+
+/*
+ * If running root, either the daemon is running and the socket
+ * exists, or we're using socket activation so the socket exists
+ * too.
+ *
+ * If running non-root, chances are that the daemon won't be
+ * running, nor any socket activation is used. We need to
+ * be able to auto-spawn the daemon. We thus just check to
+ * see what daemons are installed. This is not a big deal as
+ * only QEMU & VBox run as non-root, anyway.
+ */
+if (geteuid() != 0) {
+/* Order these the same as virDriverLoadModule
+ * calls in daemonInitialize */
+const char *drivers[] = {
+# ifdef WITH_QEMU
+"qemu",
+# endif
+# ifdef WITH_VBOX
+"vbox",
+# endif
+};
+size_t i;
+
+for (i = 0; i < ARRAY_CARDINALITY(drivers) && !*probeduri; i++) {
+VIR_AUTOFREE(char *) daemonname = NULL;
+VIR_AUTOFREE(char *) daemonpath = NULL;
+
+if (virAsprintf(, "virt%sd", drivers[i]) < 0)
+return -1;
+
+if (!(daemonpath = virFileFindResource(daemonname, "src", 
SBINDIR)))
+return -1;
+
+if (!virFileExists(daemonpath)) {
+VIR_DEBUG("Missing daemon %s for driver %s", daemonpath, 
drivers[i]);
+continue;
+}
+
+if (virAsprintf(probeduri, "%s:///session", drivers[i]) < 0)
+return -1;
+
+VIR_DEBUG("Probed URI %s via daemon %s", *probeduri, daemonpath);
+return 0;
+}
+} else {
+/* Order these the same as virDriverLoadModule
+ * calls in daemonInitialize */
+const char *drivers[] = {
+# ifdef WITH_LIBXL
+"libxl",
+# endif
+# ifdef WITH_QEMU
+"qemu",
+# endif
+# ifdef WITH_LXC
+"lxc",
+# endif
+# ifdef WITH_VBOX
+"vbox",
+# endif
+# ifdef WITH_BHYVE
+"bhyve",