uml_driver.c 59.0 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

62 63
#define VIR_FROM_THIS VIR_FROM_UML

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

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


75 76
static int umlShutdown(void);

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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);
}


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

107

108
static int umlOpenMonitor(struct uml_driver *driver,
109
                          virDomainObjPtr vm);
110
static int umlReadPidFile(struct uml_driver *driver,
111 112 113 114 115 116 117 118 119 120 121
                          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 已提交
122
    VIR_ERROR0(_("Failed to set close-on-exec file descriptor flag"));
123 124 125 126 127 128 129 130 131 132 133 134
    return -1;
}

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

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


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

static struct uml_driver *uml_driver = NULL;

142 143 144 145 146 147 148 149 150 151 152 153 154
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 已提交
155
        !virDomainObjIsActive(vm)) {
156 157 158 159
        virResetLastError();
        if (umlStartVMDaemon(data->conn, data->driver, vm) < 0) {
            virErrorPtr err = virGetLastError();
            VIR_ERROR(_("Failed to autostart VM '%s': %s"),
160
                      vm->def->name, err ? err->message : _("unknown error"));
161 162 163 164
        }
    }
    virDomainObjUnlock(vm);
}
165 166 167

static void
umlAutostartConfigs(struct uml_driver *driver) {
168 169 170 171 172
    /* 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
     */
173 174 175
    virConnectPtr conn = virConnectOpen(driver->privileged ?
                                        "uml:///system" :
                                        "uml:///session");
176
    /* Ignoring NULL conn which is mostly harmless here */
177

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

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

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


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

204
    if (res && STRPREFIX(res, "pts:")) {
205 206
        VIR_FREE(def->data.file.path);
        if ((def->data.file.path = strdup(res + 4)) == NULL) {
207
            virReportOOMError();
208 209 210 211
            VIR_FREE(res);
            VIR_FREE(cmd);
            return -1;
        }
212
    } else if (!res || STRPREFIX(res, "pts")) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        /* 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
229
umlIdentifyChrPTY(struct uml_driver *driver,
230 231 232 233 234 235
                  virDomainObjPtr dom)
{
    int i;

    if (dom->def->console &&
        dom->def->console->type == VIR_DOMAIN_CHR_TYPE_PTY)
236
        if (umlIdentifyOneChrPTY(driver, dom,
237 238 239 240 241
                                 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 &&
242
            umlIdentifyOneChrPTY(driver, dom,
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                                 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;

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

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

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

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

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

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

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

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

        if (!dom) {
            continue;
        }

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

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

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

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

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

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

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

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

350 351
    uml_driver->privileged = privileged;

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

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

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

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

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

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

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

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

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

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

    /* 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';

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

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

    VIR_FREE(base);

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

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

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

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

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

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

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

446 447
    umlAutostartConfigs(uml_driver);

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

451 452
    return 0;

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

error:
457
    VIR_FREE(userdir);
458
    VIR_FREE(base);
459 460
    umlDriverUnlock(uml_driver);
    umlShutdown();
461 462 463 464 465 466 467 468 469 470 471 472 473 474
    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;

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

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

    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) {
498
    int active = 0;
499 500 501 502

    if (!uml_driver)
        return 0;

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

507
    return active;
508 509
}

510 511 512 513 514 515 516
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 已提交
517
    if (virDomainObjIsActive(dom))
518 519 520 521
        umlShutdownVMDaemon(NULL, driver, dom);
    virDomainObjUnlock(dom);
}

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

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

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

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

    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);

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

    return 0;
}


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

    vm->pid = -1;
569 570
    if (virAsprintf(&pidfile, "%s/%s/pid",
                    driver->monitorDir, vm->def->name) < 0) {
571
        virReportOOMError();
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
        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;
587
        fclose(file);
588 589 590 591 592 593 594 595 596 597
        goto cleanup;
    }

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

    rc = 0;

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

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

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

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

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

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

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

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

    memset(addr.sun_path, 0, sizeof addr.sun_path);
656 657
    sprintf(addr.sun_path + 1, "libvirt-uml-%u", vm->pid);
    VIR_DEBUG("Reply address for monitor is '%s'", addr.sun_path+1);
658
    if (bind(priv->monitor, (struct sockaddr *)&addr, sizeof addr) < 0) {
659
        virReportSystemError(errno,
660
                             "%s", _("cannot bind socket"));
661 662
        close(priv->monitor);
        priv->monitor = -1;
663 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
        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];
};


689
static int umlMonitorCommand(const struct uml_driver *driver,
690 691 692 693 694 695 696 697 698 699
                             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;
700
    umlDomainObjPrivatePtr priv = vm->privateData;
701

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

704 705
    *reply = NULL;

706
    if (umlMonitorAddress(driver, vm, &addr) < 0)
707 708 709 710 711 712 713
        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)) {
714
        virReportSystemError(EINVAL,
715 716
                             _("cannot send too long command %s (%d bytes)"),
                             cmd, req.length);
717 718
        return -1;
    }
C
Chris Lalancette 已提交
719
    if (virStrcpyStatic(req.data, cmd) == NULL) {
720
        umlReportError(VIR_ERR_INTERNAL_ERROR,
C
Chris Lalancette 已提交
721 722 723
                       _("Command %s too long for destination"), cmd);
        return -1;
    }
724

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

    do {
E
Eric Blake 已提交
734
        ssize_t nbytes;
735
        addrlen = sizeof(addr);
E
Eric Blake 已提交
736
        nbytes = recvfrom(priv->monitor, &res, sizeof res, 0,
737
                          (struct sockaddr *)&addr, &addrlen);
E
Eric Blake 已提交
738 739 740
        if (nbytes < 0) {
            if (errno == EAGAIN || errno == EINTR)
                continue;
741
            virReportSystemError(errno, _("cannot read reply %s"), cmd);
742 743
            goto error;
        }
744 745 746
        /* 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) {
747 748 749
            virReportSystemError(0, _("incomplete reply %s"), cmd);
            goto error;
        }
750 751

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

        if (res.error)
            ret = -1;

    } while (res.extra);

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

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

    return ret;

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


779 780 781 782 783 784
static int umlCleanupTapDevices(virConnectPtr conn ATTRIBUTE_UNUSED,
                                virDomainObjPtr vm) {
    int i;
    int err;
    int ret = 0;
    brControl *brctl = NULL;
785
    VIR_ERROR0(_("Cleanup tap"));
786 787 788 789 790 791 792 793 794 795
    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;

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

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

    FD_ZERO(&keepfd);

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

    if (!vm->def->os.kernel) {
831 832
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no kernel specified"));
833 834 835 836 837 838 839
        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) {
840
        virReportSystemError(errno,
841 842
                             _("Cannot find UML kernel %s"),
                             vm->def->os.kernel);
843 844 845
        return -1;
    }

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

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

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

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

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

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

907
    priv->monitor = -1;
908

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

915 916 917 918 919 920 921 922
    /*
     * 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);

923 924 925 926 927 928 929 930
    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);

931 932
    if (ret < 0)
        umlCleanupTapDevices(conn, vm);
933 934 935

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

    return ret;
}

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

D
Daniel P. Berrange 已提交
950
    if (!virDomainObjIsActive(vm))
951 952
        return;

953
    virKillProcess(vm->pid, SIGTERM);
954

955 956 957
    if (priv->monitor != -1)
        close(priv->monitor);
    priv->monitor = -1;
958 959

    if ((ret = waitpid(vm->pid, NULL, 0)) != vm->pid) {
960
        VIR_WARN("Got unexpected pid %d != %d",
961 962 963 964 965 966 967
               ret, vm->pid);
    }

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

968 969
    umlCleanupTapDevices(conn, vm);

970 971 972 973 974 975 976 977 978 979 980 981
    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) {
982 983 984
    if (conn->uri == NULL) {
        if (uml_driver == NULL)
            return VIR_DRV_OPEN_DECLINED;
985

986
        conn->uri = xmlParseURI(uml_driver->privileged ?
987 988 989
                                "uml:///system" :
                                "uml:///session");
        if (!conn->uri) {
990
            virReportOOMError();
991 992 993 994 995 996 997 998 999 1000
            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;
1001 1002


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

        /* URI was good, but driver isn't active */
        if (uml_driver == NULL) {
1023
            umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1024
                           _("uml state driver is not active"));
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
            return VIR_DRV_OPEN_ERROR;
        }
    }

    conn->privateData = uml_driver;

    return VIR_DRV_OPEN_SUCCESS;
}

