mctp-demux-daemon: Use systemd socket activation

Take advantage of lazy initialisation.

However, this also allows (one) daemon providing services over MCTP
(pldmd) an opportunity to start prior to MCTP interfaces coming up. This
is a stunted way to provide capabilities that might be assumed by other
components (the host) without implementing some required messages from
the MCTP standard.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I4b1c21f4fd42f84e2c85a453570a74330fc70ecf
diff --git a/utils/mctp-demux-daemon.c b/utils/mctp-demux-daemon.c
index 0774350..4467fe3 100644
--- a/utils/mctp-demux-daemon.c
+++ b/utils/mctp-demux-daemon.c
@@ -2,6 +2,8 @@
 
 #define _GNU_SOURCE
 
+#include "config.h"
+
 #include <assert.h>
 #include <err.h>
 #include <errno.h>
@@ -17,6 +19,8 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#define SD_LISTEN_FDS_START 3
+
 #include "libmctp.h"
 #include "libmctp-serial.h"
 #include "libmctp-astlpc.h"
@@ -24,6 +28,15 @@
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
 #define __unused __attribute__((unused))
 
+#if HAVE_SYSTEMD_SD_DAEMON_H
+#include <systemd/sd-daemon.h>
+#else
+static inline int sd_listen_fds(int i __unused)
+{
+	return -1;
+}
+#endif
+
 static const mctp_eid_t local_eid_default = 8;
 static char sockname[] = "\0mctp-mux";
 
@@ -551,9 +564,14 @@
 	if (rc)
 		return EXIT_FAILURE;
 
-	rc = socket_init(ctx);
-	if (rc)
-		return EXIT_FAILURE;
+	rc = sd_listen_fds(true);
+	if (rc <= 0) {
+		rc = socket_init(ctx);
+		if (rc)
+			return EXIT_FAILURE;
+	} else {
+		ctx->sock = SD_LISTEN_FDS_START;
+	}
 
 	rc = run_daemon(ctx);