From: Daniel P. Berrangé Date: Mon, 8 Jul 2019 14:30:59 +0000 (+0100) Subject: remote: extract logic for determining daemon to connect to X-Git-Tag: v6.8.0-rc1~224 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ba30bc1e5889f3ae8ef1e0e2e94b48aaa03fa5a;p=thirdparty%2Flibvirt.git remote: extract logic for determining daemon to connect to We'll shortly want to reuse code for determining whether to connect to the system or session daemon from places outside the remote driver client. Pulling it out into a self contained function facilitates reuse. Signed-off-by: Daniel P. Berrangé --- diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 56c3650448..4a5bd5b679 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -1197,7 +1197,8 @@ remoteConnectOpen(virConnectPtr conn, struct private_data *priv; int ret = VIR_DRV_OPEN_ERROR; int rflags = 0; - const char *autostart = getenv("LIBVIRT_AUTOSTART"); + bool user; + bool autostart; char *driver = NULL; remoteDriverTransport transport; @@ -1236,51 +1237,11 @@ remoteConnectOpen(virConnectPtr conn, if (flags & VIR_CONNECT_RO) rflags |= VIR_DRV_OPEN_REMOTE_RO; - /* - * User session daemon is used for - * - * - Any URI with /session suffix - * - Test driver, if a protocol is given - * - * provided we are running non-root - */ - if (conn->uri && - conn->uri->path && - conn->uri->scheme && - (STREQ(conn->uri->path, "/session") || - STRPREFIX(conn->uri->scheme, "test+")) && - geteuid() > 0) { - VIR_DEBUG("User session daemon required"); + remoteGetURIDaemonInfo(conn->uri, transport, &user, &autostart); + if (user) rflags |= VIR_DRV_OPEN_REMOTE_USER; - - /* - * Furthermore if no servername is given, - * and the transport is unix, - * and uid is unprivileged then auto-spawn a daemon. - */ - if (!conn->uri->server && - (transport == REMOTE_DRIVER_TRANSPORT_UNIX) && - (!autostart || - STRNEQ(autostart, "0"))) { - VIR_DEBUG("Try daemon autostart"); - rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART; - } - } - - /* - * If URI is NULL, then do a UNIX connection possibly auto-spawning - * unprivileged server and probe remote server for URI. - */ - if (!conn->uri) { - VIR_DEBUG("Auto-probe remote URI"); - if (geteuid() > 0) { - VIR_DEBUG("Auto-spawn user daemon instance"); - rflags |= VIR_DRV_OPEN_REMOTE_USER; - if (!autostart || - STRNEQ(autostart, "0")) - rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART; - } - } + if (autostart) + rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART; ret = doRemoteOpen(conn, priv, driver, transport, auth, conf, rflags); if (ret != VIR_DRV_OPEN_SUCCESS) { diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c index 28e02e24d5..854775f401 100644 --- a/src/remote/remote_sockets.c +++ b/src/remote/remote_sockets.c @@ -224,3 +224,62 @@ remoteGetUNIXSocket(remoteDriverTransport transport, ro, session); return sock_name; } + + +void +remoteGetURIDaemonInfo(virURIPtr uri, + remoteDriverTransport transport, + bool *session, + bool *autostart) +{ + const char *autostart_str = getenv("LIBVIRT_AUTOSTART"); + + *session = false; + *autostart = false; + + /* + * User session daemon is used for + * + * - Any URI with /session suffix + * - Test driver, if a protocol is given + * + * provided we are running non-root + */ + if (uri && + uri->path && + uri->scheme && + (STREQ(uri->path, "/session") || + STRPREFIX(uri->scheme, "test+")) && + geteuid() > 0) { + VIR_DEBUG("User session daemon required"); + *session = true; + + /* + * Furthermore if no servername is given, + * and the transport is unix, + * and uid is unprivileged then auto-spawn a daemon. + */ + if (!uri->server && + (transport == REMOTE_DRIVER_TRANSPORT_UNIX) && + (!autostart_str || + STRNEQ(autostart_str, "0"))) { + VIR_DEBUG("Try daemon autostart"); + *autostart = true; + } + } + + /* + * If URI is NULL, then do a UNIX connection possibly auto-spawning + * unprivileged server and probe remote server for URI. + */ + if (!uri) { + VIR_DEBUG("Auto-probe remote URI"); + if (geteuid() > 0) { + VIR_DEBUG("Auto-spawn user daemon instance"); + *session = true; + if (!autostart_str || + STRNEQ(autostart_str, "0")) + *autostart = true; + } + } +} diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h index 64055f3d44..7526752835 100644 --- a/src/remote/remote_sockets.h +++ b/src/remote/remote_sockets.h @@ -62,3 +62,9 @@ remoteGetUNIXSocket(remoteDriverTransport transport, bool ro, bool session, char **daemon); + +void +remoteGetURIDaemonInfo(virURIPtr uri, + remoteDriverTransport transport, + bool *session, + bool *autostart);