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

View File

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

View File

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

View File

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

View File

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

View File

@ -37,9 +37,12 @@
virNetDaemonPtr virNetDaemonNew(void); virNetDaemonPtr virNetDaemonNew(void);
int virNetDaemonAddServer(virNetDaemonPtr dmn, virNetServerPtr); int virNetDaemonAddServer(virNetDaemonPtr dmn,
const char *serverName,
virNetServerPtr srv);
virNetServerPtr virNetDaemonAddServerPostExec(virNetDaemonPtr dmn, virNetServerPtr virNetDaemonAddServerPostExec(virNetDaemonPtr dmn,
const char *serverName,
virNetServerClientPrivNew clientPrivNew, virNetServerClientPrivNew clientPrivNew,
virNetServerClientPrivNewPostExecRestart clientPrivNewPostExecRestart, virNetServerClientPrivNewPostExecRestart clientPrivNewPostExecRestart,
virNetServerClientPrivPreExecRestart clientPrivPreExecRestart, virNetServerClientPrivPreExecRestart clientPrivPreExecRestart,
@ -79,6 +82,6 @@ void virNetDaemonClose(virNetDaemonPtr dmn);
bool virNetDaemonHasClients(virNetDaemonPtr dmn); bool virNetDaemonHasClients(virNetDaemonPtr dmn);
virNetServerPtr virNetDaemonGetServer(virNetDaemonPtr dmn, virNetServerPtr virNetDaemonGetServer(virNetDaemonPtr dmn,
int subServerID); const char *serverName);
#endif /* __VIR_NET_DAEMON_H__ */ #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, "min_workers": 10,
"max_workers": 50, "max_workers": 50,
"priority_workers": 5, "priority_workers": 5,
@ -61,7 +61,7 @@
} }
] ]
}, },
{ "testServer1": {
"min_workers": 2, "min_workers": 2,
"max_workers": 50, "max_workers": 50,
"priority_workers": 5, "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, "min_workers": 10,
"max_workers": 50, "max_workers": 50,
"priority_workers": 5, "priority_workers": 5,
@ -61,5 +61,5 @@
} }
] ]
} }
] }
} }

View File

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

View File

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

View File

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

View File

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