Merge the ->activevms and ->inactivevms into a single
->vms list where each VM has a "active" flag in order
to make things easier to manage.
Likewise for networks.
Signed-off-by: Mark McLoughlin <markmc(a)redhat.com>
Index: libvirt/qemud/conf.c
===================================================================
--- libvirt.orig/qemud/conf.c
+++ libvirt/qemud/conf.c
@@ -1215,7 +1215,7 @@ struct qemud_vm *qemudLoadConfigXML(stru
xmlFreeDoc(xml);
if ((vm = qemudFindVMByName(server, def->name))) {
- if (vm->id == -1) {
+ if (!qemudIsActiveVM(vm)) {
qemudFreeVMDef(vm->def);
vm->def = def;
} else {
@@ -1263,8 +1263,8 @@ struct qemud_vm *qemudLoadConfigXML(stru
}
if (newVM) {
- vm->next = server->inactivevms;
- server->inactivevms = vm;
+ vm->next = server->vms;
+ server->vms = vm;
server->ninactivevms++;
}
@@ -1570,7 +1570,7 @@ struct qemud_network *qemudLoadNetworkCo
xmlFreeDoc(xml);
if ((network = qemudFindNetworkByName(server, def->name))) {
- if (!network->active) {
+ if (!qemudIsActiveNetwork(network)) {
qemudFreeNetworkDef(network->def);
network->def = def;
} else {
@@ -1611,8 +1611,8 @@ struct qemud_network *qemudLoadNetworkCo
}
if (newNetwork) {
- network->next = server->inactivenetworks;
- server->inactivenetworks = network;
+ network->next = server->networks;
+ server->networks = network;
server->ninactivenetworks++;
}
@@ -1777,7 +1777,7 @@ char *qemudGenerateXML(struct qemud_serv
goto cleanup;
}
- if (vm->id >= 0 && live) {
+ if (qemudIsActiveVM(vm) && live) {
if (qemudBufferPrintf(&buf, "<domain type='%s'
id='%d'>\n", type, vm->id) < 0)
goto no_memory;
} else {
@@ -1945,7 +1945,7 @@ char *qemudGenerateXML(struct qemud_serv
if (def->vncPort &&
qemudBufferPrintf(&buf, " port='%d'",
- vm->id >= 0 && live ? def->vncActivePort :
def->vncPort) < 0)
+ qemudIsActiveVM(vm) && live ? def->vncActivePort
: def->vncPort) < 0)
goto no_memory;
if (qemudBufferAdd(&buf, "/>\n") < 0)
Index: libvirt/qemud/driver.c
===================================================================
--- libvirt.orig/qemud/driver.c
+++ libvirt/qemud/driver.c
@@ -222,10 +222,10 @@ static int qemudGetProcessInfo(unsigned
}
struct qemud_vm *qemudFindVMByID(const struct qemud_server *server, int id) {
- struct qemud_vm *vm = server->activevms;
+ struct qemud_vm *vm = server->vms;
while (vm) {
- if (vm->id == id)
+ if (qemudIsActiveVM(vm) && vm->id == id)
return vm;
vm = vm->next;
}
@@ -235,7 +235,7 @@ struct qemud_vm *qemudFindVMByID(const s
struct qemud_vm *qemudFindVMByUUID(const struct qemud_server *server,
const unsigned char *uuid) {
- struct qemud_vm *vm = server->activevms;
+ struct qemud_vm *vm = server->vms;
while (vm) {
if (!memcmp(vm->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
@@ -243,19 +243,12 @@ struct qemud_vm *qemudFindVMByUUID(const
vm = vm->next;
}
- vm = server->inactivevms;
- while (vm) {
- if (!memcmp(vm->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
- return vm;
- vm = vm->next;
- }
-
return NULL;
}
struct qemud_vm *qemudFindVMByName(const struct qemud_server *server,
const char *name) {
- struct qemud_vm *vm = server->activevms;
+ struct qemud_vm *vm = server->vms;
while (vm) {
if (!strcmp(vm->def->name, name))
@@ -263,13 +256,6 @@ struct qemud_vm *qemudFindVMByName(const
vm = vm->next;
}
- vm = server->inactivevms;
- while (vm) {
- if (!strcmp(vm->def->name, name))
- return vm;
- vm = vm->next;
- }
-
return NULL;
}
@@ -278,12 +264,14 @@ int qemudGetVersion(struct qemud_server
}
int qemudListDomains(struct qemud_server *server, int *ids, int nids) {
- struct qemud_vm *vm = server->activevms;
+ struct qemud_vm *vm = server->vms;
int got = 0;
while (vm && got < nids) {
- ids[got] = vm->id;
+ if (qemudIsActiveVM(vm)) {
+ ids[got] = vm->id;
+ got++;
+ }
vm = vm->next;
- got++;
}
return got;
}
@@ -313,7 +301,7 @@ int qemudDomainSuspend(struct qemud_serv
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching id
%d", id);
return -1;
}
- if (vm->pid == -1) {
+ if (!qemudIsActiveVM(vm)) {
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "domain is not
running");
return -1;
}
@@ -335,7 +323,7 @@ int qemudDomainResume(struct qemud_serve
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching id
%d", id);
return -1;
}
- if (vm->pid == -1) {
+ if (!qemudIsActiveVM(vm)) {
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "domain is not
running");
return -1;
}
@@ -355,11 +343,6 @@ int qemudDomainDestroy(struct qemud_serv
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching id
%d", id);
return -1;
}
- if (vm->pid == -1) {
- qemudReportError(server, VIR_ERR_OPERATION_FAILED, "domain is not
running");
- return -1;
- }
-
if (qemudShutdownVMDaemon(server, vm) < 0)
return -1;
return 0;
@@ -378,14 +361,14 @@ int qemudDomainGetInfo(struct qemud_serv
return -1;
}
- if (vm->pid == -1) {
+ if (!qemudIsActiveVM(vm)) {
*runstate = QEMUD_STATE_STOPPED;
} else {
/* XXX in future need to add PAUSED */
*runstate = QEMUD_STATE_RUNNING;
}
- if (vm->pid == -1) {
+ if (!qemudIsActiveVM(vm)) {
*cputime = 0;
} else {
if (qemudGetProcessInfo(cputime, vm->pid) < 0) {
@@ -408,7 +391,7 @@ int qemudDomainSave(struct qemud_server
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching id
%d", id);
return -1;
}
- if (vm->pid == -1) {
+ if (!qemudIsActiveVM(vm)) {
qemudReportError(server, VIR_ERR_OPERATION_FAILED, "domain is not
running");
return -1;
}
@@ -444,13 +427,15 @@ int qemudDomainDumpXML(struct qemud_serv
int qemudListDefinedDomains(struct qemud_server *server, char *const*names, int nnames)
{
- struct qemud_vm *vm = server->inactivevms;
+ struct qemud_vm *vm = server->vms;
int got = 0;
while (vm && got < nnames) {
- strncpy(names[got], vm->def->name, QEMUD_MAX_NAME_LEN-1);
- names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+ if (!qemudIsActiveVM(vm)) {
+ strncpy(names[got], vm->def->name, QEMUD_MAX_NAME_LEN-1);
+ names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+ got++;
+ }
vm = vm->next;
- got++;
}
return got;
}
@@ -462,30 +447,7 @@ int qemudNumDefinedDomains(struct qemud_
int qemudDomainStart(struct qemud_server *server, struct qemud_vm *vm) {
- struct qemud_vm *prev = NULL, *curr = server->inactivevms;
- if (qemudStartVMDaemon(server, vm) < 0) {
- return 1;
- }
-
- while (curr) {
- if (curr == vm) {
- if (prev)
- prev->next = curr->next;
- else
- server->inactivevms = curr->next;
- server->ninactivevms--;
- break;
- }
- prev = curr;
- curr = curr->next;
- }
-
- vm->next = server->activevms;
- server->activevms = vm;
- server->nactivevms++;
- server->nvmfds += 2;
-
- return 0;
+ return qemudStartVMDaemon(server, vm);
}
@@ -495,14 +457,14 @@ struct qemud_vm *qemudDomainDefine(struc
int qemudDomainUndefine(struct qemud_server *server, const unsigned char *uuid) {
struct qemud_vm *vm = qemudFindVMByUUID(server, uuid);
- struct qemud_vm *prev = NULL, *curr = server->inactivevms;
+ struct qemud_vm *prev = NULL, *curr = server->vms;
if (!vm) {
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no domain with matching
uuid");
return -1;
}
- if (vm->pid != -1) {
+ if (qemudIsActiveVM(vm)) {
qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot delete active
domain");
return -1;
}
@@ -517,7 +479,7 @@ int qemudDomainUndefine(struct qemud_ser
if (prev) {
prev->next = curr->next;
} else {
- server->inactivevms = curr->next;
+ server->vms = curr->next;
}
server->ninactivevms--;
break;
@@ -534,15 +496,8 @@ int qemudDomainUndefine(struct qemud_ser
struct qemud_network *qemudFindNetworkByUUID(const struct qemud_server *server,
const unsigned char *uuid) {
- struct qemud_network *network = server->activenetworks;
-
- while (network) {
- if (!memcmp(network->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
- return network;
- network = network->next;
- }
+ struct qemud_network *network = server->networks;
- network = server->inactivenetworks;
while (network) {
if (!memcmp(network->def->uuid, uuid, QEMUD_UUID_RAW_LEN))
return network;
@@ -554,7 +509,7 @@ struct qemud_network *qemudFindNetworkBy
struct qemud_network *qemudFindNetworkByName(const struct qemud_server *server,
const char *name) {
- struct qemud_network *network = server->activenetworks;
+ struct qemud_network *network = server->networks;
while (network) {
if (!strcmp(network->def->name, name))
@@ -562,13 +517,6 @@ struct qemud_network *qemudFindNetworkBy
network = network->next;
}
- network = server->inactivenetworks;
- while (network) {
- if (!strcmp(network->def->name, name))
- return network;
- network = network->next;
- }
-
return NULL;
}
@@ -577,13 +525,15 @@ int qemudNumNetworks(struct qemud_server
}
int qemudListNetworks(struct qemud_server *server, char *const*names, int nnames) {
- struct qemud_network *network = server->activenetworks;
+ struct qemud_network *network = server->networks;
int got = 0;
while (network && got < nnames) {
- strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
- names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+ if (qemudIsActiveNetwork(network)) {
+ strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
+ names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+ got++;
+ }
network = network->next;
- got++;
}
return got;
}
@@ -593,13 +543,15 @@ int qemudNumDefinedNetworks(struct qemud
}
int qemudListDefinedNetworks(struct qemud_server *server, char *const*names, int nnames)
{
- struct qemud_network *network = server->inactivenetworks;
+ struct qemud_network *network = server->networks;
int got = 0;
while (network && got < nnames) {
- strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
- names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+ if (!qemudIsActiveNetwork(network)) {
+ strncpy(names[got], network->def->name, QEMUD_MAX_NAME_LEN-1);
+ names[got][QEMUD_MAX_NAME_LEN-1] = '\0';
+ got++;
+ }
network = network->next;
- got++;
}
return got;
}
@@ -625,7 +577,7 @@ struct qemud_network *qemudNetworkDefine
int qemudNetworkUndefine(struct qemud_server *server, const unsigned char *uuid) {
struct qemud_network *network = qemudFindNetworkByUUID(server, uuid);
- struct qemud_network *prev = NULL, *curr = server->inactivenetworks;
+ struct qemud_network *prev = NULL, *curr = server->networks;
if (!network) {
qemudReportError(server, VIR_ERR_INVALID_DOMAIN, "no network with matching
uuid");
@@ -642,7 +594,7 @@ int qemudNetworkUndefine(struct qemud_se
if (prev) {
prev->next = curr->next;
} else {
- server->inactivenetworks = curr->next;
+ server->networks = curr->next;
}
server->ninactivenetworks--;
break;
@@ -658,29 +610,7 @@ int qemudNetworkUndefine(struct qemud_se
}
int qemudNetworkStart(struct qemud_server *server, struct qemud_network *network) {
- struct qemud_network *prev = NULL, *curr = server->inactivenetworks;
- if (qemudStartNetworkDaemon(server, network) < 0) {
- return 1;
- }
-
- while (curr) {
- if (curr == network) {
- if (prev)
- prev->next = curr->next;
- else
- server->inactivenetworks = curr->next;
- server->ninactivenetworks--;
- break;
- }
- prev = curr;
- curr = curr->next;
- }
-
- network->next = server->activenetworks;
- server->activenetworks = network;
- server->nactivenetworks++;
-
- return 0;
+ return qemudStartNetworkDaemon(server, network);
}
int qemudNetworkDestroy(struct qemud_server *server, const unsigned char *uuid) {
Index: libvirt/qemud/internal.h
===================================================================
--- libvirt.orig/qemud/internal.h
+++ libvirt/qemud/internal.h
@@ -280,14 +280,12 @@ struct qemud_server {
int sigread;
int nvmfds;
int nactivevms;
- struct qemud_vm *activevms;
int ninactivevms;
- struct qemud_vm *inactivevms;
+ struct qemud_vm *vms;
int nextvmid;
int nactivenetworks;
- struct qemud_network *activenetworks;
int ninactivenetworks;
- struct qemud_network *inactivenetworks;
+ struct qemud_network *networks;
brControl *brctl;
iptablesContext *iptables;
char configDir[PATH_MAX];
@@ -317,6 +315,18 @@ void qemudLog(int priority, const char *
#define qemudDebug(fmt, ...) do { } while(0);
#endif
+static inline int
+qemudIsActiveVM(struct qemud_vm *vm)
+{
+ return vm->id != -1;
+}
+
+static inline int
+qemudIsActiveNetwork(struct qemud_network *network)
+{
+ return network->active;
+}
+
#endif
/*
Index: libvirt/qemud/qemud.c
===================================================================
--- libvirt.orig/qemud/qemud.c
+++ libvirt/qemud/qemud.c
@@ -95,39 +95,43 @@ static int qemudDispatchSignal(struct qe
qemudLog(QEMUD_WARN, "Shutting down on signal %d", sigc);
/* shutdown active VMs */
- vm = server->activevms;
+ vm = server->vms;
while (vm) {
struct qemud_vm *next = vm->next;
- qemudShutdownVMDaemon(server, vm);
+ if (qemudIsActiveVM(vm))
+ qemudShutdownVMDaemon(server, vm);
vm = next;
}
/* free inactive VMs */
- vm = server->inactivevms;
+ vm = server->vms;
while (vm) {
struct qemud_vm *next = vm->next;
qemudFreeVM(vm);
vm = next;
}
- server->inactivevms = NULL;
+ server->vms = NULL;
+ server->nactivevms = 0;
server->ninactivevms = 0;
/* shutdown active networks */
- network = server->activenetworks;
+ network = server->networks;
while (network) {
struct qemud_network *next = network->next;
- qemudShutdownNetworkDaemon(server, network);
+ if (qemudIsActiveNetwork(network))
+ qemudShutdownNetworkDaemon(server, network);
network = next;
}
/* free inactive networks */
- network = server->inactivenetworks;
+ network = server->networks;
while (network) {
struct qemud_network *next = network->next;
qemudFreeNetwork(network);
network = next;
}
- server->inactivenetworks = NULL;
+ server->networks = NULL;
+ server->nactivenetworks = 0;
server->ninactivenetworks = 0;
server->shutdown = 1;
@@ -576,6 +580,12 @@ int qemudStartVMDaemon(struct qemud_serv
char **argv = NULL;
int i, ret = -1;
+ if (qemudIsActiveVM(vm)) {
+ qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
+ "VM is already active");
+ return -1;
+ }
+
if (vm->def->vncPort < 0)
vm->def->vncActivePort = 5900 + server->nextvmid;
else
@@ -586,6 +596,11 @@ int qemudStartVMDaemon(struct qemud_serv
if (qemudExec(server, argv, &vm->pid, &vm->stdout, &vm->stderr)
== 0) {
vm->id = server->nextvmid++;
+
+ server->ninactivevms--;
+ server->nactivevms++;
+ server->nvmfds += 2;
+
ret = 0;
}
@@ -805,50 +820,24 @@ qemudNetworkIfaceDisconnect(struct qemud
}
int qemudShutdownVMDaemon(struct qemud_server *server, struct qemud_vm *vm) {
- struct qemud_vm *prev = NULL, *curr = server->activevms;
struct qemud_vm_net_def *net;
- qemudLog(QEMUD_INFO, "Shutting down VM '%s'",
vm->def->name);
-
- /* Already cleaned-up */
- if (vm->pid < 0)
+ if (!qemudIsActiveVM(vm))
return 0;
- kill(vm->pid, SIGTERM);
-
- /* Move it to inactive vm list */
- while (curr) {
- if (curr == vm) {
- if (prev) {
- prev->next = curr->next;
- } else {
- server->activevms = curr->next;
- }
- server->nactivevms--;
-
- curr->next = server->inactivevms;
- server->inactivevms = curr;
- server->ninactivevms++;
- break;
- }
- prev = curr;
- curr = curr->next;
- }
+ qemudLog(QEMUD_INFO, "Shutting down VM '%s'",
vm->def->name);
- if (!curr) {
- qemudDebug("Could not find VM to shutdown");
- return 0;
- }
+ kill(vm->pid, SIGTERM);
- qemudVMData(server, vm, curr->stdout);
- qemudVMData(server, vm, curr->stderr);
- close(curr->stdout);
- close(curr->stderr);
- if (curr->monitor != -1)
- close(curr->monitor);
- curr->stdout = -1;
- curr->stderr = -1;
- curr->monitor = -1;
+ qemudVMData(server, vm, vm->stdout);
+ qemudVMData(server, vm, vm->stderr);
+ close(vm->stdout);
+ close(vm->stderr);
+ if (vm->monitor != -1)
+ close(vm->monitor);
+ vm->stdout = -1;
+ vm->stderr = -1;
+ vm->monitor = -1;
server->nvmfds -= 2;
net = vm->def->nets;
@@ -874,6 +863,9 @@ int qemudShutdownVMDaemon(struct qemud_s
vm->newDef = NULL;
}
+ server->nactivevms--;
+ server->ninactivevms++;
+
return 0;
}
@@ -1121,7 +1113,7 @@ int qemudStartNetworkDaemon(struct qemud
const char *name;
int err;
- if (network->active) {
+ if (qemudIsActiveNetwork(network)) {
qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
"network is already active");
return -1;
@@ -1185,6 +1177,9 @@ int qemudStartNetworkDaemon(struct qemud
network->active = 1;
+ server->ninactivenetworks--;
+ server->nactivenetworks++;
+
return 0;
err_delbr2:
@@ -1209,12 +1204,11 @@ int qemudStartNetworkDaemon(struct qemud
int qemudShutdownNetworkDaemon(struct qemud_server *server,
struct qemud_network *network) {
- struct qemud_network *prev, *curr;
int err;
qemudLog(QEMUD_INFO, "Shutting down network '%s'",
network->def->name);
- if (!network->active)
+ if (!qemudIsActiveNetwork(network))
return 0;
if (network->dnsmasqPid > 0)
@@ -1233,27 +1227,6 @@ int qemudShutdownNetworkDaemon(struct qe
network->bridge, strerror(err));
}
- /* Move it to inactive networks list */
- prev = NULL;
- curr = server->activenetworks;
- while (curr) {
- if (curr == network) {
- if (prev) {
- prev->next = curr->next;
- } else {
- server->activenetworks = curr->next;
- }
- server->nactivenetworks--;
-
- curr->next = server->inactivenetworks;
- server->inactivenetworks = curr;
- server->ninactivenetworks++;
- break;
- }
- prev = curr;
- curr = curr->next;
- }
-
if (network->dnsmasqPid > 0 &&
waitpid(network->dnsmasqPid, NULL, WNOHANG) != network->dnsmasqPid) {
kill(network->dnsmasqPid, SIGKILL);
@@ -1271,6 +1244,9 @@ int qemudShutdownNetworkDaemon(struct qe
network->newDef = NULL;
}
+ server->nactivenetworks--;
+ server->ninactivenetworks++;
+
return 0;
}
@@ -1278,7 +1254,7 @@ int qemudShutdownNetworkDaemon(struct qe
static int qemudDispatchPoll(struct qemud_server *server, struct pollfd *fds) {
struct qemud_socket *sock = server->sockets;
struct qemud_client *client = server->clients;
- struct qemud_vm *vm = server->activevms;
+ struct qemud_vm *vm;
struct qemud_vm *tmp;
struct qemud_network *network, *prevnet;
int ret = 0;
@@ -1314,12 +1290,18 @@ static int qemudDispatchPoll(struct qemu
fd++;
client = next;
}
+ vm = server->vms;
while (vm) {
struct qemud_vm *next = vm->next;
int failed = 0,
stdoutfd = vm->stdout,
stderrfd = vm->stderr;
+ if (!qemudIsActiveVM(vm)) {
+ vm = next;
+ continue;
+ }
+
if (stdoutfd != -1) {
if (fds[fd].revents) {
if (fds[fd].revents == POLLIN) {
@@ -1353,17 +1335,18 @@ static int qemudDispatchPoll(struct qemu
/* Cleanup any VMs which shutdown & dont have an associated
config file */
- vm = server->inactivevms;
+ vm = server->vms;
tmp = NULL;
while (vm) {
- if (!vm->configFile[0]) {
+ if (!qemudIsActiveVM(vm) && !vm->configFile[0]) {
struct qemud_vm *next = vm->next;
if (tmp) {
tmp->next = next;
} else {
- server->inactivevms = next;
+ server->vms = next;
}
qemudFreeVM(vm);
+ server->ninactivevms--;
vm = next;
} else {
tmp = vm;
@@ -1372,17 +1355,18 @@ static int qemudDispatchPoll(struct qemu
}
/* Cleanup any networks too */
- network = server->inactivenetworks;
+ network = server->networks;
prevnet = NULL;
while (network) {
- if (!network->configFile[0]) {
+ if (!qemudIsActiveNetwork(network) && !network->configFile[0]) {
struct qemud_network *next = network->next;
if (prevnet) {
prevnet->next = next;
} else {
- server->inactivenetworks = next;
+ server->networks = next;
}
qemudFreeNetwork(network);
+ server->ninactivenetworks--;
network = next;
} else {
prevnet = network;
@@ -1419,7 +1403,9 @@ static void qemudPreparePoll(struct qemu
fds[fd].events = POLLIN | POLLERR | POLLHUP;
fd++;
}
- for (vm = server->activevms ; vm ; vm = vm->next) {
+ for (vm = server->vms ; vm ; vm = vm->next) {
+ if (!qemudIsActiveVM(vm))
+ continue;
if (vm->stdout != -1) {
fds[fd].fd = vm->stdout;
fds[fd].events = POLLIN | POLLERR | POLLHUP;
--