static int umlClose(virConnectPtr conn) {
1035
    /*struct uml_driver *driver = conn->privateData;*/
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

    conn->privateData = NULL;

    return 0;
}

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


1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
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;
}


1061 1062 1063 1064
static char *umlGetCapabilities(virConnectPtr conn) {
    struct uml_driver *driver = (struct uml_driver *)conn->privateData;
    char *xml;

1065
    umlDriverLock(driver);
1066
    if ((xml = virCapabilitiesFormatXML(driver->caps)) == NULL)
1067
        virReportOOMError();
1068
    umlDriverUnlock(driver);
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

    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");
1092
        fclose(pidinfo);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
        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;
1114 1115
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1116

1117
    umlDriverLock(driver);
1118
    vm = virDomainFindByID(&driver->domains, id);
1119 1120
    umlDriverUnlock(driver);

1121
    if (!vm) {
1122
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1123
        goto cleanup;
1124 1125 1126 1127
    }

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

cleanup:
1130 1131
    if (vm)
        virDomainObjUnlock(vm);
1132 1133
    return dom;
}
1134

1135 1136 1137
static virDomainPtr umlDomainLookupByUUID(virConnectPtr conn,
                                            const unsigned char *uuid) {
    struct uml_driver *driver = (struct uml_driver *)conn->privateData;
1138 1139
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1140

1141
    umlDriverLock(driver);
1142
    vm = virDomainFindByUUID(&driver->domains, uuid);
1143 1144
    umlDriverUnlock(driver);

1145
    if (!vm) {
1146
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1147
        goto cleanup;
1148 1149 1150 1151
    }

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

cleanup:
1154 1155
    if (vm)
        virDomainObjUnlock(vm);
1156 1157
    return dom;
}
1158

1159 1160 1161
static virDomainPtr umlDomainLookupByName(virConnectPtr conn,
                                            const char *name) {
    struct uml_driver *driver = (struct uml_driver *)conn->privateData;
1162 1163
    virDomainObjPtr vm;
    virDomainPtr dom = NULL;
1164

1165
    umlDriverLock(driver);
1166
    vm = virDomainFindByName(&driver->domains, name);
1167 1168
    umlDriverUnlock(driver);

1169
    if (!vm) {
1170
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1171
        goto cleanup;
1172 1173 1174 1175
    }

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

cleanup:
1178 1179
    if (vm)
        virDomainObjUnlock(vm);
1180 1181 1182
    return dom;
}

1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

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) {
1194
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
        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) {
1216
        umlReportError(VIR_ERR_NO_DOMAIN, NULL);
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
        goto cleanup;
    }
    ret = obj->persistent;

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


1228
static int umlGetVersion(virConnectPtr conn, unsigned long *version) {
1229
    struct uml_driver *driver = conn->privateData;
1230
    struct utsname ut;
1231
    int ret = -1;
1232

1233
    umlDriverLock(driver);
1234 1235 1236 1237 1238

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

        if (virParseVersionString(ut.release, &driver->umlVersion) < 0) {
1239
            umlReportError(VIR_ERR_INTERNAL_ERROR,
1240 1241 1242
                           _("cannot parse version %s"), ut.release);
            goto cleanup;
        }
1243 1244
    }

1245 1246 1247 1248 1249 1250
    *version = driver->umlVersion;
    ret = 0;

cleanup:
    umlDriverUnlock(driver);
    return ret;
1251 1252 1253
}

static int umlListDomains(virConnectPtr conn, int *ids, int nids) {
1254
    struct uml_driver *driver = conn->privateData;
1255
    int n;
1256

1257
    umlDriverLock(driver);
1258
    n = virDomainObjListGetActiveIDs(&driver->domains, ids, nids);
1259
    umlDriverUnlock(driver);
1260

1261
    return n;
1262 1263
}
static int umlNumDomains(virConnectPtr conn) {
1264
    struct uml_driver *driver = conn->privateData;
1265
    int n;
1266

1267
    umlDriverLock(driver);
1268
    n = virDomainObjListNumOfDomains(&driver->domains, 1);
1269
    umlDriverUnlock(driver);
1270 1271 1272 1273

    return n;
}
static virDomainPtr umlDomainCreate(virConnectPtr conn, const char *xml,
1274
                                      unsigned int flags) {
1275
    struct uml_driver *driver = conn->privateData;
1276
    virDomainDefPtr def;
1277
    virDomainObjPtr vm = NULL;
1278
    virDomainPtr dom = NULL;
1279

1280 1281
    virCheckFlags(0, NULL);

1282
    umlDriverLock(driver);
1283
    if (!(def = virDomainDefParseString(driver->caps, xml,
1284
                                        VIR_DOMAIN_XML_INACTIVE)))
1285
        goto cleanup;
1286

1287
    if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
1288
        goto cleanup;
1289

1290
    if (!(vm = virDomainAssignDef(driver->caps,
1291
                                  &driver->domains,
1292
                                  def, false)))
1293 1294
        goto cleanup;
    def = NULL;
1295 1296 1297 1298

    if (umlStartVMDaemon(conn, driver, vm) < 0) {
        virDomainRemoveInactive(&driver->domains,
                                vm);
1299 1300
        vm = NULL;
        goto cleanup;
1301 1302 1303 1304
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) dom->id = vm->def->id;
1305 1306 1307

cleanup:
    virDomainDefFree(def);
1308 1309 1310
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1311 1312 1313 1314 1315
    return dom;
}


static int umlDomainShutdown(virDomainPtr dom) {
1316 1317
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
1318
    char *info = NULL;
1319
    int ret = -1;
1320

1321
    umlDriverLock(driver);
1322
    vm = virDomainFindByID(&driver->domains, dom->id);
1323
    umlDriverUnlock(driver);
1324
    if (!vm) {
1325 1326
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching id %d"), dom->id);
1327
        goto cleanup;
1328 1329 1330 1331
    }

#if 0
    if (umlMonitorCommand(driver, vm, "system_powerdown", &info) < 0) {
1332 1333
        umlReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("shutdown operation failed"));
1334
        goto cleanup;
1335
    }
1336
    ret = 0;
1337 1338
#endif

1339 1340
cleanup:
    VIR_FREE(info);
1341 1342
    if (vm)
        virDomainObjUnlock(vm);
1343
    return ret;
1344 1345 1346 1347
}


static int umlDomainDestroy(virDomainPtr dom) {
1348 1349 1350
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1351

1352
    umlDriverLock(driver);
1353
    vm = virDomainFindByID(&driver->domains, dom->id);
1354
    if (!vm) {
1355 1356
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching id %d"), dom->id);
1357
        goto cleanup;
1358 1359 1360
    }

    umlShutdownVMDaemon(dom->conn, driver, vm);
1361
    if (!vm->persistent) {
1362 1363
        virDomainRemoveInactive(&driver->domains,
                                vm);
1364 1365 1366
        vm = NULL;
    }
    ret = 0;
1367

1368
cleanup:
1369 1370 1371
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1372
    return ret;
1373 1374 1375 1376
}


