Skip to contentSkip to conversation

experimental (very simple) SYMLINK creation

udev Kay Sievers 1 participant 1 message Dec 5, 2003

lore
Share & tools
lore
#0 lore raw

Hi,
here is a experimental symlink creation patch - for discussion, in which direction we should go.
It is possible now to define SYMLINK= after the NAME= in udev.rules. The link is relative to the node, but the path is not optimized now if the node and the link are in the same nested directory. Only one link is supported, cause i need to sleep now :)

thanks,
Kay

06-simple-symlink-creation.diff
simple symlink creation
reorganized udev-remove to have access to the symlink field subdir creation/removal are functions now udev-test.pl tests for link creation/removal

namedev.c +8 -1
diff -Nru a/namedev.c b/namedev.c
@@ -121,6 +121,7 @@
 		copy_string(dev, new_dev, place);
 		copy_string(dev, new_dev, kernel_name);
 		copy_string(dev, new_dev, exec_program);
+		copy_string(dev, new_dev, symlink);
 		return 0;
 	}
 
@@ -366,6 +367,7 @@
 		if (strcmp_pattern(dev->id, udev->callout_value) != 0)
 			continue;
 		strfieldcpy(udev->name, dev->name);
+		strfieldcpy(udev->symlink, dev->symlink);
 		dbg("callout returned matching value '%s', '%s' becomes '%s'",
 		    dev->id, class_dev->name, udev->name);
 		return 0;
@@ -416,6 +418,7 @@
 			continue;
 
 		strfieldcpy(udev->name, dev->name);
+		strfieldcpy(udev->symlink, dev->symlink);
 		dbg("found matching attribute '%s', '%s' becomes '%s' ",
 		    dev->sysfs_file, class_dev->name, udev->name);
 
@@ -461,6 +464,7 @@
 		if (!found)
 			continue;
 		strfieldcpy(udev->name, dev->name);
+		strfieldcpy(udev->symlink, dev->symlink);
 		dbg("found matching id '%s', '%s' becomes '%s'",
 		    dev->id, class_dev->name, udev->name);
 		return 0;
@@ -506,6 +510,7 @@
 			continue;
 
 		strfieldcpy(udev->name, dev->name);
+		strfieldcpy(udev->symlink, dev->symlink);
 		dbg("found matching place '%s', '%s' becomes '%s'",
 		    dev->place, class_dev->name, udev->name);
 		return 0;
@@ -528,6 +533,7 @@
 			continue;
 
 		strfieldcpy(udev->name, dev->name);
+		strfieldcpy(udev->symlink, dev->symlink);
 		dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
 		
 		return 0;
@@ -618,8 +624,9 @@
 	goto done;
 
 found:
-	/* substitute placeholder in NAME  */
+	/* substitute placeholder */
 	apply_format(udev, udev->name);
+	apply_format(udev, udev->symlink);
 
 done:
 	perm = find_perm(udev->name);
namedev.h +1 -0
diff -Nru a/namedev.h b/namedev.h
@@ -63,6 +63,7 @@
 	char kernel_name[NAME_SIZE];
 	char exec_program[FILE_SIZE];
 	char name[NAME_SIZE];
+	char symlink[NAME_SIZE];
 };
 
 struct perm_device {
namedev_parse.c +10 -1
diff -Nru a/namedev_parse.c b/namedev_parse.c
@@ -291,9 +291,11 @@
 			if (retval)
 				break;
 			strfieldcpy(dev.name, temp3);
+
 			dbg_parse("REPLACE name='%s', kernel_name='%s'",
 				  dev.name, dev.kernel_name);
 		}
+
 		if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
 			/* number type */
 			dev.type = CALLOUT;
@@ -324,10 +326,17 @@
 			if (retval)
 				break;
 			strfieldcpy(dev.name, temp3);
+
 			dbg_parse("CALLOUT name='%s', program='%s'",
 				  dev.name, dev.exec_program);
 		}
 
+		/* SYMLINK="name" */
+		temp2 = strsep(&temp, ",");
+		retval = get_value("SYMLINK", &temp, &temp3);
+		if (retval == 0)
+			strfieldcpy(dev.symlink, temp3);
+
 		retval = add_config_dev(&dev);
 		if (retval) {
 			dbg("add_config_dev returned with error %d", retval);
@@ -414,7 +423,7 @@
 			  dev.mode);
 		retval = add_perm_dev(&dev);
 		if (retval) {
-			dbg("add_config_dev returned with error %d", retval);
+			dbg("add_perm_dev returned with error %d", retval);
 			goto exit;
 		}
 	}
test/udev-test.pl +25 -8
diff -Nru a/test/udev-test.pl b/test/udev-test.pl
@@ -39,7 +39,7 @@
 		expected => "boot_disk" ,
 		conf     => <<EOF
 LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="boot_disk%n"
-REPLACE, KERNEL="ttyUSB0", NAME="visor""
+REPLACE, KERNEL="ttyUSB0", NAME="visor"
 EOF
 	},
 	{
@@ -171,14 +171,30 @@
 EOF
 	},
 	{
-		desc     => "callout bus type",
+		desc     => "symlink creation (node)",
+		subsys   => "tty",
+		devpath  => "class/tty/ttyUSB0",
+		expected => "ttyUSB0" ,
+		conf     => <<EOF
+REPLACE, KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="tts/USB%n"
+EOF
+	},
+	{
+		desc     => "symlink creation (link)",
+		subsys   => "tty",
+		devpath  => "class/tty/ttyUSB0",
+		expected => "tts/USB0" ,
+		conf     => <<EOF
+REPLACE, KERNEL="ttyUSB[0-9]*", NAME="ttyUSB%n", SYMLINK="tts/USB%n"
+EOF
+	},
+	{
+		desc     => "symlink creation (link)",
 		subsys   => "block",
-		devpath  => "block/sda",
-		expected => "scsi-0:0:0:0" ,
+		devpath  => "block/sda/sda2",
+		expected => "my/own/link2" ,
 		conf     => <<EOF
-CALLOUT, BUS="usb", PROGRAM="/bin/echo -n usb-%b", ID="*", NAME="%c"
-CALLOUT, BUS="scsi", PROGRAM="/bin/echo -n scsi-%b", ID="*", NAME="%c"
-CALLOUT, BUS="foo", PROGRAM="/bin/echo -n foo-%b", ID="*", NAME="%c"
+LABEL, BUS="scsi", vendor="IBM-ESXS", NAME="lun0/%D", SYMLINK="my/own/link%n"
 EOF
 	},
 );
@@ -238,7 +254,8 @@
 	}
 
 	udev("remove", $config->{subsys}, $config->{devpath}, \$config->{conf});
-	if (-e "$PWD/$udev_root$config->{expected}") {
+	if ((-e "$PWD/$udev_root$config->{expected}") ||
+	    (-l "$PWD/$udev_root$config->{expected}")) {
 		print "remove: error\n\n";
 		system("tree $udev_root");
 		$error++;
udev-add.c +63 -27

118 lines not on this page · raw message

udev-remove.c +55 -65

151 lines not on this page · raw message

udev.h +1 -0
diff -Nru a/udev.h b/udev.h
@@ -64,6 +64,7 @@
 	int major;
 	int minor;
 	mode_t mode;
+	char symlink[NAME_SIZE];
 
 	/* fields that help us in building strings */
 	unsigned char bus_id[SYSFS_NAME_LEN];