uml_driver.c 60.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
#include "domain_nwfilter.h"
62
#include "files.h"
63

64 65
#define VIR_FROM_THIS VIR_FROM_UML

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

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


77 78
static int umlShutdown(void);

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


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

109

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

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

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


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

static struct uml_driver *uml_driver = NULL;

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

        if (!dom) {
            continue;
        }

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

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

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

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

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

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

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

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

352 353
    uml_driver->privileged = privileged;

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

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

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

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

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

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

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

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

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

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

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

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

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

    VIR_FREE(base);

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

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

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

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

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

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

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

448 449
    umlAutostartConfigs(uml_driver);

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

453 454
    return 0;

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

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

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

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

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

    if (!uml_driver)
        return 0;

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

509
    return active;
510 511
}

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

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

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

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

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

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

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

    return 0;
}


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

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

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

    rc = 0;

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

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

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

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

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

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

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

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

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

877
    if (umlBuildCommandLine(conn, driver, vm, &keepfd,
878
                            &argv, &progenv) < 0) {
879
        VIR_FORCE_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
    VIR_FORCE_CLOSE(logfd);
916

917 918 919 920 921
    /*
     * 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++)
922 923 924 925
        if (FD_ISSET(i, &keepfd)) {
            int tmpfd = i;
            VIR_FORCE_CLOSE(tmpfd);
        }
926

927 928 929 930 931 932 933 934
    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);

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

940 941 942

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

    return ret;
}

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

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

960
    virKillProcess(vm->pid, SIGTERM);
961

962
    VIR_FORCE_CLOSE(priv->monitor);
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->mem.max_balloon;
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->mem.cur_balloon) {
1450 1451
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("cannot set max memory lower than current memory"));
1452
        goto cleanup;
1453 1454
    }

1455
    vm->def->mem.max_balloon = 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->mem.max_balloon) {
1489 1490
        umlReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("cannot set memory higher than max memory"));
1491
        goto cleanup;
1492 1493
    }

1494
    vm->def->mem.cur_balloon = 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
    info->maxMem = vm->def->mem.max_balloon;
    info->memory = vm->def->mem.cur_balloon;
1533
    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
    VIR_FORCE_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
    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 */
E
Eric Blake 已提交
2132 2133
    NULL, /* domainSetVcpusFlags */
    NULL, /* domainGetVcpusFlags */
2134 2135 2136
    NULL, /* domainPinVcpu */
    NULL, /* domainGetVcpus */
    NULL, /* domainGetMaxVcpus */
2137 2138
    NULL, /* domainGetSecurityLabel */
    NULL, /* nodeGetSecurityModel */
2139
    umlDomainDumpXML, /* domainDumpXML */
2140 2141
    NULL, /* domainXMLFromNative */
    NULL, /* domainXMLToNative */
2142 2143
    umlListDefinedDomains, /* listDefinedDomains */
    umlNumDefinedDomains, /* numOfDefinedDomains */
2144
    umlDomainStart, /* domainCreate */
2145
    umlDomainStartWithFlags, /* domainCreateWithFlags */
2146 2147
    umlDomainDefine, /* domainDefineXML */
    umlDomainUndefine, /* domainUndefine */
2148 2149 2150 2151
    umlDomainAttachDevice, /* domainAttachDevice */
    umlDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
    umlDomainDetachDevice, /* domainDetachDevice */
    umlDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
2152
    NULL, /* domainUpdateDeviceFlags */
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
    umlDomainGetAutostart, /* domainGetAutostart */
    umlDomainSetAutostart, /* domainSetAutostart */
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
    NULL, /* domainMigratePrepare */
    NULL, /* domainMigratePerform */
    NULL, /* domainMigrateFinish */
    NULL, /* domainBlockStats */
    NULL, /* domainInterfaceStats */
2163
    NULL, /* domainMemoryStats */
2164 2165
    umlDomainBlockPeek, /* domainBlockPeek */
    NULL, /* domainMemoryPeek */
2166
    NULL, /* domainGetBlockInfo */
2167 2168
    nodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    nodeGetFreeMemory,  /* getFreeMemory */
2169
    NULL, /* domainEventRegister */
2170
    NULL, /* domainEventDeregister */
2171 2172
    NULL, /* domainMigratePrepare2 */
    NULL, /* domainMigrateFinish2 */
2173
    NULL, /* nodeDeviceDettach */
2174 2175
    NULL, /* nodeDeviceReAttach */
    NULL, /* nodeDeviceReset */
C
Chris Lalancette 已提交
2176
    NULL, /* domainMigratePrepareTunnel */
2177 2178 2179 2180
    umlIsEncrypted, /* isEncrypted */
    umlIsSecure, /* isSecure */
    umlDomainIsActive, /* domainIsActive */
    umlDomainIsPersistent, /* domainIsPersistent */
J
Jiri Denemark 已提交
2181
    NULL, /* cpuCompare */
2182
    NULL, /* cpuBaseline */
2183
    NULL, /* domainGetJobInfo */
2184
    NULL, /* domainAbortJob */
2185
    NULL, /* domainMigrateSetMaxDowntime */
2186 2187
    NULL, /* domainEventRegisterAny */
    NULL, /* domainEventDeregisterAny */
2188 2189 2190
    NULL, /* domainManagedSave */
    NULL, /* domainHasManagedSaveImage */
    NULL, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
2191 2192 2193 2194 2195 2196 2197 2198 2199
    NULL, /* domainSnapshotCreateXML */
    NULL, /* domainSnapshotDumpXML */
    NULL, /* domainSnapshotNum */
    NULL, /* domainSnapshotListNames */
    NULL, /* domainSnapshotLookupByName */
    NULL, /* domainHasCurrentSnapshot */
    NULL, /* domainSnapshotCurrent */
    NULL, /* domainRevertToSnapshot */
    NULL, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
2200
    NULL, /* qemuDomainMonitorCommand */
2201 2202
    NULL, /* domainSetMemoryParamters */
    NULL, /* domainGetMemoryParamters */
2203
    NULL, /* domainOpenConsole */
2204 2205
};

2206 2207 2208 2209 2210 2211 2212 2213
static int
umlVMFilterRebuild(virConnectPtr conn ATTRIBUTE_UNUSED,
                   virHashIterator iter, void *data)
{
    virHashForEach(uml_driver->domains.objs, iter, data);

    return 0;
}
2214 2215

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

2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
static void
umlVMDriverLock(void)
{
    umlDriverLock(uml_driver);
}

static void
umlVMDriverUnlock(void)
{
    umlDriverUnlock(uml_driver);
}

2235 2236 2237
static virNWFilterCallbackDriver umlCallbackDriver = {
    .name = "UML",
    .vmFilterRebuild = umlVMFilterRebuild,
2238 2239
    .vmDriverLock = umlVMDriverLock,
    .vmDriverUnlock = umlVMDriverUnlock,
2240 2241
};

2242 2243 2244
int umlRegister(void) {
    virRegisterDriver(&umlDriver);
    virRegisterStateDriver(&umlStateDriver);
2245
    virNWFilterRegisterCallbackDriver(&umlCallbackDriver);
2246 2247
    return 0;
}