Skip to contentSkip to conversation

udevd: don't use alarm() for timeouts

PATCH In review Alan Jenkins 1 participant 1 message May 26, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 udevd: don't use alarm() for timeouts

alarm() is per-process; we can't use it for timeouts in a multi-threaded udevd. We need to explicitly track the total time spent waiting for each event.

There is an issue here if the timeout expires in run_program(). If run_program() returns without calling wait(), the process it forked will become a zombie when it finally exits. Currently, udev-event is implemented as a process, so the zombie will be reparented to init and reaped once the event is finished. But that won't work for threads.

The solution is to fork twice:

udevd (event thread)
udevd child process
command

When the timeout expires, the event thread can return immediately. The child process will stay blocked in wait(), until the command finally finishes (or is killed). We'll get a nice process tree showing that the the hung process was started by udevd :-).

The child process passes the exit status of the command over a pipe. This also provides a solution to the lack of a timeout in any of the standard wait() system calls. We can set a timeout on select() when waiting on the pipe.

udev/test-udev.c +0 -10
index 35737c1..7e29b39 100644
@@ -69,12 +69,6 @@ int main(int argc, char *argv[])
 	sigaction(SIGINT, &act, NULL);
 	sigaction(SIGTERM, &act, NULL);
 
-	/* trigger timeout to prevent hanging processes */
-	if (udev_device_get_timeout(event->dev) > 0)
-		alarm(udev_device_get_timeout(event->dev));
-	else
-		alarm(UDEV_EVENT_TIMEOUT);
-
 	action = getenv("ACTION");
 	devpath = getenv("DEVPATH");
 	subsystem = getenv("SUBSYSTEM");
@@ -101,10 +95,6 @@ int main(int argc, char *argv[])
 	event = udev_event_new(dev);
 	err = udev_event_execute_rules(event, rules);
 
-	/* rules may change/disable the timeout */
-	if (udev_device_get_event_timeout(dev) >= 0)
-		alarm(udev_device_get_event_timeout(dev));
-
 	if (err = 0 && !event->ignore_device && udev_get_run(udev))
 		udev_event_execute_run(event);
 
udev/udev-event.c +324 -4

385 lines not on this page · raw message

udev/udev-rules.c +25 -21

119 lines not on this page · raw message

udev/udev-util.c +0 -211

214 lines not on this page · raw message

udev/udev.h +5 -2
index 8f2c1c6..00e6fa3 100644
@@ -28,6 +28,7 @@
 
 #define DEFAULT_FAKE_PARTITIONS_COUNT		15
 #define UDEV_EVENT_TIMEOUT			180
+#define USECS_PER_SECOND			(1000 * 1000)
 
 #define UDEV_CTRL_SOCK_PATH			"@" UDEV_PREFIX "/org/kernel/udev/udevd"
 
@@ -64,6 +65,7 @@ struct udev_event {
 	uid_t uid;
 	gid_t gid;
 	struct udev_list_node run_list;
+	int timeout_usecs;
 	pid_t pid;
 	int exitstatus;
 	time_t queue_time;
@@ -99,6 +101,9 @@ int udev_event_execute_run(struct udev_event *event);
 size_t udev_event_apply_format(struct udev_event *event, const char *src, char *dest, size_t size);
 int udev_event_apply_subsys_kernel(struct udev_event *event, const char *string,
 				   char *result, size_t maxsize, int read_value);
+int udev_event_run_program(struct udev_event *event, const char *command,
+			   char *result, size_t ressize, size_t *reslen);
+
 
 /* udev-watch.c */
 extern int inotify_fd;
@@ -120,8 +125,6 @@ int util_delete_path(struct udev *udev, const char *path);
 int util_unlink_secure(struct udev *udev, const char *filename);
 uid_t util_lookup_user(struct udev *udev, const char *user);
 gid_t util_lookup_group(struct udev *udev, const char *group);
-int util_run_program(struct udev *udev, const char *command, char **envp,
-		     char *result, size_t ressize, size_t *reslen);
 int util_resolve_subsys_kernel(struct udev *udev, const char *string,
 				      char *result, size_t maxsize, int read_value);
 
udev/udevd.c +0 -10
index 03ea26d..6e613d2 100644
@@ -155,19 +155,9 @@ static void event_fork(struct udev_event *event)
 		sigaction(SIGCHLD, &act, NULL);
 		sigaction(SIGHUP, &act, NULL);
 
-		/* set timeout to prevent hanging processes */
-		if (udev_device_get_timeout(event->dev) > 0)
-			alarm(udev_device_get_timeout(event->dev));
-		else
-			alarm(UDEV_EVENT_TIMEOUT);
-
 		/* apply rules, create node, symlinks */
 		err = udev_event_execute_rules(event, rules);
 
-		/* rules may change/disable the timeout */
-		if (udev_device_get_event_timeout(event->dev) >= 0)
-			alarm(udev_device_get_event_timeout(event->dev));
-
 		/* execute RUN= */
 		if (err = 0 && !event->ignore_device && udev_get_run(event->udev))
 			udev_event_execute_run(event);