uml_driver.c 59.6 KB
Newer Older
1 2 3
/*
 * uml_driver.c: core driver methods for managing UML guests
 *
4
 * Copyright (C) 2006-2010 Red Hat, Inc.
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
 * Copyright (C) 2006-2008 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/poll.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <paths.h>
#include <pwd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/inotify.h>
46
#include <sys/un.h>
47 48 49 50 51 52 53 54 55 56 57 58 59

#include "uml_driver.h"
#include "uml_conf.h"
#include "event.h"
#include "buf.h"
#include "util.h"
#include "nodeinfo.h"
#include "stats_linux.h"
#include "capabilities.h"
#include "memory.h"
#include "uuid.h"
#include "domain_conf.h"
#include "datatypes.h"
60
#include "logging.h"
61
#include "domain_nwfilter.h"
62

63 64
#define VIR_FROM_THIS VIR_FROM_UML

65 66 67
/* For storing short-lived temporary files. */
#define TEMPDIR LOCAL_STATE_DIR "/cache/libvirt"

68 69 70 71 72 73 74 75
typedef struct _umlDomainObjPrivate umlDomainObjPrivate;
typedef umlDomainObjPrivate *umlDomainObjPrivatePtr;
struct _umlDomainObjPrivate {
    int monitor;
    int monitorWatch;
};


76 77
static int umlShutdown(void);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static void *umlDomainObjPrivateAlloc(void)
{
    umlDomainObjPrivatePtr priv;

    if (VIR_ALLOC(priv) < 0)
        return NULL;

    priv->monitor = -1;
    priv->monitorWatch = -1;

    return priv;
}

static void umlDomainObjPrivateFree(void *data)
{
    umlDomainObjPrivatePtr priv = data;

    VIR_FREE(priv);
}


99 100
static void umlDriverLock(struct uml_driver *driver)
{
101
    virMutexLock(&driver->lock);
102 103 104
}
static void umlDriverUnlock(struct uml_driver *driver)
{
105
    virMutexUnlock(&driver->lock);
106 107
}

108

109
static int umlOpenMonitor(struct uml_driver *driver,
110
                          virDomainObjPtr vm);
111
static int umlReadPidFile(struct uml_driver *driver,
112 113 114 115 116 117 118 119 120 121 122
                          virDomainObjPtr vm);

static int umlSetCloseExec(int fd) {
    int flags;
    if ((flags = fcntl(fd, F_GETFD)) < 0)
        goto error;
    flags |= FD_CLOEXEC;
    if ((fcntl(fd, F_SETFD, flags)) < 0)
        goto error;
    return 0;
 error:
D
Daniel Veillard 已提交
123
    VIR_ERROR0(_("Failed to set close-on-exec file descriptor flag"));
124 125 126 127 128 129 130 131 132 133 134 135
    return -1;
}

static int umlStartVMDaemon(virConnectPtr conn,
                            struct uml_driver *driver,
                            virDomainObjPtr vm);

static void umlShutdownVMDaemon(virConnectPtr conn,
                                struct uml_driver *driver,
                                virDomainObjPtr vm);


136 137 138 139
static int umlMonitorCommand(const struct uml_driver *driver,
                             const virDomainObjPtr vm,
                             const char *cmd,
                             char **reply);
140 141 142

static struct uml_driver *uml_driver = NULL;

143 144 145 146 147 148 149 150 151 152 153 154 155
struct umlAutostartData {
    struct uml_driver *driver;
    virConnectPtr conn;
};

