On Sun, 15 Mar 2015 21:46:42 +0100
Harald Becker <ra...@gmx.de> wrote:

> On 15.03.2015 14:29, Natanael Copa wrote:
> >> You hacked a solution, for the mechanism of your preference, throwing
> >> out those who want to use one of the other mechanisms ...
> >> this is forcing others to do it your way?
> >
> > The RFC in subject means "request for comments", not "I force you do it
> > my way".
> 
> I apologize ... please change my words to (which was there intention):
> 
> You hacked a solution, for the mechanism of your preference, throwing 
> out those who want to use one of the other mechanisms ... which would be 
> bad, when you try to force others to do it your way?
> 
> ... sorry for my poor English.

Ininitial mdev -i patch for busybox which reads messages from stream
(so it should work with your fif manager):

http://sprunge.us/ZjLK
(also attached)

A few notes:
- Its completely untested but it compiles
- it needs the previously mentioned micro protocol ('\0' as message
  separator)
- my current nldev hack implementation (netlink socket activator) needs
  the helper application to read the messages from netlink socket and
  forward to mdev -i as the previous implementation did.
- needs be busyboxified.

Would be nice if someone with more busybox experience than me could
help clean it up.

Thanks!

-nc
>From c3c55b21ddfe4a7bb2c2492623c3b3319bf2038c Mon Sep 17 00:00:00 2001
From: Natanael Copa <nc...@alpinelinux.org>
Date: Mon, 16 Mar 2015 14:04:34 +0100
Subject: [PATCH 1/2] mdev: refactor so event handling happens in a function

This is to prepare for reading events from a stream. No changes in
logic.

Signed-off-by: Natanael Copa <nc...@alpinelinux.org>
---
 util-linux/mdev.c | 121 ++++++++++++++++++++++++++++--------------------------
 1 file changed, 63 insertions(+), 58 deletions(-)

diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index ccc00d3..34d32e5 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -1013,6 +1013,68 @@ static void signal_mdevs(unsigned my_pid)
 	}
 }
 
+static void handle_event(char *temp)
+{
+	char *fw;
+	char *seq;
+	char *action;
+	char *env_devname;
+	char *env_devpath;
+	unsigned my_pid;
+	int seq_fd;
+	smalluint op;
+
+	/* Hotplug:
+	 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
+	 * ACTION can be "add", "remove", "change"
+	 * DEVPATH is like "/block/sda" or "/class/input/mice"
+	 */
+	env_devname = getenv("DEVNAME"); /* can be NULL */
+	G.subsystem = getenv("SUBSYSTEM");
+	action = getenv("ACTION");
+	env_devpath = getenv("DEVPATH");
+	if (!action || !env_devpath /*|| !G.subsystem*/)
+		bb_show_usage();
+	fw = getenv("FIRMWARE");
+	seq = getenv("SEQNUM");
+	op = index_in_strings(keywords, action);
+
+	my_pid = getpid();
+	open_mdev_log(seq, my_pid);
+
+	seq_fd = seq ? wait_for_seqfile(seq) : -1;
+
+	dbg1("%s "
+		"ACTION:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s"
+		"%s%s",
+		curtime(),
+		action, G.subsystem, env_devname, env_devpath,
+		fw ? " FW:" : "", fw ? fw : ""
+	);
+
+	snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
+	if (op == OP_remove) {
+		/* Ignoring "remove firmware". It was reported
+		 * to happen and to cause erroneous deletion
+		 * of device nodes. */
+		if (!fw)
+			make_device(env_devname, temp, op);
+	}
+	else {
+		make_device(env_devname, temp, op);
+		if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
+			if (op == OP_add && fw)
+				load_firmware(fw, temp);
+		}
+	}
+
+	dbg1("%s exiting", curtime());
+	if (seq_fd >= 0) {
+		xwrite_str(seq_fd, utoa(xatou(seq) + 1));
+		signal_mdevs(my_pid);
+	}
+}
+
 int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int mdev_main(int argc UNUSED_PARAM, char **argv)
 {
@@ -1069,64 +1131,7 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
 			ACTION_RECURSE | ACTION_FOLLOWLINKS,
 			fileAction, dirAction, temp, 0);
 	} else {
-		char *fw;
-		char *seq;
-		char *action;
-		char *env_devname;
-		char *env_devpath;
-		unsigned my_pid;
-		int seq_fd;
-		smalluint op;
-
-		/* Hotplug:
-		 * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
-		 * ACTION can be "add", "remove", "change"
-		 * DEVPATH is like "/block/sda" or "/class/input/mice"
-		 */
-		env_devname = getenv("DEVNAME"); /* can be NULL */
-		G.subsystem = getenv("SUBSYSTEM");
-		action = getenv("ACTION");
-		env_devpath = getenv("DEVPATH");
-		if (!action || !env_devpath /*|| !G.subsystem*/)
-			bb_show_usage();
-		fw = getenv("FIRMWARE");
-		seq = getenv("SEQNUM");
-		op = index_in_strings(keywords, action);
-
-		my_pid = getpid();
-		open_mdev_log(seq, my_pid);
-
-		seq_fd = seq ? wait_for_seqfile(seq) : -1;
-
-		dbg1("%s "
-			"ACTION:%s SUBSYSTEM:%s DEVNAME:%s DEVPATH:%s"
-			"%s%s",
-			curtime(),
-			action, G.subsystem, env_devname, env_devpath,
-			fw ? " FW:" : "", fw ? fw : ""
-		);
-
-		snprintf(temp, PATH_MAX, "/sys%s", env_devpath);
-		if (op == OP_remove) {
-			/* Ignoring "remove firmware". It was reported
-			 * to happen and to cause erroneous deletion
-			 * of device nodes. */
-			if (!fw)
-				make_device(env_devname, temp, op);
-		}
-		else {
-			make_device(env_devname, temp, op);
-			if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
-				if (op == OP_add && fw)
-					load_firmware(fw, temp);
-			}
-		}
-
-		dbg1("%s exiting", curtime());
-		if (seq_fd >= 0) {
-			xwrite_str(seq_fd, utoa(xatou(seq) + 1));
-			signal_mdevs(my_pid);
-		}
+		handle_event(temp);
 	}
 
 	if (ENABLE_FEATURE_CLEAN_UP)
