Skip to contentSkip to conversation

Problem parsing %s in udev rules

DISCUSSION Andrew Patterson 3 participants 10 messages Oct 6, 2004 – Oct 15, 2004

lore
Share & tools
lore
#0 lore raw

Udev seems to be chopping off anything seen in a rule after a %s. For example, if I have the following rule:

BUS="scsi", KERNEL="sd*", PROGRAM="/sbin/scsi_id", NAME="%k", SYMLINK="scsi_disks/some-proceeding-stuff-%s{model}-some-following- stuff"

Everything after the %s{model} is ignored.

Furthermore, everything in model following whitespace is ignored as well. I.e, if model contains "HP 34.7G", the resulting device file created is /dev/scsi_disks/some-proceeding-stuff-HP.

I used the following command to run this test:

# DEVPATH=/block/sda ACTION=add ./udev block

I also get the same behavior when using udevstart (patched to fix parameter passing problems).

Environment:

OS: debian unstable
Arch: IA-64
udev version: 032
scsi_id version: 0.5

Andrew

Signature · 2 lines
Andrew Patterson                
Hewlett-Packard
#1 lore raw
Quoted 13 lines · Andrew Patterson

Udev seems to be chopping off anything seen in a rule after a %s. For example, if I have the following rule:

BUS="scsi", KERNEL="sd*", PROGRAM="/sbin/scsi_id", NAME="%k", SYMLINK="scsi_disks/some-proceeding-stuff-%s{model}-some-following- stuff"

Everything after the %s{model} is ignored.

Furthermore, everything in model following whitespace is ignored as well. I.e, if model contains "HP 34.7G", the resulting device file created is /dev/scsi_disks/some-proceeding-stuff-HP.

Quoted 6 lines

I used the following command to run this test:

# DEVPATH=/block/sda ACTION­d ./udev block

I also get the same behavior when using udevstart (patched to fix parameter passing problems).

If your sysfs value contains spaces, which is the delimiter for multiple symlinks, it will not work.

udev can't do this now, you need to wrap the call with an external script, which ensures sane values.

Kay

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
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 Andrew Patterson
Quoted 18 lines · Kay Sievers
Quoted 17 lines · Andrew Patterson

Udev seems to be chopping off anything seen in a rule after a %s. For example, if I have the following rule:

BUS="scsi", KERNEL="sd*", PROGRAM="/sbin/scsi_id", NAME="%k", SYMLINK="scsi_disks/some-proceeding-stuff-%s{model}-some-following- stuff"

Everything after the %s{model} is ignored.

Furthermore, everything in model following whitespace is ignored as well. I.e, if model contains "HP 34.7G", the resulting device file created is /dev/scsi_disks/some-proceeding-stuff-HP.

I used the following command to run this test:

# DEVPATH=/block/sda ACTION=add ./udev block

Quoted 9 lines
Quoted 2 lines

I also get the same behavior when using udevstart (patched to fix parameter passing problems).

If your sysfs value contains spaces, which is the delimiter for multiple symlinks, it will not work.

udev can't do this now, you need to wrap the call with an external script, which ensures sane values.

That explains the spaces. What about stuff trailing %s, if %s does not contain spaces. I.e, in the above example, model is ST336753LC and the resulting device file is /dev/scsi_disks/some-proceding-stuff- ST336753LC.

Andrew

Quoted 2 lines

Kay

Signature · 2 lines
Andrew Patterson                
Hewlett-Packard
#3 lore raw
In reply to Andrew Patterson
Quoted 25 lines · Andrew Patterson
Quoted 24 lines · Kay Sievers
Quoted 13 lines · Andrew Patterson

Udev seems to be chopping off anything seen in a rule after a %s. For example, if I have the following rule:

BUS="scsi", KERNEL="sd*", PROGRAM="/sbin/scsi_id", NAME="%k", SYMLINK="scsi_disks/some-proceeding-stuff-%s{model}-some-following- stuff"

Everything after the %s{model} is ignored.

Furthermore, everything in model following whitespace is ignored as well. I.e, if model contains "HP 34.7G", the resulting device file created is /dev/scsi_disks/some-proceeding-stuff-HP.

Quoted 6 lines

I used the following command to run this test:

# DEVPATH=/block/sda ACTION­d ./udev block

I also get the same behavior when using udevstart (patched to fix parameter passing problems).

If your sysfs value contains spaces, which is the delimiter for multiple symlinks, it will not work.

Quoted 8 lines
Quoted 3 lines

udev can't do this now, you need to wrap the call with an external script, which ensures sane values.

