logging: convert log daemon to use systemd activation APIs

Using the new system activation APIs allows for simpler code setting up
the network services.

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 18:29:43 +01:00
parent 26ae4e1a92
commit 7ac64b5c86

View File

@ -517,78 +517,6 @@ virLogDaemonSetupSignals(virNetDaemonPtr dmn)
} }
static int
virLogDaemonSetupNetworkingSystemD(virNetServerPtr logSrv, virNetServerPtr adminSrv)
{
unsigned int nfds;
size_t i;
if ((nfds = virGetListenFDs()) == 0)
return 0;
if (nfds > 2)
VIR_DEBUG("Too many (%d) file descriptors from systemd", nfds);
for (i = 0; i < nfds && i < 2; i++) {
virNetServerServicePtr svc;
char *path = virGetUNIXSocketPath(3 + i);
virNetServerPtr srv;
int fds[] = { 3 + i };
if (!path)
return -1;
if (strstr(path, "virtlogd-admin-sock")) {
srv = adminSrv;
} else if (strstr(path, "virtlogd-sock")) {
srv = logSrv;
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unknown UNIX socket %s passed in"),
path);
VIR_FREE(path);
return -1;
}
VIR_FREE(path);
/* Systemd passes FDs, starting immediately after stderr,
* so the first FD we'll get is '3'. */
if (!(svc = virNetServerServiceNewFDs(fds,
ARRAY_CARDINALITY(fds),
false,
0,
NULL,
false, 0, 1)))
return -1;
if (virNetServerAddService(srv, svc) < 0) {
virObjectUnref(svc);
return -1;
}
}
return 1;
}
static int
virLogDaemonSetupNetworkingNative(virNetServerPtr srv, const char *sock_path)
{
virNetServerServicePtr svc;
VIR_DEBUG("Setting up networking natively");
if (!(svc = virNetServerServiceNewUNIX(sock_path, 0700, 0, 0,
NULL,
false, 0, 1)))
return -1;
if (virNetServerAddService(srv, svc) < 0) {
virObjectUnref(svc);
return -1;
}
return 0;
}
static void static void
virLogDaemonClientFree(void *opaque) virLogDaemonClientFree(void *opaque)
{ {
@ -1129,6 +1057,12 @@ int main(int argc, char **argv) {
* scratch if rv == 0 * scratch if rv == 0
*/ */
if (rv == 0) { if (rv == 0) {
VIR_AUTOPTR(virSystemdActivation) act = NULL;
virSystemdActivationMap actmap[] = {
{ .name = "virtlogd.socket", .family = AF_UNIX, .path = sock_file },
{ .name = "virtlogd-admin.socket", .family = AF_UNIX, .path = admin_sock_file },
};
if (godaemon) { if (godaemon) {
char ebuf[1024]; char ebuf[1024];
@ -1156,31 +1090,46 @@ int main(int argc, char **argv) {
goto cleanup; goto cleanup;
} }
logSrv = virNetDaemonGetServer(logDaemon->dmn, "virtlogd"); if (virSystemdGetActivation(actmap,
adminSrv = virNetDaemonGetServer(logDaemon->dmn, "admin"); ARRAY_CARDINALITY(actmap),
if ((rv = virLogDaemonSetupNetworkingSystemD(logSrv, adminSrv)) < 0) { &act) < 0) {
ret = VIR_LOG_DAEMON_ERR_NETWORK; ret = VIR_LOG_DAEMON_ERR_NETWORK;
goto cleanup; goto cleanup;
} }
/* Only do this, if systemd did not pass a FD */ logSrv = virNetDaemonGetServer(logDaemon->dmn, "virtlogd");
if (rv == 0) {
if (virLogDaemonSetupNetworkingNative(logSrv, sock_file) < 0 ||
virLogDaemonSetupNetworkingNative(adminSrv, admin_sock_file) < 0) {
ret = VIR_LOG_DAEMON_ERR_NETWORK;
goto cleanup;
}
}
virObjectUnref(logSrv);
virObjectUnref(adminSrv);
}
logSrv = virNetDaemonGetServer(logDaemon->dmn, "virtlogd");
/* If exec-restarting from old virtlogd, we won't have an
* admin server present */
if (virNetDaemonHasServer(logDaemon->dmn, "admin"))
adminSrv = virNetDaemonGetServer(logDaemon->dmn, "admin"); adminSrv = virNetDaemonGetServer(logDaemon->dmn, "admin");
if (virNetServerAddServiceUNIX(logSrv,
act, "virtlogd.socket",
sock_file, 0700, 0, 0,
NULL,
false, 0, 1) < 0) {
ret = VIR_LOG_DAEMON_ERR_NETWORK;
goto cleanup;
}
if (virNetServerAddServiceUNIX(adminSrv,
act, "virtlogd-admin.socket",
admin_sock_file, 0700, 0, 0,
NULL,
false, 0, 1) < 0) {
ret = VIR_LOG_DAEMON_ERR_NETWORK;
goto cleanup;
}
if (act &&
virSystemdActivationComplete(act) < 0) {
ret = VIR_LOG_DAEMON_ERR_NETWORK;
goto cleanup;
}
} else {
logSrv = virNetDaemonGetServer(logDaemon->dmn, "virtlogd");
/* If exec-restarting from old virtlogd, we won't have an
* admin server present */
if (virNetDaemonHasServer(logDaemon->dmn, "admin"))
adminSrv = virNetDaemonGetServer(logDaemon->dmn, "admin");
}
if (timeout != -1) { if (timeout != -1) {
VIR_DEBUG("Registering shutdown timeout %d", timeout); VIR_DEBUG("Registering shutdown timeout %d", timeout);
virNetDaemonAutoShutdown(logDaemon->dmn, virNetDaemonAutoShutdown(logDaemon->dmn,