[PATCH v4 21/32] tools/xenstored: add early_init() function

Juergen Gross posted 32 patches 7 months ago
There is a newer version of this series
[PATCH v4 21/32] tools/xenstored: add early_init() function
Posted by Juergen Gross 7 months ago
Some xenstored initialization needs to be done in the daemon case only,
so split it out into a new early_init() function being a stub in the
stubdom case.

Remove the call of talloc_enable_leak_report_full(), as it serves no
real purpose: the daemon only ever exits due to a crash, in which case
a log of talloc()ed memory hardly has any value.

Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Jason Andryuk <jandryuk@gmail.com>
Reviewed-by: Julien Grall <jgrall@amazon.com>
---
V2:
- rename function
- move patch earlier in the series
V3:
- add more init code for the daemon to early_init()
- move mkdir(XENSTORE_LIB_DIR) into a later patch (Julien Grall)
- remove stale stubs in minios.c
---
 tools/xenstored/core.c   | 30 +++---------------------------
 tools/xenstored/core.h   |  7 +++----
 tools/xenstored/minios.c | 11 +++--------
 tools/xenstored/posix.c  | 31 +++++++++++++++++++++++++++++--
 4 files changed, 38 insertions(+), 41 deletions(-)

diff --git a/tools/xenstored/core.c b/tools/xenstored/core.c
index d4c76d71dc..eba7744fde 100644
--- a/tools/xenstored/core.c
+++ b/tools/xenstored/core.c
@@ -79,7 +79,7 @@ char **orig_argv;
 LIST_HEAD(connections);
 int tracefd = -1;
 bool keep_orphans = false;
-static int reopen_log_pipe[2];
+int reopen_log_pipe[2];
 static int reopen_log_pipe0_pollfd_idx = -1;
 char *tracefile = NULL;
 static struct hashtable *nodes;
@@ -2612,7 +2612,7 @@ static void destroy_fds(void)
 		close(sock);
 }
 
-static void init_sockets(void)
+void init_sockets(void)
 {
 	struct sockaddr_un addr;
 	const char *soc_str = xenstore_daemon_path();
@@ -2903,34 +2903,10 @@ int main(int argc, char *argv[])
 	if (optind != argc)
 		barf("%s: No arguments desired", argv[0]);
 
-	reopen_log();
-
-	/* Make sure xenstored directory exists. */
-	/* Errors ignored here, will be reported when we open files */
-	mkdir(xenstore_daemon_rundir(), 0755);
-
-	if (dofork) {
-		openlog("xenstored", 0, LOG_DAEMON);
-		if (!live_update)
-			daemonize();
-	}
-	if (pidfile)
-		write_pidfile(pidfile);
-
-	/* Talloc leak reports go to stderr, which is closed if we fork. */
-	if (!dofork)
-		talloc_enable_leak_report_full();
-
-	/* Don't kill us with SIGPIPE. */
-	signal(SIGPIPE, SIG_IGN);
+	early_init(live_update, dofork, pidfile);
 
 	talloc_enable_null_tracking();
 
-#ifndef NO_SOCKETS
-	if (!live_update)
-		init_sockets();
-#endif
-
 	init_pipe(reopen_log_pipe);
 
 	/* Listen to hypervisor. */
diff --git a/tools/xenstored/core.h b/tools/xenstored/core.h
index 480b0f5f7b..72173f1684 100644
--- a/tools/xenstored/core.h
+++ b/tools/xenstored/core.h
@@ -384,12 +384,11 @@ static inline bool domain_is_unprivileged(const struct connection *conn)
 
 /* Return the event channel used by xenbus. */
 evtchn_port_t get_xenbus_evtchn(void);
+void early_init(bool live_update, bool dofork, const char *pidfile);
 
-/* Write out the pidfile */
-void write_pidfile(const char *pidfile);
+void init_sockets(void);
+extern int reopen_log_pipe[2];
 
-/* Fork but do not close terminal FDs */
-void daemonize(void);
 /* Close stdin/stdout/stderr to complete daemonize */
 void finish_daemonize(void);
 
diff --git a/tools/xenstored/minios.c b/tools/xenstored/minios.c
index 0779efbf91..4f48f63083 100644
--- a/tools/xenstored/minios.c
+++ b/tools/xenstored/minios.c
@@ -20,14 +20,6 @@
 #include "core.h"
 #include <xen/grant_table.h>
 
-void write_pidfile(const char *pidfile)
-{
-}
-
-void daemonize(void)
-{
-}
-
 void finish_daemonize(void)
 {
 }
@@ -54,3 +46,6 @@ void unmap_xenbus(void *interface)
 	xengnttab_unmap(*xgt_handle, interface, 1);
 }
 
+void early_init(bool live_update, bool dofork, const char *pidfile)
+{
+}
diff --git a/tools/xenstored/posix.c b/tools/xenstored/posix.c
index 7e03dd982d..9463ef5c8d 100644
--- a/tools/xenstored/posix.c
+++ b/tools/xenstored/posix.c
@@ -20,14 +20,17 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <stdlib.h>
+#include <syslog.h>
 #include <sys/mman.h>
+#include <xen-tools/xenstore-common.h>
 
 #include "utils.h"
 #include "core.h"
 #include "osdep.h"
 
-void write_pidfile(const char *pidfile)
+static void write_pidfile(const char *pidfile)
 {
 	char buf[100];
 	int len;
@@ -49,7 +52,7 @@ void write_pidfile(const char *pidfile)
 }
 
 /* Stevens. */
-void daemonize(void)
+static void daemonize(void)
 {
 	pid_t pid;
 
@@ -157,3 +160,27 @@ void *xenbus_map(void)
 
 	return addr;
 }
+
+void early_init(bool live_update, bool dofork, const char *pidfile)
+{
+	reopen_log();
+
+	/* Make sure xenstored directory exists. */
+	/* Errors ignored here, will be reported when we open files */
+	mkdir(xenstore_daemon_rundir(), 0755);
+
+	if (dofork) {
+		openlog("xenstored", 0, LOG_DAEMON);
+		if (!live_update)
+			daemonize();
+	}
+
+	if (pidfile)
+		write_pidfile(pidfile);
+
+	/* Don't kill us with SIGPIPE. */
+	signal(SIGPIPE, SIG_IGN);
+
+	if (!live_update)
+		init_sockets();
+}
-- 
2.35.3