Skip to contentSkip to conversation

dev_d.c file sorting and cleanup

PATCH Kay Sievers 2 participants 4 messages Mar 25, 2004 – Mar 27, 2004

lore
Share & tools
lore
#0 lore raw
Quoted 2 lines · Kay Sievers

Please have look if it still works for you, I only did a very quick test.

Here is a unified version, with all the functions moved to udev_lib.c. We have a generic function now, to call a given fnct(char *) for every file ending with a specific suffix, sorted in lexical order. We use it to execute the dev.d/ files and read our rules.d/ files. The binary should be a bit smaller now.

I've also changed it, to not do the dev.d/ exec for net devices.

thanks,
Kay

===== dev_d.c 1.2 vs edited =====

dev_d.c +37 -79
@@ -1,30 +1,23 @@
 /*
- * dev.d multipleer
+ * dev_d.c - dev.d/ multiplexer
  * 
  * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
  *
  *	This program is free software; you can redistribute it and/or modify it
  *	under the terms of the GNU General Public License as published by the
  *	Free Software Foundation version 2 of the License.
- * 
- * Based on the klibc version of hotplug written by:
- *	Author(s) Christian Borntraeger <[email protected]>
- * which was based on the shell script written by:
- *	Greg Kroah-Hartman <[email protected]>
- *
  */
 
-/* 
+/*
  * This essentially emulates the following shell script logic in C:
-	DIR="/etc/dev.d"
-	export DEVNODE="whatever_dev_name_udev_just_gave"
-	for I in "${DIR}/$DEVNODE/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do
-		if [ -f $I ]; then $I $1 ; fi
-	done
-	exit 1;
+ *	DIR="/etc/dev.d"
+ *	export DEVNODE="whatever_dev_name_udev_just_gave"
+ *	for I in "${DIR}/$DEVNODE/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do
+ *		if [ -f $I ]; then $I $1 ; fi
+ *	done
+ *	exit 1;
  */
 
-#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -35,73 +28,38 @@
 #include "udev_lib.h"
 #include "logging.h"
 
-#define HOTPLUGDIR	"/etc/dev.d"
-#define SUFFIX		".dev" 
-#define COMMENT_PREFIX	'#'
+#define DEVD_DIR			"/etc/dev.d/"
+#define DEVD_SUFFIX			".dev"
 
-static void run_program(char *name)
+static int run_program(char *name)
 {
 	pid_t pid;
 
 	dbg("running %s", name);
 
 	pid = fork();
-
-	if (pid < 0) {
-		perror("fork");
-		return;
-	} 
-	
-	if (pid > 0) {
+	switch (pid) {
+	case 0:
+		/* child */
+		execv(name, main_argv);
+		dbg("exec of child failed");
+		exit(1);
+	case -1:
+		dbg("fork of child failed");
+		break;
+		return -1;
+	default:
 		wait(NULL);
-		return;
-	}
-
-	execv(name, main_argv);
-	exit(1);
-}
-
-static void execute_dir (char *dirname)
-{
-	DIR *directory;
-	struct dirent *entry;
-	char filename[NAME_SIZE];
-	int name_len;
-
-	dbg("opening %s", dirname);
-	directory = opendir(dirname);
-	if (!directory)
-		return;
-
-	while ((entry = readdir(directory))) {
-		if (entry->d_name[0] == '\0')
-			break;
-		/* Don't run the files '.', '..', or hidden files, 
-		 * or files that start with a '#' */
-		if ((entry->d_name[0] == '.') ||
-		    (entry->d_name[0] == COMMENT_PREFIX))
-			continue;
-
-		/* Nor do we run files that do not end in ".dev" */
-		name_len = strlen(entry->d_name);
-		if (name_len < strlen(SUFFIX))
-			continue;
-		if (strcmp(&entry->d_name[name_len - sizeof (SUFFIX) + 1], SUFFIX) != 0)
-			continue;
-
-		/* FIXME - need to use file_list_insert() here to run these in sorted order... */
-		snprintf(filename, sizeof(filename), "%s%s", dirname, entry->d_name);
-		filename[sizeof(filename)-1] = '\0';
-		run_program(filename);
 	}
 
-	closedir(directory);
+	return 0;
 }
 
-/* runs files in these directories in order:
- * 	name given by udev
- * 	subsystem
- * 	default
+/* 
+ * runs files in these directories in order:
+ * 	<node name given by udev>/
+ * 	subsystem/
+ * 	default/
  */
 void dev_d_send(struct udevice *dev, char *subsystem)
 {
@@ -112,15 +70,15 @@
 	strfieldcat(devnode, dev->name);
 	setenv("DEVNODE", devnode, 1);
 
-	snprintf(dirname, sizeof(dirname), HOTPLUGDIR "/%s/", dev->name);
-	dirname[sizeof(dirname)-1] = '\0';
-	execute_dir(dirname);
-
-	snprintf(dirname, sizeof(dirname), HOTPLUGDIR "/%s/", subsystem);
-	dirname[sizeof(dirname)-1] = '\0';
-	execute_dir(dirname);
+	strcpy(dirname, DEVD_DIR);
+	strfieldcat(dirname, dev->name);
+	call_foreach_file(run_program, dirname, DEVD_SUFFIX);
+
+	strcpy(dirname, DEVD_DIR);
+	strfieldcat(dirname, subsystem);
+	call_foreach_file(run_program, dirname, DEVD_SUFFIX);
 
-	strcpy(dirname, HOTPLUGDIR "/default/");
-	execute_dir(dirname);
+	strcpy(dirname, DEVD_DIR "default");
+	call_foreach_file(run_program, dirname, DEVD_SUFFIX);
 }
 

===== namedev.h 1.29 vs edited =====

namedev.h +2 -2
@@ -53,8 +53,8 @@
 
 #define MAX_SYSFS_PAIRS		5
 
-#define RULEFILE_EXT		".rules"
-#define PERMFILE_EXT		".permissions"
+#define RULEFILE_SUFFIX		".rules"
+#define PERMFILE_SUFFIX		".permissions"
 
 #define set_empty_perms(dev, m, o, g)		\
 	if (dev->mode == 0)			\

===== namedev_parse.c 1.33 vs edited =====

namedev_parse.c +14 -79

117 lines not on this page · raw message

===== udev-add.c 1.66 vs edited =====

udev-add.c +3 -3
@@ -393,6 +393,7 @@
 	retval = ioctl(sk, SIOCSIFNAME, &ifr);
 	if (retval != 0)
 		dbg("error changing net interface name");
+	close(sk);
 
 	return retval;
 }