static char *umlDomainGetOSType(virDomainPtr dom) {
1377 1378 1379
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *type = NULL;
1380

1381
    umlDriverLock(driver);
1382
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1383
    umlDriverUnlock(driver);
1384
    if (!vm) {
1385 1386
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1387
        goto cleanup;
1388 1389
    }

1390
    if (!(type = strdup(vm->def->os.type)))
1391
        virReportOOMError();
1392 1393

cleanup:
1394 1395
    if (vm)
        virDomainObjUnlock(vm);
1396 1397 1398 1399 1400
    return type;
}

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

1405
    umlDriverLock(driver);
1406
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1407 1408
    umlDriverUnlock(driver);

1409 1410 1411 1412
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1413
        umlReportError(VIR_ERR_INVALID_DOMAIN,
1414 1415
                       _("no domain with matching uuid '%s'"), uuidstr);
        goto cleanup;
1416
    }
1417
    ret = vm->def->maxmem;
1418

1419
cleanup:
1420 1421
    if (vm)
        virDomainObjUnlock(vm);
1422
    return ret;
1423 1424 1425
}

static int umlDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) {
1426 1427 1428
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1429

1430
    umlDriverLock(driver);
1431
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1432 1433
    umlDriverUnlock(driver);

1434 1435 1436 1437
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1438 1439
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
1440
        goto cleanup;
1441 1442 1443
    }

    if (newmax < vm->def->memory) {
1444 1445
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("cannot set max memory lower than current memory"));
1446
        goto cleanup;
1447 1448 1449
    }

    vm->def->maxmem = newmax;
1450 1451 1452
    ret = 0;

cleanup:
1453 1454
    if (vm)
        virDomainObjUnlock(vm);
1455
    return ret;
1456 1457 1458
}

static int umlDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
1459 1460 1461
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1462

1463
    umlDriverLock(driver);
1464
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1465 1466
    umlDriverUnlock(driver);

1467 1468 1469 1470
    if (!vm) {
        char uuidstr[VIR_UUID_STRING_BUFLEN];

        virUUIDFormat(dom->uuid, uuidstr);
1471 1472
        umlReportError(VIR_ERR_INVALID_DOMAIN,
                       _("no domain with matching uuid '%s'"), uuidstr);
1473
        goto cleanup;
1474 1475
    }

D
Daniel P. Berrange 已提交
1476
    if (virDomainObjIsActive(vm)) {
1477 1478
        umlReportError(VIR_ERR_NO_SUPPORT, "%s",
                       _("cannot set memory of an active domain"));
1479
        goto cleanup;
1480 1481 1482
    }

    if (newmem > vm->def->maxmem) {
1483 1484
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("cannot set memory higher than max memory"));
1485
        goto cleanup;
1486 1487 1488
    }

    vm->def->memory = newmem;
1489 1490 1491
    ret = 0;

cleanup:
1492 1493
    if (vm)
        virDomainObjUnlock(vm);
1494
    return ret;
1495 1496 1497 1498
}

static int umlDomainGetInfo(virDomainPtr dom,
                              virDomainInfoPtr info) {
1499 1500 1501 1502
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;

1503
    umlDriverLock(driver);
1504
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1505 1506
    umlDriverUnlock(driver);

1507
    if (!vm) {
1508 1509
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1510
        goto cleanup;
1511 1512 1513 1514
    }

    info->state = vm->state;

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

    info->maxMem = vm->def->maxmem;
    info->memory = vm->def->memory;
    info->nrVirtCpu = vm->def->vcpus;
1528 1529 1530
    ret = 0;

cleanup:
1531 1532
    if (vm)
        virDomainObjUnlock(vm);
1533
    return ret;
1534 1535 1536 1537 1538
}


static char *umlDomainDumpXML(virDomainPtr dom,
                                int flags ATTRIBUTE_UNUSED) {
1539 1540 1541 1542
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    char *ret = NULL;

1543
    umlDriverLock(driver);
1544
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1545 1546
    umlDriverUnlock(driver);

1547
    if (!vm) {
1548 1549
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1550
        goto cleanup;
1551 1552
    }

1553
    ret = virDomainDefFormat((flags & VIR_DOMAIN_XML_INACTIVE) && vm->newDef ?
1554 1555 1556 1557
                             vm->newDef : vm->def,
                             flags);

cleanup:
1558 1559
    if (vm)
        virDomainObjUnlock(vm);
1560
    return ret;
1561 1562 1563 1564 1565
}


