qemu_conf.c 22.8 KB
Newer Older
D
Daniel P. Berrange 已提交
1
/*
2
 * qemu_conf.c: QEMU configuration management
D
Daniel P. Berrange 已提交
3
 *
4
 * Copyright (C) 2006-2012 Red Hat, Inc.
D
Daniel P. Berrange 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
O
Osier Yang 已提交
18 19
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
D
Daniel P. Berrange 已提交
20 21 22 23
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

24
#include <config.h>
25

D
Daniel P. Berrange 已提交
26 27 28 29
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
30
#include <stdlib.h>
D
Daniel P. Berrange 已提交
31 32 33
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
34
#include <sys/wait.h>
35
#include <arpa/inet.h>
36
#include <sys/utsname.h>
37
#include <mntent.h>
D
Daniel P. Berrange 已提交
38

39
#include "virterror_internal.h"
40
#include "qemu_conf.h"
41
#include "qemu_command.h"
42
#include "qemu_capabilities.h"
43
#include "qemu_bridge_filter.h"
44
#include "uuid.h"
45
#include "buf.h"
D
Daniel P. Berrange 已提交
46
#include "conf.h"
47
#include "util.h"
48
#include "memory.h"
49 50
#include "datatypes.h"
#include "xml.h"
51
#include "nodeinfo.h"
52
#include "logging.h"
53
#include "cpu/cpu.h"
54
#include "domain_nwfilter.h"
E
Eric Blake 已提交
55
#include "virfile.h"
56
#include "configmake.h"
57

58 59
#define VIR_FROM_THIS VIR_FROM_QEMU

60 61 62 63 64
struct _qemuDriverCloseDef {
    virConnectPtr conn;
    qemuDriverCloseCallback cb;
};

65 66 67 68 69 70 71 72 73 74
void qemuDriverLock(struct qemud_driver *driver)
{
    virMutexLock(&driver->lock);
}
void qemuDriverUnlock(struct qemud_driver *driver)
{
    virMutexUnlock(&driver->lock);
}


D
Daniel P. Berrange 已提交
75 76 77 78
int qemudLoadDriverConfig(struct qemud_driver *driver,
                          const char *filename) {
    virConfPtr conf;
    virConfValuePtr p;
79 80
    char *user;
    char *group;
81
    int i;
D
Daniel P. Berrange 已提交
82

83
    /* Setup critical defaults */
84 85
    driver->securityDefaultConfined = true;
    driver->securityRequireConfined = false;
86
    driver->dynamicOwnership = 1;
87
    driver->clearEmulatorCapabilities = 1;
88

89
    if (!(driver->vncListen = strdup("127.0.0.1"))) {
90
        virReportOOMError();
91 92
        return -1;
    }
93 94 95 96

    driver->remotePortMin = QEMU_REMOTE_PORT_MIN;
    driver->remotePortMax = QEMU_REMOTE_PORT_MAX;

97
    if (!(driver->vncTLSx509certdir = strdup(SYSCONFDIR "/pki/libvirt-vnc"))) {
98
        virReportOOMError();
D
Daniel P. Berrange 已提交
99 100 101
        return -1;
    }

102 103 104 105
    if (!(driver->spiceListen = strdup("127.0.0.1"))) {
        virReportOOMError();
        return -1;
    }
106 107
    if (!(driver->spiceTLSx509certdir
          = strdup(SYSCONFDIR "/pki/libvirt-spice"))) {
108 109 110 111
        virReportOOMError();
        return -1;
    }

112
#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
113 114 115 116 117 118
    /* For privileged driver, try and find hugepage mount automatically.
     * Non-privileged driver requires admin to create a dir for the
     * user, chown it, and then let user configure it manually */
    if (driver->privileged &&
        !(driver->hugetlbfs_mount = virFileFindMountPoint("hugetlbfs"))) {
        if (errno != ENOENT) {
119
            virReportSystemError(errno, "%s",
120 121 122 123 124 125
                                 _("unable to find hugetlbfs mountpoint"));
            return -1;
        }
    }
#endif

126
    if (!(driver->lockManager =
127
          virLockManagerPluginNew("nop", NULL, 0)))
128
        return -1;
129

130 131
    driver->keepAliveInterval = 5;
    driver->keepAliveCount = 5;
132
    driver->seccompSandbox = -1;
133

D
Daniel P. Berrange 已提交
134 135 136
    /* Just check the file is readable before opening it, otherwise
     * libvirt emits an error.
     */
