linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kay Sievers <kay.sievers@vrfy.org>
To: linux-hotplug@vger.kernel.org
Subject: move udev-rules parsing into the daemon
Date: Sun, 31 Jul 2005 00:42:27 +0000	[thread overview]
Message-ID: <20050731004227.GA26803@vrfy.org> (raw)

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

Here we move the parsed rules into the udev daemon, which will no longer
execute the udev binary. This saves us the rule parsing, cause the
parsed rules will be inherited from the daemon process, which will
fork() a child which imediately becomes the event process without doing
an exec(). It's basically a retry of this old patch:
  http://marc.theaimsgroup.com/?l=linux-hotplug-devel&m=109814301613236&w=2

Changes to /etc/udev/rules.d/* will automatically recognized and will cause
a reload of the rules, also the usual -HUP should work.

Would be nice if someone brave enough can give it a try, we want to put
that upstream if it works as expected. The patch applies on top of v064.

Thanks,
Kay

[-- Attachment #2: udevd-noexec-12.patch --]
[-- Type: text/plain, Size: 5399 bytes --]

diff --git a/RELEASE-NOTES b/RELEASE-NOTES
--- a/RELEASE-NOTES
+++ b/RELEASE-NOTES
@@ -1,3 +1,21 @@
+udev 065
+========
+
+
+udev 064
+========
+Mostly bugfixes and see ChangeLog.
+
+The test for the existence of an environment value should be
+switched from:
+  ENV{KEY}=="*" to ENV{KEY}=="?*"
+cause "*" will not fail anymore, if the key does not exist or
+is empty.
+
+udev 063
+========
+Bugfixes and a few tweaks described in the ChangeLog.
+
 udev 062
 ========
 Mostly a Bugfix release.
diff --git a/udevcontrol.c b/udevcontrol.c
--- a/udevcontrol.c
+++ b/udevcontrol.c
@@ -97,7 +97,9 @@ int main(int argc, char *argv[], char *e
 		usend_msg.type = UDEVD_SET_MAX_CHILDS;
 		*intval = atoi(val);
 		info("send max_childs=%i", *intval);
-	} else {
+	} else if (!strcmp(argv[1], "reload_rules"))
+		usend_msg.type = UDEVD_RELOAD_RULES;
+	else {
 		err("error parsing command\n");
 		goto exit;
 	}
diff --git a/udevd.c b/udevd.c
--- a/udevd.c
+++ b/udevd.c
@@ -45,6 +45,7 @@
 #include "udev_libc_wrapper.h"
 #include "udev.h"
 #include "udev_version.h"
+#include "udev_rules.h"
 #include "udev_utils.h"
 #include "udevd.h"
 #include "logging.h"
@@ -54,6 +55,7 @@
 #endif
 
 /* global variables*/
+struct udev_rules rules;
 static int udevd_sock;
 static int uevent_netlink_sock;
 static pid_t sid;
@@ -65,6 +67,7 @@ static volatile int sig_flag;
 static int init_phase = 1;
 static int run_exec_q;
 static int stop_exec_q;
+static int reload_config;
 
 static LIST_HEAD(msg_list);
 static LIST_HEAD(exec_list);
@@ -167,10 +170,54 @@ static void msg_queue_insert(struct ueve
 	return;
 }
 
