3

I've written a udev rule like this:

SUBSYSTEM=="usb", ACTION=="add", RUN+="//root/usbmon/usb add %b"
SUBSYSTEM=="usb", ACTION=="remove", RUN+="//root/usbmon/usb remove %b"

now this usb script sends email whenever usb is attached and removed.

but problem is that every-time it sends 2 emails. it means rule is executing 2 times. this is what i get in log

Apr 26 14:52:00 box2 sendEmail[3374]: Email was sent successfully!
USB Added: Email sent to box2@dmz.domain.com Using Udev Rule at 04-26-14-52-00. 
Apr 26 14:52:00 box2 sendEmail[3394]: Email was sent successfully!
USB Added: Email sent to box2@dmz.domain.com Using Udev Rule at 04-26-14-52-00. 

I dont know why it is executing 2 times and how i can stop it to work once? I have verified that issue is at udev end but dont know how to fix it. this is output of udev monitor when usb is attached:

KERNEL[1335803018.283341] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4 (usb)
KERNEL[1335803018.284456] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0 (usb)
KERNEL[1335803018.284835] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6 (scsi)
KERNEL[1335803018.284863] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/scsi_host/host6 (scsi_host)
KERNEL[1335803023.524167] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0 (scsi)
KERNEL[1335803023.524735] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0 (scsi)
KERNEL[1335803023.525043] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 (scsi_disk)
KERNEL[1335803023.525154] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 (scsi_device)
KERNEL[1335803023.525505] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg6 (scsi_generic)
KERNEL[1335803023.525792] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0 (bsg)
KERNEL[1335803023.526534] add      /devices/virtual/bdi/8:80 (bdi)
UDEV  [1335803023.526895] add      /devices/virtual/bdi/8:80 (bdi)
KERNEL[1335803023.543043] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf (block)
KERNEL[1335803023.543166] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 (block)
UDEV  [1335803028.541785] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4 (usb)
UDEV  [1335803038.850181] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0 (usb)
UDEV  [1335803038.850423] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6 (scsi)
UDEV  [1335803038.850541] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0 (scsi)
UDEV  [1335803038.850568] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/scsi_host/host6 (scsi_host)
UDEV  [1335803038.851736] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0 (scsi)
UDEV  [1335803038.851912] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 (scsi_disk)
UDEV  [1335803038.861401] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 (scsi_device)
UDEV  [1335803038.862757] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0 (bsg)
UDEV  [1335803038.863880] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg6 (scsi_generic)
UDEV  [1335803039.002743] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf (block)
UDEV  [1335803039.072845] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 (block)

Now above output clearly shows that udev is attaching device again after 10 seconds and that is time when i get email again (after 10 seconds). also when script is not executable then i get following message 2 times in log file:

Apr 30 10:22:37 box2 udevd-work[1990]: exec of program '/root/usbmon/usb' failed
Apr 30 10:22:37 box2 udevd-work[1991]: exec of program '/root/usbmon/usb' failed

now I want to know that i need to change these settings to make udev run once. i have verified same thing on 2 server. CentOS and ubuntu.

Kashif
  • 473
  • 9
  • 20

2 Answers2

6

The issue was resolved by adding ENV{DEVTYPE}=="usb_device" in a rule.

jscott
  • 24,204
  • 8
  • 77
  • 99
Kashif
  • 473
  • 9
  • 20
1

I guess your rule matches the /dev/sdf device, but also the first partition /dev/sdf1, so it gets triggered twice.

If you only want to match /dev/sdf1, you could try something like KERNEL=="sd*1".

aseq
  • 4,550
  • 1
  • 22
  • 46