Switch to using a unix socket for the qemu monitor
[libvirt.git] / src / qemu_driver.c
1 /*
2  * driver.c: core driver methods for managing qemu guests
3  *
4  * Copyright (C) 2006, 2007, 2008, 2009 Red Hat, Inc.
5  * Copyright (C) 2006 Daniel P. Berrange
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
20  *
21  * Author: Daniel P. Berrange <berrange@redhat.com>
22  */
23
24 #include <config.h>
25
26 #include <sys/types.h>
27 #include <sys/poll.h>
28 #include <dirent.h>
29 #include <limits.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <strings.h>
33 #include <stdarg.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/utsname.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <signal.h>
41 #include <paths.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <sys/wait.h>
45 #include <sys/ioctl.h>
46
47 #if HAVE_SCHED_H
48 #include <sched.h>
49 #endif
50
51 #include "virterror_internal.h"
52 #include "logging.h"
53 #include "datatypes.h"
54 #include "qemu_driver.h"
55 #include "qemu_conf.h"
56 #include "c-ctype.h"
57 #include "event.h"
58 #include "buf.h"
59 #include "util.h"
60 #include "nodeinfo.h"
61 #include "stats_linux.h"
62 #include "capabilities.h"
63 #include "memory.h"
64 #include "uuid.h"
65 #include "domain_conf.h"
66 #include "node_device_conf.h"
67 #include "pci.h"
68 #include "security.h"
69
70
71 #define VIR_FROM_THIS VIR_FROM_QEMU
72
73 /* For storing short-lived temporary files. */
74 #define TEMPDIR LOCAL_STATE_DIR "/cache/libvirt"
75
76 #define QEMU_CMD_PROMPT "\n(qemu) "
77 #define QEMU_PASSWD_PROMPT "Password: "
78
79
80 static int qemudShutdown(void);
81
82 static void qemuDriverLock(struct qemud_driver *driver)
83 {
84     virMutexLock(&driver->lock);
85 }
86 static void qemuDriverUnlock(struct qemud_driver *driver)
87 {
88     virMutexUnlock(&driver->lock);
89 }
90
91 static void qemuDomainEventFlush(int timer, void *opaque);
92 static void qemuDomainEventQueue(struct qemud_driver *driver,
93                                  virDomainEventPtr event);
94
95 static void qemudDispatchVMEvent(int watch,
96                                  int fd,
97                                  int events,
98                                  void *opaque);
99
100 static int qemudStartVMDaemon(virConnectPtr conn,
101                               struct qemud_driver *driver,
102                               virDomainObjPtr vm,
103                               const char *migrateFrom,
104                               int stdin_fd);
105
106 static void qemudShutdownVMDaemon(virConnectPtr conn,
107                                   struct qemud_driver *driver,
108                                   virDomainObjPtr vm);
109
110 static int qemudDomainGetMaxVcpus(virDomainPtr dom);
111
112 static int qemudMonitorCommand(const virDomainObjPtr vm,
113                                const char *cmd,
114                                char **reply);
115 static int qemudMonitorCommandExtra(const virDomainObjPtr vm,
116                                     const char *cmd,
117                                     const char *extra,
118                                     const char *extraPrompt,
119                                     char **reply);
120 static int qemudDomainSetMemoryBalloon(virConnectPtr conn,
121                                        virDomainObjPtr vm,
122                                        unsigned long newmem);
123 static int qemudDetectVcpuPIDs(virConnectPtr conn,
124                                virDomainObjPtr vm);
125
126 static struct qemud_driver *qemu_driver = NULL;
127
128
129 static int
130 qemudLogFD(virConnectPtr conn, struct qemud_driver *driver, const char* name)
131 {
132     char logfile[PATH_MAX];
133     mode_t logmode;
134     int ret, fd = -1;
135
136     if ((ret = snprintf(logfile, sizeof(logfile), "%s/%s.log",
137                         driver->logDir, name))
138         < 0 || ret >= sizeof(logfile)) {
139         virReportOOMError(conn);
140         return -1;
141     }
142
143     logmode = O_CREAT | O_WRONLY;
144     /* Only logrotate files in /var/log, so only append if running privileged */
145     if (driver->privileged)
146         logmode |= O_APPEND;
147     else
148         logmode |= O_TRUNC;
149
150     if ((fd = open(logfile, logmode, S_IRUSR | S_IWUSR)) < 0) {
151         virReportSystemError(conn, errno,
152                              _("failed to create logfile %s"),
153                              logfile);
154         return -1;
155     }
156     if (virSetCloseExec(fd) < 0) {
157         virReportSystemError(conn, errno, "%s",
158                              _("Unable to set VM logfile close-on-exec flag"));
159         close(fd);
160         return -1;
161     }
162     return fd;
163 }
164
165
166 static int
167 qemudLogReadFD(virConnectPtr conn, const char* logDir, const char* name, off_t pos)
168 {
169     char logfile[PATH_MAX];
170     mode_t logmode = O_RDONLY;
171     int ret, fd = -1;
172
173     if ((ret = snprintf(logfile, sizeof(logfile), "%s/%s.log", logDir, name))
174         < 0 || ret >= sizeof(logfile)) {
175         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
176                          _("failed to build logfile name %s/%s.log"),
177                          logDir, name);
178         return -1;
179     }
180
181
182     if ((fd = open(logfile, logmode)) < 0) {
183         virReportSystemError(conn, errno,
184                              _("failed to create logfile %s"),
185                              logfile);
186         return -1;
187     }
188     if (virSetCloseExec(fd) < 0) {
189         virReportSystemError(conn, errno, "%s",
190                              _("Unable to set VM logfile close-on-exec flag"));
191         close(fd);
192         return -1;
193     }
194     if (lseek(fd, pos, SEEK_SET) < 0) {
195         virReportSystemError(conn, errno,
196                              _("Unable to seek to %lld in %s"),
197                              (long long) pos, logfile);
198         close(fd);
199     }
200     return fd;
201 }
202
203
204 static void
205 qemudAutostartConfigs(struct qemud_driver *driver) {
206     unsigned int i;
207     /* XXX: Figure out a better way todo this. The domain
208      * startup code needs a connection handle in order
209      * to lookup the bridge associated with a virtual
210      * network
211      */
212     virConnectPtr conn = virConnectOpen(driver->privileged ?
213                                         "qemu:///system" :
214                                         "qemu:///session");
215     /* Ignoring NULL conn which is mostly harmless here */
216
217     qemuDriverLock(driver);
218     for (i = 0 ; i < driver->domains.count ; i++) {
219         virDomainObjPtr vm = driver->domains.objs[i];
220         virDomainObjLock(vm);
221         if (vm->autostart &&
222             !virDomainIsActive(vm)) {
223             int ret = qemudStartVMDaemon(conn, driver, vm, NULL, -1);
224             if (ret < 0) {
225                 virErrorPtr err = virGetLastError();
226                 VIR_ERROR(_("Failed to autostart VM '%s': %s\n"),
227                           vm->def->name,
228                           err ? err->message : "");
229             } else {
230                 virDomainEventPtr event =
231                     virDomainEventNewFromObj(vm,
232                                              VIR_DOMAIN_EVENT_STARTED,
233                                              VIR_DOMAIN_EVENT_STARTED_BOOTED);
234                 if (event)
235                     qemuDomainEventQueue(driver, event);
236             }
237         }
238         virDomainObjUnlock(vm);
239     }
240     qemuDriverUnlock(driver);
241
242     if (conn)
243         virConnectClose(conn);
244 }
245
246
247 /**
248  * qemudRemoveDomainStatus
249  *
250  * remove all state files of a domain from statedir
251  *
252  * Returns 0 on success
253  */
254 static int
255 qemudRemoveDomainStatus(virConnectPtr conn,
256                         struct qemud_driver *driver,
257                         virDomainObjPtr vm)
258 {
259     int rc = -1;
260     char *file = NULL;
261
262     if (virAsprintf(&file, "%s/%s.xml", driver->stateDir, vm->def->name) < 0) {
263         virReportOOMError(conn);
264         goto cleanup;
265     }
266
267     if (unlink(file) < 0 && errno != ENOENT && errno != ENOTDIR) {
268         qemudReportError(conn, vm, NULL, VIR_ERR_INTERNAL_ERROR,
269                          _("Failed to unlink status file %s"), file);
270         goto cleanup;
271     }
272
273     if(virFileDeletePid(driver->stateDir, vm->def->name))
274         goto cleanup;
275
276     rc = 0;
277 cleanup:
278     VIR_FREE(file);
279     return rc;
280 }
281
282
283 static int qemudOpenMonitor(virConnectPtr conn,
284                             struct qemud_driver* driver,
285                             virDomainObjPtr vm,
286                             int reconnect);
287
288
289 /*
290  * Open an existing VM's monitor, re-detect VCPU threads
291  * and re-reserve the security labels in use
292  */
293 static int
294 qemuReconnectDomain(struct qemud_driver *driver,
295                     virDomainObjPtr obj)
296 {
297     int rc;
298
299     if ((rc = qemudOpenMonitor(NULL, driver, obj, 1)) != 0) {
300         VIR_ERROR(_("Failed to reconnect monitor for %s: %d\n"),
301                   obj->def->name, rc);
302         goto error;
303     }
304
305     if (qemudDetectVcpuPIDs(NULL, obj) < 0) {
306         goto error;
307     }
308
309     if (obj->def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC &&
310         driver->securityDriver &&
311         driver->securityDriver->domainReserveSecurityLabel &&
312         driver->securityDriver->domainReserveSecurityLabel(NULL, obj) < 0)
313         return -1;
314
315     if (obj->def->id >= driver->nextvmid)
316         driver->nextvmid = obj->def->id + 1;
317
318     return 0;
319
320 error:
321     return -1;
322 }
323
324 /**
325  * qemudReconnectVMs
326  *
327  * Try to re-open the resources for live VMs that we care
328  * about.
329  */
330 static void
331 qemuReconnectDomains(struct qemud_driver *driver)
332 {
333     int i;
334
335     for (i = 0 ; i < driver->domains.count ; i++) {
336         virDomainObjPtr obj = driver->domains.objs[i];
337
338         virDomainObjLock(obj);
339         if (qemuReconnectDomain(driver, obj) < 0) {
340             /* If we can't get the monitor back, then kill the VM
341              * so user has ability to start it again later without
342              * danger of ending up running twice */
343             qemudShutdownVMDaemon(NULL, driver, obj);
344         }
345         virDomainObjUnlock(obj);
346     }
347 }
348
349
350 static int
351 qemudSecurityCapsInit(virSecurityDriverPtr secdrv,
352                       virCapsPtr caps)
353 {
354     const char *doi, *model;
355
356     doi = virSecurityDriverGetDOI(secdrv);
357     model = virSecurityDriverGetModel(secdrv);
358
359     caps->host.secModel.model = strdup(model);
360     if (!caps->host.secModel.model) {
361         char ebuf[1024];
362         VIR_ERROR(_("Failed to copy secModel model: %s"),
363                   virStrerror(errno, ebuf, sizeof ebuf));
364         return -1;
365     }
366
367     caps->host.secModel.doi = strdup(doi);
368     if (!caps->host.secModel.doi) {
369         char ebuf[1024];
370         VIR_ERROR(_("Failed to copy secModel DOI: %s"),
371                   virStrerror(errno, ebuf, sizeof ebuf));
372         return -1;
373     }
374
375     VIR_DEBUG("Initialized caps for security driver \"%s\" with "
376               "DOI \"%s\"", model, doi);
377
378     return 0;
379 }
380
381
382 static int
383 qemudSecurityInit(struct qemud_driver *qemud_drv)
384 {
385     int ret;
386     virSecurityDriverPtr security_drv;
387
388     ret = virSecurityDriverStartup(&security_drv,
389                                    qemud_drv->securityDriverName);
390     if (ret == -1) {
391         VIR_ERROR0(_("Failed to start security driver"));
392         return -1;
393     }
394     /* No security driver wanted to be enabled: just return */
395     if (ret == -2) {
396         VIR_INFO0(_("No security driver available"));
397         return 0;
398     }
399
400     qemud_drv->securityDriver = security_drv;
401
402     VIR_INFO("Initialized security driver %s", security_drv->name);
403
404     /*
405      * Add security policy host caps now that the security driver is
406      * initialized.
407      */
408     return qemudSecurityCapsInit(security_drv, qemud_drv->caps);
409 }
410
411
412
413 /**
414  * qemudStartup:
415  *
416  * Initialization function for the QEmu daemon
417  */
418 static int
419 qemudStartup(int privileged) {
420     char *base = NULL;
421     char driverConf[PATH_MAX];
422
423     if (VIR_ALLOC(qemu_driver) < 0)
424         return -1;
425
426     if (virMutexInit(&qemu_driver->lock) < 0) {
427         VIR_ERROR("%s", _("cannot initialize mutex"));
428         VIR_FREE(qemu_driver);
429         return -1;
430     }
431     qemuDriverLock(qemu_driver);
432     qemu_driver->privileged = privileged;
433
434     /* Don't have a dom0 so start from 1 */
435     qemu_driver->nextvmid = 1;
436
437     /* Init callback list */
438     if(VIR_ALLOC(qemu_driver->domainEventCallbacks) < 0)
439         goto out_of_memory;
440     if (!(qemu_driver->domainEventQueue = virDomainEventQueueNew()))
441         goto out_of_memory;
442
443     if ((qemu_driver->domainEventTimer =
444          virEventAddTimeout(-1, qemuDomainEventFlush, qemu_driver, NULL)) < 0)
445         goto error;
446
447     if (privileged) {
448         if (virAsprintf(&qemu_driver->logDir,
449                         "%s/log/libvirt/qemu", LOCAL_STATE_DIR) == -1)
450             goto out_of_memory;
451
452         if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
453             goto out_of_memory;
454
455         if (virAsprintf(&qemu_driver->stateDir,
456                       "%s/run/libvirt/qemu/", LOCAL_STATE_DIR) == -1)
457             goto out_of_memory;
458     } else {
459         uid_t uid = geteuid();
460         char *userdir = virGetUserDirectory(NULL, uid);
461         if (!userdir)
462             goto error;
463
464         if (virAsprintf(&qemu_driver->logDir,
465                         "%s/.libvirt/qemu/log", userdir) == -1) {
466             VIR_FREE(userdir);
467             goto out_of_memory;
468         }
469
470         if (virAsprintf(&base, "%s/.libvirt", userdir) == -1) {
471             VIR_FREE(userdir);
472             goto out_of_memory;
473         }
474         VIR_FREE(userdir);
475
476         if (virAsprintf(&qemu_driver->stateDir, "%s/qemu/run", base) == -1)
477             goto out_of_memory;
478     }
479
480     if (virFileMakePath(qemu_driver->stateDir) < 0) {
481         char ebuf[1024];
482         VIR_ERROR(_("Failed to create state dir '%s': %s\n"),
483                   qemu_driver->stateDir, virStrerror(errno, ebuf, sizeof ebuf));
484         goto error;
485     }
486
487     /* Configuration paths are either ~/.libvirt/qemu/... (session) or
488      * /etc/libvirt/qemu/... (system).
489      */
490     if (snprintf (driverConf, sizeof(driverConf), "%s/qemu.conf", base) == -1)
491         goto out_of_memory;
492     driverConf[sizeof(driverConf)-1] = '\0';
493
494     if (virAsprintf(&qemu_driver->configDir, "%s/qemu", base) == -1)
495         goto out_of_memory;
496
497     if (virAsprintf(&qemu_driver->autostartDir, "%s/qemu/autostart", base) == -1)
498         goto out_of_memory;
499
500     VIR_FREE(base);
501
502     if ((qemu_driver->caps = qemudCapsInit()) == NULL)
503         goto out_of_memory;
504
505     if (qemudLoadDriverConfig(qemu_driver, driverConf) < 0) {
506         goto error;
507     }
508
509     if (qemudSecurityInit(qemu_driver) < 0) {
510         goto error;
511     }
512
513     /* Get all the running persistent or transient configs first */
514     if (virDomainLoadAllConfigs(NULL,
515                                 qemu_driver->caps,
516                                 &qemu_driver->domains,
517                                 qemu_driver->stateDir,
518                                 NULL,
519                                 1, NULL, NULL) < 0)
520         goto error;
521
522     qemuReconnectDomains(qemu_driver);
523
524     /* Then inactive persistent configs */
525     if (virDomainLoadAllConfigs(NULL,
526                                 qemu_driver->caps,
527                                 &qemu_driver->domains,
528                                 qemu_driver->configDir,
529                                 qemu_driver->autostartDir,
530                                 0, NULL, NULL) < 0)
531         goto error;
532     qemuDriverUnlock(qemu_driver);
533
534     qemudAutostartConfigs(qemu_driver);
535
536
537     return 0;
538
539 out_of_memory:
540     virReportOOMError(NULL);
541 error:
542     if (qemu_driver)
543         qemuDriverUnlock(qemu_driver);
544     VIR_FREE(base);
545     qemudShutdown();
546     return -1;
547 }
548
549 static void qemudNotifyLoadDomain(virDomainObjPtr vm, int newVM, void *opaque)
550 {
551     struct qemud_driver *driver = opaque;
552
553     if (newVM) {
554         virDomainEventPtr event =
555             virDomainEventNewFromObj(vm,
556                                      VIR_DOMAIN_EVENT_DEFINED,
557                                      VIR_DOMAIN_EVENT_DEFINED_ADDED);
558         if (event)
559             qemuDomainEventQueue(driver, event);
560     }
561 }
562
563 /**
564  * qemudReload:
565  *
566  * Function to restart the QEmu daemon, it will recheck the configuration
567  * files and update its state and the networking
568  */
569 static int
570 qemudReload(void) {
571     if (!qemu_driver)
572         return 0;
573
574     qemuDriverLock(qemu_driver);
575     virDomainLoadAllConfigs(NULL,
576                             qemu_driver->caps,
577                             &qemu_driver->domains,
578                             qemu_driver->configDir,
579                             qemu_driver->autostartDir,
580                             0, qemudNotifyLoadDomain, qemu_driver);
581     qemuDriverUnlock(qemu_driver);
582
583     qemudAutostartConfigs(qemu_driver);
584
585     return 0;
586 }
587
588 /**
589  * qemudActive:
590  *
591  * Checks if the QEmu daemon is active, i.e. has an active domain or
592  * an active network
593  *
594  * Returns 1 if active, 0 otherwise
595  */
596 static int
597 qemudActive(void) {
598     unsigned int i;
599     int active = 0;
600
601     if (!qemu_driver)
602         return 0;
603
604     qemuDriverLock(qemu_driver);
605     for (i = 0 ; i < qemu_driver->domains.count ; i++) {
606         virDomainObjPtr vm = qemu_driver->domains.objs[i];
607         virDomainObjLock(vm);
608         if (virDomainIsActive(vm))
609             active = 1;
610         virDomainObjUnlock(vm);
611     }
612
613     qemuDriverUnlock(qemu_driver);
614     return active;
615 }
616
617 /**
618  * qemudShutdown:
619  *
620  * Shutdown the QEmu daemon, it will stop all active domains and networks
621  */
622 static int
623 qemudShutdown(void) {
624
625     if (!qemu_driver)
626         return -1;
627
628     qemuDriverLock(qemu_driver);
629     virCapabilitiesFree(qemu_driver->caps);
630
631     virDomainObjListFree(&qemu_driver->domains);
632
633     VIR_FREE(qemu_driver->securityDriverName);
634     VIR_FREE(qemu_driver->logDir);
635     VIR_FREE(qemu_driver->configDir);
636     VIR_FREE(qemu_driver->autostartDir);
637     VIR_FREE(qemu_driver->stateDir);
638     VIR_FREE(qemu_driver->vncTLSx509certdir);
639     VIR_FREE(qemu_driver->vncListen);
640     VIR_FREE(qemu_driver->vncPassword);
641     VIR_FREE(qemu_driver->vncSASLdir);
642
643     /* Free domain callback list */
644     virDomainEventCallbackListFree(qemu_driver->domainEventCallbacks);
645     virDomainEventQueueFree(qemu_driver->domainEventQueue);
646
647     if (qemu_driver->domainEventTimer != -1)
648         virEventRemoveTimeout(qemu_driver->domainEventTimer);
649
650     if (qemu_driver->brctl)
651         brShutdown(qemu_driver->brctl);
652
653     qemuDriverUnlock(qemu_driver);
654     virMutexDestroy(&qemu_driver->lock);
655     VIR_FREE(qemu_driver);
656
657     return 0;
658 }
659
660 /* Return -1 for error, 1 to continue reading and 0 for success */
661 typedef int qemudHandlerMonitorOutput(virConnectPtr conn,
662                                       virDomainObjPtr vm,
663                                       const char *output,
664                                       int fd);
665
666 /*
667  * Returns -1 for error, 0 on end-of-file, 1 for success
668  */
669 static int
670 qemudReadMonitorOutput(virConnectPtr conn,
671                        virDomainObjPtr vm,
672                        int fd,
673                        char *buf,
674                        size_t buflen,
675                        qemudHandlerMonitorOutput func,
676                        const char *what,
677                        int timeout)
678 {
679     size_t got = 0;
680     buf[0] = '\0';
681     timeout *= 1000; /* poll wants milli seconds */
682
683     /* Consume & discard the initial greeting */
684     while (got < (buflen-1)) {
685         ssize_t ret;
686
687         ret = read(fd, buf+got, buflen-got-1);
688
689         if (ret < 0) {
690             struct pollfd pfd = { .fd = fd, .events = POLLIN };
691             if (errno == EINTR)
692                 continue;
693
694             if (errno != EAGAIN) {
695                 virReportSystemError(conn, errno,
696                                      _("Failure while reading %s startup output"),
697                                      what);
698                 return -1;
699             }
700
701             ret = poll(&pfd, 1, timeout);
702             if (ret == 0) {
703                 qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
704                                  _("Timed out while reading %s startup output"), what);
705                 return -1;
706             } else if (ret == -1) {
707                 if (errno != EINTR) {
708                     virReportSystemError(conn, errno,
709                                          _("Failure while reading %s startup output"),
710                                          what);
711                     return -1;
712                 }
713             } else {
714                 /* Make sure we continue loop & read any further data
715                    available before dealing with EOF */
716                 if (pfd.revents & (POLLIN | POLLHUP))
717                     continue;
718
719                 qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
720                                  _("Failure while reading %s startup output"), what);
721                 return -1;
722             }
723         } else if (ret == 0) {
724             return 0;
725         } else {
726             got += ret;
727             buf[got] = '\0';
728             ret = func(conn, vm, buf, fd);
729             if (ret == -1)
730                 return -1;
731             if (ret == 1)
732                 continue;
733             return 1;
734         }
735     }
736
737     qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
738                      _("Out of space while reading %s startup output"), what);
739     return -1;
740
741 }
742
743
744 /*
745  * Returns -1 for error, 0 on success
746  */
747 static int
748 qemudReadLogOutput(virConnectPtr conn,
749                    virDomainObjPtr vm,
750                    int fd,
751                    char *buf,
752                    size_t buflen,
753                    qemudHandlerMonitorOutput func,
754                    const char *what,
755                    int timeout)
756 {
757     int retries = (timeout*10);
758     int got = 0;
759     buf[0] = '\0';
760
761     while (retries) {
762         ssize_t func_ret, ret;
763         int isdead = 0;
764
765         func_ret = func(conn, vm, buf, fd);
766
767         if (kill(vm->pid, 0) == -1 && errno == ESRCH)
768             isdead = 1;
769
770         /* Any failures should be detected before we read the log, so we
771          * always have something useful to report on failure. */
772         ret = saferead(fd, buf+got, buflen-got-1);
773         if (ret < 0) {
774             virReportSystemError(conn, errno,
775                                  _("Failure while reading %s log output"),
776                                  what);
777             return -1;
778         }
779
780         got += ret;
781         buf[got] = '\0';
782         if (got == buflen-1) {
783             qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
784                              _("Out of space while reading %s log output"),
785                              what);
786             return -1;
787         }
788
789         if (isdead) {
790             qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
791                              _("Process exited while reading %s log output"),
792                              what);
793             return -1;
794         }
795
796         if (func_ret <= 0)
797             return func_ret;
798
799         usleep(100*1000);
800         retries--;
801     }
802     if (retries == 0)
803         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
804                          _("Timed out while reading %s log output"), what);
805     return -1;
806 }
807
808 static int
809 qemudCheckMonitorPrompt(virConnectPtr conn ATTRIBUTE_UNUSED,
810                         virDomainObjPtr vm,
811                         const char *output,
812                         int fd)
813 {
814     if (strstr(output, "(qemu) ") == NULL)
815         return 1; /* keep reading */
816
817     vm->monitor = fd;
818
819     return 0;
820 }
821
822 static int
823 qemudOpenMonitorCommon(virConnectPtr conn,
824                        struct qemud_driver* driver,
825                        virDomainObjPtr vm,
826                        int monfd,
827                        int reconnect)
828 {
829     char buf[1024];
830     int ret;
831
832     if (virSetCloseExec(monfd) < 0) {
833         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
834                          "%s", _("Unable to set monitor close-on-exec flag"));
835         return -1;
836     }
837     if (virSetNonBlock(monfd) < 0) {
838         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
839                          "%s", _("Unable to put monitor into non-blocking mode"));
840         return -1;
841     }
842
843     if (!reconnect) {
844         if (qemudReadMonitorOutput(conn,
845                                    vm, monfd,
846                                    buf, sizeof(buf),
847                                    qemudCheckMonitorPrompt,
848                                    "monitor", 10) <= 0)
849             ret = -1;
850         else
851             ret = 0;
852     } else {
853         vm->monitor = monfd;
854         ret = 0;
855     }
856
857     if (ret != 0)
858         return ret;
859
860     if ((vm->monitorWatch = virEventAddHandle(vm->monitor, 0,
861                                               qemudDispatchVMEvent,
862                                               driver, NULL)) < 0)
863         return -1;
864
865     return 0;
866 }
867
868 static int
869 qemudOpenMonitorUnix(virConnectPtr conn,
870                      struct qemud_driver* driver,
871                      virDomainObjPtr vm,
872                      const char *monitor,
873                      int reconnect)
874 {
875     struct sockaddr_un addr;
876     int monfd;
877
878     if ((monfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
879         virReportSystemError(conn, errno,
880                              "%s", _("failed to create socket"));
881         return -1;
882     }
883
884     memset(&addr, 0, sizeof(addr));
885     addr.sun_family = AF_UNIX;
886     strncpy(addr.sun_path, monitor, sizeof(addr.sun_path));
887
888     if (connect(monfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
889         virReportSystemError(conn, errno, "%s",
890                              _("failed to connect to monitor socket"));
891         goto error;
892     }
893
894     if (qemudOpenMonitorCommon(conn, driver, vm, monfd, reconnect) < 0)
895         goto error;
896
897     return 0;
898
899 error:
900     close(monfd);
901     return -1;
902 }
903
904 static int
905 qemudOpenMonitorPty(virConnectPtr conn,
906                     struct qemud_driver* driver,
907                     virDomainObjPtr vm,
908                     const char *monitor,
909                     int reconnect)
910 {
911     int monfd;
912
913     if ((monfd = open(monitor, O_RDWR)) < 0) {
914         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
915                          _("Unable to open monitor path %s"), monitor);
916         return -1;
917     }
918
919     if (qemudOpenMonitorCommon(conn, driver, vm, monfd, reconnect) < 0)
920         goto error;
921
922     return 0;
923
924 error:
925     close(monfd);
926     return -1;
927 }
928
929 static int
930 qemudOpenMonitor(virConnectPtr conn,
931                  struct qemud_driver *driver,
932                  virDomainObjPtr vm,
933                  int reconnect)
934 {
935     switch (vm->monitor_chr->type) {
936     case VIR_DOMAIN_CHR_TYPE_UNIX:
937         return qemudOpenMonitorUnix(conn, driver, vm,
938                                     vm->monitor_chr->data.nix.path,
939                                     reconnect);
940     case VIR_DOMAIN_CHR_TYPE_PTY:
941         return qemudOpenMonitorPty(conn, driver, vm,
942                                    vm->monitor_chr->data.file.path,
943                                    reconnect);
944     default:
945         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
946                          _("unable to handle monitor type: %s"),
947                          virDomainChrTypeToString(vm->monitor_chr->type));
948         return -1;
949     }
950 }
951
952 /* Returns -1 for error, 0 success, 1 continue reading */
953 static int
954 qemudExtractMonitorPath(virConnectPtr conn,
955                         const char *haystack,
956                         size_t *offset,
957                         char **path)
958 {
959     static const char needle[] = "char device redirected to";
960     char *tmp, *dev;
961
962     VIR_FREE(*path);
963     /* First look for our magic string */
964     if (!(tmp = strstr(haystack + *offset, needle))) {
965         return 1;
966     }
967     tmp += sizeof(needle);
968     dev = tmp;
969
970     /*
971      * And look for first whitespace character and nul terminate
972      * to mark end of the pty path
973      */
974     while (*tmp) {
975         if (c_isspace(*tmp)) {
976             if (VIR_ALLOC_N(*path, (tmp-dev)+1) < 0) {
977                 virReportOOMError(conn);
978                 return -1;
979             }
980             strncpy(*path, dev, (tmp-dev));
981             (*path)[(tmp-dev)] = '\0';
982             /* ... now further update offset till we get EOL */
983             *offset = tmp - haystack;
984             return 0;
985         }
986         tmp++;
987     }
988
989     /*
990      * We found a path, but didn't find any whitespace,
991      * so it must be still incomplete - we should at
992      * least see a \n - indicate that we want to carry
993      * on trying again
994      */
995     return 1;
996 }
997
998 static int
999 qemudFindCharDevicePTYs(virConnectPtr conn,
1000                         virDomainObjPtr vm,
1001                         const char *output,
1002                         int fd ATTRIBUTE_UNUSED)
1003 {
1004     size_t offset = 0;
1005     int ret, i;
1006
1007     /* The order in which QEMU prints out the PTY paths is
1008        the order in which it procsses its serial and parallel
1009        device args. This code must match that ordering.... */
1010
1011     /* first comes the serial devices */
1012     for (i = 0 ; i < vm->def->nserials ; i++) {
1013         virDomainChrDefPtr chr = vm->def->serials[i];
1014         if (chr->type == VIR_DOMAIN_CHR_TYPE_PTY) {
1015             if ((ret = qemudExtractMonitorPath(conn, output, &offset,
1016                                                &chr->data.file.path)) != 0)
1017                 return ret;
1018         }
1019     }
1020
1021     /* then the parallel devices */
1022     for (i = 0 ; i < vm->def->nparallels ; i++) {
1023         virDomainChrDefPtr chr = vm->def->parallels[i];
1024         if (chr->type == VIR_DOMAIN_CHR_TYPE_PTY) {
1025             if ((ret = qemudExtractMonitorPath(conn, output, &offset,
1026                                                &chr->data.file.path)) != 0)
1027                 return ret;
1028         }
1029     }
1030
1031     return 0;
1032 }
1033
1034 static int
1035 qemudWaitForMonitor(virConnectPtr conn,
1036                     struct qemud_driver* driver,
1037                     virDomainObjPtr vm, off_t pos)
1038 {
1039     char buf[4096]; /* Plenty of space to get startup greeting */
1040     int logfd;
1041     int ret;
1042
1043     if ((logfd = qemudLogReadFD(conn, driver->logDir, vm->def->name, pos))
1044         < 0)
1045         return -1;
1046
1047     ret = qemudReadLogOutput(conn, vm, logfd, buf, sizeof(buf),
1048                              qemudFindCharDevicePTYs,
1049                              "console", 3);
1050     if (close(logfd) < 0) {
1051         char ebuf[4096];
1052         VIR_WARN(_("Unable to close logfile: %s\n"),
1053                  virStrerror(errno, ebuf, sizeof ebuf));
1054     }
1055
1056     if (ret < 0) {
1057         /* Unexpected end of file - inform user of QEMU log data */
1058         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1059                          _("unable to start guest: %s"), buf);
1060         return -1;
1061     }
1062
1063     if (qemudOpenMonitor(conn, driver, vm, 0) < 0)
1064         return -1;
1065
1066     return 0;
1067 }
1068
1069 static int
1070 qemudDetectVcpuPIDs(virConnectPtr conn,
1071                     virDomainObjPtr vm) {
1072     char *qemucpus = NULL;
1073     char *line;
1074     int lastVcpu = -1;
1075
1076     /* Only KVM has seperate threads for CPUs,
1077        others just use main QEMU process for CPU */
1078     if (vm->def->virtType != VIR_DOMAIN_VIRT_KVM)
1079         vm->nvcpupids = 1;
1080     else
1081         vm->nvcpupids = vm->def->vcpus;
1082
1083     if (VIR_ALLOC_N(vm->vcpupids, vm->nvcpupids) < 0) {
1084         virReportOOMError(conn);
1085         return -1;
1086     }
1087
1088     if (vm->def->virtType != VIR_DOMAIN_VIRT_KVM) {
1089         vm->vcpupids[0] = vm->pid;
1090         return 0;
1091     }
1092
1093     if (qemudMonitorCommand(vm, "info cpus", &qemucpus) < 0) {
1094         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1095                          "%s", _("cannot run monitor command to fetch CPU thread info"));
1096         VIR_FREE(vm->vcpupids);
1097         vm->nvcpupids = 0;
1098         return -1;
1099     }
1100
1101     /*
1102      * This is the gross format we're about to parse :-{
1103      *
1104      * (qemu) info cpus
1105      * * CPU #0: pc=0x00000000000f0c4a thread_id=30019
1106      *   CPU #1: pc=0x00000000fffffff0 thread_id=30020
1107      *   CPU #2: pc=0x00000000fffffff0 thread_id=30021
1108      *
1109      */
1110     line = qemucpus;
1111     do {
1112         char *offset = strchr(line, '#');
1113         char *end = NULL;
1114         int vcpu = 0, tid = 0;
1115
1116         /* See if we're all done */
1117         if (offset == NULL)
1118             break;
1119
1120         /* Extract VCPU number */
1121         if (virStrToLong_i(offset + 1, &end, 10, &vcpu) < 0)
1122             goto error;
1123         if (end == NULL || *end != ':')
1124             goto error;
1125
1126         /* Extract host Thread ID */
1127         if ((offset = strstr(line, "thread_id=")) == NULL)
1128             goto error;
1129         if (virStrToLong_i(offset + strlen("thread_id="), &end, 10, &tid) < 0)
1130             goto error;
1131         if (end == NULL || !c_isspace(*end))
1132             goto error;
1133
1134         /* Validate the VCPU is in expected range & order */
1135         if (vcpu > vm->nvcpupids ||
1136             vcpu != (lastVcpu + 1))
1137             goto error;
1138
1139         lastVcpu = vcpu;
1140         vm->vcpupids[vcpu] = tid;
1141
1142         /* Skip to next data line */
1143         line = strchr(offset, '\r');
1144         if (line == NULL)
1145             line = strchr(offset, '\n');
1146     } while (line != NULL);
1147
1148     /* Validate we got data for all VCPUs we expected */
1149     if (lastVcpu != (vm->def->vcpus - 1))
1150         goto error;
1151
1152     VIR_FREE(qemucpus);
1153     return 0;
1154
1155 error:
1156     VIR_FREE(vm->vcpupids);
1157     vm->nvcpupids = 0;
1158     VIR_FREE(qemucpus);
1159
1160     /* Explicitly return success, not error. Older KVM does
1161        not have vCPU -> Thread mapping info and we don't
1162        want to break its use. This merely disables ability
1163        to pin vCPUS with libvirt */
1164     return 0;
1165 }
1166
1167 static int
1168 qemudInitCpus(virConnectPtr conn,
1169               virDomainObjPtr vm,
1170               const char *migrateFrom) {
1171     char *info = NULL;
1172 #if HAVE_SCHED_GETAFFINITY
1173     cpu_set_t mask;
1174     int i, maxcpu = QEMUD_CPUMASK_LEN;
1175     virNodeInfo nodeinfo;
1176
1177     if (nodeGetInfo(conn, &nodeinfo) < 0)
1178         return -1;
1179
1180     /* setaffinity fails if you set bits for CPUs which
1181      * aren't present, so we have to limit ourselves */
1182     if (maxcpu > nodeinfo.cpus)
1183         maxcpu = nodeinfo.cpus;
1184
1185     CPU_ZERO(&mask);
1186     if (vm->def->cpumask) {
1187         for (i = 0 ; i < maxcpu ; i++)
1188             if (vm->def->cpumask[i])
1189                 CPU_SET(i, &mask);
1190     } else {
1191         for (i = 0 ; i < maxcpu ; i++)
1192             CPU_SET(i, &mask);
1193     }
1194
1195     for (i = 0 ; i < vm->nvcpupids ; i++) {
1196         if (sched_setaffinity(vm->vcpupids[i],
1197                               sizeof(mask), &mask) < 0) {
1198             virReportSystemError(conn, errno, "%s",
1199                                  _("failed to set CPU affinity"));
1200             return -1;
1201         }
1202     }
1203 #endif /* HAVE_SCHED_GETAFFINITY */
1204
1205     if (migrateFrom == NULL) {
1206         /* Allow the CPUS to start executing */
1207         if (qemudMonitorCommand(vm, "cont", &info) < 0) {
1208             qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1209                              "%s", _("resume operation failed"));
1210             return -1;
1211         }
1212         VIR_FREE(info);
1213     }
1214
1215     return 0;
1216 }
1217
1218
1219 static int
1220 qemudInitPasswords(virConnectPtr conn,
1221                    struct qemud_driver *driver,
1222                    virDomainObjPtr vm) {
1223     char *info = NULL;
1224
1225     /*
1226      * NB: Might have more passwords to set in the future. eg a qcow
1227      * disk decryption password, but there's no monitor command
1228      * for that yet...
1229      */
1230
1231     if ((vm->def->ngraphics == 1) &&
1232         vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
1233         (vm->def->graphics[0]->data.vnc.passwd || driver->vncPassword)) {
1234
1235         if (qemudMonitorCommandExtra(vm, "change vnc password",
1236                                      vm->def->graphics[0]->data.vnc.passwd ?
1237                                      vm->def->graphics[0]->data.vnc.passwd :
1238                                      driver->vncPassword,
1239                                      QEMU_PASSWD_PROMPT,
1240                                      &info) < 0) {
1241             qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1242                              "%s", _("setting VNC password failed"));
1243             return -1;
1244         }
1245         VIR_FREE(info);
1246     }
1247
1248     return 0;
1249 }
1250
1251
1252 static int qemudNextFreeVNCPort(struct qemud_driver *driver ATTRIBUTE_UNUSED) {
1253     int i;
1254
1255     for (i = 5900 ; i < 6000 ; i++) {
1256         int fd;
1257         int reuse = 1;
1258         struct sockaddr_in addr;
1259         addr.sin_family = AF_INET;
1260         addr.sin_port = htons(i);
1261         addr.sin_addr.s_addr = htonl(INADDR_ANY);
1262         fd = socket(PF_INET, SOCK_STREAM, 0);
1263         if (fd < 0)
1264             return -1;
1265
1266         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&reuse, sizeof(reuse)) < 0) {
1267             close(fd);
1268             break;
1269         }
1270
1271         if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
1272             /* Not in use, lets grab it */
1273             close(fd);
1274             return i;
1275         }
1276         close(fd);
1277
1278         if (errno == EADDRINUSE) {
1279             /* In use, try next */
1280             continue;
1281         }
1282         /* Some other bad failure, get out.. */
1283         break;
1284     }
1285     return -1;
1286 }
1287
1288 static int qemuPrepareHostDevices(virConnectPtr conn,
1289                                   virDomainDefPtr def) {
1290     int i;
1291
1292     /* We have to use 2 loops here. *All* devices must
1293      * be detached before we reset any of them, because
1294      * in some cases you have to reset the whole PCI,
1295      * which impacts all devices on it
1296      */
1297
1298     for (i = 0 ; i < def->nhostdevs ; i++) {
1299         virDomainHostdevDefPtr hostdev = def->hostdevs[i];
1300
1301         if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
1302             continue;
1303         if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
1304             continue;
1305
1306         if (hostdev->managed) {
1307             pciDevice *dev = pciGetDevice(conn,
1308                                           hostdev->source.subsys.u.pci.domain,
1309                                           hostdev->source.subsys.u.pci.bus,
1310                                           hostdev->source.subsys.u.pci.slot,
1311                                           hostdev->source.subsys.u.pci.function);
1312             if (!dev)
1313                 goto error;
1314
1315             if (pciDettachDevice(conn, dev) < 0) {
1316                 pciFreeDevice(conn, dev);
1317                 goto error;
1318             }
1319
1320             pciFreeDevice(conn, dev);
1321         } /* else {
1322              XXX validate that non-managed device isn't in use, eg
1323              by checking that device is either un-bound, or bound
1324              to pci-stub.ko
1325         } */
1326     }
1327
1328     /* Now that all the PCI hostdevs have be dettached, we can safely
1329      * reset them */
1330     for (i = 0 ; i < def->nhostdevs ; i++) {
1331         virDomainHostdevDefPtr hostdev = def->hostdevs[i];
1332         pciDevice *dev;
1333
1334         if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
1335             continue;
1336         if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
1337             continue;
1338
1339         dev = pciGetDevice(conn,
1340                            hostdev->source.subsys.u.pci.domain,
1341                            hostdev->source.subsys.u.pci.bus,
1342                            hostdev->source.subsys.u.pci.slot,
1343                            hostdev->source.subsys.u.pci.function);
1344         if (!dev)
1345             goto error;
1346
1347         if (pciResetDevice(conn, dev) < 0) {
1348             pciFreeDevice(conn, dev);
1349             goto error;
1350         }
1351
1352         pciFreeDevice(conn, dev);
1353     }
1354
1355     return 0;
1356
1357 error:
1358     return -1;
1359 }
1360
1361 static int qemudDomainSetSecurityLabel(virConnectPtr conn, struct qemud_driver *driver, virDomainObjPtr vm)
1362 {
1363     if (vm->def->seclabel.label != NULL)
1364         if (driver->securityDriver && driver->securityDriver->domainSetSecurityLabel)
1365             return driver->securityDriver->domainSetSecurityLabel(conn, driver->securityDriver,
1366                                                                  vm);
1367     return 0;
1368 }
1369
1370 static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
1371                                             const char *name);
1372
1373 struct gemudHookData {
1374         virConnectPtr conn;
1375         virDomainObjPtr vm;
1376         struct qemud_driver *driver;
1377 };
1378
1379 static int qemudSecurityHook(void *data) {
1380         struct gemudHookData *h = (struct gemudHookData *) data;
1381
1382         if (qemudDomainSetSecurityLabel(h->conn, h->driver, h->vm) < 0) {
1383                 qemudReportError(h->conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1384                                  _("Failed to set security label"));
1385                 return -1;
1386         }
1387         return 0;
1388 }
1389
1390 static int
1391 qemuPrepareMonitorChr(virConnectPtr conn,
1392                       struct qemud_driver *driver,
1393                       virDomainChrDefPtr monitor_chr,
1394                       const char *vm)
1395 {
1396     monitor_chr->type = VIR_DOMAIN_CHR_TYPE_UNIX;
1397     monitor_chr->data.nix.listen = 1;
1398
1399     if (virAsprintf(&monitor_chr->data.nix.path, "%s/%s.monitor",
1400                     driver->stateDir, vm) < 0) {
1401         virReportOOMError(conn);
1402         return -1;
1403     }
1404
1405     return 0;
1406 }
1407
1408 static int qemudStartVMDaemon(virConnectPtr conn,
1409                               struct qemud_driver *driver,
1410                               virDomainObjPtr vm,
1411                               const char *migrateFrom,
1412                               int stdin_fd) {
1413     const char **argv = NULL, **tmp;
1414     const char **progenv = NULL;
1415     int i, ret;
1416     struct stat sb;
1417     int *tapfds = NULL;
1418     int ntapfds = 0;
1419     unsigned int qemuCmdFlags;
1420     fd_set keepfd;
1421     const char *emulator;
1422     pid_t child;
1423     int pos = -1;
1424     char ebuf[1024];
1425     char *pidfile = NULL;
1426     int logfile;
1427
1428     struct gemudHookData hookData;
1429     hookData.conn = conn;
1430     hookData.vm = vm;
1431     hookData.driver = driver;
1432
1433     FD_ZERO(&keepfd);
1434
1435     if (virDomainIsActive(vm)) {
1436         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_INVALID,
1437                          "%s", _("VM is already active"));
1438         return -1;
1439     }
1440
1441     /* If you are using a SecurityDriver with dynamic labelling,
1442        then generate a security label for isolation */
1443     if (vm->def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC &&
1444         driver->securityDriver &&
1445         driver->securityDriver->domainGenSecurityLabel &&
1446         driver->securityDriver->domainGenSecurityLabel(conn, vm) < 0)
1447         return -1;
1448
1449     if ((vm->def->ngraphics == 1) &&
1450         vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
1451         vm->def->graphics[0]->data.vnc.autoport) {
1452         int port = qemudNextFreeVNCPort(driver);
1453         if (port < 0) {
1454             qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1455                              "%s", _("Unable to find an unused VNC port"));
1456             goto cleanup;
1457         }
1458         vm->def->graphics[0]->data.vnc.port = port;
1459     }
1460
1461     if (virFileMakePath(driver->logDir) < 0) {
1462         virReportSystemError(conn, errno,
1463                              _("cannot create log directory %s"),
1464                              driver->logDir);
1465         goto cleanup;
1466     }
1467
1468     if ((logfile = qemudLogFD(conn, driver, vm->def->name)) < 0)
1469         goto cleanup;
1470
1471     emulator = vm->def->emulator;
1472
1473     /* Make sure the binary we are about to try exec'ing exists.
1474      * Technically we could catch the exec() failure, but that's
1475      * in a sub-process so its hard to feed back a useful error
1476      */
1477     if (stat(emulator, &sb) < 0) {
1478         virReportSystemError(conn, errno,
1479                              _("Cannot find QEMU binary %s"),
1480                              emulator);
1481         goto cleanup;
1482     }
1483
1484     if (qemudExtractVersionInfo(emulator,
1485                                 NULL,
1486                                 &qemuCmdFlags) < 0)
1487         goto cleanup;
1488
1489     if (qemuPrepareHostDevices(conn, vm->def) < 0)
1490         goto cleanup;
1491
1492     if (VIR_ALLOC(vm->monitor_chr) < 0) {
1493         virReportOOMError(conn);
1494         goto cleanup;
1495     }
1496
1497     if (qemuPrepareMonitorChr(conn, driver, vm->monitor_chr, vm->def->name) < 0)
1498         goto cleanup;
1499
1500     if ((ret = virFileDeletePid(driver->stateDir, vm->def->name)) != 0) {
1501         virReportSystemError(conn, ret,
1502                              _("Cannot remove stale PID file for %s"),
1503                              vm->def->name);
1504         goto cleanup;
1505     }
1506
1507     if (!(pidfile = virFilePid(driver->stateDir, vm->def->name))) {
1508         virReportSystemError(conn, errno,
1509                              "%s", _("Failed to build pidfile path."));
1510         goto cleanup;
1511     }
1512
1513     vm->def->id = driver->nextvmid++;
1514     if (qemudBuildCommandLine(conn, driver, vm->def, vm->monitor_chr,
1515                               qemuCmdFlags, &argv, &progenv,
1516                               &tapfds, &ntapfds, migrateFrom) < 0)
1517         goto cleanup;
1518
1519     tmp = progenv;
1520     while (*tmp) {
1521         if (safewrite(logfile, *tmp, strlen(*tmp)) < 0)
1522             VIR_WARN(_("Unable to write envv to logfile: %s\n"),
1523                      virStrerror(errno, ebuf, sizeof ebuf));
1524         if (safewrite(logfile, " ", 1) < 0)
1525             VIR_WARN(_("Unable to write envv to logfile: %s\n"),
1526                      virStrerror(errno, ebuf, sizeof ebuf));
1527         tmp++;
1528     }
1529     tmp = argv;
1530     while (*tmp) {
1531         if (safewrite(logfile, *tmp, strlen(*tmp)) < 0)
1532             VIR_WARN(_("Unable to write argv to logfile: %s\n"),
1533                      virStrerror(errno, ebuf, sizeof ebuf));
1534         if (safewrite(logfile, " ", 1) < 0)
1535             VIR_WARN(_("Unable to write argv to logfile: %s\n"),
1536                      virStrerror(errno, ebuf, sizeof ebuf));
1537         tmp++;
1538     }
1539     if (safewrite(logfile, "\n", 1) < 0)
1540         VIR_WARN(_("Unable to write argv to logfile: %s\n"),
1541                  virStrerror(errno, ebuf, sizeof ebuf));
1542
1543     if ((pos = lseek(logfile, 0, SEEK_END)) < 0)
1544         VIR_WARN(_("Unable to seek to end of logfile: %s\n"),
1545                  virStrerror(errno, ebuf, sizeof ebuf));
1546
1547     for (i = 0 ; i < ntapfds ; i++)
1548         FD_SET(tapfds[i], &keepfd);
1549
1550     ret = virExecDaemonize(conn, argv, progenv, &keepfd, &child,
1551                            stdin_fd, &logfile, &logfile,
1552                            VIR_EXEC_NONBLOCK | VIR_EXEC_CLEAR_CAPS,
1553                            qemudSecurityHook, &hookData,
1554                            pidfile);
1555     VIR_FREE(pidfile);
1556
1557     /* wait for qemu process to to show up */
1558     if (ret == 0) {
1559         if (virFileReadPid(driver->stateDir, vm->def->name, &vm->pid)) {
1560             qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1561                              _("Domain %s didn't show up\n"), vm->def->name);
1562             ret = -1;
1563         }
1564     } else if (ret == -2) {
1565         /* The virExec process that launches the daemon failed. Pending on
1566          * when it failed (we can't determine for sure), there may be
1567          * extra info in the domain log (if the hook failed for example).
1568          *
1569          * Pretend like things succeeded, and let 'WaitForMonitor' report
1570          * the log contents for us.
1571          */
1572         vm->pid = child;
1573         ret = 0;
1574     }
1575
1576     vm->state = migrateFrom ? VIR_DOMAIN_PAUSED : VIR_DOMAIN_RUNNING;
1577
1578     for (i = 0 ; argv[i] ; i++)
1579         VIR_FREE(argv[i]);
1580     VIR_FREE(argv);
1581
1582     for (i = 0 ; progenv[i] ; i++)
1583         VIR_FREE(progenv[i]);
1584     VIR_FREE(progenv);
1585
1586     if (tapfds) {
1587         for (i = 0 ; i < ntapfds ; i++) {
1588             close(tapfds[i]);
1589         }
1590         VIR_FREE(tapfds);
1591     }
1592
1593     if (ret == -1)
1594         goto cleanup;
1595
1596     if ((qemudWaitForMonitor(conn, driver, vm, pos) < 0) ||
1597         (qemudDetectVcpuPIDs(conn, vm) < 0) ||
1598         (qemudInitCpus(conn, vm, migrateFrom) < 0) ||
1599         (qemudInitPasswords(conn, driver, vm) < 0) ||
1600         (qemudDomainSetMemoryBalloon(conn, vm, vm->def->memory) < 0) ||
1601         (virDomainSaveStatus(conn, driver->stateDir, vm) < 0)) {
1602         qemudShutdownVMDaemon(conn, driver, vm);
1603         ret = -1;
1604         /* No need for 'goto cleanup' now since qemudShutdownVMDaemon does enough */
1605     }
1606
1607     return ret;
1608
1609 cleanup:
1610     if (vm->def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC) {
1611         VIR_FREE(vm->def->seclabel.model);
1612         VIR_FREE(vm->def->seclabel.label);
1613         VIR_FREE(vm->def->seclabel.imagelabel);
1614     }
1615     if ((vm->def->ngraphics == 1) &&
1616         vm->def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
1617         vm->def->graphics[0]->data.vnc.autoport)
1618         vm->def->graphics[0]->data.vnc.port = -1;
1619     if (logfile != -1)
1620         close(logfile);
1621     vm->def->id = -1;
1622     return -1;
1623 }
1624
1625
1626 static void qemudShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED,
1627                                   struct qemud_driver *driver,
1628                                   virDomainObjPtr vm) {
1629     int ret;
1630
1631     if (!virDomainIsActive(vm))
1632         return;
1633
1634     VIR_DEBUG(_("Shutting down VM '%s'\n"), vm->def->name);
1635
1636     if (virKillProcess(vm->pid, 0) == 0 &&
1637         virKillProcess(vm->pid, SIGTERM) < 0)
1638         virReportSystemError(conn, errno,
1639                              _("Failed to send SIGTERM to %s (%d)"),
1640                              vm->def->name, vm->pid);
1641
1642     if (vm->monitorWatch != -1) {
1643         virEventRemoveHandle(vm->monitorWatch);
1644         vm->monitorWatch = -1;
1645     }
1646
1647     if (vm->monitor != -1)
1648         close(vm->monitor);
1649     vm->monitor = -1;
1650
1651     /* shut it off for sure */
1652     virKillProcess(vm->pid, SIGKILL);
1653
1654     /* Reset Security Labels */
1655     if (driver->securityDriver)
1656         driver->securityDriver->domainRestoreSecurityLabel(conn, vm);
1657
1658     /* Clear out dynamically assigned labels */
1659     if (vm->def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC) {
1660         VIR_FREE(vm->def->seclabel.model);
1661         VIR_FREE(vm->def->seclabel.label);
1662         VIR_FREE(vm->def->seclabel.imagelabel);
1663     }
1664
1665     if (qemudRemoveDomainStatus(conn, driver, vm) < 0) {
1666         VIR_WARN(_("Failed to remove domain status for %s"),
1667                  vm->def->name);
1668     }
1669     if ((ret = virFileDeletePid(driver->stateDir, vm->def->name)) != 0) {
1670         char ebuf[1024];
1671         VIR_WARN(_("Failed to remove PID file for %s: %s"),
1672                  vm->def->name, virStrerror(errno, ebuf, sizeof ebuf));
1673     }
1674
1675     vm->pid = -1;
1676     vm->def->id = -1;
1677     vm->state = VIR_DOMAIN_SHUTOFF;
1678     VIR_FREE(vm->vcpupids);
1679     vm->nvcpupids = 0;
1680
1681     if (vm->newDef) {
1682         virDomainDefFree(vm->def);
1683         vm->def = vm->newDef;
1684         vm->def->id = -1;
1685         vm->newDef = NULL;
1686     }
1687 }
1688
1689
1690 static void
1691 qemudDispatchVMEvent(int watch, int fd, int events, void *opaque) {
1692     struct qemud_driver *driver = opaque;
1693     virDomainObjPtr vm = NULL;
1694     virDomainEventPtr event = NULL;
1695     unsigned int i;
1696     int quit = 0, failed = 0;
1697
1698     qemuDriverLock(driver);
1699     for (i = 0 ; i < driver->domains.count ; i++) {
1700         virDomainObjPtr tmpvm = driver->domains.objs[i];
1701         virDomainObjLock(tmpvm);
1702         if (virDomainIsActive(tmpvm) &&
1703             tmpvm->monitorWatch == watch) {
1704             vm = tmpvm;
1705             break;
1706         }
1707         virDomainObjUnlock(tmpvm);
1708     }
1709
1710     if (!vm)
1711         goto cleanup;
1712
1713     if (vm->monitor != fd) {
1714         failed = 1;
1715     } else {
1716         if (events & (VIR_EVENT_HANDLE_HANGUP | VIR_EVENT_HANDLE_ERROR))
1717             quit = 1;
1718         else {
1719             VIR_ERROR(_("unhandled fd event %d for %s"),
1720                       events, vm->def->name);
1721             failed = 1;
1722         }
1723     }
1724
1725     if (failed || quit) {
1726         event = virDomainEventNewFromObj(vm,
1727                                          VIR_DOMAIN_EVENT_STOPPED,
1728                                          quit ?
1729                                          VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN :
1730                                          VIR_DOMAIN_EVENT_STOPPED_FAILED);
1731         qemudShutdownVMDaemon(NULL, driver, vm);
1732         if (!vm->persistent) {
1733             virDomainRemoveInactive(&driver->domains,
1734                                     vm);
1735             vm = NULL;
1736         }
1737     }
1738
1739 cleanup:
1740     if (vm)
1741         virDomainObjUnlock(vm);
1742     if (event)
1743         qemuDomainEventQueue(driver, event);
1744     qemuDriverUnlock(driver);
1745 }
1746
1747 static int
1748 qemudMonitorCommandExtra(const virDomainObjPtr vm,
1749                          const char *cmd,
1750                          const char *extra,
1751                          const char *extraPrompt,
1752                          char **reply) {
1753     int size = 0;
1754     char *buf = NULL;
1755     size_t cmdlen = strlen(cmd);
1756     size_t extralen = extra ? strlen(extra) : 0;
1757
1758     if (safewrite(vm->monitor, cmd, cmdlen) != cmdlen)
1759         return -1;
1760     if (safewrite(vm->monitor, "\r", 1) != 1)
1761         return -1;
1762
1763     *reply = NULL;
1764
1765     for (;;) {
1766         struct pollfd fd = { vm->monitor, POLLIN | POLLERR | POLLHUP, 0 };
1767         char *tmp;
1768
1769         /* Read all the data QEMU has sent thus far */
1770         for (;;) {
1771             char data[1024];
1772             int got = read(vm->monitor, data, sizeof(data));
1773
1774             if (got == 0)
1775                 goto error;
1776             if (got < 0) {
1777                 if (errno == EINTR)
1778                     continue;
1779                 if (errno == EAGAIN)
1780                     break;
1781                 goto error;
1782             }
1783             if (VIR_REALLOC_N(buf, size+got+1) < 0)
1784                 goto error;
1785
1786             memmove(buf+size, data, got);
1787             buf[size+got] = '\0';
1788             size += got;
1789         }
1790
1791         /* Look for QEMU prompt to indicate completion */
1792         if (buf) {
1793             if (extra) {
1794                 if (strstr(buf, extraPrompt) != NULL) {
1795                     if (safewrite(vm->monitor, extra, extralen) != extralen)
1796                         return -1;
1797                     if (safewrite(vm->monitor, "\r", 1) != 1)
1798                         return -1;
1799                     extra = NULL;
1800                 }
1801             } else if ((tmp = strstr(buf, QEMU_CMD_PROMPT)) != NULL) {
1802                 char *commptr = NULL, *nlptr = NULL;
1803                 /* Preserve the newline */
1804                 tmp[1] = '\0';
1805
1806                 /* The monitor doesn't dump clean output after we have written to
1807                  * it. Every character we write dumps a bunch of useless stuff,
1808                  * so the result looks like "cXcoXcomXcommXcommaXcommanXcommand"
1809                  * Try to throw away everything before the first full command
1810                  * occurence, and inbetween the command and the newline starting
1811                  * the response
1812                  */
1813                 if ((commptr = strstr(buf, cmd)))
1814                     memmove(buf, commptr, strlen(commptr)+1);
1815                 if ((nlptr = strchr(buf, '\n')))
1816                     memmove(buf+strlen(cmd), nlptr, strlen(nlptr)+1);
1817
1818                 break;
1819             }
1820         }
1821     pollagain:
1822         /* Need to wait for more data */
1823         if (poll(&fd, 1, -1) < 0) {
1824             if (errno == EINTR)
1825                 goto pollagain;
1826             goto error;
1827         }
1828     }
1829     *reply = buf;
1830     return 0;
1831
1832  error:
1833     VIR_FREE(buf);
1834     return -1;
1835 }
1836
1837 static int
1838 qemudMonitorCommand(const virDomainObjPtr vm,
1839                     const char *cmd,
1840                     char **reply) {
1841     return qemudMonitorCommandExtra(vm, cmd, NULL, NULL, reply);
1842 }
1843
1844
1845
1846 static virDrvOpenStatus qemudOpen(virConnectPtr conn,
1847                                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
1848                                   int flags ATTRIBUTE_UNUSED) {
1849     if (conn->uri == NULL) {
1850         if (qemu_driver == NULL)
1851             return VIR_DRV_OPEN_DECLINED;
1852
1853         conn->uri = xmlParseURI(qemu_driver->privileged ?
1854                                 "qemu:///system" :
1855                                 "qemu:///session");
1856         if (!conn->uri) {
1857             virReportOOMError(conn);
1858             return VIR_DRV_OPEN_ERROR;
1859         }
1860     } else {
1861         /* If URI isn't 'qemu' its definitely not for us */
1862         if (conn->uri->scheme == NULL ||
1863             STRNEQ(conn->uri->scheme, "qemu"))
1864             return VIR_DRV_OPEN_DECLINED;
1865
1866         /* Allow remote driver to deal with URIs with hostname server */
1867         if (conn->uri->server != NULL)
1868             return VIR_DRV_OPEN_DECLINED;
1869
1870         if (qemu_driver->privileged) {
1871             if (STRNEQ (conn->uri->path, "/system") &&
1872                 STRNEQ (conn->uri->path, "/session")) {
1873                 qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1874                                  _("unexpected QEMU URI path '%s', try qemu:///system"),
1875                                  conn->uri->path);
1876                 return VIR_DRV_OPEN_ERROR;
1877             }
1878         } else {
1879             if (STRNEQ (conn->uri->path, "/session")) {
1880                 qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
1881                                  _("unexpected QEMU URI path '%s', try qemu:///session"),
1882                                  conn->uri->path);
1883                 return VIR_DRV_OPEN_ERROR;
1884             }
1885         }
1886
1887         /* URI was good, but driver isn't active */
1888         if (qemu_driver == NULL) {
1889             qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
1890                              _("qemu state driver is not active"));
1891             return VIR_DRV_OPEN_ERROR;
1892         }
1893
1894     }
1895     conn->privateData = qemu_driver;
1896
1897     return VIR_DRV_OPEN_SUCCESS;
1898 }
1899
1900 static int qemudClose(virConnectPtr conn) {
1901     struct qemud_driver *driver = conn->privateData;
1902
1903     /* Get rid of callbacks registered for this conn */
1904     qemuDriverLock(driver);
1905     virDomainEventCallbackListRemoveConn(conn, driver->domainEventCallbacks);
1906     qemuDriverUnlock(driver);
1907
1908     conn->privateData = NULL;
1909
1910     return 0;
1911 }
1912
1913 /* Which features are supported by this driver? */
1914 static int
1915 qemudSupportsFeature (virConnectPtr conn ATTRIBUTE_UNUSED, int feature)
1916 {
1917     switch (feature) {
1918     case VIR_DRV_FEATURE_MIGRATION_V2: return 1;
1919     default: return 0;
1920     }
1921 }
1922
1923 static const char *qemudGetType(virConnectPtr conn ATTRIBUTE_UNUSED) {
1924     return "QEMU";
1925 }
1926
1927
1928 static int kvmGetMaxVCPUs(void) {
1929     int maxvcpus = 1;
1930
1931     int r, fd;
1932
1933     fd = open(KVM_DEVICE, O_RDONLY);
1934     if (fd < 0) {
1935         virReportSystemError(NULL, errno, _("Unable to open %s"), KVM_DEVICE);
1936         return -1;
1937     }
1938
1939     r = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS);
1940     if (r > 0)
1941         maxvcpus = r;
1942
1943     close(fd);
1944     return maxvcpus;
1945 }
1946
1947
1948 static int qemudGetMaxVCPUs(virConnectPtr conn, const char *type) {
1949     if (!type)
1950         return 16;
1951
1952     if (STRCASEEQ(type, "qemu"))
1953         return 16;
1954
1955     if (STRCASEEQ(type, "kvm"))
1956         return kvmGetMaxVCPUs();
1957
1958     if (STRCASEEQ(type, "kqemu"))
1959         return 1;
1960
1961     qemudReportError(conn, NULL, NULL, VIR_ERR_INVALID_ARG,
1962                      _("unknown type '%s'"), type);
1963     return -1;
1964 }
1965
1966
1967 static char *qemudGetCapabilities(virConnectPtr conn) {
1968     struct qemud_driver *driver = conn->privateData;
1969     virCapsPtr caps;
1970     char *xml = NULL;
1971
1972     qemuDriverLock(driver);
1973     if ((caps = qemudCapsInit()) == NULL) {
1974         virReportOOMError(conn);
1975         goto cleanup;
1976     }
1977
1978     if (qemu_driver->securityDriver &&
1979         qemudSecurityCapsInit(qemu_driver->securityDriver, caps) < 0) {
1980         virCapabilitiesFree(caps);
1981         virReportOOMError(conn);
1982         goto cleanup;
1983     }
1984
1985     virCapabilitiesFree(qemu_driver->caps);
1986     qemu_driver->caps = caps;
1987
1988     if ((xml = virCapabilitiesFormatXML(driver->caps)) == NULL)
1989         virReportOOMError(conn);
1990
1991 cleanup:
1992     qemuDriverUnlock(driver);
1993
1994     return xml;
1995 }
1996
1997
1998 static int qemudGetProcessInfo(unsigned long long *cpuTime, int pid) {
1999     char proc[PATH_MAX];
2000     FILE *pidinfo;
2001     unsigned long long usertime, systime;
2002
2003     if (snprintf(proc, sizeof(proc), "/proc/%d/stat", pid) >= (int)sizeof(proc)) {
2004         return -1;
2005     }
2006
2007     if (!(pidinfo = fopen(proc, "r"))) {
2008         /*printf("cannot read pid info");*/
2009         /* VM probably shut down, so fake 0 */
2010         *cpuTime = 0;
2011         return 0;
2012     }
2013
2014     if (fscanf(pidinfo, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu", &usertime, &systime) != 2) {
2015         qemudDebug("not enough arg");
2016         return -1;
2017     }
2018
2019     /* We got jiffies
2020      * We want nanoseconds
2021      * _SC_CLK_TCK is jiffies per second
2022      * So calulate thus....
2023      */
2024     *cpuTime = 1000ull * 1000ull * 1000ull * (usertime + systime) / (unsigned long long)sysconf(_SC_CLK_TCK);
2025
2026     qemudDebug("Got %llu %llu %llu", usertime, systime, *cpuTime);
2027
2028     fclose(pidinfo);
2029
2030     return 0;
2031 }
2032
2033
2034 static virDomainPtr qemudDomainLookupByID(virConnectPtr conn,
2035                                           int id) {
2036     struct qemud_driver *driver = conn->privateData;
2037     virDomainObjPtr vm;
2038     virDomainPtr dom = NULL;
2039
2040     qemuDriverLock(driver);
2041     vm  = virDomainFindByID(&driver->domains, id);
2042     qemuDriverUnlock(driver);
2043
2044     if (!vm) {
2045         qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN,
2046                          _("no domain with matching id %d"), id);
2047         goto cleanup;
2048     }
2049
2050     dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
2051     if (dom) dom->id = vm->def->id;
2052
2053 cleanup:
2054     if (vm)
2055         virDomainObjUnlock(vm);
2056     return dom;
2057 }
2058
2059 static virDomainPtr qemudDomainLookupByUUID(virConnectPtr conn,
2060                                             const unsigned char *uuid) {
2061     struct qemud_driver *driver = conn->privateData;
2062     virDomainObjPtr vm;
2063     virDomainPtr dom = NULL;
2064
2065     qemuDriverLock(driver);
2066     vm = virDomainFindByUUID(&driver->domains, uuid);
2067     qemuDriverUnlock(driver);
2068
2069     if (!vm) {
2070         char uuidstr[VIR_UUID_STRING_BUFLEN];
2071         virUUIDFormat(uuid, uuidstr);
2072         qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN,
2073                          _("no domain with matching uuid '%s'"), uuidstr);
2074         goto cleanup;
2075     }
2076
2077     dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
2078     if (dom) dom->id = vm->def->id;
2079
2080 cleanup:
2081     if (vm)
2082         virDomainObjUnlock(vm);
2083     return dom;
2084 }
2085
2086 static virDomainPtr qemudDomainLookupByName(virConnectPtr conn,
2087                                             const char *name) {
2088     struct qemud_driver *driver = conn->privateData;
2089     virDomainObjPtr vm;
2090     virDomainPtr dom = NULL;
2091
2092     qemuDriverLock(driver);
2093     vm = virDomainFindByName(&driver->domains, name);
2094     qemuDriverUnlock(driver);
2095
2096     if (!vm) {
2097         qemudReportError(conn, NULL, NULL, VIR_ERR_NO_DOMAIN,
2098                          _("no domain with matching name '%s'"), name);
2099         goto cleanup;
2100     }
2101
2102     dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
2103     if (dom) dom->id = vm->def->id;
2104
2105 cleanup:
2106     if (vm)
2107         virDomainObjUnlock(vm);
2108     return dom;
2109 }
2110
2111 static int qemudGetVersion(virConnectPtr conn, unsigned long *version) {
2112     struct qemud_driver *driver = conn->privateData;
2113     int ret = -1;
2114
2115     qemuDriverLock(driver);
2116     if (qemudExtractVersion(conn, driver) < 0)
2117         goto cleanup;
2118
2119     *version = qemu_driver->qemuVersion;
2120     ret = 0;
2121
2122 cleanup:
2123     qemuDriverUnlock(driver);
2124     return ret;
2125 }
2126
2127 static char *
2128 qemudGetHostname (virConnectPtr conn)
2129 {
2130     char *result;
2131
2132     result = virGetHostname();
2133     if (result == NULL) {
2134         virReportSystemError (conn, errno,
2135                               "%s", _("failed to determine host name"));
2136         return NULL;
2137     }
2138     /* Caller frees this string. */
2139     return result;
2140 }
2141
2142 static int qemudListDomains(virConnectPtr conn, int *ids, int nids) {
2143     struct qemud_driver *driver = conn->privateData;
2144     int got = 0, i;
2145
2146     qemuDriverLock(driver);
2147     for (i = 0 ; i < driver->domains.count && got < nids ; i++) {
2148         virDomainObjLock(driver->domains.objs[i]);
2149         if (virDomainIsActive(driver->domains.objs[i]))
2150             ids[got++] = driver->domains.objs[i]->def->id;
2151         virDomainObjUnlock(driver->domains.objs[i]);
2152     }
2153     qemuDriverUnlock(driver);
2154
2155     return got;
2156 }
2157
2158 static int qemudNumDomains(virConnectPtr conn) {
2159     struct qemud_driver *driver = conn->privateData;
2160     int n = 0, i;
2161
2162     qemuDriverLock(driver);
2163     for (i = 0 ; i < driver->domains.count ; i++) {
2164         virDomainObjLock(driver->domains.objs[i]);
2165         if (virDomainIsActive(driver->domains.objs[i]))
2166             n++;
2167         virDomainObjUnlock(driver->domains.objs[i]);
2168     }
2169     qemuDriverUnlock(driver);
2170
2171     return n;
2172 }
2173
2174 static virDomainPtr qemudDomainCreate(virConnectPtr conn, const char *xml,
2175                                       unsigned int flags ATTRIBUTE_UNUSED) {
2176     struct qemud_driver *driver = conn->privateData;
2177     virDomainDefPtr def;
2178     virDomainObjPtr vm = NULL;
2179     virDomainPtr dom = NULL;
2180     virDomainEventPtr event = NULL;
2181
2182     qemuDriverLock(driver);
2183     if (!(def = virDomainDefParseString(conn, driver->caps, xml,
2184                                         VIR_DOMAIN_XML_INACTIVE)))
2185         goto cleanup;
2186
2187     if (virSecurityDriverVerify(conn, def) < 0)
2188         goto cleanup;
2189
2190     /* See if a VM with matching UUID already exists */
2191     vm = virDomainFindByUUID(&driver->domains, def->uuid);
2192     if (vm) {
2193         /* UUID matches, but if names don't match, refuse it */
2194         if (STRNEQ(vm->def->name, def->name)) {
2195             char uuidstr[VIR_UUID_STRING_BUFLEN];
2196             virUUIDFormat(vm->def->uuid, uuidstr);
2197             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2198                              _("domain '%s' is already defined with uuid %s"),
2199                              vm->def->name, uuidstr);
2200             goto cleanup;
2201         }
2202
2203         /* UUID & name match, but if VM is already active, refuse it */
2204         if (virDomainIsActive(vm)) {
2205             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2206                              _("domain is already active as '%s'"), vm->def->name);
2207             goto cleanup;
2208         }
2209         virDomainObjUnlock(vm);
2210     } else {
2211         /* UUID does not match, but if a name matches, refuse it */
2212         vm = virDomainFindByName(&driver->domains, def->name);
2213         if (vm) {
2214             char uuidstr[VIR_UUID_STRING_BUFLEN];
2215             virUUIDFormat(vm->def->uuid, uuidstr);
2216             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
2217                              _("domain '%s' is already defined with uuid %s"),
2218                              def->name, uuidstr);
2219             goto cleanup;
2220         }
2221     }
2222
2223     if (!(vm = virDomainAssignDef(conn,
2224                                   &driver->domains,
2225                                   def)))
2226         goto cleanup;
2227
2228     def = NULL;
2229
2230     if (qemudStartVMDaemon(conn, driver, vm, NULL, -1) < 0) {
2231         virDomainRemoveInactive(&driver->domains,
2232                                 vm);
2233         vm = NULL;
2234         goto cleanup;
2235     }
2236
2237     event = virDomainEventNewFromObj(vm,
2238                                      VIR_DOMAIN_EVENT_STARTED,
2239                                      VIR_DOMAIN_EVENT_STARTED_BOOTED);
2240
2241     dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
2242     if (dom) dom->id = vm->def->id;
2243
2244 cleanup:
2245     virDomainDefFree(def);
2246     if (vm)
2247         virDomainObjUnlock(vm);
2248     if (event)
2249         qemuDomainEventQueue(driver, event);
2250     qemuDriverUnlock(driver);
2251     return dom;
2252 }
2253
2254
2255 static int qemudDomainSuspend(virDomainPtr dom) {
2256     struct qemud_driver *driver = dom->conn->privateData;
2257     char *info;
2258     virDomainObjPtr vm;
2259     int ret = -1;
2260     virDomainEventPtr event = NULL;
2261
2262     qemuDriverLock(driver);
2263     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2264
2265     if (!vm) {
2266         char uuidstr[VIR_UUID_STRING_BUFLEN];
2267         virUUIDFormat(dom->uuid, uuidstr);
2268         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2269                          _("no domain with matching uuid '%s'"), uuidstr);
2270         goto cleanup;
2271     }
2272     if (!virDomainIsActive(vm)) {
2273         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
2274                          "%s", _("domain is not running"));
2275         goto cleanup;
2276     }
2277     if (vm->state != VIR_DOMAIN_PAUSED) {
2278         if (qemudMonitorCommand(vm, "stop", &info) < 0) {
2279             qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2280                              "%s", _("suspend operation failed"));
2281             goto cleanup;
2282         }
2283         vm->state = VIR_DOMAIN_PAUSED;
2284         qemudDebug("Reply %s", info);
2285         event = virDomainEventNewFromObj(vm,
2286                                          VIR_DOMAIN_EVENT_SUSPENDED,
2287                                          VIR_DOMAIN_EVENT_SUSPENDED_PAUSED);
2288         VIR_FREE(info);
2289     }
2290     if (virDomainSaveStatus(dom->conn, driver->stateDir, vm) < 0)
2291         goto cleanup;
2292     ret = 0;
2293
2294 cleanup:
2295     if (vm)
2296         virDomainObjUnlock(vm);
2297
2298     if (event)
2299         qemuDomainEventQueue(driver, event);
2300     qemuDriverUnlock(driver);
2301     return ret;
2302 }
2303
2304
2305 static int qemudDomainResume(virDomainPtr dom) {
2306     struct qemud_driver *driver = dom->conn->privateData;
2307     char *info;
2308     virDomainObjPtr vm;
2309     int ret = -1;
2310     virDomainEventPtr event = NULL;
2311
2312     qemuDriverLock(driver);
2313     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2314
2315     if (!vm) {
2316         char uuidstr[VIR_UUID_STRING_BUFLEN];
2317         virUUIDFormat(dom->uuid, uuidstr);
2318         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2319                          _("no domain with matching uuid '%s'"), uuidstr);
2320         goto cleanup;
2321     }
2322     if (!virDomainIsActive(vm)) {
2323         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
2324                          "%s", _("domain is not running"));
2325         goto cleanup;
2326     }
2327     if (vm->state == VIR_DOMAIN_PAUSED) {
2328         if (qemudMonitorCommand(vm, "cont", &info) < 0) {
2329             qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2330                              "%s", _("resume operation failed"));
2331             goto cleanup;
2332         }
2333         vm->state = VIR_DOMAIN_RUNNING;
2334         qemudDebug("Reply %s", info);
2335         event = virDomainEventNewFromObj(vm,
2336                                          VIR_DOMAIN_EVENT_RESUMED,
2337                                          VIR_DOMAIN_EVENT_RESUMED_UNPAUSED);
2338         VIR_FREE(info);
2339     }
2340     if (virDomainSaveStatus(dom->conn, driver->stateDir, vm) < 0)
2341         goto cleanup;
2342     ret = 0;
2343
2344 cleanup:
2345     if (vm)
2346         virDomainObjUnlock(vm);
2347     if (event)
2348         qemuDomainEventQueue(driver, event);
2349     qemuDriverUnlock(driver);
2350     return ret;
2351 }
2352
2353
2354 static int qemudDomainShutdown(virDomainPtr dom) {
2355     struct qemud_driver *driver = dom->conn->privateData;
2356     virDomainObjPtr vm;
2357     char* info;
2358     int ret = -1;
2359
2360     qemuDriverLock(driver);
2361     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2362     qemuDriverUnlock(driver);
2363
2364     if (!vm) {
2365         char uuidstr[VIR_UUID_STRING_BUFLEN];
2366         virUUIDFormat(dom->uuid, uuidstr);
2367         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2368                          _("no domain with matching uuid '%s'"), uuidstr);
2369         goto cleanup;
2370     }
2371
2372     if (qemudMonitorCommand(vm, "system_powerdown", &info) < 0) {
2373         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2374                          "%s", _("shutdown operation failed"));
2375         goto cleanup;
2376     }
2377     VIR_FREE(info);
2378     ret = 0;
2379
2380 cleanup:
2381     if (vm)
2382         virDomainObjUnlock(vm);
2383     return ret;
2384 }
2385
2386
2387 static int qemudDomainDestroy(virDomainPtr dom) {
2388     struct qemud_driver *driver = dom->conn->privateData;
2389     virDomainObjPtr vm;
2390     int ret = -1;
2391     virDomainEventPtr event = NULL;
2392
2393     qemuDriverLock(driver);
2394     vm  = virDomainFindByUUID(&driver->domains, dom->uuid);
2395     if (!vm) {
2396         char uuidstr[VIR_UUID_STRING_BUFLEN];
2397         virUUIDFormat(dom->uuid, uuidstr);
2398         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2399                          _("no domain with matching uuid '%s'"), uuidstr);
2400         goto cleanup;
2401     }
2402     if (!virDomainIsActive(vm)) {
2403         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2404                          "%s", _("domain is not running"));
2405         goto cleanup;
2406     }
2407
2408     qemudShutdownVMDaemon(dom->conn, driver, vm);
2409     event = virDomainEventNewFromObj(vm,
2410                                      VIR_DOMAIN_EVENT_STOPPED,
2411                                      VIR_DOMAIN_EVENT_STOPPED_DESTROYED);
2412     if (!vm->persistent) {
2413         virDomainRemoveInactive(&driver->domains,
2414                                 vm);
2415         vm = NULL;
2416     }
2417     ret = 0;
2418
2419 cleanup:
2420     if (vm)
2421         virDomainObjUnlock(vm);
2422     if (event)
2423         qemuDomainEventQueue(driver, event);
2424     qemuDriverUnlock(driver);
2425     return ret;
2426 }
2427
2428
2429 static char *qemudDomainGetOSType(virDomainPtr dom) {
2430     struct qemud_driver *driver = dom->conn->privateData;
2431     virDomainObjPtr vm;
2432     char *type = NULL;
2433
2434     qemuDriverLock(driver);
2435     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2436     qemuDriverUnlock(driver);
2437     if (!vm) {
2438         char uuidstr[VIR_UUID_STRING_BUFLEN];
2439         virUUIDFormat(dom->uuid, uuidstr);
2440         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2441                          _("no domain with matching uuid '%s'"), uuidstr);
2442         goto cleanup;
2443     }
2444
2445     if (!(type = strdup(vm->def->os.type)))
2446         virReportOOMError(dom->conn);
2447
2448 cleanup:
2449     if (vm)
2450         virDomainObjUnlock(vm);
2451     return type;
2452 }
2453
2454 /* Returns max memory in kb, 0 if error */
2455 static unsigned long qemudDomainGetMaxMemory(virDomainPtr dom) {
2456     struct qemud_driver *driver = dom->conn->privateData;
2457     virDomainObjPtr vm;
2458     unsigned long ret = 0;
2459
2460     qemuDriverLock(driver);
2461     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2462     qemuDriverUnlock(driver);
2463
2464     if (!vm) {
2465         char uuidstr[VIR_UUID_STRING_BUFLEN];
2466         virUUIDFormat(dom->uuid, uuidstr);
2467         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2468                          _("no domain with matching uuid '%s'"), uuidstr);
2469         goto cleanup;
2470     }
2471
2472     ret = vm->def->maxmem;
2473
2474 cleanup:
2475     if (vm)
2476         virDomainObjUnlock(vm);
2477     return ret;
2478 }
2479
2480 static int qemudDomainSetMaxMemory(virDomainPtr dom, unsigned long newmax) {
2481     struct qemud_driver *driver = dom->conn->privateData;
2482     virDomainObjPtr vm;
2483     int ret = -1;
2484
2485     qemuDriverLock(driver);
2486     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2487     qemuDriverUnlock(driver);
2488
2489     if (!vm) {
2490         char uuidstr[VIR_UUID_STRING_BUFLEN];
2491         virUUIDFormat(dom->uuid, uuidstr);
2492         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2493                          _("no domain with matching uuid '%s'"), uuidstr);
2494         goto cleanup;
2495     }
2496
2497     if (newmax < vm->def->memory) {
2498         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
2499                          "%s", _("cannot set max memory lower than current memory"));
2500         goto cleanup;;
2501     }
2502
2503     vm->def->maxmem = newmax;
2504     ret = 0;
2505
2506 cleanup:
2507     if (vm)
2508         virDomainObjUnlock(vm);
2509     return ret;
2510 }
2511
2512
2513 /* The reply from QEMU contains 'ballon: actual=421' where value is in MB */
2514 #define BALLOON_PREFIX "balloon: actual="
2515
2516 /*
2517  * Returns: 0 if balloon not supported, +1 if balloon query worked
2518  * or -1 on failure
2519  */
2520 static int qemudDomainGetMemoryBalloon(virConnectPtr conn,
2521                                        virDomainObjPtr vm,
2522                                        unsigned long *currmem) {
2523     char *reply = NULL;
2524     int ret = -1;
2525     char *offset;
2526
2527     if (!virDomainIsActive(vm))
2528         return 0;
2529
2530     if (qemudMonitorCommand(vm, "info balloon", &reply) < 0) {
2531         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2532                          "%s", _("could not query memory balloon allocation"));
2533         goto cleanup;
2534     }
2535
2536     DEBUG ("%s: balloon reply: '%s'", vm->def->name, reply);
2537     if ((offset = strstr(reply, BALLOON_PREFIX)) != NULL) {
2538         unsigned int memMB;
2539         char *end;
2540         offset += strlen(BALLOON_PREFIX);
2541         if (virStrToLong_ui(offset, &end, 10, &memMB) < 0) {
2542             qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2543                              "%s", _("could not parse memory balloon allocation"));
2544             goto cleanup;
2545         }
2546         *currmem = memMB * 1024;
2547         ret = 1;
2548     } else {
2549         /* We don't raise an error here, since its to be expected that
2550          * many QEMU's don't support ballooning
2551          */
2552         ret = 0;
2553     }
2554
2555 cleanup:
2556     VIR_FREE(reply);
2557     return ret;
2558 }
2559
2560 /*
2561  * Returns: 0 if balloon not supported, +1 if balloon query worked
2562  * or -1 on failure
2563  */
2564 static int qemudDomainSetMemoryBalloon(virConnectPtr conn,
2565                                        virDomainObjPtr vm,
2566                                        unsigned long newmem) {
2567     char *cmd;
2568     char *reply = NULL;
2569     int ret = -1;
2570
2571     /*
2572      * 'newmem' is in KB, QEMU monitor works in MB, and we all wish
2573      * we just worked in bytes with unsigned long long everywhere.
2574      */
2575     if (virAsprintf(&cmd, "balloon %lu", (newmem / 1024)) < 0) {
2576         virReportOOMError(conn);
2577         goto cleanup;
2578     }
2579
2580     if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
2581         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2582                          "%s", _("could not balloon memory allocation"));
2583         VIR_FREE(cmd);
2584         goto cleanup;
2585     }
2586     VIR_FREE(cmd);
2587
2588     /* If the command failed qemu prints: 'unknown command'
2589      * No message is printed on success it seems */
2590     DEBUG ("%s: balloon reply: %s",vm->def->name,  reply);
2591     if (strstr(reply, "\nunknown command:")) {
2592         /* Don't set error - it is expected memory balloon fails on many qemu */
2593         ret = 0;
2594     } else {
2595         ret = 1;
2596     }
2597
2598 cleanup:
2599     VIR_FREE(reply);
2600     return ret;
2601 }
2602
2603
2604 static int qemudDomainSetMemory(virDomainPtr dom, unsigned long newmem) {
2605     struct qemud_driver *driver = dom->conn->privateData;
2606     virDomainObjPtr vm;
2607     int ret = -1;
2608
2609     qemuDriverLock(driver);
2610     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2611     qemuDriverUnlock(driver);
2612     if (!vm) {
2613         char uuidstr[VIR_UUID_STRING_BUFLEN];
2614         virUUIDFormat(dom->uuid, uuidstr);
2615         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2616                          _("no domain with matching uuid '%s'"), uuidstr);
2617         goto cleanup;
2618     }
2619
2620     if (newmem > vm->def->maxmem) {
2621         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
2622                          "%s", _("cannot set memory higher than max memory"));
2623         goto cleanup;
2624     }
2625
2626     if (virDomainIsActive(vm)) {
2627         ret = qemudDomainSetMemoryBalloon(dom->conn, vm, newmem);
2628         if (ret == 0)
2629             qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
2630                              "%s", _("cannot set memory of an active domain"));
2631     } else {
2632         vm->def->memory = newmem;
2633         ret = 0;
2634     }
2635
2636 cleanup:
2637     if (vm)
2638         virDomainObjUnlock(vm);
2639     return ret;
2640 }
2641
2642 static int qemudDomainGetInfo(virDomainPtr dom,
2643                               virDomainInfoPtr info) {
2644     struct qemud_driver *driver = dom->conn->privateData;
2645     virDomainObjPtr vm;
2646     int ret = -1;
2647     int err;
2648     unsigned long balloon;
2649
2650     qemuDriverLock(driver);
2651     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2652     qemuDriverUnlock(driver);
2653     if (!vm) {
2654         char uuidstr[VIR_UUID_STRING_BUFLEN];
2655         virUUIDFormat(dom->uuid, uuidstr);
2656         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2657                          _("no domain with matching uuid '%s'"), uuidstr);
2658         goto cleanup;
2659     }
2660
2661     info->state = vm->state;
2662
2663     if (!virDomainIsActive(vm)) {
2664         info->cpuTime = 0;
2665     } else {
2666         if (qemudGetProcessInfo(&(info->cpuTime), vm->pid) < 0) {
2667             qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, ("cannot read cputime for domain"));
2668             goto cleanup;
2669         }
2670     }
2671
2672     info->maxMem = vm->def->maxmem;
2673
2674     if (virDomainIsActive(vm)) {
2675         err = qemudDomainGetMemoryBalloon(dom->conn, vm, &balloon);
2676         if (err < 0)
2677             goto cleanup;
2678
2679         if (err == 0)
2680             /* Balloon not supported, so maxmem is always the allocation */
2681             info->memory = vm->def->maxmem;
2682         else
2683             info->memory = balloon;
2684     } else {
2685         info->memory = vm->def->memory;
2686     }
2687
2688     info->nrVirtCpu = vm->def->vcpus;
2689     ret = 0;
2690
2691 cleanup:
2692     if (vm)
2693         virDomainObjUnlock(vm);
2694     return ret;
2695 }
2696
2697
2698 static char *qemudEscape(const char *in, int shell)
2699 {
2700     int len = 0;
2701     int i, j;
2702     char *out;
2703
2704     /* To pass through the QEMU monitor, we need to use escape
2705        sequences: \r, \n, \", \\
2706
2707        To pass through both QEMU + the shell, we need to escape
2708        the single character ' as the five characters '\\''
2709     */
2710
2711     for (i = 0; in[i] != '\0'; i++) {
2712         switch(in[i]) {
2713         case '\r':
2714         case '\n':
2715         case '"':
2716         case '\\':
2717             len += 2;
2718             break;
2719         case '\'':
2720             if (shell)
2721                 len += 5;
2722             else
2723                 len += 1;
2724             break;
2725         default:
2726             len += 1;
2727             break;
2728         }
2729     }
2730
2731     if (VIR_ALLOC_N(out, len + 1) < 0)
2732         return NULL;
2733
2734     for (i = j = 0; in[i] != '\0'; i++) {
2735         switch(in[i]) {
2736         case '\r':
2737             out[j++] = '\\';
2738             out[j++] = 'r';
2739             break;
2740         case '\n':
2741             out[j++] = '\\';
2742             out[j++] = 'n';
2743             break;
2744         case '"':
2745         case '\\':
2746             out[j++] = '\\';
2747             out[j++] = in[i];
2748             break;
2749         case '\'':
2750             if (shell) {
2751                 out[j++] = '\'';
2752                 out[j++] = '\\';
2753                 out[j++] = '\\';
2754                 out[j++] = '\'';
2755                 out[j++] = '\'';
2756             } else {
2757                 out[j++] = in[i];
2758             }
2759             break;
2760         default:
2761             out[j++] = in[i];
2762             break;
2763         }
2764     }
2765     out[j] = '\0';
2766
2767     return out;
2768 }
2769
2770 static char *qemudEscapeMonitorArg(const char *in)
2771 {
2772     return qemudEscape(in, 0);
2773 }
2774
2775 static char *qemudEscapeShellArg(const char *in)
2776 {
2777     return qemudEscape(in, 1);
2778 }
2779
2780 #define QEMUD_SAVE_MAGIC "LibvirtQemudSave"
2781 #define QEMUD_SAVE_VERSION 1
2782
2783 struct qemud_save_header {
2784     char magic[sizeof(QEMUD_SAVE_MAGIC)-1];
2785     int version;
2786     int xml_len;
2787     int was_running;
2788     int unused[16];
2789 };
2790
2791 static int qemudDomainSave(virDomainPtr dom,
2792                            const char *path) {
2793     struct qemud_driver *driver = dom->conn->privateData;
2794     virDomainObjPtr vm;
2795     char *command = NULL;
2796     char *info = NULL;
2797     int fd = -1;
2798     char *safe_path = NULL;
2799     char *xml = NULL;
2800     struct qemud_save_header header;
2801     int ret = -1;
2802     virDomainEventPtr event = NULL;
2803
2804     memset(&header, 0, sizeof(header));
2805     memcpy(header.magic, QEMUD_SAVE_MAGIC, sizeof(header.magic));
2806     header.version = QEMUD_SAVE_VERSION;
2807
2808     qemuDriverLock(driver);
2809     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2810
2811     if (!vm) {
2812         char uuidstr[VIR_UUID_STRING_BUFLEN];
2813         virUUIDFormat(dom->uuid, uuidstr);
2814         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2815                          _("no domain with matching uuid '%s'"), uuidstr);
2816         goto cleanup;
2817     }
2818
2819     if (!virDomainIsActive(vm)) {
2820         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
2821                          "%s", _("domain is not running"));
2822         goto cleanup;
2823     }
2824
2825     /* Pause */
2826     if (vm->state == VIR_DOMAIN_RUNNING) {
2827         header.was_running = 1;
2828         if (qemudMonitorCommand(vm, "stop", &info) < 0) {
2829             qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2830                              "%s", _("suspend operation failed"));
2831             goto cleanup;
2832         }
2833         vm->state = VIR_DOMAIN_PAUSED;
2834         qemudDebug("Reply %s", info);
2835         VIR_FREE(info);
2836     }
2837
2838     /* Get XML for the domain */
2839     xml = virDomainDefFormat(dom->conn, vm->def, VIR_DOMAIN_XML_SECURE);
2840     if (!xml) {
2841         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2842                          "%s", _("failed to get domain xml"));
2843         goto cleanup;
2844     }
2845     header.xml_len = strlen(xml) + 1;
2846
2847     /* Write header to file, followed by XML */
2848     if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) {
2849         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2850                          _("failed to create '%s'"), path);
2851         goto cleanup;
2852     }
2853
2854     if (safewrite(fd, &header, sizeof(header)) != sizeof(header)) {
2855         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2856                          "%s", _("failed to write save header"));
2857         goto cleanup;
2858     }
2859
2860     if (safewrite(fd, xml, header.xml_len) != header.xml_len) {
2861         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2862                          "%s", _("failed to write xml"));
2863         goto cleanup;
2864     }
2865
2866     if (close(fd) < 0) {
2867         virReportSystemError(dom->conn, errno,
2868                              _("unable to save file %s"),
2869                              path);
2870         goto cleanup;
2871     }
2872     fd = -1;
2873
2874     /* Migrate to file */
2875     safe_path = qemudEscapeShellArg(path);
2876     if (!safe_path) {
2877         virReportOOMError(dom->conn);
2878         goto cleanup;
2879     }
2880     if (virAsprintf(&command, "migrate \"exec:"
2881                   "dd of='%s' oflag=append conv=notrunc 2>/dev/null"
2882                   "\"", safe_path) == -1) {
2883         virReportOOMError(dom->conn);
2884         command = NULL;
2885         goto cleanup;
2886     }
2887
2888     if (qemudMonitorCommand(vm, command, &info) < 0) {
2889         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
2890                          "%s", _("migrate operation failed"));
2891         goto cleanup;
2892     }
2893
2894     DEBUG ("%s: migrate reply: %s", vm->def->name, info);
2895
2896     /* If the command isn't supported then qemu prints:
2897      * unknown command: migrate" */
2898     if (strstr(info, "unknown command:")) {
2899         qemudReportError (dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
2900                           "%s",
2901                           _("'migrate' not supported by this qemu"));
2902         goto cleanup;
2903     }
2904
2905     /* Shut it down */
2906     qemudShutdownVMDaemon(dom->conn, driver, vm);
2907     event = virDomainEventNewFromObj(vm,
2908                                      VIR_DOMAIN_EVENT_STOPPED,
2909                                      VIR_DOMAIN_EVENT_STOPPED_SAVED);
2910     if (!vm->persistent) {
2911         virDomainRemoveInactive(&driver->domains,
2912                                 vm);
2913         vm = NULL;
2914     }
2915     ret = 0;
2916
2917 cleanup:
2918     if (fd != -1)
2919         close(fd);
2920     VIR_FREE(xml);
2921     VIR_FREE(safe_path);
2922     VIR_FREE(command);
2923     VIR_FREE(info);
2924     if (ret != 0)
2925         unlink(path);
2926     if (vm)
2927         virDomainObjUnlock(vm);
2928     if (event)
2929         qemuDomainEventQueue(driver, event);
2930     qemuDriverUnlock(driver);
2931     return ret;
2932 }
2933
2934
2935 static int qemudDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus) {
2936     struct qemud_driver *driver = dom->conn->privateData;
2937     virDomainObjPtr vm;
2938     int max;
2939     int ret = -1;
2940     const char *type;
2941
2942     qemuDriverLock(driver);
2943     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
2944     qemuDriverUnlock(driver);
2945
2946     if (!vm) {
2947         char uuidstr[VIR_UUID_STRING_BUFLEN];
2948         virUUIDFormat(dom->uuid, uuidstr);
2949         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
2950                          _("no domain with matching uuid '%s'"), uuidstr);
2951         goto cleanup;
2952     }
2953
2954     if (virDomainIsActive(vm)) {
2955         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID, "%s",
2956                          _("cannot change vcpu count of an active domain"));
2957         goto cleanup;
2958     }
2959
2960     if (!(type = virDomainVirtTypeToString(vm->def->virtType))) {
2961         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
2962                          _("unknown virt type in domain definition '%d'"),
2963                          vm->def->virtType);
2964         goto cleanup;
2965     }
2966
2967     if ((max = qemudGetMaxVCPUs(dom->conn, type)) < 0) {
2968         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
2969                          _("could not determine max vcpus for the domain"));
2970         goto cleanup;
2971     }
2972
2973     if (nvcpus > max) {
2974         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
2975                          _("requested vcpus is greater than max allowable"
2976                            " vcpus for the domain: %d > %d"), nvcpus, max);
2977         goto cleanup;
2978     }
2979
2980     vm->def->vcpus = nvcpus;
2981     ret = 0;
2982
2983 cleanup:
2984     if (vm)
2985         virDomainObjUnlock(vm);
2986     return ret;
2987 }
2988
2989
2990 #if HAVE_SCHED_GETAFFINITY
2991 static int
2992 qemudDomainPinVcpu(virDomainPtr dom,
2993                    unsigned int vcpu,
2994                    unsigned char *cpumap,
2995                    int maplen) {
2996     struct qemud_driver *driver = dom->conn->privateData;
2997     virDomainObjPtr vm;
2998     cpu_set_t mask;
2999     int i, maxcpu;
3000     virNodeInfo nodeinfo;
3001     int ret = -1;
3002
3003     qemuDriverLock(driver);
3004     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3005     qemuDriverUnlock(driver);
3006
3007     if (!vm) {
3008         char uuidstr[VIR_UUID_STRING_BUFLEN];
3009         virUUIDFormat(dom->uuid, uuidstr);
3010         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
3011                          _("no domain with matching uuid '%s'"), uuidstr);
3012         goto cleanup;
3013     }
3014
3015     if (!virDomainIsActive(vm)) {
3016         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
3017                          "%s",_("cannot pin vcpus on an inactive domain"));
3018         goto cleanup;
3019     }
3020
3021     if (vcpu > (vm->nvcpupids-1)) {
3022         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
3023                          _("vcpu number out of range %d > %d"),
3024                          vcpu, vm->nvcpupids);
3025         goto cleanup;
3026     }
3027
3028     if (nodeGetInfo(dom->conn, &nodeinfo) < 0)
3029         goto cleanup;
3030
3031     maxcpu = maplen * 8;
3032     if (maxcpu > nodeinfo.cpus)
3033         maxcpu = nodeinfo.cpus;
3034
3035     CPU_ZERO(&mask);
3036     for (i = 0 ; i < maxcpu ; i++) {
3037         if ((cpumap[i/8] >> (i % 8)) & 1)
3038             CPU_SET(i, &mask);
3039     }
3040
3041     if (vm->vcpupids != NULL) {
3042         if (sched_setaffinity(vm->vcpupids[vcpu], sizeof(mask), &mask) < 0) {
3043             virReportSystemError(dom->conn, errno, "%s",
3044                                  _("cannot set affinity"));
3045             goto cleanup;
3046         }
3047     } else {
3048         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
3049                          "%s", _("cpu affinity is not supported"));
3050         goto cleanup;
3051     }
3052     ret = 0;
3053
3054 cleanup:
3055     if (vm)
3056         virDomainObjUnlock(vm);
3057     return ret;
3058 }
3059
3060 static int
3061 qemudDomainGetVcpus(virDomainPtr dom,
3062                     virVcpuInfoPtr info,
3063                     int maxinfo,
3064                     unsigned char *cpumaps,
3065                     int maplen) {
3066     struct qemud_driver *driver = dom->conn->privateData;
3067     virDomainObjPtr vm;
3068     virNodeInfo nodeinfo;
3069     int i, v, maxcpu;
3070     int ret = -1;
3071
3072     qemuDriverLock(driver);
3073     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3074     qemuDriverUnlock(driver);
3075
3076     if (!vm) {
3077         char uuidstr[VIR_UUID_STRING_BUFLEN];
3078         virUUIDFormat(dom->uuid, uuidstr);
3079         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
3080                          _("no domain with matching uuid '%s'"), uuidstr);
3081         goto cleanup;
3082     }
3083
3084     if (!virDomainIsActive(vm)) {
3085         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
3086                          "%s",_("cannot pin vcpus on an inactive domain"));
3087         goto cleanup;
3088     }
3089
3090     if (nodeGetInfo(dom->conn, &nodeinfo) < 0)
3091         goto cleanup;
3092
3093     maxcpu = maplen * 8;
3094     if (maxcpu > nodeinfo.cpus)
3095         maxcpu = nodeinfo.cpus;
3096
3097     /* Clamp to actual number of vcpus */
3098     if (maxinfo > vm->nvcpupids)
3099         maxinfo = vm->nvcpupids;
3100
3101     if (maxinfo >= 1) {
3102         if (info != NULL) {
3103             memset(info, 0, sizeof(*info) * maxinfo);
3104             for (i = 0 ; i < maxinfo ; i++) {
3105                 info[i].number = i;
3106                 info[i].state = VIR_VCPU_RUNNING;
3107                 /* XXX cpu time, current pCPU mapping */
3108             }
3109         }
3110
3111         if (cpumaps != NULL) {
3112             memset(cpumaps, 0, maplen * maxinfo);
3113             if (vm->vcpupids != NULL) {
3114                 for (v = 0 ; v < maxinfo ; v++) {
3115                     cpu_set_t mask;
3116                     unsigned char *cpumap = VIR_GET_CPUMAP(cpumaps, maplen, v);
3117                     CPU_ZERO(&mask);
3118
3119                     if (sched_getaffinity(vm->vcpupids[v], sizeof(mask), &mask) < 0) {
3120                         virReportSystemError(dom->conn, errno, "%s",
3121                                              _("cannot get affinity"));
3122                         goto cleanup;
3123                     }
3124
3125                     for (i = 0 ; i < maxcpu ; i++)
3126                         if (CPU_ISSET(i, &mask))
3127                             VIR_USE_CPU(cpumap, i);
3128                 }
3129             } else {
3130                 qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
3131                                  "%s", _("cpu affinity is not available"));
3132                 goto cleanup;
3133             }
3134         }
3135     }
3136     ret = maxinfo;
3137
3138 cleanup:
3139     if (vm)
3140         virDomainObjUnlock(vm);
3141     return ret;
3142 }
3143 #endif /* HAVE_SCHED_GETAFFINITY */
3144
3145
3146 static int qemudDomainGetMaxVcpus(virDomainPtr dom) {
3147     struct qemud_driver *driver = dom->conn->privateData;
3148     virDomainObjPtr vm;
3149     const char *type;
3150     int ret = -1;
3151
3152     qemuDriverLock(driver);
3153     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3154     qemuDriverUnlock(driver);
3155
3156     if (!vm) {
3157         char uuidstr[VIR_UUID_STRING_BUFLEN];
3158         virUUIDFormat(dom->uuid, uuidstr);
3159         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
3160                          _("no domain with matching uuid '%s'"), uuidstr);
3161         goto cleanup;
3162     }
3163
3164     if (!(type = virDomainVirtTypeToString(vm->def->virtType))) {
3165         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3166                          _("unknown virt type in domain definition '%d'"),
3167                          vm->def->virtType);
3168         goto cleanup;
3169     }
3170
3171     ret = qemudGetMaxVCPUs(dom->conn, type);
3172
3173 cleanup:
3174     if (vm)
3175         virDomainObjUnlock(vm);
3176     return ret;
3177 }
3178
3179 static int qemudDomainGetSecurityLabel(virDomainPtr dom, virSecurityLabelPtr seclabel)
3180 {
3181     struct qemud_driver *driver = (struct qemud_driver *)dom->conn->privateData;
3182     virDomainObjPtr vm;
3183     const char *type;
3184     int ret = -1;
3185
3186     qemuDriverLock(driver);
3187     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3188
3189     memset(seclabel, 0, sizeof(*seclabel));
3190
3191     if (!vm) {
3192         char uuidstr[VIR_UUID_STRING_BUFLEN];
3193         virUUIDFormat(dom->uuid, uuidstr);
3194         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
3195                          _("no domain with matching uuid '%s'"), uuidstr);
3196         goto cleanup;
3197     }
3198
3199     if (!(type = virDomainVirtTypeToString(vm->def->virtType))) {
3200         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3201                          _("unknown virt type in domain definition '%d'"),
3202                          vm->def->virtType);
3203         goto cleanup;
3204     }
3205
3206     /*
3207      * Theoretically, the pid can be replaced during this operation and
3208      * return the label of a different process.  If atomicity is needed,
3209      * further validation will be required.
3210      *
3211      * Comment from Dan Berrange:
3212      *
3213      *   Well the PID as stored in the virDomainObjPtr can't be changed
3214      *   because you've got a locked object.  The OS level PID could have
3215      *   exited, though and in extreme circumstances have cycled through all
3216      *   PIDs back to ours. We could sanity check that our PID still exists
3217      *   after reading the label, by checking that our FD connecting to the
3218      *   QEMU monitor hasn't seen SIGHUP/ERR on poll().
3219      */
3220     if (virDomainIsActive(vm)) {
3221         if (driver->securityDriver && driver->securityDriver->domainGetSecurityLabel) {
3222             if (driver->securityDriver->domainGetSecurityLabel(dom->conn, vm, seclabel) == -1) {
3223                 qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3224                                  _("Failed to get security label"));
3225                 goto cleanup;
3226             }
3227         }
3228     }
3229
3230     ret = 0;
3231
3232 cleanup:
3233     if (vm)
3234         virDomainObjUnlock(vm);
3235     qemuDriverUnlock(driver);
3236     return ret;
3237 }
3238
3239 static int qemudNodeGetSecurityModel(virConnectPtr conn,
3240                                      virSecurityModelPtr secmodel)
3241 {
3242     struct qemud_driver *driver = (struct qemud_driver *)conn->privateData;
3243     char *p;
3244     int ret = 0;
3245
3246     qemuDriverLock(driver);
3247     if (!driver->securityDriver) {
3248         memset(secmodel, 0, sizeof (*secmodel));
3249         goto cleanup;
3250     }
3251
3252     p = driver->caps->host.secModel.model;
3253     if (strlen(p) >= VIR_SECURITY_MODEL_BUFLEN-1) {
3254         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
3255                          _("security model string exceeds max %d bytes"),
3256                          VIR_SECURITY_MODEL_BUFLEN-1);
3257         ret = -1;
3258         goto cleanup;
3259     }
3260     strcpy(secmodel->model, p);
3261
3262     p = driver->caps->host.secModel.doi;
3263     if (strlen(p) >= VIR_SECURITY_DOI_BUFLEN-1) {
3264         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
3265                          _("security DOI string exceeds max %d bytes"),
3266                          VIR_SECURITY_DOI_BUFLEN-1);
3267         ret = -1;
3268         goto cleanup;
3269     }
3270     strcpy(secmodel->doi, p);
3271
3272 cleanup:
3273     qemuDriverUnlock(driver);
3274     return ret;
3275 }
3276
3277 /* TODO: check seclabel restore */
3278 static int qemudDomainRestore(virConnectPtr conn,
3279                               const char *path) {
3280     struct qemud_driver *driver = conn->privateData;
3281     virDomainDefPtr def = NULL;
3282     virDomainObjPtr vm = NULL;
3283     int fd = -1;
3284     int ret = -1;
3285     char *xml = NULL;
3286     struct qemud_save_header header;
3287     virDomainEventPtr event = NULL;
3288
3289     qemuDriverLock(driver);
3290     /* Verify the header and read the XML */
3291     if ((fd = open(path, O_RDONLY)) < 0) {
3292         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3293                          "%s", _("cannot read domain image"));
3294         goto cleanup;
3295     }
3296
3297     if (saferead(fd, &header, sizeof(header)) != sizeof(header)) {
3298         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3299                          "%s", _("failed to read qemu header"));
3300         goto cleanup;
3301     }
3302
3303     if (memcmp(header.magic, QEMUD_SAVE_MAGIC, sizeof(header.magic)) != 0) {
3304         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3305                          "%s", _("image magic is incorrect"));
3306         goto cleanup;
3307     }
3308
3309     if (header.version > QEMUD_SAVE_VERSION) {
3310         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3311                          _("image version is not supported (%d > %d)"),
3312                          header.version, QEMUD_SAVE_VERSION);
3313         goto cleanup;
3314     }
3315
3316     if (VIR_ALLOC_N(xml, header.xml_len) < 0) {
3317         virReportOOMError(conn);
3318         goto cleanup;
3319     }
3320
3321     if (saferead(fd, xml, header.xml_len) != header.xml_len) {
3322         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3323                          "%s", _("failed to read XML"));
3324         goto cleanup;
3325     }
3326
3327     /* Create a domain from this XML */
3328     if (!(def = virDomainDefParseString(conn, driver->caps, xml,
3329                                         VIR_DOMAIN_XML_INACTIVE))) {
3330         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3331                          "%s", _("failed to parse XML"));
3332         goto cleanup;
3333     }
3334
3335     /* See if a VM with matching UUID already exists */
3336     vm = virDomainFindByUUID(&driver->domains, def->uuid);
3337     if (vm) {
3338         /* UUID matches, but if names don't match, refuse it */
3339         if (STRNEQ(vm->def->name, def->name)) {
3340             char uuidstr[VIR_UUID_STRING_BUFLEN];
3341             virUUIDFormat(vm->def->uuid, uuidstr);
3342             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3343                              _("domain '%s' is already defined with uuid %s"),
3344                              vm->def->name, uuidstr);
3345             goto cleanup;
3346         }
3347
3348         /* UUID & name match, but if VM is already active, refuse it */
3349         if (virDomainIsActive(vm)) {
3350             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_INVALID,
3351                              _("domain is already active as '%s'"), vm->def->name);
3352             goto cleanup;
3353         }
3354         virDomainObjUnlock(vm);
3355     } else {
3356         /* UUID does not match, but if a name matches, refuse it */
3357         vm = virDomainFindByName(&driver->domains, def->name);
3358         if (vm) {
3359             char uuidstr[VIR_UUID_STRING_BUFLEN];
3360             virUUIDFormat(vm->def->uuid, uuidstr);
3361             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3362                              _("domain '%s' is already defined with uuid %s"),
3363                              def->name, uuidstr);
3364             goto cleanup;
3365         }
3366     }
3367
3368     if (!(vm = virDomainAssignDef(conn,
3369                                   &driver->domains,
3370                                   def))) {
3371         qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3372                          "%s", _("failed to assign new VM"));
3373         goto cleanup;
3374     }
3375     def = NULL;
3376
3377     /* Set the migration source and start it up. */
3378     ret = qemudStartVMDaemon(conn, driver, vm, "stdio", fd);
3379     close(fd);
3380     fd = -1;
3381     if (ret < 0) {
3382         if (!vm->persistent) {
3383             virDomainRemoveInactive(&driver->domains,
3384                                     vm);
3385             vm = NULL;
3386         }
3387         goto cleanup;
3388     }
3389
3390     event = virDomainEventNewFromObj(vm,
3391                                      VIR_DOMAIN_EVENT_STARTED,
3392                                      VIR_DOMAIN_EVENT_STARTED_RESTORED);
3393
3394     /* If it was running before, resume it now. */
3395     if (header.was_running) {
3396         char *info;
3397         if (qemudMonitorCommand(vm, "cont", &info) < 0) {
3398             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3399                              "%s", _("failed to resume domain"));
3400             goto cleanup;
3401         }
3402         VIR_FREE(info);
3403         vm->state = VIR_DOMAIN_RUNNING;
3404     }
3405     ret = 0;
3406
3407 cleanup:
3408     virDomainDefFree(def);
3409     VIR_FREE(xml);
3410     if (fd != -1)
3411         close(fd);
3412     if (vm)
3413         virDomainObjUnlock(vm);
3414     if (event)
3415         qemuDomainEventQueue(driver, event);
3416     qemuDriverUnlock(driver);
3417     return ret;
3418 }
3419
3420
3421 static char *qemudDomainDumpXML(virDomainPtr dom,
3422                                 int flags) {
3423     struct qemud_driver *driver = dom->conn->privateData;
3424     virDomainObjPtr vm;
3425     char *ret = NULL;
3426     unsigned long balloon;
3427     int err;
3428
3429     qemuDriverLock(driver);
3430     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3431     qemuDriverUnlock(driver);
3432
3433     if (!vm) {
3434         char uuidstr[VIR_UUID_STRING_BUFLEN];
3435         virUUIDFormat(dom->uuid, uuidstr);
3436         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
3437                          _("no domain with matching uuid '%s'"), uuidstr);
3438         goto cleanup;
3439     }
3440
3441     /* Refresh current memory based on balloon info */
3442     err = qemudDomainGetMemoryBalloon(dom->conn, vm, &balloon);
3443     if (err < 0)
3444         goto cleanup;
3445     if (err > 0)
3446         vm->def->memory = balloon;
3447
3448     ret = virDomainDefFormat(dom->conn,
3449                              (flags & VIR_DOMAIN_XML_INACTIVE) && vm->newDef ?
3450                              vm->newDef : vm->def,
3451                              flags);
3452
3453 cleanup:
3454     if (vm)
3455         virDomainObjUnlock(vm);
3456     return ret;
3457 }
3458
3459
3460 static char *qemuDomainXMLFromNative(virConnectPtr conn,
3461                                      const char *format,
3462                                      const char *config,
3463                                      unsigned int flags ATTRIBUTE_UNUSED) {
3464     struct qemud_driver *driver = conn->privateData;
3465     virDomainDefPtr def = NULL;
3466     char *xml = NULL;
3467
3468     if (STRNEQ(format, QEMU_CONFIG_FORMAT_ARGV)) {
3469         qemudReportError(conn, NULL, NULL, VIR_ERR_INVALID_ARG,
3470                          _("unsupported config type %s"), format);
3471         goto cleanup;
3472     }
3473
3474     def = qemuParseCommandLineString(conn, driver->caps, config);
3475     if (!def)
3476         goto cleanup;
3477
3478     xml = virDomainDefFormat(conn, def, VIR_DOMAIN_XML_INACTIVE);
3479
3480 cleanup:
3481     virDomainDefFree(def);
3482     return xml;
3483 }
3484
3485 static char *qemuDomainXMLToNative(virConnectPtr conn,
3486                                    const char *format,
3487                                    const char *xmlData,
3488                                    unsigned int flags ATTRIBUTE_UNUSED) {
3489     struct qemud_driver *driver = conn->privateData;
3490     virDomainDefPtr def = NULL;
3491     virDomainChrDef monitor_chr;
3492     const char *emulator;
3493     unsigned int qemuCmdFlags;
3494     struct stat sb;
3495     const char **retargv = NULL;
3496     const char **retenv = NULL;
3497     const char **tmp;
3498     virBuffer buf = VIR_BUFFER_INITIALIZER;
3499     char *ret = NULL;
3500     int i;
3501
3502     qemuDriverLock(driver);
3503
3504     if (STRNEQ(format, QEMU_CONFIG_FORMAT_ARGV)) {
3505         qemudReportError(conn, NULL, NULL, VIR_ERR_INVALID_ARG,
3506                          _("unsupported config type %s"), format);
3507         goto cleanup;
3508     }
3509
3510     def = virDomainDefParseString(conn, driver->caps, xmlData, 0);
3511     if (!def)
3512         goto cleanup;
3513
3514     /* Since we're just exporting args, we can't do bridge/network
3515      * setups, since libvirt will normally create TAP devices
3516      * directly. We convert those configs into generic 'ethernet'
3517      * config and assume the user has suitable 'ifup-qemu' scripts
3518      */
3519     for (i = 0 ; i < def->nnets ; i++) {
3520         virDomainNetDefPtr net = def->nets[i];
3521         if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
3522             VIR_FREE(net->data.network.name);
3523
3524             memset(net, 0, sizeof *net);
3525
3526             net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
3527             net->data.ethernet.dev = NULL;
3528             net->data.ethernet.script = NULL;
3529             net->data.ethernet.ipaddr = NULL;
3530         } else if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
3531             char *brname = net->data.bridge.brname;
3532             char *script = net->data.bridge.script;
3533             char *ipaddr = net->data.bridge.ipaddr;
3534
3535             memset(net, 0, sizeof *net);
3536
3537             net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
3538             net->data.ethernet.dev = brname;
3539             net->data.ethernet.script = script;
3540             net->data.ethernet.ipaddr = ipaddr;
3541         }
3542     }
3543     for (i = 0 ; i < def->ngraphics ; i++) {
3544         if (def->graphics[i]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC &&
3545             def->graphics[i]->data.vnc.autoport)
3546             def->graphics[i]->data.vnc.port = 5900;
3547     }
3548     emulator = def->emulator;
3549
3550     /* Make sure the binary we are about to try exec'ing exists.
3551      * Technically we could catch the exec() failure, but that's
3552      * in a sub-process so its hard to feed back a useful error
3553      */
3554     if (stat(emulator, &sb) < 0) {
3555         virReportSystemError(conn, errno,
3556                              _("Cannot find QEMU binary %s"),
3557                              emulator);
3558         goto cleanup;
3559     }
3560
3561     if (qemudExtractVersionInfo(emulator,
3562                                 NULL,
3563                                 &qemuCmdFlags) < 0) {
3564         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
3565                          _("Cannot determine QEMU argv syntax %s"),
3566                          emulator);
3567         goto cleanup;
3568     }
3569
3570     if (qemuPrepareMonitorChr(conn, driver, &monitor_chr, def->name) < 0)
3571         goto cleanup;
3572
3573     if (qemudBuildCommandLine(conn, driver, def,
3574                               &monitor_chr, qemuCmdFlags,
3575                               &retargv, &retenv,
3576                               NULL, NULL, /* Don't want it to create TAP devices */
3577                               NULL) < 0) {
3578         goto cleanup;
3579     }
3580
3581     tmp = retenv;
3582     while (*tmp) {
3583         virBufferAdd(&buf, *tmp, strlen(*tmp));
3584         virBufferAddLit(&buf, " ");
3585         tmp++;
3586     }
3587     tmp = retargv;
3588     while (*tmp) {
3589         virBufferAdd(&buf, *tmp, strlen(*tmp));
3590         virBufferAddLit(&buf, " ");
3591         tmp++;
3592     }
3593
3594     if (virBufferError(&buf))
3595         goto cleanup;
3596
3597     ret = virBufferContentAndReset(&buf);
3598
3599 cleanup:
3600     qemuDriverUnlock(driver);
3601     for (tmp = retargv ; tmp && *tmp ; tmp++)
3602         VIR_FREE(*tmp);
3603     VIR_FREE(retargv);
3604
3605     for (tmp = retenv ; tmp && *tmp ; tmp++)
3606         VIR_FREE(*tmp);
3607     VIR_FREE(retenv);
3608
3609     virDomainDefFree(def);
3610     return ret;
3611 }
3612
3613
3614 static int qemudListDefinedDomains(virConnectPtr conn,
3615                             char **const names, int nnames) {
3616     struct qemud_driver *driver = conn->privateData;
3617     int got = 0, i;
3618
3619     qemuDriverLock(driver);
3620     for (i = 0 ; i < driver->domains.count && got < nnames ; i++) {
3621         virDomainObjLock(driver->domains.objs[i]);
3622         if (!virDomainIsActive(driver->domains.objs[i])) {
3623             if (!(names[got++] = strdup(driver->domains.objs[i]->def->name))) {
3624                 virReportOOMError(conn);
3625                 virDomainObjUnlock(driver->domains.objs[i]);
3626                 goto cleanup;
3627             }
3628         }
3629         virDomainObjUnlock(driver->domains.objs[i]);
3630     }
3631
3632     qemuDriverUnlock(driver);
3633     return got;
3634
3635  cleanup:
3636     for (i = 0 ; i < got ; i++)
3637         VIR_FREE(names[i]);
3638     qemuDriverUnlock(driver);
3639     return -1;
3640 }
3641
3642 static int qemudNumDefinedDomains(virConnectPtr conn) {
3643     struct qemud_driver *driver = conn->privateData;
3644     int n = 0, i;
3645
3646     qemuDriverLock(driver);
3647     for (i = 0 ; i < driver->domains.count ; i++)
3648         if (!virDomainIsActive(driver->domains.objs[i]))
3649             n++;
3650     qemuDriverUnlock(driver);
3651
3652     return n;
3653 }
3654
3655
3656 static int qemudDomainStart(virDomainPtr dom) {
3657     struct qemud_driver *driver = dom->conn->privateData;
3658     virDomainObjPtr vm;
3659     int ret = -1;
3660     virDomainEventPtr event = NULL;
3661
3662     qemuDriverLock(driver);
3663     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3664
3665     if (!vm) {
3666         char uuidstr[VIR_UUID_STRING_BUFLEN];
3667         virUUIDFormat(dom->uuid, uuidstr);
3668         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
3669                          _("no domain with matching uuid '%s'"), uuidstr);
3670         goto cleanup;
3671     }
3672
3673     ret = qemudStartVMDaemon(dom->conn, driver, vm, NULL, -1);
3674     if (ret != -1)
3675         event = virDomainEventNewFromObj(vm,
3676                                          VIR_DOMAIN_EVENT_STARTED,
3677                                          VIR_DOMAIN_EVENT_STARTED_BOOTED);
3678
3679 cleanup:
3680     if (vm)
3681         virDomainObjUnlock(vm);
3682     if (event)
3683         qemuDomainEventQueue(driver, event);
3684     qemuDriverUnlock(driver);
3685     return ret;
3686 }
3687
3688
3689 static virDomainPtr qemudDomainDefine(virConnectPtr conn, const char *xml) {
3690     struct qemud_driver *driver = conn->privateData;
3691     virDomainDefPtr def;
3692     virDomainObjPtr vm = NULL;
3693     virDomainPtr dom = NULL;
3694     virDomainEventPtr event = NULL;
3695     int newVM = 1;
3696
3697     qemuDriverLock(driver);
3698     if (!(def = virDomainDefParseString(conn, driver->caps, xml,
3699                                         VIR_DOMAIN_XML_INACTIVE)))
3700         goto cleanup;
3701
3702     if (virSecurityDriverVerify(conn, def) < 0)
3703         goto cleanup;
3704
3705     /* See if a VM with matching UUID already exists */
3706     vm = virDomainFindByUUID(&driver->domains, def->uuid);
3707     if (vm) {
3708         /* UUID matches, but if names don't match, refuse it */
3709         if (STRNEQ(vm->def->name, def->name)) {
3710             char uuidstr[VIR_UUID_STRING_BUFLEN];
3711             virUUIDFormat(vm->def->uuid, uuidstr);
3712             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3713                              _("domain '%s' is already defined with uuid %s"),
3714                              vm->def->name, uuidstr);
3715             goto cleanup;
3716         }
3717
3718         /* UUID & name match */
3719         virDomainObjUnlock(vm);
3720         newVM = 0;
3721     } else {
3722         /* UUID does not match, but if a name matches, refuse it */
3723         vm = virDomainFindByName(&driver->domains, def->name);
3724         if (vm) {
3725             char uuidstr[VIR_UUID_STRING_BUFLEN];
3726             virUUIDFormat(vm->def->uuid, uuidstr);
3727             qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
3728                              _("domain '%s' is already defined with uuid %s"),
3729                              def->name, uuidstr);
3730             goto cleanup;
3731         }
3732     }
3733
3734     if (!(vm = virDomainAssignDef(conn,
3735                                   &driver->domains,
3736                                   def))) {
3737         goto cleanup;
3738     }
3739     def = NULL;
3740     vm->persistent = 1;
3741
3742     if (virDomainSaveConfig(conn,
3743                             driver->configDir,
3744                             vm->newDef ? vm->newDef : vm->def) < 0) {
3745         virDomainRemoveInactive(&driver->domains,
3746                                 vm);
3747         vm = NULL;
3748         goto cleanup;
3749     }
3750
3751     event = virDomainEventNewFromObj(vm,
3752                                      VIR_DOMAIN_EVENT_DEFINED,
3753                                      newVM ?
3754                                      VIR_DOMAIN_EVENT_DEFINED_ADDED :
3755                                      VIR_DOMAIN_EVENT_DEFINED_UPDATED);
3756
3757     dom = virGetDomain(conn, vm->def->name, vm->def->uuid);
3758     if (dom) dom->id = vm->def->id;
3759
3760 cleanup:
3761     virDomainDefFree(def);
3762     if (vm)
3763         virDomainObjUnlock(vm);
3764     if (event)
3765         qemuDomainEventQueue(driver, event);
3766     qemuDriverUnlock(driver);
3767     return dom;
3768 }
3769
3770 static int qemudDomainUndefine(virDomainPtr dom) {
3771     struct qemud_driver *driver = dom->conn->privateData;
3772     virDomainObjPtr vm;
3773     virDomainEventPtr event = NULL;
3774     int ret = -1;
3775
3776     qemuDriverLock(driver);
3777     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
3778
3779     if (!vm) {
3780         char uuidstr[VIR_UUID_STRING_BUFLEN];
3781         virUUIDFormat(dom->uuid, uuidstr);
3782         qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
3783                          _("no domain with matching uuid '%s'"), uuidstr);
3784         goto cleanup;
3785     }
3786
3787     if (virDomainIsActive(vm)) {
3788         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
3789                          "%s", _("cannot delete active domain"));
3790         goto cleanup;
3791     }
3792
3793     if (!vm->persistent) {
3794         qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3795                          "%s", _("cannot undefine transient domain"));
3796         goto cleanup;
3797     }
3798
3799     if (virDomainDeleteConfig(dom->conn, driver->configDir, driver->autostartDir, vm) < 0)
3800         goto cleanup;
3801
3802     event = virDomainEventNewFromObj(vm,
3803                                      VIR_DOMAIN_EVENT_UNDEFINED,
3804                                      VIR_DOMAIN_EVENT_UNDEFINED_REMOVED);
3805
3806     virDomainRemoveInactive(&driver->domains,
3807                             vm);
3808     vm = NULL;
3809     ret = 0;
3810
3811 cleanup:
3812     if (vm)
3813         virDomainObjUnlock(vm);
3814     if (event)
3815         qemuDomainEventQueue(driver, event);
3816     qemuDriverUnlock(driver);
3817     return ret;
3818 }
3819
3820 /* Return the disks name for use in monitor commands */
3821 static char *qemudDiskDeviceName(const virConnectPtr conn,
3822                                  const virDomainDiskDefPtr disk) {
3823
3824     int busid, devid;
3825     int ret;
3826     char *devname;
3827
3828     if (virDiskNameToBusDeviceIndex(disk, &busid, &devid) < 0) {
3829         qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
3830                          _("cannot convert disk '%s' to bus/device index"),
3831                          disk->dst);
3832         return NULL;
3833     }
3834
3835     switch (disk->bus) {
3836         case VIR_DOMAIN_DISK_BUS_IDE:
3837             if (disk->device== VIR_DOMAIN_DISK_DEVICE_DISK)
3838                 ret = virAsprintf(&devname, "ide%d-hd%d", busid, devid);
3839             else
3840                 ret = virAsprintf(&devname, "ide%d-cd%d", busid, devid);
3841             break;
3842         case VIR_DOMAIN_DISK_BUS_SCSI:
3843             if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK)
3844                 ret = virAsprintf(&devname, "scsi%d-hd%d", busid, devid);
3845             else
3846                 ret = virAsprintf(&devname, "scsi%d-cd%d", busid, devid);
3847             break;
3848         case VIR_DOMAIN_DISK_BUS_FDC:
3849             ret = virAsprintf(&devname, "floppy%d", devid);
3850             break;
3851         case VIR_DOMAIN_DISK_BUS_VIRTIO:
3852             ret = virAsprintf(&devname, "virtio%d", devid);
3853             break;
3854         default:
3855             qemudReportError(conn, NULL, NULL, VIR_ERR_NO_SUPPORT,
3856                              _("Unsupported disk name mapping for bus '%s'"),
3857                              virDomainDiskBusTypeToString(disk->bus));
3858             return NULL;
3859     }
3860
3861     if (ret == -1) {
3862         virReportOOMError(conn);
3863         return NULL;
3864     }
3865
3866     return devname;
3867 }
3868
3869 static int qemudDomainChangeEjectableMedia(virConnectPtr conn,
3870                                            virDomainObjPtr vm,
3871                                            virDomainDeviceDefPtr dev)
3872 {
3873     virDomainDiskDefPtr origdisk = NULL, newdisk;
3874     char *cmd, *reply, *safe_path;
3875     char *devname = NULL;
3876     unsigned int qemuCmdFlags;
3877     int i;
3878
3879     origdisk = NULL;
3880     newdisk = dev->data.disk;
3881     for (i = 0 ; i < vm->def->ndisks ; i++) {
3882         if (vm->def->disks[i]->bus == newdisk->bus &&
3883             STREQ(vm->def->disks[i]->dst, newdisk->dst)) {
3884             origdisk = vm->def->disks[i];
3885             break;
3886         }
3887     }
3888
3889     if (!origdisk) {
3890         qemudReportError(conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3891                          _("No device with bus '%s' and target '%s'"),
3892                          virDomainDiskBusTypeToString(newdisk->bus),
3893                          newdisk->dst);
3894         return -1;
3895     }
3896
3897     if (qemudExtractVersionInfo(vm->def->emulator,
3898                                 NULL,
3899                                 &qemuCmdFlags) < 0)
3900         return -1;
3901
3902     if (qemuCmdFlags & QEMUD_CMD_FLAG_DRIVE) {
3903         if (!(devname = qemudDiskDeviceName(conn, newdisk)))
3904             return -1;
3905     } else {
3906         /* Back compat for no -drive option */
3907         if (newdisk->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
3908             devname = strdup(newdisk->dst);
3909         else if (newdisk->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
3910                  STREQ(newdisk->dst, "hdc"))
3911             devname = strdup("cdrom");
3912         else {
3913             qemudReportError(conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
3914                              _("Emulator version does not support removable "
3915                                "media for device '%s' and target '%s'"),
3916                                virDomainDiskDeviceTypeToString(newdisk->device),
3917                                newdisk->dst);
3918             return -1;
3919         }
3920
3921         if (!devname) {
3922             virReportOOMError(conn);
3923             return -1;
3924         }
3925     }
3926
3927     if (newdisk->src) {
3928         safe_path = qemudEscapeMonitorArg(newdisk->src);
3929         if (!safe_path) {
3930             virReportOOMError(conn);
3931             VIR_FREE(devname);
3932             return -1;
3933         }
3934         if (virAsprintf(&cmd, "change %s \"%s\"", devname, safe_path) == -1) {
3935             virReportOOMError(conn);
3936             VIR_FREE(safe_path);
3937             VIR_FREE(devname);
3938             return -1;
3939         }
3940         VIR_FREE(safe_path);
3941
3942     } else if (virAsprintf(&cmd, "eject %s", devname) == -1) {
3943         virReportOOMError(conn);
3944         VIR_FREE(devname);
3945         return -1;
3946     }
3947     VIR_FREE(devname);
3948
3949     if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
3950         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3951                          "%s", _("could not change cdrom media"));
3952         VIR_FREE(cmd);
3953         return -1;
3954     }
3955
3956     /* If the command failed qemu prints:
3957      * device not found, device is locked ...
3958      * No message is printed on success it seems */
3959     DEBUG ("%s: ejectable media change reply: %s", vm->def->name, reply);
3960     if (strstr(reply, "\ndevice ")) {
3961         qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3962                           _("changing cdrom media failed: %s"), reply);
3963         VIR_FREE(reply);
3964         VIR_FREE(cmd);
3965         return -1;
3966     }
3967     VIR_FREE(reply);
3968     VIR_FREE(cmd);
3969
3970     VIR_FREE(origdisk->src);
3971     origdisk->src = newdisk->src;
3972     newdisk->src = NULL;
3973     origdisk->type = newdisk->type;
3974     return 0;
3975 }
3976
3977 static int qemudDomainAttachPciDiskDevice(virConnectPtr conn,
3978                                           virDomainObjPtr vm,
3979                                           virDomainDeviceDefPtr dev)
3980 {
3981     int ret, i;
3982     char *cmd, *reply, *s;
3983     char *safe_path;
3984     const char* type = virDomainDiskBusTypeToString(dev->data.disk->bus);
3985
3986     for (i = 0 ; i < vm->def->ndisks ; i++) {
3987         if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
3988             qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
3989                            _("target %s already exists"), dev->data.disk->dst);
3990             return -1;
3991         }
3992     }
3993
3994     if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
3995         virReportOOMError(conn);
3996         return -1;
3997     }
3998
3999     safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
4000     if (!safe_path) {
4001         virReportOOMError(conn);
4002         return -1;
4003     }
4004
4005     ret = virAsprintf(&cmd, "pci_add 0 storage file=%s,if=%s",
4006                       safe_path, type);
4007     VIR_FREE(safe_path);
4008     if (ret == -1) {
4009         virReportOOMError(conn);
4010         return ret;
4011     }
4012
4013     if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
4014         qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
4015                          _("cannot attach %s disk"), type);
4016         VIR_FREE(cmd);
4017         return -1;
4018     }
4019
4020     DEBUG ("%s: pci_add reply: %s", vm->def->name, reply);
4021     /* If the command succeeds qemu prints:
4022      * OK bus 0... */
4023 #define PCI_ATTACH_OK_MSG "OK bus 0, slot "
4024     if ((s=strstr(reply, PCI_ATTACH_OK_MSG))) {
4025         char* dummy = s;
4026         s += strlen(PCI_ATTACH_OK_MSG);
4027
4028         if (virStrToLong_i ((const char*)s, &dummy, 10, &dev->data.disk->slotnum) == -1)
4029             VIR_WARN("%s", _("Unable to parse slot number\n"));
4030     } else {
4031         qemudReportError (conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
4032                           _("adding %s disk failed"), type);
4033         VIR_FREE(reply);
4034         VIR_FREE(cmd);
4035         return -1;
4036     }
4037
4038     vm->def->disks[vm->def->ndisks++] = dev->data.disk;
4039     qsort(vm->def->disks, vm->def->ndisks, sizeof(*vm->def->disks),
4040           virDomainDiskQSort);
4041
4042     VIR_FREE(reply);
4043     VIR_FREE(cmd);
4044     return 0;
4045 }
4046
4047 static int qemudDomainAttachUsbMassstorageDevice(virConnectPtr conn,
4048                                                  virDomainObjPtr vm,
4049                                                  virDomainDeviceDefPtr dev)
4050 {
4051     int ret, i;
4052     char *safe_path;
4053     char *cmd, *reply;
4054
4055     for (i = 0 ; i < vm->def->ndisks ; i++) {
4056         if (STREQ(vm->def->disks[i]->dst, dev->data.disk->dst)) {
4057             qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
4058                            _("target %s already exists"), dev->data.disk->dst);
4059             return -1;
4060         }
4061     }
4062
4063     if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0) {
4064         virReportOOMError(conn);
4065         return -1;
4066     }
4067
4068     safe_path = qemudEscapeMonitorArg(dev->data.disk->src);
4069     if (!safe_path) {