static int umlListDefinedDomains(virConnectPtr conn,
                            char **const names, int nnames) {
1566
    struct uml_driver *driver = conn->privateData;
1567
    int n;
1568

1569
    umlDriverLock(driver);
1570
    n = virDomainObjListGetInactiveNames(&driver->domains, names, nnames);
1571
    umlDriverUnlock(driver);
1572

1573
    return n;
1574 1575 1576
}

static int umlNumDefinedDomains(virConnectPtr conn) {
1577
    struct uml_driver *driver = conn->privateData;
1578
    int n;
1579

1580
    umlDriverLock(driver);
1581
    n = virDomainObjListNumOfDomains(&driver->domains, 0);
1582
    umlDriverUnlock(driver);
1583 1584 1585 1586 1587

    return n;
}


1588
static int umlDomainStartWithFlags(virDomainPtr dom, unsigned int flags) {
1589 1590 1591
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1592

1593 1594
    virCheckFlags(0, -1);

1595
    umlDriverLock(driver);
1596
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1597

1598
    if (!vm) {
1599 1600
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1601
        goto cleanup;
1602 1603
    }

1604 1605 1606
    ret = umlStartVMDaemon(dom->conn, driver, vm);

cleanup:
1607 1608 1609
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1610
    return ret;
1611 1612
}

1613 1614 1615
static int umlDomainStart(virDomainPtr dom) {
    return umlDomainStartWithFlags(dom, 0);
}
1616 1617

static virDomainPtr umlDomainDefine(virConnectPtr conn, const char *xml) {
1618
    struct uml_driver *driver = conn->privateData;
1619
    virDomainDefPtr def;
1620
    virDomainObjPtr vm = NULL;
1621
    virDomainPtr dom = NULL;
1622

1623
    umlDriverLock(driver);
1624
    if (!(def = virDomainDefParseString(driver->caps, xml,
1625
                                        VIR_DOMAIN_XML_INACTIVE)))
1626
        goto cleanup;
1627

1628 1629 1630
    if (virDomainObjIsDuplicate(&driver->domains, def, 0) < 0)
        goto cleanup;

1631
    if (!(vm = virDomainAssignDef(driver->caps,
1632
                                  &driver->domains,
1633
                                  def, false)))
1634 1635
        goto cleanup;
    def = NULL;
1636 1637
    vm->persistent = 1;

1638
    if (virDomainSaveConfig(driver->configDir,
1639 1640 1641
                            vm->newDef ? vm->newDef : vm->def) < 0) {
        virDomainRemoveInactive(&driver->domains,
                                vm);
1642
        vm = NULL;
1643
        goto cleanup;
1644 1645 1646 1647
    }

    dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
    if (dom) dom->id = vm->def->id;
1648 1649 1650

cleanup:
    virDomainDefFree(def);
1651 1652 1653
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1654 1655 1656 1657
    return dom;
}

static int umlDomainUndefine(virDomainPtr dom) {
1658 1659 1660
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1661

1662
    umlDriverLock(driver);
1663
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1664
    if (!vm) {
1665 1666
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1667
        goto cleanup;
1668 1669
    }

D
Daniel P. Berrange 已提交
1670
    if (virDomainObjIsActive(vm)) {
1671 1672
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot delete active domain"));
1673
        goto cleanup;
1674 1675 1676
    }

    if (!vm->persistent) {
1677 1678
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot undefine transient domain"));
1679
        goto cleanup;
1680 1681
    }

1682
    if (virDomainDeleteConfig(driver->configDir, driver->autostartDir, vm) < 0)
1683
        goto cleanup;
1684 1685 1686

    virDomainRemoveInactive(&driver->domains,
                            vm);
1687
    vm = NULL;
1688
    ret = 0;
1689

1690
cleanup:
1691 1692 1693
    if (vm)
        virDomainObjUnlock(vm);
    umlDriverUnlock(driver);
1694
    return ret;
1695 1696 1697
}


1698 1699 1700 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
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);
}

1926 1927 1928

static int umlDomainGetAutostart(virDomainPtr dom,
                            int *autostart) {
1929 1930 1931
    struct uml_driver *driver = dom->conn->privateData;
    virDomainObjPtr vm;
    int ret = -1;
1932

1933
    umlDriverLock(driver);
1934
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
1935

1936
    if (!vm) {
1937 1938
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1939
        goto cleanup;
1940 1941 1942
    }

    *autostart = vm->autostart;
1943
    ret = 0;
1944

1945
cleanup:
1946 1947
    if (vm)
        virDomainObjUnlock(vm);
1948
    umlDriverUnlock(driver);
1949
    return ret;
1950 1951 1952 1953
}

static int umlDomainSetAutostart(virDomainPtr dom,
                                   int autostart) {
1954
    struct uml_driver *driver = dom->conn->privateData;
1955
    virDomainObjPtr vm;
1956 1957 1958
    char *configFile = NULL, *autostartLink = NULL;
    int ret = -1;

1959 1960 1961
    umlDriverLock(driver);
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);

1962
    if (!vm) {
1963 1964
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
1965
        goto cleanup;
1966 1967 1968
    }

    if (!vm->persistent) {
1969 1970
        umlReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot set autostart for transient domain"));
1971
        goto cleanup;
1972 1973 1974 1975
    }

    autostart = (autostart != 0);

1976
    if (vm->autostart != autostart) {
1977
        if ((configFile = virDomainConfigFile(driver->configDir, vm->def->name)) == NULL)
1978
            goto cleanup;
1979
        if ((autostartLink = virDomainConfigFile(driver->autostartDir, vm->def->name)) == NULL)
1980
            goto cleanup;
1981

1982 1983
        if (autostart) {
            int err;
1984

1985
            if ((err = virFileMakePath(driver->autostartDir))) {
1986
                virReportSystemError(err,
1987 1988
                                     _("cannot create autostart directory %s"),
                                     driver->autostartDir);
1989 1990
                goto cleanup;
            }
1991

1992
            if (symlink(configFile, autostartLink) < 0) {
1993
                virReportSystemError(errno,
1994 1995
                                     _("Failed to create symlink '%s to '%s'"),
                                     autostartLink, configFile);
1996 1997 1998 1999
                goto cleanup;
            }
        } else {
            if (unlink(autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR) {
2000
                virReportSystemError(errno,
2001 2002
                                     _("Failed to delete symlink '%s'"),
                                     autostartLink);
2003 2004
                goto cleanup;
            }
2005 2006
        }

2007
        vm->autostart = autostart;
2008 2009 2010 2011 2012 2013
    }
    ret = 0;

cleanup:
    VIR_FREE(configFile);
    VIR_FREE(autostartLink);
2014 2015
    if (vm)
        virDomainObjUnlock(vm);
2016
    umlDriverUnlock(driver);
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
    return ret;
}


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

2032
    umlDriverLock(driver);
2033
    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2034 2035
    umlDriverUnlock(driver);

2036
    if (!vm) {
2037 2038
        umlReportError(VIR_ERR_INVALID_DOMAIN, "%s",
                       _("no domain with matching uuid"));
2039
        goto cleanup;
2040 2041 2042
    }

    if (!path || path[0] == '\0') {
2043 2044
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("NULL or empty path"));
2045
        goto cleanup;
2046 2047 2048 2049 2050
    }

    /* Check the path belongs to this domain. */
    for (i = 0 ; i < vm->def->ndisks ; i++) {
        if (vm->def->disks[i]->src != NULL &&
2051 2052 2053 2054
            STREQ (vm->def->disks[i]->src, path)) {
            ret = 0;
            break;
        }
2055 2056
    }

2057 2058 2059 2060 2061
    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) {
2062
            virReportSystemError(errno,
2063
                                 _("cannot open %s"), path);
2064 2065
            goto cleanup;
        }
2066

2067 2068 2069 2070 2071 2072
        /* 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) {
2073
            virReportSystemError(errno,
2074
                                 _("cannot read %s"), path);
2075 2076 2077 2078 2079
            goto cleanup;
        }

        ret = 0;
    } else {
2080 2081
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("invalid path"));
2082 2083
    }

2084
cleanup:
2085
    if (fd >= 0) close (fd);
2086 2087
    if (vm)
        virDomainObjUnlock(vm);
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
    return ret;
}



static virDriver umlDriver = {
    VIR_DRV_UML,
    "UML",
    umlOpen, /* open */
    umlClose, /* close */
    NULL, /* supports_feature */
    umlGetType, /* type */
    umlGetVersion, /* version */
2101
    NULL, /* libvirtVersion (impl. in libvirt.c) */
2102
    virGetHostname, /* getHostname */
2103
    NULL, /* getMaxVcpus */
2104
    nodeGetInfo, /* nodeGetInfo */
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
    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 */
2129 2130
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
2131
    umlDomainDumpXML, /* domainDumpXML */
2132 2133
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
2134 2135
    umlListDefinedDomains, /* listDefinedDomains */
    umlNumDefinedDomains, /* numOfDefinedDomains */
2136
    umlDomainStart, /* domainCreate */
2137
    umlDomainStartWithFlags, /* domainCreateWithFlags */
2138 2139
    umlDomainDefine, /* domainDefineXML */
    umlDomainUndefine, /* domainUndefine */
2140 2141 2142 2143
    umlDomainAttachDevice, /* domainAttachDevice */
    umlDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
    umlDomainDetachDevice, /* domainDetachDevice */
    umlDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
2144
    NULL, /* domainUpdateDeviceFlags */
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
    umlDomainGetAutostart, /* domainGetAutostart */
    umlDomainSetAutostart, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
2155
    NULL, /* domainMemoryStats */
2156 2157
    umlDomainBlockPeek, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
2158
    NULL, /* domainGetBlockInfo */
2159 2160
    nodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    nodeGetFreeMemory,  /* getFreeMemory */
2161
    NULL, /* domainEventRegister */
2162
    NULL, /* domainEventDeregister */
2163 2164
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
2165
    NULL, /* nodeDeviceDettach */
2166 2167
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
2168
    NULL, /* domainMigratePrepareTunnel */
2169 2170 2171 2172
    umlIsEncrypted, /* isEncrypted */
    umlIsSecure, /* isSecure */
    umlDomainIsActive, /* domainIsActive */
    umlDomainIsPersistent, /* domainIsPersistent */
J
Jiri Denemark 已提交
2173
    NULL, /* cpuCompare */
2174
    NULL, /* cpuBaseline */
2175
    NULL, /* domainGetJobInfo */
2176
    NULL, /* domainAbortJob */
2177
    NULL, /* domainMigrateSetMaxDowntime */
2178 2179
    NULL, /* domainEventRegisterAny */
    NULL, /* domainEventDeregisterAny */
2180 2181 2182
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
2183 2184 2185 2186 2187 2188 2189 2190 2191
    NULL, /* domainSnapshotCreateXML */
    NULL, /* domainSnapshotDumpXML */
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
2192
    NULL, /* qemuDomainMonitorCommand */
2193 2194 2195 2196
};


static virStateDriver umlStateDriver = {
2197
    .name = "UML",
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
    .initialize = umlStartup,
    .cleanup = umlShutdown,
    .reload = umlReload,
    .active = umlActive,
};

int umlRegister(void) {
    virRegisterDriver(&umlDriver);
    virRegisterStateDriver(&umlStateDriver);
    return 0;
}