That explains the spaces. What about stuff trailing %s, if %s does not contain spaces. I.e, in the above example, model is ST336753LC and the resulting device file is /dev/scsi_disks/some-proceding-stuff- ST336753LC.

I expect the model value has trailing spaces.

You may look with:
udevinfo -a -p /block/sdX

Kay

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#4 lore raw
In reply to Andrew Patterson

Kay,

Quoted 35 lines · Kay Sievers
Quoted 25 lines · Andrew Patterson
Quoted 24 lines · Kay Sievers
Quoted 13 lines · Andrew Patterson

Udev seems to be chopping off anything seen in a rule after a %s. For example, if I have the following rule:

BUS="scsi", KERNEL="sd*", PROGRAM="/sbin/scsi_id", NAME="%k", SYMLINK="scsi_disks/some-proceeding-stuff-%s{model}-some-following- stuff"

Everything after the %s{model} is ignored.

Furthermore, everything in model following whitespace is ignored as well. I.e, if model contains "HP 34.7G", the resulting device file created is /dev/scsi_disks/some-proceeding-stuff-HP.

Quoted 6 lines

I used the following command to run this test:

# DEVPATH=/block/sda ACTION=add ./udev block

I also get the same behavior when using udevstart (patched to fix parameter passing problems).

If your sysfs value contains spaces, which is the delimiter for multiple symlinks, it will not work.

Quoted 8 lines
Quoted 3 lines

udev can't do this now, you need to wrap the call with an external script, which ensures sane values.

That explains the spaces. What about stuff trailing %s, if %s does not contain spaces. I.e, in the above example, model is ST336753LC and the resulting device file is /dev/scsi_disks/some-proceding-stuff- ST336753LC.

Quoted 4 lines

I expect the model value has trailing spaces.

You may look with:
udevinfo -a -p /block/sdX

Yes it does, and it seems for most SCSI devices, vendor and model will have trailing spaces. I have included a patch to udev-036 to deal with this issue. It trims off trailing whitespace for all sysfs attributes. It might be better to trim off leading whitespace as well.

I also wonder if it might be a good idea to do some sort of transformation for characters in an attribute that are illegal in a file system, rather than just chopping everything off. e.g, spaces that are not leading or trailing would be changed to underscores.

Andrew

Quoted 3 lines

Kay

Signature · 28 lines
Andrew Patterson                
Hewlett-Packard

diff -Nur udev-036.orig/namedev.c udev-036/namedev.c
--- udev-036.orig/namedev.c	2004-10-12 14:52:18.000000000 -0600
+++ udev-036/namedev.c	2004-10-12 16:51:05.000000000 -0600
@@ -511,7 +511,7 @@
 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
 {
 	struct sysfs_attribute *tmpattr = NULL;
-	char *c;
+	int endidx;
 
 	dbg("look for device attribute '%s'", attr);
 	/* try to find the attribute in the class device directory */
@@ -529,9 +529,9 @@
 	return NULL;
 
 attr_found:
-	c = strchr(tmpattr->value, '\n');
-	if (c != NULL)
-		c[0] = '\0';
+	endidx = strcspn(tmpattr->value, " \t\n");
+	if (endidx > 0)
+		tmpattr->value[endidx - 1] = '\0';
 
 	dbg("found attribute '%s'", tmpattr->path);
 	return tmpattr;
#5 lore raw
In reply to Andrew Patterson
Quoted 38 lines · Andrew Patterson

Kay,

Quoted 35 lines · Kay Sievers
Quoted 33 lines · Andrew Patterson
Quoted 18 lines · Kay Sievers
Quoted 17 lines · Andrew Patterson

Udev seems to be chopping off anything seen in a rule after a %s. For example, if I have the following rule:

BUS="scsi", KERNEL="sd*", PROGRAM="/sbin/scsi_id", NAME="%k", SYMLINK="scsi_disks/some-proceeding-stuff-%s{model}-some-following- stuff"

Everything after the %s{model} is ignored.

Furthermore, everything in model following whitespace is ignored as well. I.e, if model contains "HP 34.7G", the resulting device file created is /dev/scsi_disks/some-proceeding-stuff-HP.

I used the following command to run this test:

# DEVPATH=/block/sda ACTION­d ./udev block

Quoted 9 lines
Quoted 2 lines

I also get the same behavior when using udevstart (patched to fix parameter passing problems).

If your sysfs value contains spaces, which is the delimiter for multiple symlinks, it will not work.

udev can't do this now, you need to wrap the call with an external script, which ensures sane values.

That explains the spaces. What about stuff trailing %s, if %s does not contain spaces. I.e, in the above example, model is ST336753LC and the resulting device file is /dev/scsi_disks/some-proceding-stuff- ST336753LC.

Quoted 7 lines
Quoted 4 lines

I expect the model value has trailing spaces.

You may look with:
udevinfo -a -p /block/sdX

Yes it does, and it seems for most SCSI devices, vendor and model will have trailing spaces.

It all depends on the vendor and model :)