-- 
2.3.2

>From 3ce1d7f82d39e0aaf4ea53599c6ae2345f157927 Mon Sep 17 00:00:00 2001
From: Natanael Copa <nc...@alpinelinux.org>
Date: Mon, 16 Mar 2015 17:08:06 +0100
Subject: [PATCH 2/2] mdev: inital support for -i

Signed-off-by: Natanael Copa <nc...@alpinelinux.org>
---
 util-linux/mdev.c | 51 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 6 deletions(-)

diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index 34d32e5..689fe3a 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -71,10 +71,12 @@
 //kbuild:lib-$(CONFIG_MDEV) += mdev.o
 
 //usage:#define mdev_trivial_usage
-//usage:       "[-s]"
+//usage:       "[-s|-i]"
 //usage:#define mdev_full_usage "\n\n"
 //usage:       "mdev -s is to be run during boot to scan /sys and populate /dev.\n"
 //usage:       "\n"
+//usage:       "mdev -i will read events from stdin\n"
+//usage:       "\n"
 //usage:       "Bare mdev is a kernel hotplug helper. To activate it:\n"
 //usage:       "	echo /sbin/mdev >/proc/sys/kernel/hotplug\n"
 //usage:	IF_FEATURE_MDEV_CONF(
@@ -1013,7 +1015,7 @@ static void signal_mdevs(unsigned my_pid)
 	}
 }
 
-static void handle_event(char *temp)
+static void handle_event(char *temp, int usage_on_error)
 {
 	char *fw;
 	char *seq;
@@ -1033,8 +1035,11 @@ static void handle_event(char *temp)
 	G.subsystem = getenv("SUBSYSTEM");
 	action = getenv("ACTION");
 	env_devpath = getenv("DEVPATH");
-	if (!action || !env_devpath /*|| !G.subsystem*/)
-		bb_show_usage();
+	if (!action || !env_devpath /*|| !G.subsystem*/) {
+		if (usage_on_error)
+			bb_show_usage();
+		return;
+	}
 	fw = getenv("FIRMWARE");
 	seq = getenv("SEQNUM");
 	op = index_in_strings(keywords, action);
@@ -1088,7 +1093,8 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
 
 	/* We can be called as hotplug helper */
 	/* Kernel cannot provide suitable stdio fds for us, do it ourself */
-	bb_sanitize_stdio();
+	if (argv[1] && strcmp(argv[1], "-i") != 0)
+		bb_sanitize_stdio();
 
 	/* Force the configuration file settings exactly */
 	umask(0);
@@ -1130,8 +1136,41 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
 		recursive_action("/sys/class",
 			ACTION_RECURSE | ACTION_FOLLOWLINKS,
 			fileAction, dirAction, temp, 0);
+	} else if (argv[1] && strcmp(argv[1], "-i") != 0) {
+		RESERVE_CONFIG_BUFFER(msgbuf, 16*1024);
+		struct pollfd fds;
+		int r;
+		fds.fd = 0;
+		fds.events = POLLIN;
+		while ((r = poll(&fds, 1, 2000)) > 0) {
+			int i, len, slen;
+			clearenv();
+
+			if (!(fds.revents & POLLIN))
+				continue;
+			len = read(fds.fd, msgbuf, sizeof(msgbuf));
+			for (i = 0; i < len; i += slen + 1) {
+				char *key, *value;
+
+				key = msgbuf + i;
+				value = strchr(key, '=');
+				slen = strlen(msgbuf+i);
+				if (!slen || value == NULL)
+					continue;
+
+				value[0] = '\0';
+				value++;
+
+				setenv(key, value, 1);
+			}
+			handle_event(temp, 0);
+			if (fds.revents & POLLHUP)
+				break;
+		}
+		if (r == -1)
+			return EXIT_FAILURE;
 	} else {
-		handle_event(temp);
+		handle_event(temp, 1);
 	}
 
 	if (ENABLE_FEATURE_CLEAN_UP)
-- 
2.3.2

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to