[PATCH 3/8] mdev: move daemon setup to dedicated function

Jan Klötzke jan at kloetzke.net
Mon Dec 16 21:56:51 UTC 2019


Signed-off-by: Jan Klötzke <jan at kloetzke.net>
---
 util-linux/mdev.c | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index 4e3299d22..cfc1a38bc 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -1172,6 +1172,29 @@ static void initial_scan(char *temp)
 # define RCVBUF (128 * 1024 * 1024)
 # define MAX_ENV 32
 
+static int daemon_init(char *temp)
+{
+	int fd;
+
+	/* Subscribe for UEVENT kernel messages */
+	/* Without a sufficiently big RCVBUF, a ton of simultaneous events
+	 * can trigger ENOBUFS on read, which is unrecoverable.
+	 * Reproducer:
+	 *	mdev -d
+	 *	find /sys -name uevent -exec sh -c 'echo add >"{}"' ';'
+	 */
+	fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, 1 << 0, RCVBUF);
+
+	/*
+	 * Make inital scan after the uevent socket is alive and
+	 * _before_ we fork away. Already open mdev.log because we work
+	 * in daemon mode.
+	 */
+	initial_scan(temp);
+
+	return fd;
+}
+
 static void daemon_loop(char *temp, int fd)
 {
 	for (;;) {
@@ -1241,24 +1264,11 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
 #if ENABLE_FEATURE_MDEV_DAEMON
 	if (opt & MDEV_OPT_DAEMON) {
 		/*
-		 * Daemon mode listening on uevent netlink socket.
-		 */
-		int fd;
-
-		/* Subscribe for UEVENT kernel messages */
-		/* Without a sufficiently big RCVBUF, a ton of simultaneous events
-		 * can trigger ENOBUFS on read, which is unrecoverable.
-		 * Reproducer:
-		 *	mdev -d
-		 *	find /sys -name uevent -exec sh -c 'echo add >"{}"' ';'
-		 */
-		fd = create_and_bind_to_netlink(NETLINK_KOBJECT_UEVENT, 1 << 0, RCVBUF);
-
-		/*
-		 * Make inital scan after the uevent socket is alive and
-		 * _before_ we fork away.
+		 * Daemon mode listening on uevent netlink socket. Fork away
+		 * after initial scan so that caller can be sure everything
+		 * is up-to-date when mdev process returns.
 		 */
-		initial_scan(temp);
+		int fd = daemon_init(temp);
 
 		if (!(opt & MDEV_OPT_FOREGROUND))
 			bb_daemonize_or_rexec(0, argv);
-- 
2.20.1



More information about the busybox mailing list