Quoted 3 lines

I have included a patch to udev-036 to deal with
this issue. It trims off trailing whitespace for all sysfs attributes. It might be better to trim off leading whitespace as well.

We already trim it off when matching, but we also allow matching if you do put the spaces in there. This patch breaks that, right?

Quoted 4 lines

I also wonder if it might be a good idea to do some sort of transformation for characters in an attribute that are illegal in a file system, rather than just chopping everything off. e.g, spaces that are not leading or trailing would be changed to underscores.

ick, no.

thanks,

greg k-h

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#6 lore raw
In reply to Andrew Patterson
Quoted 47 lines · Greg KH
Quoted 38 lines · Andrew Patterson

Kay,

Quoted 35 lines · Kay Sievers
Quoted 33 lines · Andrew Patterson
Quoted 18 lines · Kay Sievers
Quoted 17 lines · Andrew Patterson

Udev seems to be chopping off anything seen in a rule after a %s. For example, if I have the following rule:

BUS="scsi", KERNEL="sd*", PROGRAM="/sbin/scsi_id", NAME="%k", SYMLINK="scsi_disks/some-proceeding-stuff-%s{model}-some-following- stuff"

Everything after the %s{model} is ignored.

Furthermore, everything in model following whitespace is ignored as well. I.e, if model contains "HP 34.7G", the resulting device file created is /dev/scsi_disks/some-proceeding-stuff-HP.

I used the following command to run this test:

# DEVPATH=/block/sda ACTION=add ./udev block

Quoted 9 lines
Quoted 2 lines

I also get the same behavior when using udevstart (patched to fix parameter passing problems).

If your sysfs value contains spaces, which is the delimiter for multiple symlinks, it will not work.

udev can't do this now, you need to wrap the call with an external script, which ensures sane values.

That explains the spaces. What about stuff trailing %s, if %s does not contain spaces. I.e, in the above example, model is ST336753LC and the resulting device file is /dev/scsi_disks/some-proceding-stuff- ST336753LC.

Quoted 7 lines
Quoted 4 lines

I expect the model value has trailing spaces.

You may look with:
udevinfo -a -p /block/sdX

Yes it does, and it seems for most SCSI devices, vendor and model will have trailing spaces.

Quoted 8 lines

It all depends on the vendor and model :)

Quoted 3 lines

I have included a patch to udev-036 to deal with
this issue. It trims off trailing whitespace for all sysfs attributes. It might be better to trim off leading whitespace as well.

We already trim it off when matching, but we also allow matching if you do put the spaces in there. This patch breaks that, right?

Correct, I have a new patch that trims after the comparison, so it should work in both cases.

Quoted 7 lines
Quoted 5 lines

I also wonder if it might be a good idea to do some sort of transformation for characters in an attribute that are illegal in a file system, rather than just chopping everything off. e.g, spaces that are not leading or trailing would be changed to underscores.

ick, no.

Really? Care to elaborate?

Do you not like any transformation (besides the "let's chop off everything after anything illegal") or just the underscores for spaces one? Perhaps a configuration value to make it optional? This sort of thing might relieve a lot of the script writing that must be done to handle cases that may be common.

Andrew

namedev.c +11 -0
diff -Nur udev-036.orig/namedev.c udev-036/namedev.c
@@ -307,6 +307,17 @@
 					dbg("sysfa attribute '%s' not found", attr);
 					break;
 				}
