PDA

View Full Version : single udev-rule is matched several times



jody
7th September 2008, 01:28 PM
Hi
I managed now to write a udev-rule which calls a script
which writes a timestamp into a file when i plug in a memory stick or my camera.

Strange thing however: with a single rule for a memory stick

SUBSYSTEMS=="usb", ACTION="add", ATTRS{idVendor}=="05dc", ATTRS{idProduct}=="0080", RUN+="/home/jody/stampit dell_64M"
my script gets called 16 times. About 8 times before the window showing the contents appears,
and 8 times after. Adding the match rule
SUBSYSTEM=="block"
reduces the number of calls to my script to 2.

The same rule (with different idVendor and idProduct) for a different stick only
calls my script once.

With a similar single rule for my camera

SUBSYSTEM=="block" SUBSYSTEMS=="scsi", ATTRS{vendor}=="MAT****A", ATTRS{model}=="SD Video Camera", RUN+="/home/jody/stampit panasonic"
my script gets called twice. (The asterisks are inserted by this editor they stand for the
common expression for "fecal matter" :)

Does anybody have an idea why this happens, and how i can write
a rule which only is "executed" once?

Thank you
Jody

brebs
7th September 2008, 04:51 PM
First thing I would try is ATTR instead of ATTRS.

ATTRS{filename}
Search the devpath upwards for a device with matching sysfs
attribute values.

jody
7th September 2008, 05:21 PM
Thanks for the suggestion, but unfortunately,
if i use 'ATTR' instead of 'ATTRS', it doesn't work at all ( i.e. no match)

I think ATTR refers only to those attributes listed first in udev info,
in my case:
looking at device '/block/sdb':
KERNEL=="sdb"
SUBSYSTEM=="block"
DRIVER==""
ATTR{dev}=="8:16"
ATTR{range}=="16"
ATTR{removable}=="1"
ATTR{size}=="125952"
ATTR{capability}=="13"
ATTR{stat}==" 92 161 653 1374 0 0 0 0 0 924 1374"

The attributes in the following (parent?) devices are
referred to with 'ATTRS' - for example:

looking at parent device '/devices/pci0000:00/0000:00:02.0/usb3/3-2/3-2:1.0/host8/target8:0:0/8:0:0:0':
KERNELS=="8:0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS=="sd"
ATTRS{device_blocked}=="0"
ATTRS{type}=="0"
ATTRS{scsi_level}=="3"
ATTRS{vendor}=="LEXAR "
ATTRS{model}=="DIGITAL FILM "
ATTRS{rev}=="/W1."


Jody

brebs
7th September 2008, 05:35 PM
You've shown yourself what to use ATTR on:

ATTR{removable}=="1"
ATTR{size}=="125952"
SUBSYSTEM=="block"
KERNEL=="sd?" (yes that's a question-mark wildcard)
So try ATTR rather than ATTRS, with those. Obviously, it's not as convenient.

jody
13th September 2008, 02:50 PM
@brebs: your suggestion brought me onto the right track...


Just to document the solution...

I used a mixture of brebs' suggestion and my rule:

SUBSYSTEM=="block", KERNEL=="sd?", ACTION=="add", ATTRS{idVendor}=="05dc", ATTRS{idProduct}=="0080", RUN+="/home/jody/stampit dell_64M"

I.e. i threw out the SUBSYSTEMS=="usb", but added SUBSYSTEM=="block" and KERNEL=="sd?"
Now the rule matches only once.

The remaining question though:
why did the old rule match twice?
Now SUBSYSTEMS=="usb" occurs 3 times in the listing for my memory stick:

[root@localhost rules.d]# udevinfo -a -p /sys/block/sdb | grep -c "SUBSYSTEMS==\"usb\""
3

Is it possible that the reason for the multiple matching is that there are more than one device with SUBSYSTEMS=="usb"?
So that i have a first match for SUBSYSTEMS=="usb" in the first device together with the idVendor and the idProduct, and another match for SUBSYSTEMS=="usb" in the second device together with the ids?
But why didn't i get a triple match?

Thanks
Jod@brebs: your suggestion brought me onto the right track...


Just to document the solution...

I used a mixture of brebs' suggestion and my rule:

SUBSYSTEM=="block", KERNEL=="sd?", ACTION=="add", ATTRS{idVendor}=="05dc", ATTRS{idProduct}=="0080", RUN+="/home/jody/stampit dell_64M"

I.e. i threw out the SUBSYSTEMS=="usb", but added SUBSYSTEM=="block" and KERNEL=="sd?"
Now the rule matches only once.

The remaining question though:
why did the old rule match twice?
Now SUBSYSTEMS=="usb" occurs 3 times in the listing for my memory stick:

[root@localhost rules.d]# udevinfo -a -p /sys/block/sdb | grep -c "SUBSYSTEMS==\"usb\""
3

Is it possible that the reason for the multiple matching is that there are more than one device with SUBSYSTEMS=="usb"?
So that i have a first match for SUBSYSTEMS=="usb" in the first device together with the idVendor and the idProduct, and another match for SUBSYSTEMS=="usb" in the second device together with the ids?
But why didn't i get a triple match?

Thanks
Jody