linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] udev - next round of udev event order daemon
@ 2004-01-23 15:14 Kay Sievers
  2004-01-23 22:30 ` Greg KH
  2004-01-24  1:21 ` Kay Sievers
  0 siblings, 2 replies; 3+ messages in thread
From: Kay Sievers @ 2004-01-23 15:14 UTC (permalink / raw)
  To: linux-hotplug

[-- Attachment #1: Type: text/plain, Size: 3051 bytes --]

Here is the next round of udevd/udevsend:

udevsend - If the IPC message we send is not catched by a receiver we fork
           the udevd daemon to process this and the following events

udevd    - We reorder the events we receive and execute our current udev for
           every event. If one or more events are missing, we wait
           10 seconds and then go ahead in the queue.
           If the queue is empty and we don't receive any event for the next
           30 seconds, the daemon exits.
           The next incoming event will fork the daemon again.

config   - The path's to the executable are specified in udevd.h
           Now they are pointing to the current directory only.


I don't like daemons hiding secrets (and mem leaks :)) inside,
so I want to try this model. It should be enough logic to get all possible
hotplug events executed in the right order.

If no event, then no daemon! So everybody should be happy :)

What do you think? I hope you like the idea.

thanks,
Kay



Here we see:
  1. the daemon fork,
  2. the udev work,
  3. the 10 sec timeout and the skipped events,
  4. the udev work,
     ...,
  5. and the 30 sec timeout and exit.

EVENTS:
  pim:/home/kay/src/udev.kay# test/udevd_test.sh 
  pim:/home/kay/src/udev.kay# SEQNUM=15 ./udevsend block
  pim:/home/kay/src/udev.kay# SEQNUM=16 ./udevsend block
  pim:/home/kay/src/udev.kay# SEQNUM=17 ./udevsend block
  pim:/home/kay/src/udev.kay# SEQNUM=18 ./udevsend block
  pim:/home/kay/src/udev.kay# SEQNUM=20 ./udevsend block
  pim:/home/kay/src/udev.kay# SEQNUM=21 ./udevsend block


LOG:
  Jan 23 15:35:35 pim udev[11795]: message is still in the ipc queue, starting daemon...
  Jan 23 15:35:35 pim udev[11799]: configured rule in '/etc/udev/udev.rules' at line 19 applied, 'sda' becomes '%k-flash'
  Jan 23 15:35:35 pim udev[11799]: creating device node '/udev/sda-flash'
  Jan 23 15:35:35 pim udev[11800]: creating device node '/udev/sdb'
  Jan 23 15:35:35 pim udev[11804]: creating device node '/udev/sdc'
  Jan 23 15:35:35 pim udev[11805]: removing device node '/udev/sda-flash'
  Jan 23 15:35:35 pim udev[11808]: removing device node '/udev/sdb'
  Jan 23 15:35:35 pim udev[11809]: removing device node '/udev/sdc'
  Jan 23 15:35:45 pim udev[11797]: timeout reached, skip events 7 - 7
  Jan 23 15:35:45 pim udev[11811]: creating device node '/udev/sdb'
  Jan 23 15:35:45 pim udev[11812]: creating device node '/udev/sdc'
  Jan 23 15:36:01 pim udev[11797]: timeout reached, skip events 10 - 14
  Jan 23 15:36:01 pim udev[11814]: creating device node '/udev/sdc'
  Jan 23 15:36:04 pim udev[11816]: creating device node '/udev/sdc'
  Jan 23 15:36:12 pim udev[11818]: creating device node '/udev/sdc'
  Jan 23 15:36:16 pim udev[11820]: creating device node '/udev/sdc'
  Jan 23 15:36:38 pim udev[11797]: timeout reached, skip events 19 - 19
  Jan 23 15:36:38 pim udev[11823]: creating device node '/udev/sdc'
  Jan 23 15:36:38 pim udev[11824]: creating device node '/udev/sdc'
  Jan 23 15:37:08 pim udev[11797]: we have nothing to do, so daemon exits...


