rpc: allow creating RPC service from an array of FDs

The virNetServerServiceNewFD API only accepts a single FD, but it is
easily changed to allow for an array of FDs to be passed in.

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-25 15:54:48 +01:00
parent 3b6bfde089
commit 9f3d1c5c8f
5 changed files with 52 additions and 34 deletions

View File

@ -202,8 +202,8 @@ virNetServerServiceGetMaxRequests;
virNetServerServiceGetPort; virNetServerServiceGetPort;
virNetServerServiceGetTLSContext; virNetServerServiceGetTLSContext;
virNetServerServiceIsReadonly; virNetServerServiceIsReadonly;
virNetServerServiceNewFD;
virNetServerServiceNewFDOrUNIX; virNetServerServiceNewFDOrUNIX;
virNetServerServiceNewFDs;
virNetServerServiceNewPostExecRestart; virNetServerServiceNewPostExecRestart;
virNetServerServiceNewTCP; virNetServerServiceNewTCP;
virNetServerServiceNewUNIX; virNetServerServiceNewUNIX;

View File

@ -597,6 +597,7 @@ virLockDaemonSetupNetworkingSystemD(virNetServerPtr lockSrv, virNetServerPtr adm
virNetServerServicePtr svc; virNetServerServicePtr svc;
char *path = virGetUNIXSocketPath(3 + i); char *path = virGetUNIXSocketPath(3 + i);
virNetServerPtr srv; virNetServerPtr srv;
int fds[] = { 3 + i };
if (!path) if (!path)
return -1; return -1;
@ -616,7 +617,9 @@ virLockDaemonSetupNetworkingSystemD(virNetServerPtr lockSrv, virNetServerPtr adm
/* Systemd passes FDs, starting immediately after stderr, /* Systemd passes FDs, starting immediately after stderr,
* so the first FD we'll get is '3'. */ * so the first FD we'll get is '3'. */
if (!(svc = virNetServerServiceNewFD(3 + i, 0, if (!(svc = virNetServerServiceNewFDs(fds,
ARRAY_CARDINALITY(fds),
0,
NULL, NULL,
false, 0, 1))) false, 0, 1)))
return -1; return -1;

View File

@ -532,6 +532,7 @@ virLogDaemonSetupNetworkingSystemD(virNetServerPtr logSrv, virNetServerPtr admin
virNetServerServicePtr svc; virNetServerServicePtr svc;
char *path = virGetUNIXSocketPath(3 + i); char *path = virGetUNIXSocketPath(3 + i);
virNetServerPtr srv; virNetServerPtr srv;
int fds[] = { 3 + i };
if (!path) if (!path)
return -1; return -1;
@ -551,7 +552,9 @@ virLogDaemonSetupNetworkingSystemD(virNetServerPtr logSrv, virNetServerPtr admin
/* Systemd passes FDs, starting immediately after stderr, /* Systemd passes FDs, starting immediately after stderr,
* so the first FD we'll get is '3'. */ * so the first FD we'll get is '3'. */
if (!(svc = virNetServerServiceNewFD(3 + i, 0, if (!(svc = virNetServerServiceNewFDs(fds,
ARRAY_CARDINALITY(fds),
0,
NULL, NULL,
false, 0, 1))) false, 0, 1)))
return -1; return -1;

View File

@ -112,13 +112,15 @@ virNetServerServiceNewFDOrUNIX(const char *path,
nrequests_client_max); nrequests_client_max);
} else { } else {
int fds[] = {(*cur_fd)++};
/* /*
* There's still enough file descriptors. In this case we'll * There's still enough file descriptors. In this case we'll
* use the current one and increment it afterwards. Take care * use the current one and increment it afterwards. Take care
* with order of operation for pointer arithmetic and auto * with order of operation for pointer arithmetic and auto
* increment on cur_fd - the parentheses are necessary. * increment on cur_fd - the parentheses are necessary.
*/ */
return virNetServerServiceNewFD((*cur_fd)++, return virNetServerServiceNewFDs(fds,
ARRAY_CARDINALITY(fds),
auth, auth,
tls, tls,
readonly, readonly,
@ -253,30 +255,39 @@ virNetServerServicePtr virNetServerServiceNewUNIX(const char *path,
return svc; return svc;
} }
virNetServerServicePtr virNetServerServiceNewFD(int fd, virNetServerServicePtr virNetServerServiceNewFDs(int *fds,
size_t nfds,
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 = NULL;
virNetSocketPtr sock; virNetSocketPtr *socks;
size_t i;
if (virNetSocketNewListenFD(fd, if (VIR_ALLOC_N(socks, nfds) < 0)
&sock) < 0) goto cleanup;
return NULL;
svc = virNetServerServiceNewSocket(&sock, for (i = 0; i < nfds; i++) {
1, if (virNetSocketNewListenFD(fds[i],
&socks[i]) < 0)
goto cleanup;
}
svc = virNetServerServiceNewSocket(socks,
nfds,
auth, auth,
tls, tls,
readonly, readonly,
max_queued_clients, max_queued_clients,
nrequests_client_max); nrequests_client_max);
virObjectUnref(sock); cleanup:
for (i = 0; i < nfds && socks; i++)
virObjectUnref(socks[i]);
VIR_FREE(socks);
return svc; return svc;
} }

View File

@ -60,7 +60,8 @@ virNetServerServicePtr virNetServerServiceNewUNIX(const char *path,
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 virNetServerServiceNewFD(int fd, virNetServerServicePtr virNetServerServiceNewFDs(int *fd,
size_t nfds,
int auth, int auth,
virNetTLSContextPtr tls, virNetTLSContextPtr tls,
bool readonly, bool readonly,