+				/* strip trailing whitespace of matching value */
+				if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
+					i = len = strlen(tmpattr->value);
+					while (i > 0 &&  isspace(tmpattr->value[i-1]))
+						i--;
+					if (i < len) {
+						tmpattr->value[i] = '\0';
+						dbg("remove %i trailing whitespace chars from '%s'",
+							 len - i, tmpattr->value);
+					}
+				}
 				strfieldcatmax(string, tmpattr->value, maxsize);
 				dbg("substitute sysfs value '%s'", tmpattr->value);
 			} else {
Quoted 4 lines

thanks,

greg k-h

Signature · 2 lines
Andrew Patterson                
Hewlett-Packard
#7 lore raw
In reply to Andrew Patterson
Quoted 5 lines · Andrew Patterson
Quoted 2 lines

We already trim it off when matching, but we also allow matching if you do put the spaces in there. This patch breaks that, right?

Correct, I have a new patch that trims after the comparison, so it should work in both cases.

Thanks, I've applied this.

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

I also wonder if it might be a good idea to do some sort of transformation for characters in an attribute that are illegal in a file system, rather than just chopping everything off. e.g, spaces that are not leading or trailing would be changed to underscores.

ick, no.

Quoted 7 lines

Really? Care to elaborate?

Do you not like any transformation (besides the "let's chop off everything after anything illegal") or just the underscores for spaces one? Perhaps a configuration value to make it optional? This sort of thing might relieve a lot of the script writing that must be done to handle cases that may be common.

Have you run into any cases where something like this is needed? I don't really want to do any transformation within udev itself if possible, as it's a slippery slope down... Helper programs can be used if people run into anything really nasty, and the fact that this hasn't come up yet, lends me to belive it really isn't a problem.

Do you have any device that would need such transformation?

thanks,

greg k-h

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#8 lore raw
In reply to Andrew Patterson
Quoted 8 lines · Greg KH
Quoted 5 lines · Andrew Patterson
Quoted 2 lines

We already trim it off when matching, but we also allow matching if you do put the spaces in there. This patch breaks that, right?

Correct, I have a new patch that trims after the comparison, so it should work in both cases.

Thanks, I've applied this.

Thanks.

Quoted 16 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines

I also wonder if it might be a good idea to do some sort of transformation for characters in an attribute that are illegal in a file system, rather than just chopping everything off. e.g, spaces that are not leading or trailing would be changed to underscores.

ick, no.

Quoted 7 lines

Really? Care to elaborate?

Do you not like any transformation (besides the "let's chop off everything after anything illegal") or just the underscores for spaces one? Perhaps a configuration value to make it optional? This sort of thing might relieve a lot of the script writing that must be done to handle cases that may be common.

Quoted 7 lines

Have you run into any cases where something like this is needed? I don't really want to do any transformation within udev itself if possible, as it's a slippery slope down... Helper programs can be used if people run into anything really nasty, and the fact that this hasn't come up yet, lends me to belive it really isn't a problem.

Do you have any device that would need such transformation?

Need, no. Want perhaps. Example: using the following rule:

BUS="scsi", KERNEL="sd*", NAME="%k", SYMLINK="scsi_disks/%s{vendor}-% s{model}"

I have this stupid disk drive that has the following vendor and model entry:

Vendor: HP 36.4G Model: ST336753LC

So the resulting device file created by udev is
/dev/scsi_disks/HP-ST336753LC

Intuitively, I would expect /dev/scsi_disks/HP 36.4G-ST336753LC. Given that spaces are bad, it would be be nice to see something like:

/dev/scsi_disks/HP_36.4G-ST336753LC.

Is the %s stuff used a lot? I am somewhat surprised that I am the only one running into issues like this.

Andrew

Quoted 5 lines

thanks,

greg k-h

Signature · 2 lines
Andrew Patterson                
Hewlett-Packard
#9 lore raw
In reply to Andrew Patterson
Quoted 33 lines · Andrew Patterson
Quoted 15 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

I also wonder if it might be a good idea to do some sort of transformation for characters in an attribute that are illegal in a file system, rather than just chopping everything off. e.g, spaces that are not leading or trailing would be changed to underscores.

ick, no.

Quoted 7 lines

Really? Care to elaborate?

Do you not like any transformation (besides the "let's chop off everything after anything illegal") or just the underscores for spaces one? Perhaps a configuration value to make it optional? This sort of thing might relieve a lot of the script writing that must be done to handle cases that may be common.

Quoted 7 lines

Have you run into any cases where something like this is needed? I don't really want to do any transformation within udev itself if possible, as it's a slippery slope down... Helper programs can be used if people run into anything really nasty, and the fact that this hasn't come up yet, lends me to belive it really isn't a problem.

Do you have any device that would need such transformation?

Need, no. Want perhaps. Example: using the following rule:

BUS="scsi", KERNEL="sd*", NAME="%k", SYMLINK="scsi_disks/%s{vendor}-% s{model}"

I have this stupid disk drive that has the following vendor and model entry:

Vendor: HP 36.4G Model: ST336753LC

Quoted 7 lines

So the resulting device file created by udev is
/dev/scsi_disks/HP-ST336753LC

Intuitively, I would expect /dev/scsi_disks/HP 36.4G-ST336753LC. Given that spaces are bad, it would be be nice to see something like:

/dev/scsi_disks/HP_36.4G-ST336753LC.

Then I suggest you use a helper program to create such an insane name :) Seriously, there are a load of better things to use to create a name, than a vendor provided string.

Quoted 1 line

Is the %s stuff used a lot?

I think in this manner, no.

thanks,

greg k-h

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel