2020-02-25 09:55:08 +00:00
|
|
|
/*
|
|
|
|
* qemu_dbus.c: QEMU dbus daemon
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "qemu_dbus.h"
|
|
|
|
#include "qemu_security.h"
|
|
|
|
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virtime.h"
|
|
|
|
#include "virpidfile.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("qemu.dbus");
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusCreatePidFilename(virQEMUDriverConfig *cfg,
|
2020-02-25 09:55:08 +00:00
|
|
|
const char *shortName)
|
|
|
|
{
|
|
|
|
g_autofree char *name = g_strdup_printf("%s-dbus", shortName);
|
|
|
|
|
|
|
|
return virPidFileBuildPath(cfg->dbusStateDir, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
qemuDBusCreateFilename(const char *stateDir,
|
|
|
|
const char *shortName,
|
|
|
|
const char *ext)
|
|
|
|
{
|
|
|
|
g_autofree char *name = g_strdup_printf("%s-dbus", shortName);
|
|
|
|
|
|
|
|
return virFileBuildPath(stateDir, name, ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusCreateSocketPath(virQEMUDriverConfig *cfg,
|
2020-02-25 09:55:08 +00:00
|
|
|
const char *shortName)
|
|
|
|
{
|
|
|
|
return qemuDBusCreateFilename(cfg->dbusStateDir, shortName, ".sock");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusCreateConfPath(virQEMUDriverConfig *cfg,
|
2020-02-25 09:55:08 +00:00
|
|
|
const char *shortName)
|
|
|
|
{
|
|
|
|
return qemuDBusCreateFilename(cfg->dbusStateDir, shortName, ".conf");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusGetAddress(virQEMUDriver *driver,
|
|
|
|
virDomainObj *vm)
|
2020-02-25 09:55:08 +00:00
|
|
|
{
|
|
|
|
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
|
|
|
|
g_autofree char *shortName = virDomainDefGetShortName(vm->def);
|
|
|
|
g_autofree char *path = NULL;
|
|
|
|
|
|
|
|
if (!shortName)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
path = qemuDBusCreateSocketPath(cfg, shortName);
|
|
|
|
|
|
|
|
return g_strdup_printf("unix:path=%s", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuDBusWriteConfig(const char *filename, const char *path)
|
|
|
|
{
|
2020-07-02 22:26:41 +00:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2020-02-25 09:55:08 +00:00
|
|
|
g_autofree char *config = NULL;
|
|
|
|
|
|
|
|
virBufferAddLit(&buf, "<!DOCTYPE busconfig PUBLIC \"-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN\"\n");
|
|
|
|
virBufferAddLit(&buf, " \"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd\">\n");
|
|
|
|
virBufferAddLit(&buf, "<busconfig>\n");
|
|
|
|
virBufferAdjustIndent(&buf, 2);
|
|
|
|
|
|
|
|
virBufferAddLit(&buf, "<type>org.libvirt.qemu</type>\n");
|
|
|
|
virBufferAsprintf(&buf, "<listen>unix:path=%s</listen>\n", path);
|
|
|
|
virBufferAddLit(&buf, "<auth>EXTERNAL</auth>\n");
|
|
|
|
|
|
|
|
virBufferAddLit(&buf, "<policy context='default'>\n");
|
|
|
|
virBufferAdjustIndent(&buf, 2);
|
|
|
|
virBufferAddLit(&buf, "<!-- Allow everything to be sent -->\n");
|
|
|
|
virBufferAddLit(&buf, "<allow send_destination='*' eavesdrop='true'/>\n");
|
|
|
|
virBufferAddLit(&buf, "<!-- Allow everything to be received -->\n");
|
|
|
|
virBufferAddLit(&buf, "<allow eavesdrop='true'/>\n");
|
|
|
|
virBufferAddLit(&buf, "<!-- Allow anyone to own anything -->\n");
|
|
|
|
virBufferAddLit(&buf, "<allow own='*'/>\n");
|
|
|
|
virBufferAdjustIndent(&buf, -2);
|
|
|
|
virBufferAddLit(&buf, "</policy>\n");
|
|
|
|
|
|
|
|
virBufferAddLit(&buf, "<include if_selinux_enabled='yes' selinux_root_relative='yes'>contexts/dbus_contexts</include>\n");
|
|
|
|
|
|
|
|
virBufferAdjustIndent(&buf, -2);
|
|
|
|
virBufferAddLit(&buf, "</busconfig>\n");
|
|
|
|
|
|
|
|
config = virBufferContentAndReset(&buf);
|
|
|
|
|
|
|
|
return virFileWriteStr(filename, config, 0600);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusStop(virQEMUDriver *driver,
|
|
|
|
virDomainObj *vm)
|
2020-02-25 09:55:08 +00:00
|
|
|
{
|
|
|
|
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDomainObjPrivate *priv = vm->privateData;
|
2020-02-25 09:55:08 +00:00
|
|
|
g_autofree char *shortName = NULL;
|
|
|
|
g_autofree char *pidfile = NULL;
|
|
|
|
|
|
|
|
if (!(shortName = virDomainDefGetShortName(vm->def)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
pidfile = qemuDBusCreatePidFilename(cfg, shortName);
|
|
|
|
|
|
|
|
if (virPidFileForceCleanupPath(pidfile) < 0) {
|
|
|
|
VIR_WARN("Unable to kill dbus-daemon process");
|
|
|
|
} else {
|
|
|
|
priv->dbusDaemonRunning = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-08 17:23:50 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusSetupCgroup(virQEMUDriver *driver,
|
|
|
|
virDomainObj *vm,
|
|
|
|
virCgroup *cgroup)
|
2020-04-08 17:23:50 +00:00
|
|
|
{
|
|
|
|
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDomainObjPrivate *priv = vm->privateData;
|
2020-04-08 17:23:50 +00:00
|
|
|
g_autofree char *shortName = NULL;
|
|
|
|
g_autofree char *pidfile = NULL;
|
|
|
|
pid_t cpid = -1;
|
|
|
|
|
|
|
|
if (!priv->dbusDaemonRunning)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!(shortName = virDomainDefGetShortName(vm->def)))
|
|
|
|
return -1;
|
|
|
|
pidfile = qemuDBusCreatePidFilename(cfg, shortName);
|
|
|
|
if (virPidFileReadPath(pidfile, &cpid) < 0) {
|
|
|
|
VIR_WARN("Unable to get DBus PID");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-28 02:20:37 +00:00
|
|
|
return virCgroupAddProcess(cgroup, cpid);
|
2020-04-08 17:23:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 09:55:08 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusStart(virQEMUDriver *driver,
|
|
|
|
virDomainObj *vm)
|
2020-02-25 09:55:08 +00:00
|
|
|
{
|
|
|
|
g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDomainObjPrivate *priv = vm->privateData;
|
2020-02-25 09:55:08 +00:00
|
|
|
g_autoptr(virCommand) cmd = NULL;
|
2023-05-10 17:56:59 +00:00
|
|
|
g_autofree char *dbusDaemonPath = NULL;
|
2020-02-25 09:55:08 +00:00
|
|
|
g_autofree char *shortName = NULL;
|
|
|
|
g_autofree char *pidfile = NULL;
|
|
|
|
g_autofree char *configfile = NULL;
|
|
|
|
g_autofree char *sockpath = NULL;
|
|
|
|
virTimeBackOffVar timebackoff;
|
|
|
|
const unsigned long long timeout = 500 * 1000; /* ms */
|
|
|
|
VIR_AUTOCLOSE errfd = -1;
|
|
|
|
pid_t cpid = -1;
|
|
|
|
int ret = -1;
|
|
|
|
|
2020-04-08 17:23:48 +00:00
|
|
|
if (priv->dbusDaemonRunning)
|
|
|
|
return 0;
|
|
|
|
|
2023-05-10 17:56:59 +00:00
|
|
|
dbusDaemonPath = virFindFileInPath(cfg->dbusDaemonName);
|
|
|
|
if (!dbusDaemonPath) {
|
2020-02-25 09:55:08 +00:00
|
|
|
virReportSystemError(errno,
|
2023-03-09 12:15:22 +00:00
|
|
|
_("'%1$s' is not a suitable dbus-daemon"),
|
2020-02-25 09:55:08 +00:00
|
|
|
cfg->dbusDaemonName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-05-10 17:56:59 +00:00
|
|
|
VIR_DEBUG("Using dbus-daemon: %s", dbusDaemonPath);
|
|
|
|
|
2020-02-25 09:55:08 +00:00
|
|
|
if (!(shortName = virDomainDefGetShortName(vm->def)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pidfile = qemuDBusCreatePidFilename(cfg, shortName);
|
|
|
|
configfile = qemuDBusCreateConfPath(cfg, shortName);
|
|
|
|
sockpath = qemuDBusCreateSocketPath(cfg, shortName);
|
|
|
|
|
|
|
|
if (qemuDBusWriteConfig(configfile, sockpath) < 0) {
|
2023-03-09 12:15:22 +00:00
|
|
|
virReportSystemError(errno, _("Failed to write '%1$s'"), configfile);
|
2020-02-25 09:55:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemuSecurityDomainSetPathLabel(driver, vm, configfile, false) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2023-05-10 17:56:59 +00:00
|
|
|
cmd = virCommandNew(dbusDaemonPath);
|
2020-02-25 09:55:08 +00:00
|
|
|
virCommandClearCaps(cmd);
|
|
|
|
virCommandSetPidFile(cmd, pidfile);
|
|
|
|
virCommandSetErrorFD(cmd, &errfd);
|
|
|
|
virCommandDaemonize(cmd);
|
|
|
|
virCommandAddArgFormat(cmd, "--config-file=%s", configfile);
|
|
|
|
|
2023-03-01 20:34:32 +00:00
|
|
|
if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
|
2020-02-25 09:55:08 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virPidFileReadPath(pidfile, &cpid) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2023-03-09 12:15:22 +00:00
|
|
|
_("dbus-daemon %1$s didn't show up"),
|
2023-05-10 17:56:59 +00:00
|
|
|
dbusDaemonPath);
|
2020-02-25 09:55:08 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virTimeBackOffStart(&timebackoff, 1, timeout) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
while (virTimeBackOffWait(&timebackoff)) {
|
|
|
|
char errbuf[1024] = { 0 };
|
|
|
|
|
|
|
|
if (virFileExists(sockpath))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (virProcessKill(cpid, 0) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (saferead(errfd, errbuf, sizeof(errbuf) - 1) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2023-03-09 12:15:22 +00:00
|
|
|
_("dbus-daemon %1$s died unexpectedly"),
|
2023-05-10 17:56:59 +00:00
|
|
|
dbusDaemonPath);
|
2020-02-25 09:55:08 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
2023-03-09 12:15:22 +00:00
|
|
|
_("dbus-daemon died and reported: %1$s"), errbuf);
|
2020-02-25 09:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virFileExists(sockpath)) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_TIMEOUT,
|
2023-04-25 14:27:13 +00:00
|
|
|
_("dbus-daemon %1$s didn't show up"),
|
2023-05-10 17:56:59 +00:00
|
|
|
dbusDaemonPath);
|
2020-02-25 09:55:08 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemuSecurityDomainSetPathLabel(driver, vm, sockpath, false) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
priv->dbusDaemonRunning = true;
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
if (ret < 0) {
|
|
|
|
virCommandAbort(cmd);
|
|
|
|
if (cpid >= 0)
|
|
|
|
virProcessKillPainfully(cpid, true);
|
|
|
|
unlink(pidfile);
|
|
|
|
unlink(configfile);
|
|
|
|
unlink(sockpath);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2020-02-25 09:55:11 +00:00
|
|
|
|
|
|
|
|
2021-02-05 09:48:51 +00:00
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusVMStateAdd(virDomainObj *vm, const char *id)
|
2020-02-25 09:55:11 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDomainObjPrivate *priv = vm->privateData;
|
2021-02-05 09:48:51 +00:00
|
|
|
|
|
|
|
priv->dbusVMStateIds = g_slist_append(priv->dbusVMStateIds, g_strdup(id));
|
2020-02-25 09:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDBusVMStateRemove(virDomainObj *vm, const char *id)
|
2020-02-25 09:55:11 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
qemuDomainObjPrivate *priv = vm->privateData;
|
2021-02-05 09:48:51 +00:00
|
|
|
GSList *next;
|
|
|
|
|
|
|
|
for (next = priv->dbusVMStateIds; next; next = next->next) {
|
|
|
|
const char *elem = next->data;
|
|
|
|
|
|
|
|
if (STREQ(id, elem)) {
|
|
|
|
priv->dbusVMStateIds = g_slist_remove_link(priv->dbusVMStateIds, next);
|
|
|
|
g_slist_free_full(next, g_free);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-25 09:55:11 +00:00
|
|
|
}
|