[-- Attachment #2: 01-udevd.patch --]
[-- Type: text/plain, Size: 9562 bytes --]

diff -Nru a/test/udevd_test.sh b/test/udevd_test.sh
--- a/test/udevd_test.sh	Fri Jan 23 15:40:09 2004
+++ b/test/udevd_test.sh	Fri Jan 23 15:40:09 2004
@@ -1,47 +1,52 @@
 #!/bin/bash
 
-# reset udevd, expected sequence number and empty queue
-killall -HUP udevd
+# kill daemon, first event will start it again
+killall udevd
 
-export ACTION=add
-export DEVPATH=/block/sda
+# connect(123) - disconnect(456) - connect(789) sequence of sda/sdb/sdc
 
 export SEQNUM=1
+export ACTION=add
+export DEVPATH=/block/sda
 ./udevsend block
 
 export SEQNUM=2
-./udevsend block
-
-export SEQNUM=3
-./udevsend block
-
-export SEQNUM=5
+export ACTION=add
+export DEVPATH=/block/sdb
 ./udevsend block
 
 export SEQNUM=4
+export ACTION=remove
+export DEVPATH=/block/sda
 ./udevsend block
 
-export SEQNUM=6
+export SEQNUM=3
+export ACTION=add
+export DEVPATH=/block/sdc
 ./udevsend block
 
-export SEQNUM=7
+export SEQNUM=6
+export ACTION=remove
+export DEVPATH=/block/sdc
 ./udevsend block
 
-export SEQNUM=10
+export SEQNUM=5
+export ACTION=remove
+export DEVPATH=/block/sdb
 ./udevsend block
 
-export SEQNUM=9
-#./udevsend block
-
-export SEQNUM=8
+export SEQNUM=7
+export ACTION=add
+export DEVPATH=/block/sda
 #./udevsend block
 
-export SEQNUM=13
-./udevsend block
-
-export SEQNUM=12
+export SEQNUM=9
+export ACTION=add
+export DEVPATH=/block/sdc
 ./udevsend block
 
-export SEQNUM=11
+export SEQNUM=8
+export ACTION=add
+export DEVPATH=/block/sdb
 ./udevsend block
 
diff -Nru a/udevd.c b/udevd.c
--- a/udevd.c	Fri Jan 23 15:40:09 2004
+++ b/udevd.c	Fri Jan 23 15:40:09 2004
@@ -37,49 +37,20 @@
 #include "udevd.h"
 #include "logging.h"
 
-#define BUFFER_SIZE		1024
-#define TIMEOUT_SECONDS		10
-
-static void reset_timer(void);
-static void reset_queue(void);
+#define BUFFER_SIZE			1024
+#define EVENT_TIMEOUT_SECONDS		10
+#define DAEMON_TIMEOUT_SECONDS		30
 
 
 static int expect_seqnum = 0;
-static int timeout_value = TIMEOUT_SECONDS;
-static int timeout = 0;
 static struct hotplug_msg *head = NULL;
-static char exec_program[100];
-
-
-static void sig_handler(int signum)
-{
-	dbg("caught signal %d", signum);
-	switch (signum) {
-		case SIGHUP:
-			dbg("reset requested, all waiting events killed");
-			reset_timer();
-			reset_queue();
-			timeout = 0;
-			expect_seqnum = 0;
-			break;
-
-		case SIGINT:
-		case SIGTERM:
-		case SIGKILL:
-			exit(20 + signum);
-			break;
 
-		default:
-			dbg("unhandled signal");
-	}
-}
 
 static void sig_alarmhandler(int signum)
 {
 	dbg("caught signal %d", signum);
 	switch (signum) {
 	case SIGALRM:
-		timeout = 1;
 		dbg("event timeout reached");
 		break;
 
@@ -94,9 +65,9 @@
 	p = head;
 
 	dbg("next expected sequence is %d", expect_seqnum);
-	while(p) {
+	while(p != NULL) {
 		dbg("sequence %d in queue", p->seqnum);
-		p=p->next;
+		p = p->next;
 	}
 }
 
@@ -110,59 +81,37 @@
 {
 	pid_t pid;
 	char *argv[3];
-	int retval;
 	extern char **environ;
 
 	dump_msg(pmsg);
-	dbg("exec '%s'", exec_program);
 
 	setenv("ACTION", pmsg->action, 1);
 	setenv("DEVPATH", pmsg->devpath, 1);
-
-	argv[0] = exec_program;
+	argv[0] = DEFAULT_UDEV_EXEC;
 	argv[1] = pmsg->subsystem;
 	argv[2] = NULL;
 
 	pid = fork();
 	switch (pid) {
 	case 0:
-		retval = execve(argv[0], argv, environ);
-		if (retval != 0) {
-			dbg("child execve failed");
-			exit(1);
-		}
+		/* child */
+		execve(argv[0], argv, environ);
+		dbg("exec of child failed");
+		exit(1);
 		break;
 	case -1:
-		dbg("fork failed");
+		dbg("fork of child failed");
 		return -1;
 	default:
 		wait(0);
-		break;
 	}
 	return 0;
 }
 
-static void reset_timer(void)
-{
-	alarm(0);
-}
-
-static void set_timer(void)
+static void set_timer(int seconds)
 {
 	signal(SIGALRM, sig_alarmhandler);
-	alarm(timeout_value);
-}
-
-static void reset_queue(void)
-{
-	struct hotplug_msg *p;
-	p = head;
-
-	while(head) {
-		p = head;
-		head = head->next;
-		free(p);
-	}
+	alarm(seconds);
 }
 
 static void check_queue(void)
@@ -171,7 +120,7 @@
 	p = head;
 
 	dump_queue();
-	while(head && head->seqnum == expect_seqnum) {
+	while(head != NULL && head->seqnum == expect_seqnum) {
 		dispatch_msg(head);
 		expect_seqnum++;
 		p = head;
@@ -179,9 +128,9 @@
 		free(p);
 	}
 	if (head != NULL)
-		set_timer();
+		set_timer(EVENT_TIMEOUT_SECONDS);
 	else
-		reset_timer();
+		set_timer(DAEMON_TIMEOUT_SECONDS);
 }
 
 static void add_queue(struct hotplug_msg *pmsg)
@@ -195,7 +144,7 @@
 	pnewmsg = malloc(sizeof(struct hotplug_msg));
 	*pnewmsg = *pmsg;
 	pnewmsg->next = NULL;
-	while(p && pmsg->seqnum > p->seqnum) {
+	while(p != NULL && pmsg->seqnum > p->seqnum) {
 		p1 = p;
 		p = p->next;
 	}
@@ -216,12 +165,12 @@
 	char buf[BUFFER_SIZE];
 	int ret;
 
-	key = ftok(DEFAULT_EXEC_PROGRAM, IPC_KEY_ID);
+	key = ftok(DEFAULT_UDEVD_EXEC, IPC_KEY_ID);
 	pmsg = (struct hotplug_msg *) buf;
 	msgid = msgget(key, IPC_CREAT);
 	if (msgid == -1) {
 		dbg("open message queue error");
-		goto exit;
+		return -1;
 	}
 	while (1) {
 		ret = msgrcv(msgid, (struct msgbuf *) buf, BUFFER_SIZE-4, HOTPLUGMSGTYPE, 0);
@@ -236,7 +185,7 @@
 
 			if (pmsg->seqnum > expect_seqnum) {
 				add_queue(pmsg);
-				set_timer();
+				set_timer(EVENT_TIMEOUT_SECONDS);
 			} else {
 				if (pmsg->seqnum == expect_seqnum) {
 					dispatch_msg(pmsg);
@@ -246,37 +195,49 @@
 					dbg("timeout event for unexpected sequence number %d", pmsg->seqnum);
 				}
 			}
-		} else
+		} else {
 			if (errno == EINTR) {
 				if (head != NULL) {
-					/* timeout, skip all missing, proceed with next queued event */
-					dbg("timeout reached, skip events %d - %d", expect_seqnum, head->seqnum-1);
+					/* event timeout, skip all missing, proceed with next queued event */
+					info("timeout reached, skip events %d - %d", expect_seqnum, head->seqnum-1);
 					expect_seqnum = head->seqnum;
+				} else {
+					info("we have nothing to do, so daemon exits...");
+					exit(0);
 				}
 				check_queue();
-				timeout = 0;
 			} else {
 				dbg("ipc message receive error '%s'", strerror(errno));
 			}
+		}
 	}
 	return 0;
-exit:
-	return -1;
 }
 
-int main(int argc, char *argv[])
+static void sig_handler(int signum)
 {
-	/* get program to exec on events */
-	if (argc == 2)
-		strncpy(exec_program, argv[1], sizeof(exec_program));
-	else
-		strcpy(exec_program, DEFAULT_EXEC_PROGRAM);
+	dbg("caught signal %d", signum);
+	switch (signum) {
+		case SIGINT:
+		case SIGTERM:
+		case SIGKILL:
+			exit(20 + signum);
+			break;
+
+		default:
+			dbg("unhandled signal");
+	}
+}
 
+int main(int argc, char *argv[])
+{
 	/* set up signal handler */
 	signal(SIGINT, sig_handler);
 	signal(SIGTERM, sig_handler);
 	signal(SIGKILL, sig_handler);
-	signal(SIGHUP, sig_handler);
+
+	/* we exit if we have nothing to do, next event will start us again */
+	set_timer(DAEMON_TIMEOUT_SECONDS);
 
 	/* main loop */
 	process_queue();
diff -Nru a/udevd.h b/udevd.h
--- a/udevd.h	Fri Jan 23 15:40:09 2004
+++ b/udevd.h	Fri Jan 23 15:40:09 2004
@@ -21,9 +21,11 @@
  *
  */
 
-#define HOTPLUGMSGTYPE		44
-#define DEFAULT_EXEC_PROGRAM	"/sbin/udev"
+#define DEFAULT_UDEV_EXEC	"./udev"
+#define DEFAULT_UDEVD_EXEC	"./udevd"
+
 #define IPC_KEY_ID		0
+#define HOTPLUGMSGTYPE		44
 
 
 struct hotplug_msg {
diff -Nru a/udevsend.c b/udevsend.c
--- a/udevsend.c	Fri Jan 23 15:40:09 2004
+++ b/udevsend.c	Fri Jan 23 15:40:09 2004
@@ -29,6 +29,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include <wait.h>
 
 #include "udev.h"
 #include "udevd.h"
@@ -78,6 +81,39 @@
 	free(pmsg);
 }
 
+static int start_daemon(void)
+{
+	pid_t pid;
+	pid_t child_pid;
+
+	pid = fork();
+	switch (pid) {
+	case 0:
+		/* helper child */
+		child_pid = fork();
+		switch (child_pid) {
+		case 0:
+			/* daemon */
+			execl(DEFAULT_UDEVD_EXEC, NULL);
+			dbg("exec of daemon failed");
+			exit(1);
+		case -1:
+			dbg("fork of daemon failed");
+			return -1;
+		default:
+			exit(0);
+		}
+		break;
+	case -1:
+		dbg("fork of helper failed");
+		return -1;
+	default:
+		wait(0);
+	}
+	return 0;
+}
+
+
 int main(int argc, char* argv[])
 {
 	int msgid;
@@ -91,6 +127,8 @@
 	int seq;
 	int retval = -EINVAL;
 	int size;
+	int loop;
+	struct timespec tspec;
 
 	subsystem = argv[1];
 	if (subsystem == NULL) {
@@ -118,7 +156,7 @@
 	seq = atoi(seqnum);
 
 	/* create ipc message queue or get id of our existing one */
-	key = ftok(DEFAULT_EXEC_PROGRAM, IPC_KEY_ID);
+	key = ftok(DEFAULT_UDEVD_EXEC, IPC_KEY_ID);
 	size =  build_hotplugmsg( (struct hotplug_msg**) &pmsg, action, devpath, subsystem, seq);
 	msgid = msgget(key, IPC_CREAT);
 	if (msgid == -1) {
@@ -126,15 +164,6 @@
 		goto exit;
 	}
 
-	/* get state of ipc queue */
-	retval = msgctl(msgid, IPC_STAT, &msg_queue);
-	if (retval == -1) {
-		dbg("error getting info on ipc queue");
-		goto exit;
-	}
-	if (msg_queue.msg_qnum > 0)
-		dbg("%li messages already in the ipc queue", msg_queue.msg_qnum);
-
 	/* send ipc message to the daemon */
 	retval = msgsnd(msgid, pmsg, size, 0);
 	free_hotplugmsg( (struct hotplug_msg*) pmsg);
@@ -142,11 +171,25 @@
 		dbg("error sending ipc message");
 		goto exit;
 	}
-	return 0;
 
-exit:
-	if (retval > 0)
-		retval = 0;
+	/* get state of ipc queue */
+	tspec.tv_sec = 0;
+	tspec.tv_nsec = 10000000;  /* 10 millisec */
+	loop = 20;
+	while (loop--) {
+		retval = msgctl(msgid, IPC_STAT, &msg_queue);
+		if (retval == -1) {
+			dbg("error getting info on ipc queue");
+			goto exit;
+		}
+		if (msg_queue.msg_qnum == 0)
+			goto exit;
+		nanosleep(&tspec, NULL);
+	}
 
+	info("message is still in the ipc queue, starting daemon...");
+	retval = start_daemon();
+
+exit:
 	return retval;
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] udev - next round of udev event order daemon
  2004-01-23 15:14 [PATCH] udev - next round of udev event order daemon Kay Sievers
@ 2004-01-23 22:30 ` Greg KH
  2004-01-24  1:21 ` Kay Sievers
  1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2004-01-23 22:30 UTC (permalink / raw)
  To: linux-hotplug

On Fri, Jan 23, 2004 at 04:14:25PM +0100, Kay Sievers wrote:
> Here is the next round of udevd/udevsend:
> 
> udevsend - If the IPC message we send is not catched by a receiver we fork
>            the udevd daemon to process this and the following events

Problem is we end up forking a lot of udevd instances if the system is
under load.  That's not good :(

I applied this patch, and then added some logic to udevd to only let one
instance of it run at a time.  It isn't the nicest, but seems to work
for now...

Hm, sometimes I still loose an event, I think it's back to the udevsend
creating too many udevd instances.  Care to look at this?

Is using ipc calls too messy for this?  Others have proposed sockets.
Would that make the startup logic simpler?

thanks,

greg k-h


-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] udev - next round of udev event order daemon
  2004-01-23 15:14 [PATCH] udev - next round of udev event order daemon Kay Sievers
  2004-01-23 22:30 ` Greg KH
@ 2004-01-24  1:21 ` Kay Sievers
  1 sibling, 0 replies; 3+ messages in thread
From: Kay Sievers @ 2004-01-24  1:21 UTC (permalink / raw)
  To: linux-hotplug

On Fri, Jan 23, 2004 at 02:30:48PM -0800, Greg KH wrote:
> On Fri, Jan 23, 2004 at 04:14:25PM +0100, Kay Sievers wrote:
> > Here is the next round of udevd/udevsend:
> > 
> > udevsend - If the IPC message we send is not catched by a receiver we fork
> >            the udevd daemon to process this and the following events
> 
> Problem is we end up forking a lot of udevd instances if the system is
> under load.  That's not good :(
>
> I applied this patch, and then added some logic to udevd to only let one
> instance of it run at a time.  It isn't the nicest, but seems to work
> for now...

Maybe 190 millisec's are too short?
Hmm, we may also look what pid received the last event and check if this pid
is still running. So we prevent forking a new daemon just because of the
timeout.


> Hm, sometimes I still loose an event, I think it's back to the udevsend
> creating too many udevd instances.  Care to look at this?

While we wait for the return of the udev exec, we don't get the messages
out of the queue. That may cause the problem with the timeout.
Do we need a receiver thread? Or fork udev in the background, but then
a "remove" may be faster than a "add"?


> Is using ipc calls too messy for this?  Others have proposed sockets.
> Would that make the startup logic simpler?

Yes and no. We notice that we can't connect so there is a better
indicator, that there is no daemon. But I think the main problem remains the
same. I have less experience with this time critical stuff, so we may try
it out, if nobody comes with a better idea?

thanks,
Kay


-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-01-24  1:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-23 15:14 [PATCH] udev - next round of udev event order daemon Kay Sievers
2004-01-23 22:30 ` Greg KH
2004-01-24  1:21 ` Kay Sievers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).