static void
umlAutostartDomain(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
{
    virDomainObjPtr vm = payload;
    const struct umlAutostartData *data = opaque;

    virDomainObjLock(vm);
    if (vm->autostart &&
D
Daniel P. Berrange 已提交
156
        !virDomainObjIsActive(vm)) {
157 158 159 160
        virResetLastError();
        if (umlStartVMDaemon(data->conn, data->driver, vm) < 0) {
            virErrorPtr err = virGetLastError();
            VIR_ERROR(_("Failed to autostart VM '%s': %s"),
161
                      vm->def->name, err ? err->message : _("unknown error"));
162 163 164 165
        }
    }
    virDomainObjUnlock(vm);
}
166 167 168

static void
umlAutostartConfigs(struct uml_driver *driver) {
169 170 171 172 173
    /* XXX: Figure out a better way todo this. The domain
     * startup code needs a connection handle in order
     * to lookup the bridge associated with a virtual
     * network
     */
174 175 176
    virConnectPtr conn = virConnectOpen(driver->privileged ?
                                        "uml:///system" :
                                        "uml:///session");
177
    /* Ignoring NULL conn which is mostly harmless here */
178

179 180 181
    struct umlAutostartData data = { driver, conn };

    virHashForEach(driver->domains.objs, umlAutostartDomain, &data);
182

183 184
    if (conn)
        virConnectClose(conn);
185 186 187 188
}


static int
189
umlIdentifyOneChrPTY(struct uml_driver *driver,
190 191 192 193 194 195 196
                     virDomainObjPtr dom,
                     virDomainChrDefPtr def,
                     const char *dev)
{
    char *cmd;
    char *res = NULL;
    int retries = 0;
197
    if (virAsprintf(&cmd, "config %s%d", dev, def->target.port) < 0) {
198
        virReportOOMError();
199 200 201
        return -1;
    }
requery:
202
    if (umlMonitorCommand(driver, dom, cmd, &res) < 0)
203
        return -1;
204

205
    if (res && STRPREFIX(res, "pts:")) {
206 207
        VIR_FREE(def->data.file.path);
        if ((def->data.file.path = strdup(res + 4)) == NULL) {
208
            virReportOOMError();
209 210 211 212
            VIR_FREE(res);
            VIR_FREE(cmd);
            return -1;
        }
213
    } else if (!res || STRPREFIX(res, "pts")) {
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        /* It can take a while to startup, so retry for
           upto 5 seconds */
        /* XXX should do this in a better non-blocking
           way somehow ...perhaps register a timer */
        if (retries++ < 50) {
            usleep(1000*10);
            goto requery;
        }
    }

    VIR_FREE(cmd);
    VIR_FREE(res);
    return 0;
}

static int
230
umlIdentifyChrPTY(struct uml_driver *driver,
231 232 233 234 235 236
                  virDomainObjPtr dom)
{
    int i;

    if (dom->def->console &&
        dom->def->console->type == VIR_DOMAIN_CHR_TYPE_PTY)
237
        if (umlIdentifyOneChrPTY(driver, dom,
238 239 240 241 242
                                 dom->def->console, "con") < 0)
            return -1;

    for (i = 0 ; i < dom->def->nserials; i++)
        if (dom->def->serials[i]->type == VIR_DOMAIN_CHR_TYPE_PTY &&
243
            umlIdentifyOneChrPTY(driver, dom,
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                                 dom->def->serials[i], "ssl") < 0)
            return -1;

    return 0;
}

static void
umlInotifyEvent(int watch,
                int fd,
                int events ATTRIBUTE_UNUSED,
                void *data)
{
    char buf[1024];
    struct inotify_event *e;
    int got;
    char *tmp, *name;
    struct uml_driver *driver = data;
    virDomainObjPtr dom;

263
    umlDriverLock(driver);
264
    if (watch != driver->inotifyWatch)
265
        goto cleanup;
266 267 268 269 270 271

reread:
    got = read(fd, buf, sizeof(buf));
    if (got == -1) {
        if (errno == EINTR)
            goto reread;
272
        goto cleanup;
273 274 275 276 277
    }

    tmp = buf;
    while (got) {
        if (got < sizeof(struct inotify_event))
278
            goto cleanup; /* bad */
279 280 281 282 283 284

        e = (struct inotify_event *)tmp;
        tmp += sizeof(struct inotify_event);
        got -= sizeof(struct inotify_event);

        if (got < e->len)
285
            goto cleanup;
286 287 288 289 290 291 292 293 294 295 296 297 298

        tmp += e->len;
        got -= e->len;

        name = (char *)&(e->name);

        dom = virDomainFindByName(&driver->domains, name);

        if (!dom) {
            continue;
        }

        if (e->mask & IN_DELETE) {
299
            VIR_DEBUG("Got inotify domain shutdown '%s'", name);
D
Daniel P. Berrange 已提交
300
            if (!virDomainObjIsActive(dom)) {
301
                virDomainObjUnlock(dom);
302 303 304
                continue;
            }

305
            umlShutdownVMDaemon(NULL, driver, dom);
306
        } else if (e->mask & (IN_CREATE | IN_MODIFY)) {
307
            VIR_DEBUG("Got inotify domain startup '%s'", name);
D
Daniel P. Berrange 已提交
308
            if (virDomainObjIsActive(dom)) {
309
                virDomainObjUnlock(dom);
310 311 312
                continue;
            }

313
            if (umlReadPidFile(driver, dom) < 0) {
314
                virDomainObjUnlock(dom);
315 316 317 318 319 320
                continue;
            }

            dom->def->id = driver->nextvmid++;
            dom->state = VIR_DOMAIN_RUNNING;

321
            if (umlOpenMonitor(driver, dom) < 0) {
322
                VIR_WARN0("Could not open monitor for new domain");
323
                umlShutdownVMDaemon(NULL, driver, dom);
324
            } else if (umlIdentifyChrPTY(driver, dom) < 0) {
325
                VIR_WARN0("Could not identify charater devices for new domain");
326
                umlShutdownVMDaemon(NULL, driver, dom);
327
            }
328
        }
329
        virDomainObjUnlock(dom);
330
    }
331 332 333

cleanup:
    umlDriverUnlock(driver);
334 335 336 337 338 339 340 341
}

/**
 * umlStartup:
 *
 * Initialization function for the Uml daemon
 */
static int
342
umlStartup(int privileged) {
343 344 345
    uid_t uid = geteuid();
    char *base = NULL;
    char driverConf[PATH_MAX];
346
    char *userdir = NULL;
347 348 349 350

    if (VIR_ALLOC(uml_driver) < 0)
        return -1;

351 352
    uml_driver->privileged = privileged;

353 354 355 356
    if (virMutexInit(&uml_driver->lock) < 0) {
        VIR_FREE(uml_driver);
        return -1;
    }
357 358
    umlDriverLock(uml_driver);

359 360
    /* Don't have a dom0 so start from 1 */
    uml_driver->nextvmid = 1;
361
    uml_driver->inotifyWatch = -1;
362

363 364 365
    if (virDomainObjListInit(&uml_driver->domains) < 0)
        goto error;

366
    userdir = virGetUserDirectory(uid);
367
    if (!userdir)
368
        goto error;
369

370
    if (privileged) {
371 372
        if (virAsprintf(&uml_driver->logDir,
                        "%s/log/libvirt/uml", LOCAL_STATE_DIR) == -1)
373 374 375 376
            goto out_of_memory;

        if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
            goto out_of_memory;
377 378 379 380

        if (virAsprintf(&uml_driver->monitorDir,
                        "%s/run/libvirt/uml-guest", LOCAL_STATE_DIR) == -1)
            goto out_of_memory;
381
    } else {
382

383
        if (virAsprintf(&uml_driver->logDir,
384
                        "%s/.libvirt/uml/log", userdir) == -1)
385 386
            goto out_of_memory;

387
        if (virAsprintf(&base, "%s/.libvirt", userdir) == -1)
388 389
            goto out_of_memory;

390 391 392 393
        if (virAsprintf(&uml_driver->monitorDir,
                        "%s/.uml", userdir) == -1)
            goto out_of_memory;
    }
394 395 396 397 398 399 400 401

    /* Configuration paths are either ~/.libvirt/uml/... (session) or
     * /etc/libvirt/uml/... (system).
     */
    if (snprintf (driverConf, sizeof(driverConf), "%s/uml.conf", base) == -1)
        goto out_of_memory;
    driverConf[sizeof(driverConf)-1] = '\0';

402
    if (virAsprintf(&uml_driver->configDir, "%s/uml", base) == -1)
403 404
        goto out_of_memory;

405
    if (virAsprintf(&uml_driver->autostartDir, "%s/uml/autostart", base) == -1)
406 407 408 409 410 411 412
        goto out_of_memory;

    VIR_FREE(base);

    if ((uml_driver->caps = umlCapsInit()) == NULL)
        goto out_of_memory;

413 414
    uml_driver->caps->privateDataAllocFunc = umlDomainObjPrivateAlloc;
    uml_driver->caps->privateDataFreeFunc = umlDomainObjPrivateFree;
415 416

    if ((uml_driver->inotifyFD = inotify_init()) < 0) {
D
Daniel Veillard 已提交
417
        VIR_ERROR0(_("cannot initialize inotify"));
418
        goto error;
419 420
    }

L
Laine Stump 已提交
421
    if (virFileMakePath(uml_driver->monitorDir) != 0) {
422
        char ebuf[1024];
D
Daniel Veillard 已提交
423
        VIR_ERROR(_("Failed to create monitor directory %s: %s"),
424
               uml_driver->monitorDir, virStrerror(errno, ebuf, sizeof ebuf));
425
        goto error;
426 427
    }

428
    VIR_INFO("Adding inotify watch on %s", uml_driver->monitorDir);
429 430 431
    if (inotify_add_watch(uml_driver->inotifyFD,
                          uml_driver->monitorDir,
                          IN_CREATE | IN_MODIFY | IN_DELETE) < 0) {
432
        goto error;
433 434
    }

435 436
    if ((uml_driver->inotifyWatch =
         virEventAddHandle(uml_driver->inotifyFD, POLLIN,
437 438
                           umlInotifyEvent, uml_driver, NULL)) < 0)
        goto error;
439

440
    if (virDomainLoadAllConfigs(uml_driver->caps,
441 442 443
                                &uml_driver->domains,
                                uml_driver->configDir,
                                uml_driver->autostartDir,
444
                                0, NULL, NULL) < 0)
445 446
        goto error;

447 448
    umlAutostartConfigs(uml_driver);

449
    umlDriverUnlock(uml_driver);
450 451
    VIR_FREE(userdir);

452 453
    return 0;

454
out_of_memory:
D
Daniel Veillard 已提交
455
    VIR_ERROR0(_("umlStartup: out of memory"));
456 457

error:
458
    VIR_FREE(userdir);
459
    VIR_FREE(base);
460 461
    umlDriverUnlock(uml_driver);
    umlShutdown();
462 463 464 465 466 467 468 469 470 471 472 473 474 475
    return -1;
}

/**
 * umlReload:
 *
 * Function to restart the Uml daemon, it will recheck the configuration
 * files and update its state and the networking
 */
static int
umlReload(void) {
    if (!uml_driver)
        return 0;

476
    umlDriverLock(uml_driver);
477
    virDomainLoadAllConfigs(uml_driver->caps,
478 479 480
                            &uml_driver->domains,
                            uml_driver->configDir,
                            uml_driver->autostartDir,
481
                            0, NULL, NULL);
482 483

    umlAutostartConfigs(uml_driver);
484
    umlDriverUnlock(uml_driver);
485 486 487 488 489 490 491 492 493 494 495 496 497 498

    return 0;
}

/**
 * umlActive:
 *
 * Checks if the Uml daemon is active, i.e. has an active domain or
 * an active network
 *
 * Returns 1 if active, 0 otherwise
 */
static int
umlActive(void) {
499
    int active = 0;
500 501 502 503

    if (!uml_driver)
        return 0;

504
    umlDriverLock(uml_driver);
505
    active = virDomainObjListNumOfDomains(&uml_driver->domains, 1);
506
    umlDriverUnlock(uml_driver);
507

508
    return active;
509 510
}

511 512 513 514 515 516 517
static void
umlShutdownOneVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
{
    virDomainObjPtr dom = payload;
    struct uml_driver *driver = opaque;

    virDomainObjLock(dom);
D
Daniel P. Berrange 已提交
518
    if (virDomainObjIsActive(dom))
519 520 521 522
        umlShutdownVMDaemon(NULL, driver, dom);
    virDomainObjUnlock(dom);
}

523 524 525 526 527 528 529 530 531 532
/**
 * umlShutdown:
 *
 * Shutdown the Uml daemon, it will stop all active domains and networks
 */
static int
umlShutdown(void) {
    if (!uml_driver)
        return -1;

533
    umlDriverLock(uml_driver);
534 535
    if (uml_driver->inotifyWatch != -1)
        virEventRemoveHandle(uml_driver->inotifyWatch);
536 537 538
    close(uml_driver->inotifyFD);
    virCapabilitiesFree(uml_driver->caps);

539 540 541
    /* shutdown active VMs
     * XXX allow them to stay around & reconnect */
    virHashForEach(uml_driver->domains.objs, umlShutdownOneVM, uml_driver);
542

543
    virDomainObjListDeinit(&uml_driver->domains);
544 545 546 547 548 549 550 551 552

    VIR_FREE(uml_driver->logDir);
    VIR_FREE(uml_driver->configDir);
    VIR_FREE(uml_driver->autostartDir);
    VIR_FREE(uml_driver->monitorDir);

    if (uml_driver->brctl)
        brShutdown(uml_driver->brctl);

553
    umlDriverUnlock(uml_driver);
554
    virMutexDestroy(&uml_driver->lock);
555 556 557 558 559 560
    VIR_FREE(uml_driver);

    return 0;
}


561
static int umlReadPidFile(struct uml_driver *driver,
562 563 564 565 566 567 568 569
                          virDomainObjPtr vm)
{
    int rc = -1;
    FILE *file;
    char *pidfile = NULL;
    int retries = 0;

    vm->pid = -1;
570 571
    if (virAsprintf(&pidfile, "%s/%s/pid",
                    driver->monitorDir, vm->def->name) < 0) {
572
        virReportOOMError();
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
        return -1;
    }

reopen:
    if (!(file = fopen(pidfile, "r"))) {
        if (errno == ENOENT &&
            retries++ < 50) {
            usleep(1000 * 100);
            goto reopen;
        }
        goto cleanup;
    }

    if (fscanf(file, "%d", &vm->pid) != 1) {
        errno = EINVAL;
588
        fclose(file);
589 590 591 592 593 594 595 596 597 598
        goto cleanup;
    }

    if (fclose(file) < 0)
        goto cleanup;

    rc = 0;

 cleanup:
    if (rc != 0)
599
        virReportSystemError(errno,
600 601
                             _("failed to read pid: %s"),
                             pidfile);
602 603 604 605
    VIR_FREE(pidfile);
    return rc;
}

606
static int umlMonitorAddress(const struct uml_driver *driver,
607 608 609
                             virDomainObjPtr vm,
                             struct sockaddr_un *addr) {
    char *sockname;
C
Chris Lalancette 已提交
610
    int retval = 0;
611

612 613
    if (virAsprintf(&sockname, "%s/%s/mconsole",
                    driver->monitorDir, vm->def->name) < 0) {
614
        virReportOOMError();
615 616 617 618 619
        return -1;
    }

    memset(addr, 0, sizeof *addr);
    addr->sun_family = AF_UNIX;
C
Chris Lalancette 已提交
620
    if (virStrcpyStatic(addr->sun_path, sockname) == NULL) {
621
        umlReportError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
622 623 624
                       _("Unix path %s too long for destination"), sockname);
        retval = -1;
    }
625
    VIR_FREE(sockname);
C
Chris Lalancette 已提交
626
    return retval;
627 628
}

629
static int umlOpenMonitor(struct uml_driver *driver,
630 631 632 633
                          virDomainObjPtr vm) {
    struct sockaddr_un addr;
    struct stat sb;
    int retries = 0;
634
    umlDomainObjPrivatePtr priv = vm->privateData;
635

636
    if (umlMonitorAddress(driver, vm, &addr) < 0)
637 638
        return -1;

639
    VIR_DEBUG("Dest address for monitor is '%s'", addr.sun_path);
640 641 642
restat:
    if (stat(addr.sun_path, &sb) < 0) {
        if (errno == ENOENT &&
643
            retries++ < 50) {
644 645 646 647 648 649
            usleep(1000 * 100);
            goto restat;
        }
        return -1;
    }

650
    if ((priv->monitor = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
651
        virReportSystemError(errno,
652
                             "%s", _("cannot open socket"));
653 654 655 656
        return -1;
    }

    memset(addr.sun_path, 0, sizeof addr.sun_path);
657 658
    sprintf(addr.sun_path + 1, "libvirt-uml-%u", vm->pid);
    VIR_DEBUG("Reply address for monitor is '%s'", addr.sun_path+1);
659
    if (bind(priv->monitor, (struct sockaddr *)&addr, sizeof addr) < 0) {
660
        virReportSystemError(errno,
661
                             "%s", _("cannot bind socket"));
662 663
        close(priv->monitor);
        priv->monitor = -1;
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
        return -1;
    }

    return 0;
}


#define MONITOR_MAGIC 0xcafebabe
#define MONITOR_BUFLEN 512
#define MONITOR_VERSION 2

struct monitor_request {
    uint32_t magic;
    uint32_t version;
    uint32_t length;
    char data[MONITOR_BUFLEN];
};

struct monitor_response {
    uint32_t error;
    uint32_t extra;
    uint32_t length;
    char data[MONITOR_BUFLEN];
};


690
static int umlMonitorCommand(const struct uml_driver *driver,
691 692 693 694 695 696 697 698 699 700
                             const virDomainObjPtr vm,
                             const char *cmd,
                             char **reply)
{
    struct monitor_request req;
    struct monitor_response res;
    char *retdata = NULL;
    int retlen = 0, ret = 0;
    struct sockaddr_un addr;
    unsigned int addrlen;
701
    umlDomainObjPrivatePtr priv = vm->privateData;
702

703 704
    VIR_DEBUG("Run command '%s'", cmd);

705 706
    *reply = NULL;

707
    if (umlMonitorAddress(driver, vm, &addr) < 0)
708 709 710 711 712 713 714
        return -1;

    memset(&req, 0, sizeof(req));
    req.magic = MONITOR_MAGIC;
    req.version = MONITOR_VERSION;
    req.length = strlen(cmd);
    if (req.length > (MONITOR_BUFLEN-1)) {
715
        virReportSystemError(EINVAL,
716 717
                             _("cannot send too long command %s (%d bytes)"),
                             cmd, req.length);
718 719
        return -1;
    }
C
Chris Lalancette 已提交
720
    if (virStrcpyStatic(req.data, cmd) == NULL) {
721
        umlReportError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
722 723 724
                       _("Command %s too long for destination"), cmd);
        return -1;
    }
725

726
    if (sendto(priv->monitor, &req, sizeof req, 0,
727
               (struct sockaddr *)&addr, sizeof addr) != (sizeof req)) {
728
        virReportSystemError(errno,
729 730
                             _("cannot send command %s"),
                             cmd);
731 732 733 734
        return -1;
    }

    do {
E
Eric Blake 已提交
735
        ssize_t nbytes;
736
        addrlen = sizeof(addr);
E
Eric Blake 已提交
737
        nbytes = recvfrom(priv->monitor, &res, sizeof res, 0,
738
                          (struct sockaddr *)&addr, &addrlen);
E
Eric Blake 已提交
739 740 741
        if (nbytes < 0) {
            if (errno == EAGAIN || errno == EINTR)
                continue;
742
            virReportSystemError(errno, _("cannot read reply %s"), cmd);
743 744
            goto error;
        }
745 746 747
        /* Ensure res.length is safe to read before validating its value.  */
        if (nbytes < offsetof(struct monitor_request, data) ||
            nbytes < offsetof(struct monitor_request, data) + res.length) {
748 749 750
            virReportSystemError(0, _("incomplete reply %s"), cmd);
            goto error;
        }
751 752

        if (VIR_REALLOC_N(retdata, retlen + res.length) < 0) {
753
            virReportOOMError();
754 755 756 757 758 759 760 761 762 763 764
            goto error;
        }
        memcpy(retdata + retlen, res.data, res.length);
        retlen += res.length - 1;
        retdata[retlen] = '\0';

        if (res.error)
            ret = -1;

    } while (res.extra);

765 766
    VIR_DEBUG("Command reply is '%s'", NULLSTR(retdata));

767 768 769 770
    if (ret < 0)
        VIR_FREE(retdata);
    else
        *reply = retdata;
771 772 773 774 775 776 777 778 779

    return ret;

error:
    VIR_FREE(retdata);
    return -1;
}


780 781 782 783 784 785
static int umlCleanupTapDevices(virConnectPtr conn ATTRIBUTE_UNUSED,
                                virDomainObjPtr vm) {
    int i;
    int err;
    int ret = 0;
    brControl *brctl = NULL;
786
    VIR_ERROR0(_("Cleanup tap"));
787 788 789 790 791 792 793 794 795 796
    if (brInit(&brctl) < 0)
        return -1;

    for (i = 0 ; i < vm->def->nnets ; i++) {
        virDomainNetDefPtr def = vm->def->nets[i];

        if (def->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
            def->type != VIR_DOMAIN_NET_TYPE_NETWORK)
            continue;

797
        VIR_ERROR(_("Cleanup '%s'"), def->ifname);
798 799
        err = brDeleteTap(brctl, def->ifname);
        if (err) {
800
            VIR_ERROR(_("Cleanup failed %d"), err);
801 802 803
            ret = -1;
        }
    }
804
    VIR_ERROR0(_("Cleanup tap done"));
805 806 807 808
    brShutdown(brctl);
    return ret;
}

809 810 811 812 813
static int umlStartVMDaemon(virConnectPtr conn,
                            struct uml_driver *driver,
                            virDomainObjPtr vm) {
    const char **argv = NULL, **tmp;
    const char **progenv = NULL;
814 815
    int i, ret;
    pid_t pid;
816 817 818 819
    char *logfile;
    int logfd = -1;
    struct stat sb;
    fd_set keepfd;
820
    char ebuf[1024];
821
    umlDomainObjPrivatePtr priv = vm->privateData;
822 823 824

    FD_ZERO(&keepfd);

D
Daniel P. Berrange 已提交
825
    if (virDomainObjIsActive(vm)) {
826 827
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("VM is already active"));
828 829 830 831
        return -1;
    }

    if (!vm->def->os.kernel) {
832 833
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no kernel specified"));
834 835 836 837 838 839 840
        return -1;
    }
    /* Make sure the binary we are about to try exec'ing exists.
     * Technically we could catch the exec() failure, but that's
     * in a sub-process so its hard to feed back a useful error
     */
    if (stat(vm->def->os.kernel, &sb) < 0) {
841
        virReportSystemError(errno,
842 843
                             _("Cannot find UML kernel %s"),
                             vm->def->os.kernel);
844 845 846
        return -1;
    }

L
Laine Stump 已提交
847
    if (virFileMakePath(driver->logDir) != 0) {
848
        virReportSystemError(errno,
849 850
                             _("cannot create log directory %s"),
                             driver->logDir);
851 852 853
        return -1;
    }

854 855
    if (virAsprintf(&logfile, "%s/%s.log",
                    driver->logDir, vm->def->name) < 0) {
856
        virReportOOMError();
857 858 859 860 861
        return -1;
    }

    if ((logfd = open(logfile, O_CREAT | O_TRUNC | O_WRONLY,
                      S_IRUSR | S_IWUSR)) < 0) {
862
        virReportSystemError(errno,
863 864
                             _("failed to create logfile %s"),
                             logfile);
865 866 867 868 869 870
        VIR_FREE(logfile);
        return -1;
    }
    VIR_FREE(logfile);

    if (umlSetCloseExec(logfd) < 0) {
871
        virReportSystemError(errno,
872
                             "%s", _("Unable to set VM logfile close-on-exec flag"));
873 874 875 876
        close(logfd);
        return -1;
    }

877
    if (umlBuildCommandLine(conn, driver, vm, &keepfd,
878
                            &argv, &progenv) < 0) {
879
        close(logfd);
880
        virDomainConfVMNWFilterTeardown(vm);
881
        umlCleanupTapDevices(conn, vm);
882 883 884 885 886 887
        return -1;
    }

    tmp = progenv;
    while (*tmp) {
        if (safewrite(logfd, *tmp, strlen(*tmp)) < 0)
888
            VIR_WARN("Unable to write envv to logfile: %s",
889
                   virStrerror(errno, ebuf, sizeof ebuf));
890
        if (safewrite(logfd, " ", 1) < 0)
891
            VIR_WARN("Unable to write envv to logfile: %s",
892
                   virStrerror(errno, ebuf, sizeof ebuf));
893 894 895 896 897
        tmp++;
    }
    tmp = argv;
    while (*tmp) {
        if (safewrite(logfd, *tmp, strlen(*tmp)) < 0)
898
            VIR_WARN("Unable to write argv to logfile: %s",
899
                   virStrerror(errno, ebuf, sizeof ebuf));
900
        if (safewrite(logfd, " ", 1) < 0)
901
            VIR_WARN("Unable to write argv to logfile: %s",
902
                   virStrerror(errno, ebuf, sizeof ebuf));
903 904 905
        tmp++;
    }
    if (safewrite(logfd, "\n", 1) < 0)
906
        VIR_WARN("Unable to write argv to logfile: %s",
907
                 virStrerror(errno, ebuf, sizeof ebuf));
908

909
    priv->monitor = -1;
910

911
    ret = virExecDaemonize(argv, progenv, &keepfd, &pid,
912
                           -1, &logfd, &logfd,
913 914
                           VIR_EXEC_CLEAR_CAPS,
                           NULL, NULL, NULL);
915 916
    close(logfd);

917 918 919 920 921 922 923 924
    /*
     * At the moment, the only thing that populates keepfd is
     * umlBuildCommandLineChr. We want to close every fd it opens.
     */
    for (i = 0; i < FD_SETSIZE; i++)
        if (FD_ISSET(i, &keepfd))
            close(i);

925 926 927 928 929 930 931 932
    for (i = 0 ; argv[i] ; i++)
        VIR_FREE(argv[i]);
    VIR_FREE(argv);

    for (i = 0 ; progenv[i] ; i++)
        VIR_FREE(progenv[i]);
    VIR_FREE(progenv);

933 934
    if (ret < 0) {
        virDomainConfVMNWFilterTeardown(vm);
935
        umlCleanupTapDevices(conn, vm);
936 937
    }

938 939 940

    /* NB we don't mark it running here - we do that async
       with inotify */
941 942 943
    /* XXX what if someone else tries to start it again
       before we get the inotification ? Sounds like
       trouble.... */
944 945 946 947 948 949 950 951 952

    return ret;
}

static void umlShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED,
                                struct uml_driver *driver ATTRIBUTE_UNUSED,
                                virDomainObjPtr vm)
{
    int ret;
953 954
    umlDomainObjPrivatePtr priv = vm->privateData;

D
Daniel P. Berrange 已提交
955
    if (!virDomainObjIsActive(vm))
956 957
        return;

958
    virKillProcess(vm->pid, SIGTERM);
959

960 961 962
    if (priv->monitor != -1)
        close(priv->monitor);
    priv->monitor = -1;
963 964

    if ((ret = waitpid(vm->pid, NULL, 0)) != vm->pid) {
965
        VIR_WARN("Got unexpected pid %d != %d",
966 967 968 969 970 971 972
               ret, vm->pid);
    }

    vm->pid = -1;
    vm->def->id = -1;
    vm->state = VIR_DOMAIN_SHUTOFF;

973
    virDomainConfVMNWFilterTeardown(vm);
974 975
    umlCleanupTapDevices(conn, vm);

976 977 978 979 980 981 982 983 984 985 986 987
    if (vm->newDef) {
        virDomainDefFree(vm->def);
        vm->def = vm->newDef;
        vm->def->id = -1;
        vm->newDef = NULL;
    }
}


static virDrvOpenStatus umlOpen(virConnectPtr conn,
                                virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                int flags ATTRIBUTE_UNUSED) {
988 989 990
    if (conn->uri == NULL) {
        if (uml_driver == NULL)
            return VIR_DRV_OPEN_DECLINED;
991

992
        conn->uri = xmlParseURI(uml_driver->privileged ?
993 994 995
                                "uml:///system" :
                                "uml:///session");
        if (!conn->uri) {
996
            virReportOOMError();
997 998 999 1000 1001 1002 1003 1004 1005 1006
            return VIR_DRV_OPEN_ERROR;
        }
    } else {
        if (conn->uri->scheme == NULL ||
            STRNEQ (conn->uri->scheme, "uml"))
            return VIR_DRV_OPEN_DECLINED;

        /* Allow remote driver to deal with URIs with hostname server */
        if (conn->uri->server != NULL)
            return VIR_DRV_OPEN_DECLINED;
1007 1008


1009
        /* Check path and tell them correct path if they made a mistake */
1010
        if (uml_driver->privileged) {
1011
            if (STRNEQ (conn->uri->path, "/system") &&
1012
                STRNEQ (conn->uri->path, "/session")) {
1013
                umlReportError(VIR_ERR_INTERNAL_ERROR,
1014 1015 1016 1017 1018 1019
                               _("unexpected UML URI path '%s', try uml:///system"),
                               conn->uri->path);
                return VIR_DRV_OPEN_ERROR;
            }
        } else {
            if (STRNEQ (conn->uri->path, "/session")) {
1020
                umlReportError(VIR_ERR_INTERNAL_ERROR,
1021 1022 1023 1024
                               _("unexpected UML URI path '%s', try uml:///session"),
                               conn->uri->path);
                return VIR_DRV_OPEN_ERROR;
            }
1025
        }
1026 1027 1028

        /* URI was good, but driver isn't active */
        if (uml_driver == NULL) {
1029
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1030
                           _("uml state driver is not active"));
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
            return VIR_DRV_OPEN_ERROR;
        }
    }

    conn->privateData = uml_driver;

    return VIR_DRV_OPEN_SUCCESS;
}

static int umlClose(virConnectPtr conn) {
1041
    /*struct uml_driver *driver = conn->privateData;*/
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

    conn->privateData = NULL;

    return 0;
}

static const char *umlGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
    return "UML";
}


1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
static int umlIsSecure(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Trivially secure, since always inside the daemon */
    return 1;
}


static int umlIsEncrypted(virConnectPtr conn ATTRIBUTE_UNUSED)
{
    /* Not encrypted, but remote driver takes care of that */
    return 0;
}


1067 1068 1069 1070
static char *umlGetCapabilities(virConnectPtr conn) {
    struct uml_driver *driver = (struct uml_driver *)conn->privateData;
    char *xml;

1071
    umlDriverLock(driver);
1072
    if ((xml = virCapabilitiesFormatXML(driver->caps)) == NULL)
1073
        virReportOOMError();
1074
    umlDriverUnlock(driver);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

    return xml;
}



static int umlGetProcessInfo(unsigned long long *cpuTime, int pid) {
    char proc[PATH_MAX];
    FILE *pidinfo;
    unsigned long long usertime, systime;

    if (snprintf(proc, sizeof(proc), "/proc/%d/stat", pid) >= (int)sizeof(proc)) {
        return -1;
    }

    if (!(pidinfo = fopen(proc, "r"))) {
        /* VM probably shut down, so fake 0 */
        *cpuTime = 0;
        return 0;
    }

    if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu", &usertime, &systime) != 2) {
        umlDebug("not enough arg");
1098
        fclose(pidinfo);
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
        return -1;
    }

    /* We got jiffies
     * We want nanoseconds
     * _SC_CLK_TCK is jiffies per second
     * So calulate thus....
     */
    *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime) / (unsigned long long)sysconf(_SC_CLK_TCK);

    umlDebug("Got %llu %llu %llu", usertime, systime, *cpuTime);

    fclose(pidinfo);

    return 0;
}


static virDomainPtr umlDomainLookupByID(virConnectPtr conn,
                                          int id) {
    struct uml_driver *driver = (struct uml_driver *)conn->privateData;
1120 1121
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1122

1123
    umlDriverLock(driver);
1124
    vm = virDomainFindByID(&driver->domains, id);
1125 1126
    umlDriverUnlock(driver);

1127
    if (!vm) {
1128
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1129
        goto cleanup;
1130 1131 1132 1133
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) dom->id = vm->def->id;
1134 1135

cleanup:
1136 1137
    if (vm)
        virDomainObjUnlock(vm);
1138 1139
    return dom;
}
1140

1141 1142 1143
static virDomainPtr umlDomainLookupByUUID(virConnectPtr conn,
                                            const unsigned char *uuid) {
    struct uml_driver *driver = (struct uml_driver *)conn->privateData;
1144 1145
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1146

1147
    umlDriverLock(driver);
1148
    vm = virDomainFindByUUID(&driver->domains, uuid);
1149 1150
    umlDriverUnlock(driver);

1151
    if (!vm) {
1152
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1153
        goto cleanup;
1154 1155 1156 1157
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) dom->id = vm->def->id;
1158 1159

cleanup:
1160 1161
    if (vm)
        virDomainObjUnlock(vm);
1162 1163
    return dom;
}
1164

1165 1166 1167
static virDomainPtr umlDomainLookupByName(virConnectPtr conn,
                                            const char *name) {
    struct uml_driver *driver = (struct uml_driver *)conn->privateData;
1168 1169
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1170

1171
    umlDriverLock(driver);
1172
    vm = virDomainFindByName(&driver->domains, name);
1173 1174
    umlDriverUnlock(driver);

1175
    if (!vm) {
1176
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1177
        goto cleanup;
1178 1179 1180 1181
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) dom->id = vm->def->id;
1182 1183

cleanup:
1184 1185
    if (vm)
        virDomainObjUnlock(vm);
1186 1187 1188
    return dom;
}

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

static int umlDomainIsActive(virDomainPtr dom)
{
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    umlDriverLock(driver);
    obj = virDomainFindByUUID(&driver->domains, dom->uuid);
    umlDriverUnlock(driver);
    if (!obj) {
1200
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
        goto cleanup;
    }
    ret = virDomainObjIsActive(obj);

cleanup:
    if (obj)
        virDomainObjUnlock(obj);
    return ret;
}


static int umlDomainIsPersistent(virDomainPtr dom)
{
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr obj;
    int ret = -1;

    umlDriverLock(driver);
    obj = virDomainFindByUUID(&driver->domains, dom->uuid);
    umlDriverUnlock(driver);
    if (!obj) {
1222
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
        goto cleanup;
    }
    ret = obj->persistent;

cleanup:
    if (obj)
        virDomainObjUnlock(obj);
    return ret;
}


1234
static int umlGetVersion(virConnectPtr conn, unsigned long *version) {
1235
    struct uml_driver *driver = conn->privateData;
1236
    struct utsname ut;
1237
    int ret = -1;
1238

1239
    umlDriverLock(driver);
1240 1241 1242 1243 1244

    if (driver->umlVersion == 0) {
        uname(&ut);

        if (virParseVersionString(ut.release, &driver->umlVersion) < 0) {
1245
            umlReportError(VIR_ERR_INTERNAL_ERROR,
1246 1247 1248
                           _("cannot parse version %s"), ut.release);
            goto cleanup;
        }
1249 1250
    }

1251 1252 1253 1254 1255 1256
    *version = driver->umlVersion;
    ret = 0;

cleanup:
    umlDriverUnlock(driver);
    return ret;
1257 1258 1259
}

static int umlListDomains(virConnectPtr conn, int *ids, int nids) {
1260
    struct uml_driver *driver = conn->privateData;
1261
    int n;
1262

1263
    umlDriverLock(driver);
1264
    n = virDomainObjListGetActiveIDs(&driver->domains, ids, nids);
1265
    umlDriverUnlock(driver);
1266

1267
    return n;
1268 1269
}
static int umlNumDomains(virConnectPtr conn) {
1270
    struct uml_driver *driver = conn->privateData;
1271
    int n;
1272

1273
    umlDriverLock(driver);
1274
    n = virDomainObjListNumOfDomains(&driver->domains, 1);
1275
    umlDriverUnlock(driver);
1276 1277 1278 1279

    return n;
}
static virDomainPtr umlDomainCreate(virConnectPtr conn, const char *xml,
1280
                                      unsigned int flags) {
1281
    struct uml_driver *driver = conn->privateData;
1282
    virDomainDefPtr def;
1283
    virDomainObjPtr vm = NULL;
1284
    virDomainPtr dom = NULL;
1285

1286 1287
    virCheckFlags(0, NULL);

1288
    umlDriverLock(driver);
1289
    if (!(def = virDomainDefParseString(driver->caps, xml,
1290
                                        VIR_DOMAIN_XML_INACTIVE)))
1291
        goto cleanup;
1292

1293
    if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
1294
        goto cleanup;
1295

1296
    if (!(vm = virDomainAssignDef(driver->caps,
1297
                                  &driver->domains,
1298
                                  def, false)))
1299 1300
        goto cleanup;
    def = NULL;
1301 1302 1303 1304

    if (umlStartVMDaemon(conn, driver, vm) < 0) {
        virDomainRemoveInactive(&driver->domains,
                                vm);
1305 1306
        vm = NULL;
        goto cleanup;
1307 1308 1309 1310
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) dom->id = vm->def->id;
1311 1312 1313

cleanup:
    virDomainDefFree(def);
1314 1315 1316
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1317 1318 1319 1320 1321
    return dom;
}


static int umlDomainShutdown(virDomainPtr dom) {
1322 1323
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
1324
    char *info = NULL;
1325
    int ret = -1;
1326

1327
    umlDriverLock(driver);
1328
    vm = virDomainFindByID(&driver->domains, dom->id);
1329
    umlDriverUnlock(driver);
1330
    if (!vm) {
1331 1332
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching id %d"), dom->id);
1333
        goto cleanup;
1334 1335 1336 1337
    }

#if 0
    if (umlMonitorCommand(driver, vm, "system_powerdown", &info) < 0) {
1338 1339
        umlReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("shutdown operation failed"));
1340
        goto cleanup;
1341
    }
1342
    ret = 0;
1343 1344
#endif

1345 1346
cleanup:
    VIR_FREE(info);
1347 1348
    if (vm)
        virDomainObjUnlock(vm);
1349
    return ret;
1350 1351 1352 1353
}


static int umlDomainDestroy(virDomainPtr dom) {
1354 1355 1356
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1357

1358
    umlDriverLock(driver);
1359
    vm = virDomainFindByID(&driver->domains, dom->id);
1360
    if (!vm) {
1361 1362
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching id %d"), dom->id);
1363
        goto cleanup;
1364 1365 1366
    }

    umlShutdownVMDaemon(dom->conn, driver, vm);
1367
    if (!vm->persistent) {
1368 1369
        virDomainRemoveInactive(&driver->domains,
                                vm);
1370 1371 1372
        vm = NULL;
    }
    ret = 0;
1373

1374
cleanup:
1375 1376 1377
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1378
    return ret;
1379 1380 1381 1382
}


static char *umlDomainGetOSType(virDomainPtr dom) {
1383 1384 1385
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *type = NULL;
1386

1387
    umlDriverLock(driver);
1388
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1389
    umlDriverUnlock(driver);
1390
    if (!vm) {
1391 1392
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1393
        goto cleanup;
1394 1395
    }

1396
    if (!(type = strdup(vm->def->os.type)))
1397
        virReportOOMError();
1398 1399

cleanup:
1400 1401
    if (vm)
        virDomainObjUnlock(vm);
1402 1403 1404 1405 1406
    return type;
}

/* Returns max memory in kb, 0 if error */
static unsigned long umlDomainGetMaxMemory(virDomainPtr dom) {
1407 1408 1409
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    unsigned long ret = 0;
1410

1411
    umlDriverLock(driver);
1412
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1413 1414
    umlDriverUnlock(driver);

1415 1416 1417 1418
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1419
        umlReportError(VIR_ERR_INVALID_DOMAIN,
1420 1421
                       _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
1422
    }
1423
    ret = vm->def->maxmem;
1424

1425
cleanup:
1426 1427
    if (vm)
        virDomainObjUnlock(vm);
1428
    return ret;
1429 1430 1431
}

static int umlDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) {
1432 1433 1434
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1435

1436
    umlDriverLock(driver);
1437
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1438 1439
    umlDriverUnlock(driver);

1440 1441 1442 1443
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1444 1445
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
1446
        goto cleanup;
1447 1448 1449
    }

    if (newmax < vm->def->memory) {
1450 1451
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("cannot set max memory lower than current memory"));
1452
        goto cleanup;
1453 1454 1455
    }

    vm->def->maxmem = newmax;
1456 1457 1458
    ret = 0;

cleanup:
1459 1460
    if (vm)
        virDomainObjUnlock(vm);
1461
    return ret;
1462 1463 1464
}

static int umlDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
1465 1466 1467
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1468

1469
    umlDriverLock(driver);
1470
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1471 1472
    umlDriverUnlock(driver);

1473 1474 1475 1476
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1477 1478
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
1479
        goto cleanup;
1480 1481
    }

D
Daniel P. Berrange 已提交
1482
    if (virDomainObjIsActive(vm)) {
1483 1484
        umlReportError(VIR_ERR_NO_SUPPORT, "%s",
                       _("cannot set memory of an active domain"));
1485
        goto cleanup;
1486 1487 1488
    }

    if (newmem > vm->def->maxmem) {
1489 1490
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("cannot set memory higher than max memory"));
1491
        goto cleanup;
1492 1493 1494
    }

    vm->def->memory = newmem;
1495 1496 1497
    ret = 0;

cleanup:
1498 1499
    if (vm)
        virDomainObjUnlock(vm);
1500
    return ret;
1501 1502 1503 1504
}

static int umlDomainGetInfo(virDomainPtr dom,
                              virDomainInfoPtr info) {
1505 1506 1507 1508
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

1509
    umlDriverLock(driver);
1510
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1511 1512
    umlDriverUnlock(driver);

1513
    if (!vm) {
1514 1515
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1516
        goto cleanup;
1517 1518 1519 1520
    }

    info->state = vm->state;

D
Daniel P. Berrange 已提交
1521
    if (!virDomainObjIsActive(vm)) {
1522 1523 1524
        info->cpuTime = 0;
    } else {
        if (umlGetProcessInfo(&(info->cpuTime), vm->pid) < 0) {
1525 1526
            umlReportError(VIR_ERR_OPERATION_FAILED, "%s",
                           _("cannot read cputime for domain"));
1527
            goto cleanup;
1528 1529 1530 1531 1532 1533
        }
    }

    info->maxMem = vm->def->maxmem;
    info->memory = vm->def->memory;
    info->nrVirtCpu = vm->def->vcpus;
1534 1535 1536
    ret = 0;

cleanup:
1537 1538
    if (vm)
        virDomainObjUnlock(vm);
1539
    return ret;
1540 1541 1542 1543 1544
}


static char *umlDomainDumpXML(virDomainPtr dom,
                                int flags ATTRIBUTE_UNUSED) {
1545 1546 1547 1548
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;

1549
    umlDriverLock(driver);
1550
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1551 1552
    umlDriverUnlock(driver);

1553
    if (!vm) {
1554 1555
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1556
        goto cleanup;
1557 1558
    }

1559
    ret = virDomainDefFormat((flags & VIR_DOMAIN_XML_INACTIVE) && vm->newDef ?
1560 1561 1562 1563
                             vm->newDef : vm->def,
                             flags);

cleanup:
1564 1565
    if (vm)
        virDomainObjUnlock(vm);
1566
    return ret;
1567 1568 1569 1570 1571
}


static int umlListDefinedDomains(virConnectPtr conn,
                            char **const names, int nnames) {
1572
    struct uml_driver *driver = conn->privateData;
1573
    int n;
1574

1575
    umlDriverLock(driver);
1576
    n = virDomainObjListGetInactiveNames(&driver->domains, names, nnames);
1577
    umlDriverUnlock(driver);
1578

1579
    return n;
1580 1581 1582
}

static int umlNumDefinedDomains(virConnectPtr conn) {
1583
    struct uml_driver *driver = conn->privateData;
1584
    int n;
1585

1586
    umlDriverLock(driver);
1587
    n = virDomainObjListNumOfDomains(&driver->domains, 0);
1588
    umlDriverUnlock(driver);
1589 1590 1591 1592 1593

    return n;
}


1594
static int umlDomainStartWithFlags(virDomainPtr dom, unsigned int flags) {
1595 1596 1597
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1598

1599 1600
    virCheckFlags(0, -1);

1601
    umlDriverLock(driver);
1602
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1603

1604
    if (!vm) {
1605 1606
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1607
        goto cleanup;
1608 1609
    }

1610 1611 1612
    ret = umlStartVMDaemon(dom->conn, driver, vm);

cleanup:
1613 1614 1615
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1616
    return ret;
1617 1618
}

1619 1620 1621
static int umlDomainStart(virDomainPtr dom) {
    return umlDomainStartWithFlags(dom, 0);
}
1622 1623

static virDomainPtr umlDomainDefine(virConnectPtr conn, const char *xml) {
1624
    struct uml_driver *driver = conn->privateData;
1625
    virDomainDefPtr def;
1626
    virDomainObjPtr vm = NULL;
1627
    virDomainPtr dom = NULL;
1628

1629
    umlDriverLock(driver);
1630
    if (!(def = virDomainDefParseString(driver->caps, xml,
1631
                                        VIR_DOMAIN_XML_INACTIVE)))
1632
        goto cleanup;
1633

1634 1635 1636
    if (virDomainObjIsDuplicate(&driver->domains, def, 0) < 0)
        goto cleanup;

1637
    if (!(vm = virDomainAssignDef(driver->caps,
1638
                                  &driver->domains,
1639
                                  def, false)))
1640 1641
        goto cleanup;
    def = NULL;
1642 1643
    vm->persistent = 1;

1644
    if (virDomainSaveConfig(driver->configDir,
1645 1646 1647
                            vm->newDef ? vm->newDef : vm->def) < 0) {
        virDomainRemoveInactive(&driver->domains,
                                vm);
1648
        vm = NULL;
1649
        goto cleanup;
1650 1651 1652 1653
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) dom->id = vm->def->id;
1654 1655 1656

cleanup:
    virDomainDefFree(def);
1657 1658 1659
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1660 1661 1662 1663
    return dom;
}

static int umlDomainUndefine(virDomainPtr dom) {
1664 1665 1666
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1667

1668
    umlDriverLock(driver);
1669
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1670
    if (!vm) {
1671 1672
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1673
        goto cleanup;
1674 1675
    }

D
Daniel P. Berrange 已提交
1676
    if (virDomainObjIsActive(vm)) {
1677 1678
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot delete active domain"));
1679
        goto cleanup;
1680 1681 1682
    }

    if (!vm->persistent) {
1683 1684
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot undefine transient domain"));
1685
        goto cleanup;
1686 1687
    }

1688
    if (virDomainDeleteConfig(driver->configDir, driver->autostartDir, vm) < 0)
1689
        goto cleanup;
1690 1691 1692

    virDomainRemoveInactive(&driver->domains,
                            vm);
1693
    vm = NULL;
1694
    ret = 0;
1695

1696
cleanup:
1697 1698 1699
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1700
    return ret;
1701 1702 1703
}


1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
static int umlDomainAttachUmlDisk(struct uml_driver *driver,
                                  virDomainObjPtr vm,
                                  virDomainDiskDefPtr disk)
{
    int i;
    char *cmd = NULL;
    char *reply = NULL;

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, disk->dst)) {
            umlReportError(VIR_ERR_OPERATION_FAILED,
                           _("target %s already exists"), disk->dst);
            return -1;
        }
    }

    if (!disk->src) {
        umlReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("disk source path is missing"));
        goto error;
    }

    if (virAsprintf(&cmd, "config %s=%s", disk->dst, disk->src) < 0) {
        virReportOOMError();
        return -1;
    }

    if (umlMonitorCommand(driver, vm, cmd, &reply) < 0)
        goto error;

    if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
        virReportOOMError();
        goto error;
    }

    virDomainDiskInsertPreAlloced(vm->def, disk);

    VIR_FREE(reply);
    VIR_FREE(cmd);

    return 0;

error:

    VIR_FREE(reply);
    VIR_FREE(cmd);

    return -1;
}


static int umlDomainAttachDevice(virDomainPtr dom, const char *xml)
{
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;

    umlDriverLock(driver);

    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        umlReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    if (!virDomainObjIsActive(vm)) {
        umlReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot attach device on inactive domain"));
        goto cleanup;
    }

    dev = virDomainDeviceDefParse(driver->caps, vm->def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);

    if (dev == NULL)
        goto cleanup;

    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
        if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_UML) {
            ret = umlDomainAttachUmlDisk(driver, vm, dev->data.disk);
            if (ret == 0)
                dev->data.disk = NULL;
        } else {
            umlReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("disk bus '%s' cannot be hotplugged."),
                           virDomainDiskBusTypeToString(dev->data.disk->bus));
        }
    } else {
        umlReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("device type '%s' cannot be attached"),
                       virDomainDeviceTypeToString(dev->type));
        goto cleanup;
    }

cleanup:

    virDomainDeviceDefFree(dev);
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
    return ret;
}


static int umlDomainAttachDeviceFlags(virDomainPtr dom,
                                      const char *xml,
                                      unsigned int flags) {
    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        umlReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot modify the persistent configuration of a domain"));
        return -1;
    }

    return umlDomainAttachDevice(dom, xml);
}


static int umlDomainDetachUmlDisk(struct uml_driver *driver,
                                  virDomainObjPtr vm,
                                  virDomainDeviceDefPtr dev)
{
    int i, ret = -1;
    virDomainDiskDefPtr detach = NULL;
    char *cmd;
    char *reply;

    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
            break;
        }
    }

    if (i == vm->def->ndisks) {
        umlReportError(VIR_ERR_OPERATION_FAILED,
                       _("disk %s not found"), dev->data.disk->dst);
        return -1;
    }

    detach = vm->def->disks[i];

    if (virAsprintf(&cmd, "remove %s", detach->dst) < 0) {
        virReportOOMError();
        return -1;
    }

    if (umlMonitorCommand(driver, vm, cmd, &reply) < 0)
        goto cleanup;

    virDomainDiskRemove(vm->def, i);

    virDomainDiskDefFree(detach);

    ret = 0;

    VIR_FREE(reply);

cleanup:
    VIR_FREE(cmd);

    return ret;
}


static int umlDomainDetachDevice(virDomainPtr dom, const char *xml) {
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    virDomainDeviceDefPtr dev = NULL;
    int ret = -1;

    umlDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];
        virUUIDFormat(dom->uuid, uuidstr);
        umlReportError(VIR_ERR_NO_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
    }

    if (!virDomainObjIsActive(vm)) {
        umlReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot detach device on inactive domain"));
        goto cleanup;
    }

    dev = virDomainDeviceDefParse(driver->caps, vm->def, xml,
                                  VIR_DOMAIN_XML_INACTIVE);
    if (dev == NULL)
        goto cleanup;

    if (dev->type == VIR_DOMAIN_DEVICE_DISK &&
        dev->data.disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
        if (dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_UML)
            ret = umlDomainDetachUmlDisk(driver, vm, dev);
        else {
            umlReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("This type of disk cannot be hot unplugged"));
        }
    } else {
        umlReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       "%s", _("This type of device cannot be hot unplugged"));
    }

cleanup:
    virDomainDeviceDefFree(dev);
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
    return ret;
}


static int umlDomainDetachDeviceFlags(virDomainPtr dom,
                                      const char *xml,
                                      unsigned int flags) {
    if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
        umlReportError(VIR_ERR_OPERATION_INVALID,
                       "%s", _("cannot modify the persistent configuration of a domain"));
        return -1;
    }

    return umlDomainDetachDevice(dom, xml);
}

1932 1933 1934

static int umlDomainGetAutostart(virDomainPtr dom,
                            int *autostart) {
1935 1936 1937
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1938

1939
    umlDriverLock(driver);
1940
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1941

1942
    if (!vm) {
1943 1944
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1945
        goto cleanup;
1946 1947 1948
    }

    *autostart = vm->autostart;
1949
    ret = 0;
1950

1951
cleanup:
1952 1953
    if (vm)
        virDomainObjUnlock(vm);
1954
    umlDriverUnlock(driver);
1955
    return ret;
1956 1957 1958 1959
}

static int umlDomainSetAutostart(virDomainPtr dom,
                                   int autostart) {
1960
    struct uml_driver *driver = dom->conn->privateData;
1961
    virDomainObjPtr vm;
1962 1963 1964
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;

1965 1966 1967
    umlDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);

1968
    if (!vm) {
1969 1970
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1971
        goto cleanup;
1972 1973 1974
    }

    if (!vm->persistent) {
1975 1976
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot set autostart for transient domain"));
1977
        goto cleanup;
1978 1979 1980 1981
    }

    autostart = (autostart != 0);

1982
    if (vm->autostart != autostart) {
1983
        if ((configFile = virDomainConfigFile(driver->configDir, vm->def->name)) == NULL)
1984
            goto cleanup;
1985
        if ((autostartLink = virDomainConfigFile(driver->autostartDir, vm->def->name)) == NULL)
1986
            goto cleanup;
1987

1988 1989
        if (autostart) {
            int err;
1990

1991
            if ((err = virFileMakePath(driver->autostartDir))) {
1992
                virReportSystemError(err,
1993 1994
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1995 1996
                goto cleanup;
            }
1997

1998
            if (symlink(configFile, autostartLink) < 0) {
1999
                virReportSystemError(errno,
2000 2001
                                     _("Failed to create symlink '%s to '%s'"),
                                     autostartLink, configFile);
2002 2003 2004 2005
                goto cleanup;
            }
        } else {
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
2006
                virReportSystemError(errno,
2007 2008
                                     _("Failed to delete symlink '%s'"),
                                     autostartLink);
2009 2010
                goto cleanup;
            }
2011 2012
        }

2013
        vm->autostart = autostart;
2014 2015 2016 2017 2018 2019
    }
    ret = 0;

cleanup:
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
2020 2021
    if (vm)
        virDomainObjUnlock(vm);
2022
    umlDriverUnlock(driver);
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
    return ret;
}


static int
umlDomainBlockPeek (virDomainPtr dom,
                      const char *path,
                      unsigned long long offset, size_t size,
                      void *buffer,
                      unsigned int flags ATTRIBUTE_UNUSED)
{
2034 2035 2036
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int fd = -1, ret = -1, i;
2037

2038
    umlDriverLock(driver);
2039
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2040 2041
    umlDriverUnlock(driver);

2042
    if (!vm) {
2043 2044
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
2045
        goto cleanup;
2046 2047 2048
    }

    if (!path || path[0] == '\0') {
2049 2050
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("NULL or empty path"));
2051
        goto cleanup;
2052 2053 2054 2055 2056
    }

    /* Check the path belongs to this domain. */
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->src != NULL &&
2057 2058 2059 2060
            STREQ (vm->def->disks[i]->src, path)) {
            ret = 0;
            break;
        }
2061 2062
    }

2063 2064 2065 2066 2067
    if (ret == 0) {
        ret = -1;
        /* The path is correct, now try to open it and get its size. */
        fd = open (path, O_RDONLY);
        if (fd == -1) {
2068
            virReportSystemError(errno,
2069
                                 _("cannot open %s"), path);
2070 2071
            goto cleanup;
        }
2072

2073 2074 2075 2076 2077 2078
        /* Seek and read. */
        /* NB. Because we configure with AC_SYS_LARGEFILE, off_t should
         * be 64 bits on all platforms.
         */
        if (lseek (fd, offset, SEEK_SET) == (off_t) -1 ||
            saferead (fd, buffer, size) == (ssize_t) -1) {
2079
            virReportSystemError(errno,
2080
                                 _("cannot read %s"), path);
2081 2082 2083 2084 2085
            goto cleanup;
        }

        ret = 0;
    } else {
2086 2087
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("invalid path"));
2088 2089
    }

2090
cleanup:
2091
    if (fd >= 0) close (fd);
2092 2093
    if (vm)
        virDomainObjUnlock(vm);
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
    return ret;
}



static virDriver umlDriver = {
    VIR_DRV_UML,
    "UML",
    umlOpen, /* open */
    umlClose, /* close */
    NULL, /* supports_feature */
    umlGetType, /* type */
    umlGetVersion, /* version */
2107
    NULL, /* libvirtVersion (impl. in libvirt.c) */
2108
    virGetHostname, /* getHostname */
2109
    NULL, /* getMaxVcpus */
2110
    nodeGetInfo, /* nodeGetInfo */
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
    umlGetCapabilities, /* getCapabilities */
    umlListDomains, /* listDomains */
    umlNumDomains, /* numOfDomains */
    umlDomainCreate, /* domainCreateXML */
    umlDomainLookupByID, /* domainLookupByID */
    umlDomainLookupByUUID, /* domainLookupByUUID */
    umlDomainLookupByName, /* domainLookupByName */
    NULL, /* domainSuspend */
    NULL, /* domainResume */
    umlDomainShutdown, /* domainShutdown */
    NULL, /* domainReboot */
    umlDomainDestroy, /* domainDestroy */
    umlDomainGetOSType, /* domainGetOSType */
    umlDomainGetMaxMemory, /* domainGetMaxMemory */
    umlDomainSetMaxMemory, /* domainSetMaxMemory */
    umlDomainSetMemory, /* domainSetMemory */
    umlDomainGetInfo, /* domainGetInfo */
    NULL, /* domainSave */
    NULL, /* domainRestore */
    NULL, /* domainCoreDump */
    NULL, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    NULL, /* domainGetMaxVcpus */
2135 2136
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
2137
    umlDomainDumpXML, /* domainDumpXML */
2138 2139
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
2140 2141
    umlListDefinedDomains, /* listDefinedDomains */
    umlNumDefinedDomains, /* numOfDefinedDomains */
2142
    umlDomainStart, /* domainCreate */
2143
    umlDomainStartWithFlags, /* domainCreateWithFlags */
2144 2145
    umlDomainDefine, /* domainDefineXML */
    umlDomainUndefine, /* domainUndefine */
2146 2147 2148 2149
    umlDomainAttachDevice, /* domainAttachDevice */
    umlDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
    umlDomainDetachDevice, /* domainDetachDevice */
    umlDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
2150
    NULL, /* domainUpdateDeviceFlags */
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
    umlDomainGetAutostart, /* domainGetAutostart */
    umlDomainSetAutostart, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
2161
    NULL, /* domainMemoryStats */
2162 2163
    umlDomainBlockPeek, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
2164
    NULL, /* domainGetBlockInfo */
2165 2166
    nodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    nodeGetFreeMemory,  /* getFreeMemory */
2167
    NULL, /* domainEventRegister */
2168
    NULL, /* domainEventDeregister */
2169 2170
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
2171
    NULL, /* nodeDeviceDettach */
2172 2173
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
2174
    NULL, /* domainMigratePrepareTunnel */
2175 2176 2177 2178
    umlIsEncrypted, /* isEncrypted */
    umlIsSecure, /* isSecure */
    umlDomainIsActive, /* domainIsActive */
    umlDomainIsPersistent, /* domainIsPersistent */
J
Jiri Denemark 已提交
2179
    NULL, /* cpuCompare */
2180
    NULL, /* cpuBaseline */
2181
    NULL, /* domainGetJobInfo */
2182
    NULL, /* domainAbortJob */
2183
    NULL, /* domainMigrateSetMaxDowntime */
2184 2185
    NULL, /* domainEventRegisterAny */
    NULL, /* domainEventDeregisterAny */
2186 2187 2188
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
2189 2190 2191 2192 2193 2194 2195 2196 2197
    NULL, /* domainSnapshotCreateXML */
    NULL, /* domainSnapshotDumpXML */
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
2198
    NULL, /* qemuDomainMonitorCommand */
2199 2200
};

2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
static int
umlVMFilterRebuild(virConnectPtr conn ATTRIBUTE_UNUSED,
                   virHashIterator iter, void *data)
{
    struct uml_driver *driver = uml_driver;

    umlDriverLock(driver);
    virHashForEach(uml_driver->domains.objs, iter, data);
    umlDriverUnlock(driver);

    return 0;
}
2213 2214

static virStateDriver umlStateDriver = {
2215
    .name = "UML",
2216 2217 2218 2219 2220 2221
    .initialize = umlStartup,
    .cleanup = umlShutdown,
    .reload = umlReload,
    .active = umlActive,
};

2222 2223 2224 2225 2226
static virNWFilterCallbackDriver umlCallbackDriver = {
    .name = "UML",
    .vmFilterRebuild = umlVMFilterRebuild,
};

2227 2228 2229
int umlRegister(void) {
    virRegisterDriver(&umlDriver);
    virRegisterStateDriver(&umlStateDriver);
2230
    virNWFilterRegisterCallbackDriver(&umlCallbackDriver);
2231 2232
    return 0;
}