virnetdaemon: Store servers in a hash table

Since the daemon can manage and add (at fresh start) multiple servers,
we also should be able to add them from a JSON state file in case of a
daemon restart, so post exec restart support for multiple servers is also
provided. Patch also updates virnetdaemontest accordingly.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Erik Skultety 2015-08-10 13:01:44 +02:00
parent 353de572a6
commit 252610f7dd
14 changed files with 561 additions and 234 deletions

View File

@ -1399,7 +1399,7 @@ int main(int argc, char **argv) {
}
if (!(dmn = virNetDaemonNew()) ||
virNetDaemonAddServer(dmn, srv) < 0) {
virNetDaemonAddServer(dmn, "libvirtd", srv) < 0) {
ret = VIR_DAEMON_ERR_INIT;
goto cleanup;
}
@ -1472,7 +1472,7 @@ int main(int argc, char **argv) {
goto cleanup;
}
if (virNetDaemonAddServer(dmn, srvAdm) < 0) {
if (virNetDaemonAddServer(dmn, "admin", srvAdm) < 0) {
ret = VIR_DAEMON_ERR_INIT;
goto cleanup;
}

View File

@ -170,7 +170,7 @@ virLockDaemonNew(virLockDaemonConfigPtr config, bool privileged)
goto error;
if (!(lockd->dmn = virNetDaemonNew()) ||
virNetDaemonAddServer(lockd->dmn, srv) < 0)
virNetDaemonAddServer(lockd->dmn, "virtlockd", srv) < 0)
goto error;
if (!(lockd->lockspaces = virHashCreate(VIR_LOCK_DAEMON_NUM_LOCKSPACES,
@ -267,6 +267,7 @@ virLockDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged)
goto error;
if (!(srv = virNetDaemonAddServerPostExec(lockd->dmn,
"virtlockd",
virLockDaemonClientNew,
virLockDaemonClientNewPostExecRestart,
virLockDaemonClientPreExecRestart,
@ -1369,7 +1370,7 @@ int main(int argc, char **argv) {
goto cleanup;
}
srv = virNetDaemonGetServer(lockDaemon->dmn, 0);
srv = virNetDaemonGetServer(lockDaemon->dmn, "virtlockd");
if ((rv = virLockDaemonSetupNetworkingSystemD(srv) < 0)) {
ret = VIR_LOCK_DAEMON_ERR_NETWORK;
goto cleanup;
@ -1382,7 +1383,7 @@ int main(int argc, char **argv) {
goto cleanup;
}
} else if (rv == 1) {
srv = virNetDaemonGetServer(lockDaemon->dmn, 0);
srv = virNetDaemonGetServer(lockDaemon->dmn, "virtlockd");
}
if (timeout != -1) {

View File

@ -160,7 +160,7 @@ virLogDaemonNew(virLogDaemonConfigPtr config, bool privileged)
goto error;
if (!(logd->dmn = virNetDaemonNew()) ||
virNetDaemonAddServer(logd->dmn, logd->srv) < 0)
virNetDaemonAddServer(logd->dmn, "virtlogd", logd->srv) < 0)
goto error;
if (!(logd->handler = virLogHandlerNew(privileged,
@ -209,6 +209,7 @@ virLogDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged)
goto error;
if (!(logd->srv = virNetDaemonAddServerPostExec(logd->dmn,
"virtlogd",
virLogDaemonClientNew,
virLogDaemonClientNewPostExecRestart,
virLogDaemonClientPreExecRestart,

View File

@ -967,7 +967,7 @@ static int virLXCControllerSetupServer(virLXCControllerPtr ctrl)
goto error;
if (!(ctrl->daemon = virNetDaemonNew()) ||
virNetDaemonAddServer(ctrl->daemon, srv) < 0)
virNetDaemonAddServer(ctrl->daemon, "LXC", srv) < 0)
goto error;
virNetDaemonUpdateServices(ctrl->daemon, true);

View File

@ -37,6 +37,7 @@
#include "virnetserver.h"
#include "virnetservermdns.h"
#include "virdbus.h"
#include "virhash.h"
#include "virstring.h"
#include "virsystemd.h"
@ -69,8 +70,7 @@ struct _virNetDaemon {
int sigwrite;
int sigwatch;
size_t nservers;
virNetServerPtr *servers;
virHashTablePtr servers;
virJSONValuePtr srvObject;
bool quit;
@ -102,9 +102,7 @@ virNetDaemonDispose(void *obj)
if (dmn->sigwatch > 0)
virEventRemoveHandle(dmn->sigwatch);
for (i = 0; i < dmn->nservers; i++)
virObjectUnref(dmn->servers[i]);
VIR_FREE(dmn->servers);
virHashFree(dmn->servers);
virJSONValueFree(dmn->srvObject);
}
@ -136,6 +134,9 @@ virNetDaemonNew(void)
if (!(dmn = virObjectLockableNew(virNetDaemonClass)))
return NULL;
if (!(dmn->servers = virHashCreate(5, virObjectFreeHashData)))
goto error;
dmn->sigwrite = dmn->sigread = -1;
dmn->privileged = geteuid() == 0;
dmn->autoShutdownInhibitFd = -1;
@ -156,49 +157,34 @@ virNetDaemonNew(void)
int
virNetDaemonAddServer(virNetDaemonPtr dmn, virNetServerPtr srv)
virNetDaemonAddServer(virNetDaemonPtr dmn,
const char *serverName,
virNetServerPtr srv)
{
int ret = -1;
virObjectLock(dmn);
if (VIR_APPEND_ELEMENT_COPY(dmn->servers, dmn->nservers, srv) < 0)
if (virHashAddEntry(dmn->servers, serverName, srv) < 0)
goto cleanup;
virObjectRef(srv);
ret = dmn->nservers - 1;
ret = 0;
cleanup:
virObjectUnlock(dmn);
return ret;
}
/*
* Separate function merely for the purpose of unified error
* reporting.
*/
static virNetServerPtr
virNetDaemonGetServerInternal(virNetDaemonPtr dmn,
int subServerID)
{
if (subServerID < 0 || subServerID >= dmn->nservers) {
virReportError(VIR_ERR_INVALID_ARG,
_("Invalid server ID: %d"),
subServerID);
return NULL;
}
return virObjectRef(dmn->servers[subServerID]);
}
virNetServerPtr
virNetDaemonGetServer(virNetDaemonPtr dmn,
int subServerID)
const char *serverName)
{
virNetServerPtr srv = NULL;
virObjectLock(dmn);
srv = virNetDaemonGetServerInternal(dmn, subServerID);
srv = virObjectRef(virHashLookup(dmn->servers, serverName));
virObjectUnlock(dmn);
return srv;
@ -206,6 +192,7 @@ virNetDaemonGetServer(virNetDaemonPtr dmn,
virNetServerPtr
virNetDaemonAddServerPostExec(virNetDaemonPtr dmn,
const char *serverName,
virNetServerClientPrivNew clientPrivNew,
virNetServerClientPrivNewPostExecRestart clientPrivNewPostExecRestart,
virNetServerClientPrivPreExecRestart clientPrivPreExecRestart,
@ -230,9 +217,23 @@ virNetDaemonAddServerPostExec(virNetDaemonPtr dmn,
virJSONValueFree(dmn->srvObject);
dmn->srvObject = NULL;
}
} else {
} else if (virJSONValueObjectGetByType(dmn->srvObject,
"min_workers",
VIR_JSON_TYPE_NUMBER)) {
object = dmn->srvObject;
dmn->srvObject = NULL;
} else {
int ret = virJSONValueObjectRemoveKey(dmn->srvObject,
serverName,
&object);
if (ret != 1) {
if (ret == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Server '%s' not found in JSON"), serverName);
}
goto error;
}
}
srv = virNetServerNewPostExecRestart(object,
@ -242,7 +243,10 @@ virNetDaemonAddServerPostExec(virNetDaemonPtr dmn,
clientPrivFree,
clientPrivOpaque);
if (!srv || VIR_APPEND_ELEMENT_COPY(dmn->servers, dmn->nservers, srv) < 0)
if (!srv)
goto error;
if (virHashAddEntry(dmn->servers, serverName, srv) < 0)
goto error;
virJSONValueFree(object);
@ -283,29 +287,51 @@ virNetDaemonNewPostExecRestart(virJSONValuePtr object)
}
static int
daemonServerCompare(const virHashKeyValuePair *a, const virHashKeyValuePair *b)
{
const char *as = a->key;
const char *bs = b->key;
return strcmp(as, bs);
}
virJSONValuePtr
virNetDaemonPreExecRestart(virNetDaemonPtr dmn)
{
virJSONValuePtr object, srvArray = NULL;
size_t i;
size_t i = 0;
virJSONValuePtr object = NULL;
virJSONValuePtr srvObj = NULL;
virHashKeyValuePairPtr srvArray = NULL;
virObjectLock(dmn);
if (!(object = virJSONValueNewObject()))
goto error;
if (!(srvArray = virJSONValueNewArray()) ||
virJSONValueObjectAppend(object, "servers", srvArray) < 0)
if (!(srvObj = virJSONValueNewObject()))
goto error;
for (i = 0; i < dmn->nservers; i++) {
virJSONValuePtr srvJSON = NULL;
srvJSON = virNetServerPreExecRestart(dmn->servers[i]);
if (virJSONValueObjectAppend(object, "servers", srvObj) < 0) {
virJSONValueFree(srvObj);
goto error;
}
if (!(srvArray = virHashGetItems(dmn->servers, daemonServerCompare)))
goto error;
for (i = 0; srvArray[i].key; i++) {
virNetServerPtr server = virHashLookup(dmn->servers, srvArray[i].key);
virJSONValuePtr srvJSON;
if (!server)
goto error;
srvJSON = virNetServerPreExecRestart(server);
if (!srvJSON)
goto error;
if (virJSONValueArrayAppend(srvArray, srvJSON) < 0) {
if (virJSONValueObjectAppend(srvObj, srvArray[i].key, srvJSON) < 0) {
virJSONValueFree(srvJSON);
goto error;
}
@ -316,8 +342,8 @@ virNetDaemonPreExecRestart(virNetDaemonPtr dmn)
return object;
error:
VIR_FREE(srvArray);
virJSONValueFree(object);
virJSONValueFree(srvArray);
virObjectUnlock(dmn);
return NULL;
}
@ -627,24 +653,53 @@ virNetDaemonAutoShutdownTimer(int timerid ATTRIBUTE_UNUSED,
virObjectUnlock(dmn);
}
static int
daemonServerUpdateServices(void *payload,
const void *key ATTRIBUTE_UNUSED,
void *opaque)
{
bool *enable = opaque;
virNetServerPtr srv = payload;
virNetServerUpdateServices(srv, *enable);
return 0;
}
void
virNetDaemonUpdateServices(virNetDaemonPtr dmn,
bool enabled)
{
size_t i;
virObjectLock(dmn);
for (i = 0; i < dmn->nservers; i++)
virNetServerUpdateServices(dmn->servers[i], enabled);
virHashForEach(dmn->servers, daemonServerUpdateServices, &enabled);
virObjectUnlock(dmn);
}
static int
daemonServerRun(void *payload,
const void *key ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
virNetServerPtr srv = payload;
return virNetServerStart(srv);
};
static int
daemonServerProcessClients(void *payload,
const void *key ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
virNetServerPtr srv = payload;
virNetServerProcessClients(srv);
return 0;
}
void
virNetDaemonRun(virNetDaemonPtr dmn)
{
int timerid = -1;
bool timerActive = false;
size_t i;
virObjectLock(dmn);
@ -654,10 +709,8 @@ virNetDaemonRun(virNetDaemonPtr dmn)
goto cleanup;
}
for (i = 0; i < dmn->nservers; i++) {
if (virNetServerStart(dmn->servers[i]) < 0)
goto cleanup;
}
if (virHashForEach(dmn->servers, daemonServerRun, NULL) < 0)
goto cleanup;
dmn->quit = false;
@ -705,8 +758,7 @@ virNetDaemonRun(virNetDaemonPtr dmn)
}
virObjectLock(dmn);
for (i = 0; i < dmn->nservers; i++)
virNetServerProcessClients(dmn->servers[i]);
virHashForEach(dmn->servers, daemonServerProcessClients, NULL);
}
cleanup:
@ -725,32 +777,42 @@ virNetDaemonQuit(virNetDaemonPtr dmn)
virObjectUnlock(dmn);
}
static int
daemonServerClose(void *payload,
const void *key ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
virNetServerPtr srv = payload;
virNetServerClose(srv);
return 0;
}
void
virNetDaemonClose(virNetDaemonPtr dmn)
{
size_t i;
if (!dmn)
return;
virObjectLock(dmn);
for (i = 0; i < dmn->nservers; i++)
virNetServerClose(dmn->servers[i]);
virHashForEach(dmn->servers, daemonServerClose, NULL);
virObjectUnlock(dmn);
}
static int
daemonServerHasClients(void *payload,
const void *key ATTRIBUTE_UNUSED,
void *opaque ATTRIBUTE_UNUSED)
{
virNetServerPtr srv = payload;
return virNetServerHasClients(srv);
}
bool
virNetDaemonHasClients(virNetDaemonPtr dmn)
{
size_t i = 0;
for (i = 0; i < dmn->nservers; i++) {
if (virNetServerHasClients(dmn->servers[i]))
return true;
}
return false;
return virHashForEach(dmn->servers, daemonServerHasClients, NULL) > 0;
}

View File

@ -37,9 +37,12 @@
virNetDaemonPtr virNetDaemonNew(void);
int virNetDaemonAddServer(virNetDaemonPtr dmn, virNetServerPtr);
int virNetDaemonAddServer(virNetDaemonPtr dmn,
const char *serverName,
virNetServerPtr srv);
virNetServerPtr virNetDaemonAddServerPostExec(virNetDaemonPtr dmn,
const char *serverName,
virNetServerClientPrivNew clientPrivNew,
virNetServerClientPrivNewPostExecRestart clientPrivNewPostExecRestart,
virNetServerClientPrivPreExecRestart clientPrivPreExecRestart,
@ -79,6 +82,6 @@ void virNetDaemonClose(virNetDaemonPtr dmn);
bool virNetDaemonHasClients(virNetDaemonPtr dmn);
virNetServerPtr virNetDaemonGetServer(virNetDaemonPtr dmn,
int subServerID);
const char *serverName);
#endif /* __VIR_NET_DAEMON_H__ */

View File

@ -0,0 +1,129 @@
{
"servers":
{
"testServer0":
{
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"keepaliveRequired": true,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
},
"testServer1":
{
"min_workers": 2,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"keepaliveRequired": true,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
}
}
}

View File

@ -1,6 +1,6 @@
{
"servers": [
{
"servers": {
"testServer0": {
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
@ -61,7 +61,7 @@
}
]
},
{
"testServer1": {
"min_workers": 2,
"max_workers": 50,
"priority_workers": 5,
@ -122,5 +122,5 @@
}
]
}
]
}
}

View File

@ -0,0 +1,126 @@
{
"servers": {
"testServer0": {
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"max_anonymous_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
},
"testServer1": {
"min_workers": 2,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"max_anonymous_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
}
}
}

View File

@ -1,6 +1,6 @@
{
"servers": [
{
"servers": {
"testServer0": {
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
@ -61,5 +61,5 @@
}
]
}
]
}
}

View File

@ -1,6 +1,6 @@
{
"servers": [
{
"servers": {
"testServer0": {
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
@ -61,5 +61,5 @@
}
]
}
]
}
}

View File

@ -1,6 +1,6 @@
{
"servers": [
{
"servers": {
"testServer0": {
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
@ -62,5 +62,5 @@
}
]
}
]
}
}

View File

@ -1,124 +1,126 @@
{
"servers": [
{
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
"servers": {
"testServer0": {
"min_workers": 10,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
},
{
"min_workers": 2,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
},
"testServer1": {
{
"min_workers": 2,
"max_workers": 50,
"priority_workers": 5,
"max_clients": 100,
"keepaliveInterval": 120,
"keepaliveCount": 5,
"services": [
{
"auth": 0,
"readonly": true,
"nrequests_client_max": 2,
"socks": [
{
"fd": 100,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
},
{
"auth": 2,
"readonly": false,
"nrequests_client_max": 5,
"socks": [
{
"fd": 101,
"errfd": -1,
"pid": 0,
"isClient": false
}
]
}
],
"clients": [
{
"auth": 1,
"readonly": true,
"nrequests_max": 15,
"sock": {
"fd": 102,
"errfd": -1,
"pid": -1,
"isClient": true
}
},
{
"auth": 2,
"readonly": true,
"nrequests_max": 66,
"sock": {
"fd": 103,
"errfd": -1,
"pid": -1,
"isClient": true
}
}
]
}
}
}
]
}

View File

@ -135,7 +135,7 @@ testCreateServer(const char *host, int family)
goto cleanup;
}
static char *testGenerateJSON(void)
static char *testGenerateJSON(const char *server_name)
{
virNetDaemonPtr dmn = NULL;
virNetServerPtr srv = NULL;
@ -155,15 +155,14 @@ static char *testGenerateJSON(void)
if (!has_ipv4 && !has_ipv6)
return NULL;
if (!(srv = testCreateServer(
has_ipv4 ? "127.0.0.1" : "::1",
has_ipv4 ? AF_INET : AF_INET6)))
if (!(srv = testCreateServer(has_ipv4 ? "127.0.0.1" : "::1",
has_ipv4 ? AF_INET : AF_INET6)))
goto cleanup;
if (!(dmn = virNetDaemonNew()))
goto cleanup;
if (virNetDaemonAddServer(dmn, srv) < 0)
if (virNetDaemonAddServer(dmn, server_name, srv) < 0)
goto cleanup;
if (!(json = virNetDaemonPreExecRestart(dmn)))
@ -186,6 +185,7 @@ static char *testGenerateJSON(void)
struct testExecRestartData {
const char *jsonfile;
const char **serverNames;
int nservers;
bool pass;
};
@ -241,7 +241,7 @@ static int testExecRestart(const void *opaque)
goto cleanup;
for (i = 0; i < data->nservers; i++) {
if (!(srv = virNetDaemonAddServerPostExec(dmn,
if (!(srv = virNetDaemonAddServerPostExec(dmn, data->serverNames[i],
NULL, NULL, NULL,
NULL, NULL)))
goto cleanup;
@ -257,18 +257,18 @@ static int testExecRestart(const void *opaque)
if (virtTestCompareToFile(outjsonstr, outfile) < 0)
goto cleanup;
if (!data->pass) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", "Test should've failed");
goto cleanup;
}
ret = 0;
cleanup:
if (ret < 0) {
if (!data->pass)
if (!data->pass) {
VIR_TEST_DEBUG("Got expected error: %s\n",
virGetLastErrorMessage());
virResetLastError();
ret = 0;
else
virDispatchError(NULL);
}
} else if (!data->pass) {
VIR_TEST_DEBUG("Test should have failed\n");
ret = -1;
}
VIR_FREE(infile);
VIR_FREE(outfile);
@ -289,6 +289,7 @@ static int
mymain(void)
{
int ret = 0;
const char *server_names[] = { "testServer0", "testServer1" };
if (virInitialize() < 0 ||
virEventRegisterDefaultImpl() < 0) {
@ -302,7 +303,7 @@ mymain(void)
* numbers with 100, 101, 102, 103.
*/
if (getenv("VIR_GENERATE_JSON")) {
char *json = testGenerateJSON();
char *json = testGenerateJSON(server_names[0]);
if (!json)
return EXIT_FAILURE;
@ -311,26 +312,28 @@ mymain(void)
return ret;
}
# define EXEC_RESTART_TEST_FULL(file, servers, pass) \
do { \
struct testExecRestartData data = { \
file, servers, pass \
}; \
if (virtTestRun("ExecRestart " file, \
testExecRestart, &data) < 0) \
ret = -1; \
# define EXEC_RESTART_TEST_FULL(file, nservers, pass) \
do { \
struct testExecRestartData data = { \
file, server_names, nservers, pass \
}; \
if (virtTestRun("ExecRestart " file, \
testExecRestart, &data) < 0) \
ret = -1; \
} while (0)
# define EXEC_RESTART_TEST(file) EXEC_RESTART_TEST_FULL(file, 1, true)
# define EXEC_RESTART_TEST(file, N) EXEC_RESTART_TEST_FULL(file, N, true)
# define EXEC_RESTART_TEST_FAIL(file, N) EXEC_RESTART_TEST_FULL(file, N, false)
# ifdef WITH_AVAHI
EXEC_RESTART_TEST("initial");
EXEC_RESTART_TEST("initial", 1);
# endif
EXEC_RESTART_TEST("initial-nomdns");
EXEC_RESTART_TEST("anon-clients");
EXEC_RESTART_TEST_FULL("anon-clients", 2, false);
EXEC_RESTART_TEST_FULL("admin-nomdns", 2, true);
EXEC_RESTART_TEST("initial-nomdns", 1);
EXEC_RESTART_TEST("anon-clients", 1);
EXEC_RESTART_TEST("admin-nomdns", 2);
EXEC_RESTART_TEST("admin-server-names", 2);
EXEC_RESTART_TEST_FAIL("anon-clients", 2);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}