137 138 139 140
    if (access (filename, R_OK) == -1) {
        VIR_INFO("Could not read qemu config file %s", filename);
        return 0;
    }
D
Daniel P. Berrange 已提交
141

142
    conf = virConfReadFile (filename, 0);
143 144 145
    if (!conf) {
        return -1;
    }
D
Daniel P. Berrange 已提交
146 147 148


#define CHECK_TYPE(name,typ) if (p && p->type != (typ)) {               \
149 150 151
        virReportError(VIR_ERR_INTERNAL_ERROR,                          \
                       "%s: %s: expected type " #typ,                   \
                       filename, (name));                               \
D
Daniel P. Berrange 已提交
152 153 154 155
        virConfFree(conf);                                              \
        return -1;                                                      \
    }

156 157 158 159
    p = virConfGetValue (conf, "vnc_auto_unix_socket");
    CHECK_TYPE ("vnc_auto_unix_socket", VIR_CONF_LONG);
    if (p) driver->vncAutoUnixSocket = p->l;

D
Daniel P. Berrange 已提交
160 161 162 163 164 165 166 167 168 169 170
    p = virConfGetValue (conf, "vnc_tls");
    CHECK_TYPE ("vnc_tls", VIR_CONF_LONG);
    if (p) driver->vncTLS = p->l;

    p = virConfGetValue (conf, "vnc_tls_x509_verify");
    CHECK_TYPE ("vnc_tls_x509_verify", VIR_CONF_LONG);
    if (p) driver->vncTLSx509verify = p->l;

    p = virConfGetValue (conf, "vnc_tls_x509_cert_dir");
    CHECK_TYPE ("vnc_tls_x509_cert_dir", VIR_CONF_STRING);
    if (p && p->str) {
171
        VIR_FREE(driver->vncTLSx509certdir);
D
Daniel P. Berrange 已提交
172
        if (!(driver->vncTLSx509certdir = strdup(p->str))) {
173
            virReportOOMError();
D
Daniel P. Berrange 已提交
174 175 176 177 178 179 180 181
            virConfFree(conf);
            return -1;
        }
    }

    p = virConfGetValue (conf, "vnc_listen");
    CHECK_TYPE ("vnc_listen", VIR_CONF_STRING);
    if (p && p->str) {
J
Jim Meyering 已提交
182
        VIR_FREE(driver->vncListen);
183
        if (!(driver->vncListen = strdup(p->str))) {
184
            virReportOOMError();
185 186 187
            virConfFree(conf);
            return -1;
        }
D
Daniel P. Berrange 已提交
188 189
    }

190 191 192 193 194
    p = virConfGetValue (conf, "vnc_password");
    CHECK_TYPE ("vnc_password", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->vncPassword);
        if (!(driver->vncPassword = strdup(p->str))) {
195
            virReportOOMError();
196 197 198 199 200
            virConfFree(conf);
            return -1;
        }
    }

201
    p = virConfGetValue (conf, "security_driver");
202 203 204 205 206 207 208 209 210 211 212 213 214 215
    if (p && p->type == VIR_CONF_LIST) {
        size_t len;
        virConfValuePtr pp;

        /* Calc lenght and check items */
        for (len = 0, pp = p->list; pp; len++, pp = pp->next) {
            if (pp->type != VIR_CONF_STRING) {
                VIR_ERROR(_("security_driver be a list of strings"));
                virConfFree(conf);
                return -1;
            }
        }

        if (VIR_ALLOC_N(driver->securityDriverNames, len + 1) < 0) {
216
            virReportOOMError();
217 218 219
            virConfFree(conf);
            return -1;
        }
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

        for (i = 0, pp = p->list; pp; i++, pp = pp->next) {
            driver->securityDriverNames[i] = strdup(pp->str);
            if (driver->securityDriverNames == NULL) {
                virReportOOMError();
                virConfFree(conf);
                return -1;
            }
        }
        driver->securityDriverNames[len] = NULL;
    } else {
        CHECK_TYPE ("security_driver", VIR_CONF_STRING);
        if (p && p->str) {
            if (VIR_ALLOC_N(driver->securityDriverNames, 2) < 0 ||
                !(driver->securityDriverNames[0] = strdup(p->str))) {
                virReportOOMError();
                virConfFree(conf);
                return -1;
            }
            driver->securityDriverNames[1] = NULL;
        }
241 242
    }

243 244 245 246 247 248 249 250 251
    p = virConfGetValue (conf, "security_default_confined");
    CHECK_TYPE ("security_default_confined", VIR_CONF_LONG);
    if (p) driver->securityDefaultConfined = p->l;

    p = virConfGetValue (conf, "security_require_confined");
    CHECK_TYPE ("security_require_confined", VIR_CONF_LONG);
    if (p) driver->securityRequireConfined = p->l;


252 253 254 255 256 257 258 259 260
    p = virConfGetValue (conf, "vnc_sasl");
    CHECK_TYPE ("vnc_sasl", VIR_CONF_LONG);
    if (p) driver->vncSASL = p->l;

    p = virConfGetValue (conf, "vnc_sasl_dir");
    CHECK_TYPE ("vnc_sasl_dir", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->vncSASLdir);
        if (!(driver->vncSASLdir = strdup(p->str))) {
261
            virReportOOMError();
262 263 264 265 266
            virConfFree(conf);
            return -1;
        }
    }

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
    p = virConfGetValue (conf, "spice_tls");
    CHECK_TYPE ("spice_tls", VIR_CONF_LONG);
    if (p) driver->spiceTLS = p->l;

    p = virConfGetValue (conf, "spice_tls_x509_cert_dir");
    CHECK_TYPE ("spice_tls_x509_cert_dir", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->spiceTLSx509certdir);
        if (!(driver->spiceTLSx509certdir = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

    p = virConfGetValue (conf, "spice_listen");
    CHECK_TYPE ("spice_listen", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->spiceListen);
        if (!(driver->spiceListen = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

    p = virConfGetValue (conf, "spice_password");
    CHECK_TYPE ("spice_password", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->spicePassword);
        if (!(driver->spicePassword = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    p = virConfGetValue (conf, "remote_display_port_min");
    CHECK_TYPE ("remote_display_port_min", VIR_CONF_LONG);
    if (p) {
        if (p->l < QEMU_REMOTE_PORT_MIN) {
            /* if the port is too low, we can't get the display name
             * to tell to vnc (usually subtract 5900, e.g. localhost:1
             * for port 5901) */
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("%s: remote_display_port_min: port must be greater than or equal to %d"),
                            filename, QEMU_REMOTE_PORT_MIN);
            virConfFree(conf);
            return -1;
        }
        driver->remotePortMin = p->l;
    }

    p = virConfGetValue (conf, "remote_display_port_max");
    CHECK_TYPE ("remote_display_port_max", VIR_CONF_LONG);
    if (p) {
        if (p->l > QEMU_REMOTE_PORT_MAX ||
            p->l < driver->remotePortMin) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("%s: remote_display_port_max: port must be between the minimal port and %d"),
                            filename, QEMU_REMOTE_PORT_MAX);
            virConfFree(conf);
            return -1;
        }
        /* increasing the value by 1 makes all the loops going through
        the bitmap (i = remotePortMin; i < remotePortMax; i++), work as
        expected. */
        driver->remotePortMax = p->l + 1;
    }

    if (driver->remotePortMin > driver->remotePortMax) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("%s: remote_display_port_min: min port must not be greater than max port"),
                        filename);
        virConfFree(conf);
        return -1;
    }

345 346 347
    p = virConfGetValue (conf, "user");
    CHECK_TYPE ("user", VIR_CONF_STRING);
    if (!(user = strdup(p && p->str ? p->str : QEMU_USER))) {
348
        virReportOOMError();
349 350 351
        virConfFree(conf);
        return -1;
    }
352
    if (virGetUserID(user, &driver->user) < 0) {
353 354 355 356 357 358
        VIR_FREE(user);
        virConfFree(conf);
        return -1;
    }
    VIR_FREE(user);

359

360 361 362
    p = virConfGetValue (conf, "group");
    CHECK_TYPE ("group", VIR_CONF_STRING);
    if (!(group = strdup(p && p->str ? p->str : QEMU_GROUP))) {
363
        virReportOOMError();
364 365 366
        virConfFree(conf);
        return -1;
    }
367
    if (virGetGroupID(group, &driver->group) < 0) {
368 369 370 371 372 373
        VIR_FREE(group);
        virConfFree(conf);
        return -1;
    }
    VIR_FREE(group);

374 375 376 377 378 379

    p = virConfGetValue (conf, "dynamic_ownership");
    CHECK_TYPE ("dynamic_ownership", VIR_CONF_LONG);
    if (p) driver->dynamicOwnership = p->l;


380 381 382 383 384 385 386
    p = virConfGetValue (conf, "cgroup_controllers");
    CHECK_TYPE ("cgroup_controllers", VIR_CONF_LIST);
    if (p) {
        virConfValuePtr pp;
        for (i = 0, pp = p->list; pp; ++i, pp = pp->next) {
            int ctl;
            if (pp->type != VIR_CONF_STRING) {
387
                VIR_ERROR(_("cgroup_controllers must be a list of strings"));
388 389 390 391 392
                virConfFree(conf);
                return -1;
            }
            ctl = virCgroupControllerTypeFromString(pp->str);
            if (ctl < 0) {
393
                VIR_ERROR(_("Unknown cgroup controller '%s'"), pp->str);
394 395 396 397 398 399 400 401
                virConfFree(conf);
                return -1;
            }
            driver->cgroupControllers |= (1 << ctl);
        }
    } else {
        driver->cgroupControllers =
            (1 << VIR_CGROUP_CONTROLLER_CPU) |
402
            (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
403
            (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
H
Hu Tao 已提交
404
            (1 << VIR_CGROUP_CONTROLLER_BLKIO) |
405 406
            (1 << VIR_CGROUP_CONTROLLER_CPUSET) |
            (1 << VIR_CGROUP_CONTROLLER_CPUACCT);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    }
    for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
        if (driver->cgroupControllers & (1 << i)) {
            VIR_INFO("Configured cgroup controller '%s'",
                     virCgroupControllerTypeToString(i));
        }
    }

    p = virConfGetValue (conf, "cgroup_device_acl");
    CHECK_TYPE ("cgroup_device_acl", VIR_CONF_LIST);
    if (p) {
        int len = 0;
        virConfValuePtr pp;
        for (pp = p->list; pp; pp = pp->next)
            len++;
        if (VIR_ALLOC_N(driver->cgroupDeviceACL, 1+len) < 0) {
423
            virReportOOMError();
424 425 426 427 428
            virConfFree(conf);
            return -1;
        }
        for (i = 0, pp = p->list; pp; ++i, pp = pp->next) {
            if (pp->type != VIR_CONF_STRING) {
429
                VIR_ERROR(_("cgroup_device_acl must be a list of strings"));
430 431 432 433 434
                virConfFree(conf);
                return -1;
            }
            driver->cgroupDeviceACL[i] = strdup (pp->str);
            if (driver->cgroupDeviceACL[i] == NULL) {
435
                virReportOOMError();
436 437 438 439 440 441 442 443
                virConfFree(conf);
                return -1;
            }

        }
        driver->cgroupDeviceACL[i] = NULL;
    }

444 445 446 447 448
    p = virConfGetValue (conf, "save_image_format");
    CHECK_TYPE ("save_image_format", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->saveImageFormat);
        if (!(driver->saveImageFormat = strdup(p->str))) {
449
            virReportOOMError();
450 451 452 453 454
            virConfFree(conf);
            return -1;
        }
    }

455 456 457 458 459 460 461 462 463 464 465
    p = virConfGetValue (conf, "dump_image_format");
    CHECK_TYPE ("dump_image_format", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->dumpImageFormat);
        if (!(driver->dumpImageFormat = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

H
Hu Tao 已提交
466 467 468 469 470 471 472 473 474 475 476
    p = virConfGetValue (conf, "auto_dump_path");
    CHECK_TYPE ("auto_dump_path", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->autoDumpPath);
        if (!(driver->autoDumpPath = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }

477 478 479 480 481 482 483 484
    p = virConfGetValue (conf, "auto_dump_bypass_cache");
    CHECK_TYPE ("auto_dump_bypass_cache", VIR_CONF_LONG);
    if (p) driver->autoDumpBypassCache = true;

    p = virConfGetValue (conf, "auto_start_bypass_cache");
    CHECK_TYPE ("auto_start_bypass_cache", VIR_CONF_LONG);
    if (p) driver->autoStartBypassCache = true;

485 486 487 488 489 490 491 492 493 494
    p = virConfGetValue (conf, "hugetlbfs_mount");
    CHECK_TYPE ("hugetlbfs_mount", VIR_CONF_STRING);
    if (p && p->str) {
        VIR_FREE(driver->hugetlbfs_mount);
        if (!(driver->hugetlbfs_mount = strdup(p->str))) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
    }
495

496 497
    p = virConfGetValue (conf, "mac_filter");
    CHECK_TYPE ("mac_filter", VIR_CONF_LONG);
498
    if (p && p->l) {
499 500 501
        driver->macFilter = p->l;
        if (!(driver->ebtables = ebtablesContextNew("qemu"))) {
            driver->macFilter = 0;
502
            virReportSystemError(errno,
S
Stefan Berger 已提交
503
                                 _("failed to enable mac filter in '%s'"),
504
                                 __FILE__);
505 506
            virConfFree(conf);
            return -1;
507 508 509
        }

        if ((errno = networkDisableAllFrames(driver))) {
510
            virReportSystemError(errno,
511 512
                         _("failed to add rule to drop all frames in '%s'"),
                                 __FILE__);
513 514
            virConfFree(conf);
            return -1;
515 516 517
        }
    }

518 519 520 521
    p = virConfGetValue (conf, "relaxed_acs_check");
    CHECK_TYPE ("relaxed_acs_check", VIR_CONF_LONG);
    if (p) driver->relaxedACS = p->l;

522 523 524 525
    p = virConfGetValue (conf, "vnc_allow_host_audio");
    CHECK_TYPE ("vnc_allow_host_audio", VIR_CONF_LONG);
    if (p) driver->vncAllowHostAudio = p->l;

526 527 528 529
    p = virConfGetValue (conf, "clear_emulator_capabilities");
    CHECK_TYPE ("clear_emulator_capabilities", VIR_CONF_LONG);
    if (p) driver->clearEmulatorCapabilities = p->l;

530 531 532 533
    p = virConfGetValue (conf, "allow_disk_format_probing");
    CHECK_TYPE ("allow_disk_format_probing", VIR_CONF_LONG);
    if (p) driver->allowDiskFormatProbing = p->l;

534 535 536 537
    p = virConfGetValue (conf, "set_process_name");
    CHECK_TYPE ("set_process_name", VIR_CONF_LONG);
    if (p) driver->setProcessName = p->l;

538 539 540 541
    p = virConfGetValue(conf, "max_processes");
    CHECK_TYPE("max_processes", VIR_CONF_LONG);
    if (p) driver->maxProcesses = p->l;

542 543 544 545
    p = virConfGetValue(conf, "max_files");
    CHECK_TYPE("max_files", VIR_CONF_LONG);
    if (p) driver->maxFiles = p->l;

546 547 548
    p = virConfGetValue (conf, "lock_manager");
    CHECK_TYPE ("lock_manager", VIR_CONF_STRING);
    if (p && p->str) {
549
        char *lockConf;
550
        virLockManagerPluginUnref(driver->lockManager);
551 552 553 554 555
        if (virAsprintf(&lockConf, "%s/libvirt/qemu-%s.conf", SYSCONFDIR, p->str) < 0) {
            virReportOOMError();
            virConfFree(conf);
            return -1;
        }
556
        if (!(driver->lockManager =
557
              virLockManagerPluginNew(p->str, lockConf, 0)))
558
            VIR_ERROR(_("Failed to load lock manager %s"), p->str);
559
        VIR_FREE(lockConf);
560 561
    }

562 563 564 565
    p = virConfGetValue(conf, "max_queued");
    CHECK_TYPE("max_queued", VIR_CONF_LONG);
    if (p) driver->max_queued = p->l;

566 567 568 569 570 571 572 573
    p = virConfGetValue(conf, "keepalive_interval");
    CHECK_TYPE("keepalive_interval", VIR_CONF_LONG);
    if (p) driver->keepAliveInterval = p->l;

    p = virConfGetValue(conf, "keepalive_count");
    CHECK_TYPE("keepalive_count", VIR_CONF_LONG);
    if (p) driver->keepAliveCount = p->l;

574 575 576 577
    p = virConfGetValue(conf, "seccomp_sandbox");
    CHECK_TYPE("seccomp_sandbox", VIR_CONF_LONG);
    if (p) driver->seccompSandbox = p->l;

D
Daniel P. Berrange 已提交
578 579 580
    virConfFree (conf);
    return 0;
}
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

static void
qemuDriverCloseCallbackFree(void *payload,
                            const void *name ATTRIBUTE_UNUSED)
{
    VIR_FREE(payload);
}

int
qemuDriverCloseCallbackInit(struct qemud_driver *driver)
{
    driver->closeCallbacks = virHashCreate(5, qemuDriverCloseCallbackFree);
    if (!driver->closeCallbacks)
        return -1;

    return 0;
}

void
qemuDriverCloseCallbackShutdown(struct qemud_driver *driver)
{
    virHashFree(driver->closeCallbacks);
}

int
qemuDriverCloseCallbackSet(struct qemud_driver *driver,
                           virDomainObjPtr vm,
                           virConnectPtr conn,
                           qemuDriverCloseCallback cb)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    qemuDriverCloseDefPtr closeDef;

    virUUIDFormat(vm->def->uuid, uuidstr);
    VIR_DEBUG("vm=%s, uuid=%s, conn=%p, cb=%p",
              vm->def->name, uuidstr, conn, cb);

    closeDef = virHashLookup(driver->closeCallbacks, uuidstr);
    if (closeDef) {
        if (closeDef->conn != conn) {
621 622 623 624
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Close callback for domain %s already registered"
                             " with another connection %p"),
                           vm->def->name, closeDef->conn);
625 626 627
            return -1;
        }
        if (closeDef->cb && closeDef->cb != cb) {
628 629 630
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Another close callback is already defined for"
                             " domain %s"), vm->def->name);
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
            return -1;
        }

        closeDef->cb = cb;
    } else {
        if (VIR_ALLOC(closeDef) < 0) {
            virReportOOMError();
            return -1;
        }

        closeDef->conn = conn;
        closeDef->cb = cb;
        if (virHashAddEntry(driver->closeCallbacks, uuidstr, closeDef) < 0) {
            VIR_FREE(closeDef);
            return -1;
        }
    }
    return 0;
}

int
qemuDriverCloseCallbackUnset(struct qemud_driver *driver,
                             virDomainObjPtr vm,
                             qemuDriverCloseCallback cb)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    qemuDriverCloseDefPtr closeDef;

    virUUIDFormat(vm->def->uuid, uuidstr);
    VIR_DEBUG("vm=%s, uuid=%s, cb=%p",
              vm->def->name, uuidstr, cb);

    closeDef = virHashLookup(driver->closeCallbacks, uuidstr);
    if (!closeDef)
        return -1;

    if (closeDef->cb && closeDef->cb != cb) {
668 669 670
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Trying to remove mismatching close callback for"
                         " domain %s"), vm->def->name);
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
        return -1;
    }

    return virHashRemoveEntry(driver->closeCallbacks, uuidstr);
}

qemuDriverCloseCallback
qemuDriverCloseCallbackGet(struct qemud_driver *driver,
                           virDomainObjPtr vm,
                           virConnectPtr conn)
{
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    qemuDriverCloseDefPtr closeDef;
    qemuDriverCloseCallback cb = NULL;

    virUUIDFormat(vm->def->uuid, uuidstr);
    VIR_DEBUG("vm=%s, uuid=%s, conn=%p",
              vm->def->name, uuidstr, conn);

    closeDef = virHashLookup(driver->closeCallbacks, uuidstr);
    if (closeDef && (!conn || closeDef->conn == conn))
        cb = closeDef->cb;

    VIR_DEBUG("cb=%p", cb);
    return cb;
}

struct qemuDriverCloseCallbackData {
    struct qemud_driver *driver;
    virConnectPtr conn;
};

static void
qemuDriverCloseCallbackRun(void *payload,
                           const void *name,
                           void *opaque)
{
    struct qemuDriverCloseCallbackData *data = opaque;
    qemuDriverCloseDefPtr closeDef = payload;
    const char *uuidstr = name;
    unsigned char uuid[VIR_UUID_BUFLEN];
    virDomainObjPtr dom;

    VIR_DEBUG("conn=%p, thisconn=%p, uuid=%s, cb=%p",
              closeDef->conn, data->conn, uuidstr, closeDef->cb);

    if (data->conn != closeDef->conn || !closeDef->cb)
        return;

    if (virUUIDParse(uuidstr, uuid) < 0) {
        VIR_WARN("Failed to parse %s", uuidstr);
        return;
    }

    if (!(dom = virDomainFindByUUID(&data->driver->domains, uuid))) {
        VIR_DEBUG("No domain object with UUID %s", uuidstr);
        return;
    }

    dom = closeDef->cb(data->driver, dom, data->conn);
    if (dom)
        virDomainObjUnlock(dom);

    virHashRemoveEntry(data->driver->closeCallbacks, uuidstr);
}

void
qemuDriverCloseCallbackRunAll(struct qemud_driver *driver,
                              virConnectPtr conn)
{
    struct qemuDriverCloseCallbackData data = {
        driver, conn
    };
    VIR_DEBUG("conn=%p", conn);

    virHashForEach(driver->closeCallbacks, qemuDriverCloseCallbackRun, &data);
}