linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch] compatibility symlinks for udev
@ 2004-09-06 14:46 David Zeuthen
  2004-09-06 15:45 ` Kay Sievers
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: David Zeuthen @ 2004-09-06 14:46 UTC (permalink / raw)
  To: linux-hotplug

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

Hey Greg,

Here's a patch against udev-030 that can help create compatibility
symlinks like /dev/cdrom, /dev/cdrom1 etc. The patch introduces a new
substitution type %C (for Compatibility) that can be used as follows

        KERNEL="sr*", NAME="%k", SYMLINK="cdrom%C"
        KERNEL="scd*", NAME="%k", SYMLINK="cdrom%C"
        KERNEL="pcd*", NAME="%k", SYMLINK="cdrom%C"
        KERNEL="fd[0-9]", NAME="%k", SYMLINK="floppy%C"
        KERNEL="hd[a-z]", PROGRAM="/home/david/udev_ide_cdrom_or_floppy.sh %k", NAME="%k", SYMLINK="%c%C"
        KERNEL="fd[0-9]", NAME="%k", SYMLINK="floppy%C"

%C will be substitued with nothing if the text prior to the %C doesn't
exist, otherwise it will be substituted with the minimal positive
integer such that text prior to %C and the integer doesn't exist. Pretty
basic.

Together with a simple helper script (that can be removed as soon as IDE
exports the type in sysfs; alternatively this is trivial to write in C
for e.g. early boot)

        #!/bin/sh
        TYPE=`cat /proc/ide/$1/media`
        if test "$TYPE" = "cdrom"; then
            echo -n cdrom
            exit 0
        elif test "$TYPE" = "floppy"; then
            echo -n floppy
            exit 0
        fi
        exit 1
        
I get the following symlinks

        [root@laptop udev]# ls -l floppy
        lrwxrwxrwx  1 root root 3 Sep  6 16:29 floppy -> hdc
        [root@laptop udev]# ls -l cdrom*
        lrwxrwxrwx  1 root root 3 Sep  6 16:29 cdrom -> hdd
        lrwxrwxrwx  1 root root 3 Sep  6 16:29 cdrom1 -> sr0
        
which I think is nice (... as far as programs relying on names of device
nodes is nice, ahem). While the patch is somewhat against the spirit of
udev it's very useful for maintaining compatibility with old software.
People who depend and care about what /dev/cdrom%d is can always write
exact rules to match them perfectly.

I hope you will apply this patch.

Thanks,
David

[-- Attachment #2: udev-compat-symlinks.patch --]
[-- Type: text/x-patch, Size: 2186 bytes --]

--- udev-030.orig/namedev.c	2004-07-09 19:59:10.000000000 +0200
+++ udev-030/namedev.c	2004-09-06 16:22:03.000000000 +0200
@@ -42,6 +42,7 @@
 #include "logging.h"
 #include "namedev.h"
 #include "klibc_fixups.h"
+#include "udevdb.h"
 
 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
 
@@ -179,6 +180,37 @@ static int get_format_len(char **str)
 	return -1;
 }
 
+/** Finds the lowest positive N such that <name>N isn't present in 
+ *  $(udevroot) either as a file or a symlink.
+ *
+ *  @param  name                Name to check for
+ *  @return                     0 if <name> didn't exist and N otherwise.
+ */
+static unsigned int find_free_number (struct udevice *udev, char *name)
+{
+	char temp[NAME_SIZE];
+	char path[NAME_SIZE];
+	struct udevice dev;
+	int result;
+
+	/* have to sweep the database for each lookup */
+	result = 0;
+	strncpy(temp, name, sizeof (temp));
+	while (1) {
+		if (udevdb_get_dev_byname(temp, path, &dev) != 0)
+			goto found;
+		/* symlink might be stale if $(udevroot) isn't cleaned; check
+		 * on major/minor to see if it's the same device
+		 */
+		if (dev.major == udev->major && dev.minor == udev->minor)
+			goto found;
+		snprintf (temp, sizeof(temp), "%s%d", name, ++result);
+	}
+
+found:
+	return result;
+}
+
 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
 			 struct sysfs_class_device *class_dev,
 			 struct sysfs_device *sysfs_device)
@@ -195,6 +227,7 @@ static void apply_format(struct udevice 
 	char *rest;
 	int slen;
 	struct sysfs_attribute *tmpattr;
+	unsigned int next_free_number;
 
 	pos = string;
 	while (1) {
@@ -284,6 +317,13 @@ static void apply_format(struct udevice 
 			strfieldcatmax(string, "%", maxsize);
 			pos++;
 			break;
+		case 'C':
+			next_free_number = find_free_number(udev, string);
+			if (next_free_number > 0) {
+				snprintf(temp2, sizeof(temp2), "%d", next_free_number);
+				strfieldcatmax(string, temp2, maxsize);
+			}
+			break;
 		default:
 			dbg("unknown substitution type '%%%c'", c);
 			break;

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

end of thread, other threads:[~2004-09-14 20:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-06 14:46 [patch] compatibility symlinks for udev David Zeuthen
2004-09-06 15:45 ` Kay Sievers
2004-09-07 11:19 ` David Zeuthen
2004-09-10 20:09 ` Greg KH
2004-09-14 17:50 ` Kay Sievers
2004-09-14 20:50 ` Greg KH

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).