summaryrefslogtreecommitdiff
path: root/src/qemu/qemu_dbus.c
blob: 06b655d870883c25d067132e7ad3f5e02645893f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * 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 *
qemuDBusCreatePidFilename(virQEMUDriverConfig *cfg,
                          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 *
qemuDBusCreateSocketPath(virQEMUDriverConfig *cfg,
                         const char *shortName)
{
    return qemuDBusCreateFilename(cfg->dbusStateDir, shortName, ".sock");
}


static char *
qemuDBusCreateConfPath(virQEMUDriverConfig *cfg,
                       const char *shortName)
{
    return qemuDBusCreateFilename(cfg->dbusStateDir, shortName, ".conf");
}


char *
qemuDBusGetAddress(virQEMUDriver *driver,
                   virDomainObj *vm)
{
    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)
{
    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
    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
qemuDBusStop(virQEMUDriver *driver,
             virDomainObj *vm)
{
    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
    qemuDomainObjPrivate *priv = vm->privateData;
    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;
    }
}


int
qemuDBusSetupCgroup(virQEMUDriver *driver,
                    virDomainObj *vm,
                    virCgroup *cgroup)
{
    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
    qemuDomainObjPrivate *priv = vm->privateData;
    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;
    }

    return virCgroupAddProcess(cgroup, cpid);
}

int
qemuDBusStart(virQEMUDriver *driver,
              virDomainObj *vm)
{
    g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
    qemuDomainObjPrivate *priv = vm->privateData;
    g_autoptr(virCommand) cmd = NULL;
    g_autofree char *dbusDaemonPath = NULL;
    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;

    if (priv->dbusDaemonRunning)
        return 0;

    dbusDaemonPath = virFindFileInPath(cfg->dbusDaemonName);
    if (!dbusDaemonPath) {
        virReportSystemError(errno,
                             _("'%1$s' is not a suitable dbus-daemon"),
                             cfg->dbusDaemonName);
        return -1;
    }

    VIR_DEBUG("Using dbus-daemon: %s", dbusDaemonPath);

    if (!(shortName = virDomainDefGetShortName(vm->def)))
        return -1;

    pidfile = qemuDBusCreatePidFilename(cfg, shortName);
    configfile = qemuDBusCreateConfPath(cfg, shortName);
    sockpath = qemuDBusCreateSocketPath(cfg, shortName);

    if (qemuDBusWriteConfig(configfile, sockpath) < 0) {
        virReportSystemError(errno, _("Failed to write '%1$s'"), configfile);
        return -1;
    }

    if (qemuSecurityDomainSetPathLabel(driver, vm, configfile, false) < 0)
        goto cleanup;

    cmd = virCommandNew(dbusDaemonPath);
    virCommandClearCaps(cmd);
    virCommandSetPidFile(cmd, pidfile);
    virCommandSetErrorFD(cmd, &errfd);
    virCommandDaemonize(cmd);
    virCommandAddArgFormat(cmd, "--config-file=%s", configfile);

    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
        goto cleanup;

    if (virPidFileReadPath(pidfile, &cpid) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("dbus-daemon %1$s didn't show up"),
                       dbusDaemonPath);
        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,
                                 _("dbus-daemon %1$s died unexpectedly"),
                                 dbusDaemonPath);
        } else {
            virReportError(VIR_ERR_OPERATION_FAILED,
                           _("dbus-daemon died and reported: %1$s"), errbuf);
        }

        goto cleanup;
    }

    if (!virFileExists(sockpath)) {
        virReportError(VIR_ERR_OPERATION_TIMEOUT,
                       _("dbus-daemon %1$s didn't show up"),
                       dbusDaemonPath);
        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;
}


void
qemuDBusVMStateAdd(virDomainObj *vm, const char *id)
{
    qemuDomainObjPrivate *priv = vm->privateData;

    priv->dbusVMStateIds = g_slist_append(priv->dbusVMStateIds, g_strdup(id));
}


void
qemuDBusVMStateRemove(virDomainObj *vm, const char *id)
{
    qemuDomainObjPrivate *priv = vm->privateData;
    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;
        }
    }
}