mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
rpc: refactor RPC service constructors to share more code
Introduce a virNetServerServiceNewSocket API that allows the various constructors to share more code. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
5b8569dd6e
commit
3b6bfde089
@ -128,14 +128,14 @@ virNetServerServiceNewFDOrUNIX(const char *path,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
|
static virNetServerServicePtr
|
||||||
const char *service,
|
virNetServerServiceNewSocket(virNetSocketPtr *socks,
|
||||||
int family,
|
size_t nsocks,
|
||||||
int auth,
|
int auth,
|
||||||
virNetTLSContextPtr tls,
|
virNetTLSContextPtr tls,
|
||||||
bool readonly,
|
bool readonly,
|
||||||
size_t max_queued_clients,
|
size_t max_queued_clients,
|
||||||
size_t nrequests_client_max)
|
size_t nrequests_client_max)
|
||||||
{
|
{
|
||||||
virNetServerServicePtr svc;
|
virNetServerServicePtr svc;
|
||||||
size_t i;
|
size_t i;
|
||||||
@ -146,18 +146,18 @@ virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
|
|||||||
if (!(svc = virObjectNew(virNetServerServiceClass)))
|
if (!(svc = virObjectNew(virNetServerServiceClass)))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
if (VIR_ALLOC_N(svc->socks, nsocks) < 0)
|
||||||
|
goto error;
|
||||||
|
svc->nsocks = nsocks;
|
||||||
|
for (i = 0; i < svc->nsocks; i++) {
|
||||||
|
svc->socks[i] = socks[i];
|
||||||
|
virObjectRef(svc->socks[i]);
|
||||||
|
}
|
||||||
svc->auth = auth;
|
svc->auth = auth;
|
||||||
svc->readonly = readonly;
|
svc->readonly = readonly;
|
||||||
svc->nrequests_client_max = nrequests_client_max;
|
svc->nrequests_client_max = nrequests_client_max;
|
||||||
svc->tls = virObjectRef(tls);
|
svc->tls = virObjectRef(tls);
|
||||||
|
|
||||||
if (virNetSocketNewListenTCP(nodename,
|
|
||||||
service,
|
|
||||||
family,
|
|
||||||
&svc->socks,
|
|
||||||
&svc->nsocks) < 0)
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
for (i = 0; i < svc->nsocks; i++) {
|
for (i = 0; i < svc->nsocks; i++) {
|
||||||
if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0)
|
if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
@ -184,6 +184,43 @@ virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
|
||||||
|
const char *service,
|
||||||
|
int family,
|
||||||
|
int auth,
|
||||||
|
virNetTLSContextPtr tls,
|
||||||
|
bool readonly,
|
||||||
|
size_t max_queued_clients,
|
||||||
|
size_t nrequests_client_max)
|
||||||
|
{
|
||||||
|
virNetServerServicePtr svc;
|
||||||
|
size_t i;
|
||||||
|
virNetSocketPtr *socks;
|
||||||
|
size_t nsocks;
|
||||||
|
|
||||||
|
if (virNetSocketNewListenTCP(nodename,
|
||||||
|
service,
|
||||||
|
family,
|
||||||
|
&socks,
|
||||||
|
&nsocks) < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
svc = virNetServerServiceNewSocket(socks,
|
||||||
|
nsocks,
|
||||||
|
auth,
|
||||||
|
tls,
|
||||||
|
readonly,
|
||||||
|
max_queued_clients,
|
||||||
|
nrequests_client_max);
|
||||||
|
|
||||||
|
for (i = 0; i < nsocks; i++)
|
||||||
|
virObjectUnref(socks[i]);
|
||||||
|
VIR_FREE(socks);
|
||||||
|
|
||||||
|
return svc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
virNetServerServicePtr virNetServerServiceNewUNIX(const char *path,
|
virNetServerServicePtr virNetServerServiceNewUNIX(const char *path,
|
||||||
mode_t mask,
|
mode_t mask,
|
||||||
gid_t grp,
|
gid_t grp,
|
||||||
@ -194,53 +231,26 @@ virNetServerServicePtr virNetServerServiceNewUNIX(const char *path,
|
|||||||
size_t nrequests_client_max)
|
size_t nrequests_client_max)
|
||||||
{
|
{
|
||||||
virNetServerServicePtr svc;
|
virNetServerServicePtr svc;
|
||||||
size_t i;
|
virNetSocketPtr sock;
|
||||||
|
|
||||||
if (virNetServerServiceInitialize() < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (!(svc = virObjectNew(virNetServerServiceClass)))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
svc->auth = auth;
|
|
||||||
svc->readonly = readonly;
|
|
||||||
svc->nrequests_client_max = nrequests_client_max;
|
|
||||||
svc->tls = virObjectRef(tls);
|
|
||||||
|
|
||||||
if (VIR_ALLOC_N(svc->socks, 1) < 0)
|
|
||||||
goto error;
|
|
||||||
svc->nsocks = 1;
|
|
||||||
|
|
||||||
if (virNetSocketNewListenUNIX(path,
|
if (virNetSocketNewListenUNIX(path,
|
||||||
mask,
|
mask,
|
||||||
-1,
|
-1,
|
||||||
grp,
|
grp,
|
||||||
&svc->socks[0]) < 0)
|
&sock) < 0)
|
||||||
goto error;
|
return NULL;
|
||||||
|
|
||||||
for (i = 0; i < svc->nsocks; i++) {
|
svc = virNetServerServiceNewSocket(&sock,
|
||||||
if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0)
|
1,
|
||||||
goto error;
|
auth,
|
||||||
|
tls,
|
||||||
/* IO callback is initially disabled, until we're ready
|
readonly,
|
||||||
* to deal with incoming clients */
|
max_queued_clients,
|
||||||
virObjectRef(svc);
|
nrequests_client_max);
|
||||||
if (virNetSocketAddIOCallback(svc->socks[i],
|
|
||||||
0,
|
|
||||||
virNetServerServiceAccept,
|
|
||||||
svc,
|
|
||||||
virObjectFreeCallback) < 0) {
|
|
||||||
virObjectUnref(svc);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
virObjectUnref(sock);
|
||||||
|
|
||||||
return svc;
|
return svc;
|
||||||
|
|
||||||
error:
|
|
||||||
virObjectUnref(svc);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virNetServerServicePtr virNetServerServiceNewFD(int fd,
|
virNetServerServicePtr virNetServerServiceNewFD(int fd,
|
||||||
@ -251,50 +261,23 @@ virNetServerServicePtr virNetServerServiceNewFD(int fd,
|
|||||||
size_t nrequests_client_max)
|
size_t nrequests_client_max)
|
||||||
{
|
{
|
||||||
virNetServerServicePtr svc;
|
virNetServerServicePtr svc;
|
||||||
size_t i;
|
virNetSocketPtr sock;
|
||||||
|
|
||||||
if (virNetServerServiceInitialize() < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (!(svc = virObjectNew(virNetServerServiceClass)))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
svc->auth = auth;
|
|
||||||
svc->readonly = readonly;
|
|
||||||
svc->nrequests_client_max = nrequests_client_max;
|
|
||||||
svc->tls = virObjectRef(tls);
|
|
||||||
|
|
||||||
if (VIR_ALLOC_N(svc->socks, 1) < 0)
|
|
||||||
goto error;
|
|
||||||
svc->nsocks = 1;
|
|
||||||
|
|
||||||
if (virNetSocketNewListenFD(fd,
|
if (virNetSocketNewListenFD(fd,
|
||||||
&svc->socks[0]) < 0)
|
&sock) < 0)
|
||||||
goto error;
|
return NULL;
|
||||||
|
|
||||||
for (i = 0; i < svc->nsocks; i++) {
|
svc = virNetServerServiceNewSocket(&sock,
|
||||||
if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0)
|
1,
|
||||||
goto error;
|
auth,
|
||||||
|
tls,
|
||||||
/* IO callback is initially disabled, until we're ready
|
readonly,
|
||||||
* to deal with incoming clients */
|
max_queued_clients,
|
||||||
virObjectRef(svc);
|
nrequests_client_max);
|
||||||
if (virNetSocketAddIOCallback(svc->socks[i],
|
|
||||||
0,
|
|
||||||
virNetServerServiceAccept,
|
|
||||||
svc,
|
|
||||||
virObjectFreeCallback) < 0) {
|
|
||||||
virObjectUnref(svc);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
virObjectUnref(sock);
|
||||||
|
|
||||||
return svc;
|
return svc;
|
||||||
|
|
||||||
error:
|
|
||||||
virObjectUnref(svc);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user