+static void asmlinkage udev_event_sig_handler(int signum)
+{
+	if (signum == SIGALRM)
+		exit(1);
+}
+
+static int udev_event_process(struct uevent_msg *msg)
+{
+	struct sigaction act;
+	struct udevice udev;
+	struct name_entry *name_loop;
+	int i;
+	int retval;
+
+	logging_init("udevd-event");
+
+	/* set signal handlers */
+	memset(&act, 0x00, sizeof(act));
+	act.sa_handler = (void (*)(int)) udev_event_sig_handler;
+	sigemptyset (&act.sa_mask);
+	act.sa_flags = 0;
+	sigaction(SIGALRM, &act, NULL);
+
+	/* trigger timeout to prevent hanging processes */
+	alarm(UDEV_ALARM_TIMEOUT);
+
+	/* reconstruct env */
+	for (i = 0; msg->envp[i]; i++)
+		putenv(msg->envp[i]);
+
+	udev_init_device(&udev, msg->devpath, msg->subsystem, msg->action);
+	retval = udev_process_event(&rules, &udev);
+
+	/* run programs collected by RUN-key*/
+	if (!retval) {
+		list_for_each_entry(name_loop, &udev.run_list, node)
+			execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
+	}
+
+	udev_cleanup_device(&udev);
+	logging_close();
+
+	return 0;
+}
+
 /* forks event and removes event from run queue when finished */
 static void udev_event_fork(struct uevent_msg *msg)
 {
-	char *const argv[] = { "udev", msg->subsystem, NULL };
 	pid_t pid;
 	struct sysinfo info;
 
@@ -183,9 +230,9 @@ static void udev_event_fork(struct ueven
 		close(udevd_sock);
 		logging_close();
 		setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
-		execve(udev_bin, argv, msg->envp);
-		err("exec of child failed");
-		_exit(1);
+
+		udev_event_process(msg);
+		exit(0);
 	case -1:
 		err("fork of child failed");
 		msg_queue_delete(msg);
@@ -580,6 +627,11 @@ static struct uevent_msg *get_udevd_msg(
 		info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
 		max_childs = *intval;
 		break;
+	case UDEVD_RELOAD_RULES:
+		info("udevd message (RELOAD_RULES) received");
+		udev_rules_close(&rules);
+		udev_rules_init(&rules, 1);
+		break;
 	default:
 		dbg("unknown message type");
 	}
@@ -650,16 +702,17 @@ static void asmlinkage sig_handler(int s
 		case SIGALRM:
 			/* set flag, then write to pipe if needed */
 			run_msg_q = 1;
-			goto do_write;
 			break;
 		case SIGCHLD:
 			/* set flag, then write to pipe if needed */
 			sigchilds_waiting = 1;
-			goto do_write;
+			break;
+		case SIGHUP:
+		case SIGIO:
+			reload_config = 1;
 			break;
 	}
 
-do_write:
 	/* if pipe is empty, write to pipe to force select to return
 	 * immediately when it gets called
 	 */
@@ -787,6 +840,24 @@ static int init_uevent_netlink_sock(void
 	return 0;
 }
 
+static int watch_directory(const char *dir)
+{
+	int fd;
+
+	fd = open(dir, O_RDONLY);
+	if (fd < 0) {
+		err("could not open '%s' to watch it", dir);
+		return -1;
+	}
+	if (fcntl(fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_DELETE | DN_MULTISHOT) == -1) {
+		err("could not set dnotify on '%s'", dir);
+		return -1;
+		close(fd);
+	}
+
+	return 0;
+}
+
 int main(int argc, char *argv[], char *envp[])
 {
 	int maxsockplus;
@@ -891,6 +962,11 @@ int main(int argc, char *argv[], char *e
 	sigaction(SIGTERM, &act, NULL);
 	sigaction(SIGALRM, &act, NULL);
 	sigaction(SIGCHLD, &act, NULL);
+	sigaction(SIGHUP, &act, NULL);
+	sigaction(SIGIO, &act, NULL);
+
+	udev_rules_init(&rules, 1);
+	watch_directory(udev_rules_filename);
 
 	if (init_uevent_netlink_sock() < 0) {
 		dbg("uevent socket not available");
@@ -1010,6 +1086,12 @@ int main(int argc, char *argv[], char *e
 			if (!stop_exec_q)
 				exec_queue_manager();
 		}
+
+		if (reload_config) {
+			reload_config = 0;
+			udev_rules_close(&rules);
+			udev_rules_init(&rules, 1);
+		}
 	}
 
 exit:
diff --git a/udevd.h b/udevd.h
--- a/udevd.h
+++ b/udevd.h
@@ -54,6 +54,7 @@ enum udevd_msg_type {
 	UDEVD_START_EXEC_QUEUE,
 	UDEVD_SET_LOG_LEVEL,
 	UDEVD_SET_MAX_CHILDS,
+	UDEVD_RELOAD_RULES,
 };
 
 

             reply	other threads:[~2005-07-31  0:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-31  0:42 Kay Sievers [this message]
2005-08-02  2:42 ` move udev-rules parsing into the daemon Kay Sievers
2005-08-06  0:39 ` Greg KH
2005-08-06  3:41 ` Kay Sievers
2005-08-09  2:30 ` Kay Sievers
2005-08-11  6:13 ` Greg KH
2005-08-11 23:06 ` Kay Sievers
2005-08-12  2:49 ` Kay Sievers
2005-08-12 18:04 ` Greg KH
2005-08-18 19:23 ` Kay Sievers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050731004227.GA26803@vrfy.org \
    --to=kay.sievers@vrfy.org \
    --cc=linux-hotplug@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).