@@ -453,6 +454,8 @@
 	case 'b':
 	case 'c':
 		retval = create_node(&dev, fake);
+		if ((retval == 0) && (!fake))
+			dev_d_send(&dev, subsystem);
 		break;
 
 	case 'n':
@@ -461,9 +464,6 @@
 			dbg("net device naming failed");
 		break;
 	}
-
-	if ((retval == 0) && (!fake))
-		dev_d_send(&dev, subsystem);
 
 exit:
 	if (class_dev)

===== udev_lib.c 1.3 vs edited =====

udev_lib.c +86 -0

103 lines not on this page · raw message

===== udev_lib.h 1.3 vs edited =====

udev_lib.h +1 -0
@@ -75,6 +75,7 @@
 extern int file_map(const char *filename, char **buf, size_t *bufsize);
 extern void file_unmap(char *buf, size_t bufsize);
 extern size_t buf_get_line(char *buf, size_t buflen, size_t cur);
+extern int  call_foreach_file(int fnct(char *f) , char *filename, char *extension);
 
 
 #endif

===== udevd.c 1.24 vs edited =====

udevd.c +3 -3
@@ -45,9 +45,9 @@
 volatile static int children_waiting;
 volatile static int msg_q_timeout;
 
-LIST_HEAD(msg_list);
-LIST_HEAD(exec_list);
-LIST_HEAD(running_list);
+static LIST_HEAD(msg_list);
+static LIST_HEAD(exec_list);
+static LIST_HEAD(running_list);
 
 static void exec_queue_manager(void);
 static void msg_queue_manager(void);

===== udevruler.c 1.1 vs edited =====

udevruler.c +4 -4
@@ -81,8 +81,8 @@
 	int added;
 };
 
-LIST_HEAD(device_list);
-int device_count;
+static LIST_HEAD(device_list);
+static int device_count;
 
 /* callback for database dump */
 static int add_record(char *path, struct udevice *udev)
@@ -149,8 +149,8 @@
 	char key[NAME_SIZE];
 };
 
-LIST_HEAD(attribute_list);
-int attribute_count;
+static LIST_HEAD(attribute_list);
+static int attribute_count;
 
 static int add_attribute(const char *key, int level)
 {
#1 lore raw

Looks good except for:

udev_lib.c line 204

Quoted 7 lines · Kay Sievers
udev_lib.c +7 -0
+		/* look for file with specified suffix */
+		ext = strrchr(ent->d_name, '.');
+		if (ext = NULL)
+			continue;
+
+		if (strcmp(ext, suffix) != 0)
+			continue;

That would still execute files named "foo.dev.rpmsave" right? That's what we want to avoid happening. We need to make sure we match "*.dev$" only.

So I'll hold off applying this for now.

thanks,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Kay Sievers
Quoted 15 lines · Greg KH

Looks good except for:

udev_lib.c line 204

Quoted 7 lines · Kay Sievers
udev_lib.c +7 -0
+		/* look for file with specified suffix */
+		ext = strrchr(ent->d_name, '.');
+		if (ext = NULL)
+			continue;
+
+		if (strcmp(ext, suffix) != 0)
+			continue;

That would still execute files named "foo.dev.rpmsave" right? That's what we want to avoid happening. We need to make sure we match "*.dev$" only.

Hey, just read a few lines more :)

+ if (strcmp(ext, suffix) != 0)
+ continue;
+

Kay

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw
In reply to Kay Sievers
Quoted 11 lines · Kay Sievers
Quoted 2 lines · Kay Sievers

Please have look if it still works for you, I only did a very quick test.

Here is a unified version, with all the functions moved to udev_lib.c. We have a generic function now, to call a given fnct(char *) for every file ending with a specific suffix, sorted in lexical order. We use it to execute the dev.d/ files and read our rules.d/ files. The binary should be a bit smaller now.

I've also changed it, to not do the dev.d/ exec for net devices.

Ok, care to fix it back so we call exec for net devices?

And now that I can read code, I've applied it :)

thanks,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id70&alloc_id638&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel