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:
Daniel P. Berrangé 2019-06-24 11:48:38 +01:00
parent 5b8569dd6e
commit 3b6bfde089

View File

@ -128,14 +128,14 @@ virNetServerServiceNewFDOrUNIX(const char *path,
}
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)
static virNetServerServicePtr
virNetServerServiceNewSocket(virNetSocketPtr *socks,
size_t nsocks,
int auth,
virNetTLSContextPtr tls,
bool readonly,
size_t max_queued_clients,
size_t nrequests_client_max)
{
virNetServerServicePtr svc;
size_t i;
@ -146,18 +146,18 @@ virNetServerServicePtr virNetServerServiceNewTCP(const char *nodename,
if (!(svc = virObjectNew(virNetServerServiceClass)))
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->readonly = readonly;
svc->nrequests_client_max = nrequests_client_max;
svc->tls = virObjectRef(tls);
if (virNetSocketNewListenTCP(nodename,
service,
family,
&svc->socks,
&svc->nsocks) < 0)
goto error;
for (i = 0; i < svc->nsocks; i++) {
if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0)
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,
mode_t mask,
gid_t grp,
@ -194,53 +231,26 @@ virNetServerServicePtr virNetServerServiceNewUNIX(const char *path,
size_t nrequests_client_max)
{
virNetServerServicePtr svc;
size_t i;
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;
virNetSocketPtr sock;
if (virNetSocketNewListenUNIX(path,
mask,
-1,
grp,
&svc->socks[0]) < 0)
goto error;
&sock) < 0)
return NULL;
for (i = 0; i < svc->nsocks; i++) {
if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0)
goto error;
/* IO callback is initially disabled, until we're ready
* to deal with incoming clients */
virObjectRef(svc);
if (virNetSocketAddIOCallback(svc->socks[i],
0,
virNetServerServiceAccept,
svc,
virObjectFreeCallback) < 0) {
virObjectUnref(svc);
goto error;
}
}
svc = virNetServerServiceNewSocket(&sock,
1,
auth,
tls,
readonly,
max_queued_clients,
nrequests_client_max);
virObjectUnref(sock);
return svc;
error:
virObjectUnref(svc);
return NULL;
}
virNetServerServicePtr virNetServerServiceNewFD(int fd,
@ -251,50 +261,23 @@ virNetServerServicePtr virNetServerServiceNewFD(int fd,
size_t nrequests_client_max)
{
virNetServerServicePtr svc;
size_t i;
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;
virNetSocketPtr sock;
if (virNetSocketNewListenFD(fd,
&svc->socks[0]) < 0)
goto error;
&sock) < 0)
return NULL;
for (i = 0; i < svc->nsocks; i++) {
if (virNetSocketListen(svc->socks[i], max_queued_clients) < 0)
goto error;
/* IO callback is initially disabled, until we're ready
* to deal with incoming clients */
virObjectRef(svc);
if (virNetSocketAddIOCallback(svc->socks[i],
0,
virNetServerServiceAccept,
svc,
virObjectFreeCallback) < 0) {
virObjectUnref(svc);
goto error;
}
}
svc = virNetServerServiceNewSocket(&sock,
1,
auth,
tls,
readonly,
max_queued_clients,
nrequests_client_max);
virObjectUnref(sock);
return svc;
error:
virObjectUnref(svc);
return NULL;
}