Skip to contentSkip to conversation

Run script when USB scanner unplugged

DISCUSSION Ian Pilcher 5 participants 12 messages Jun 2, 2004 – Jun 8, 2004

lore
Share & tools
lore
#0 lore raw

Running Fedora Core 2 here. I've found that the hotplug scripts shipped with the distribution do a fine job of setting the permissions when a USB scanner is plugged in *after* a user has logged in. Unfortunately, this is not the most common usage scenario; I leave my scanner plugged in pretty much all the time, and I suspect that most other users do the same.

What's needed, at least on Red Hat/Fedora Core systems, is a symlink which will tell PAM to adjust the permissions when a "console" user logs in. I've hacked /etc/hotplug/usb/libusbscanner to do the following when a scanner is plugged in:

     DEVID="${REMOVER#/var/run/usb/}"
     rm -f "/dev/usb/scanner-${DEVID}"
     ln -s "${DEVICE}" "/dev/usb/scanner-${DEVID}"

So far so good. The link is created when the system boots, and PAM follows it and sets the device permissions properly when a "console user" logs in.

Now I want to remove the link when (if) the scanner is unplugged. I've tried adding an 'if [ "${ACTION}" = "remove ]' section to /etc/hotplug/usb/libusbscanner, but that script doesn't appear to get executed when a USB scanner is unplugged.

What do I need to do?

Thanks!

Signature · 15 lines
====================================
Ian Pilcher                                        [email protected]
====================================



-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#1 lore raw
Quoted 20 lines · Ian Pilcher

Running Fedora Core 2 here. I've found that the hotplug scripts shipped with the distribution do a fine job of setting the permissions when a USB scanner is plugged in *after* a user has logged in. Unfortunately, this is not the most common usage scenario; I leave my scanner plugged in pretty much all the time, and I suspect that most other users do the same.

What's needed, at least on Red Hat/Fedora Core systems, is a symlink which will tell PAM to adjust the permissions when a "console" user logs in. I've hacked /etc/hotplug/usb/libusbscanner to do the following when a scanner is plugged in:

    DEVID="${REMOVER#/var/run/usb/}"
    rm -f "/dev/usb/scanner-${DEVID}"
    ln -s "${DEVICE}" "/dev/usb/scanner-${DEVID}"

So far so good. The link is created when the system boots, and PAM follows it and sets the device permissions properly when a "console user" logs in.

Quoted 9 lines

Now I want to remove the link when (if) the scanner is unplugged. I've tried adding an 'if [ "${ACTION}" = "remove ]' section to /etc/hotplug/usb/libusbscanner, but that script doesn't appear to get executed when a USB scanner is unplugged.

What do I need to do?

Thanks!

Install the FC2 udev rpm... read the manpage and add a *.rule file to /etc/udev/rules.d . Also add your scanner to /etc/security/console.perms (man 5 console.perms).

#2 lore raw
In reply to Ian Pilcher
Quoted 3 lines · Harald Hoyer

Install the FC2 udev rpm... read the manpage and add a *.rule file to /etc/udev/rules.d . Also add your scanner to /etc/security/console.perms (man 5 console.perms).

OK, I've installed the udev RPM and added the following line to /etc/udev/rules.d/50-udev.rules:

     BUS="usb", SYSFS{idVendor}="04b8", SYSFS{idProduct}="0110",
         NAME="usb/epson_scanner"

Here is the output of lsusb:

     Bus 002 Device 001: ID 0000:0000
     Bus 001 Device 003: ID 04e8:323a Samsung Electronics Co., Ltd
     Bus 001 Device 002: ID 04b8:0110 Seiko Epson Corp. Perfection 1650
     Bus 001 Device 001: ID 0000:0000

This seems like it should work, but the /udev/usb/epson_scanner node never gets created. (/udev/usb/lp0 is created.) What could I be doing wrong?

Thanks!

Signature · 14 lines
====================================
Ian Pilcher                                        [email protected]
====================================


-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#3 lore raw
In reply to Ian Pilcher

Hard to believe that I've already stumped the entire hotplug development community. Since I don't see this in the archives, I'm resending it. Sorry if it turns out to be a dupe.

Quoted 3 lines · Harald Hoyer

Install the FC2 udev rpm... read the manpage and add a *.rule file to /etc/udev/rules.d . Also add your scanner to /etc/security/console.perms (man 5 console.perms).

OK, I've installed the udev RPM and added the following line to /etc/udev/rules.d/50-udev.rules:

     BUS="usb", SYSFS{idVendor}="04b8", SYSFS{idProduct}="0110",
         NAME="usb/epson_scanner"

Here is the output of lsusb:

     Bus 002 Device 001: ID 0000:0000
     Bus 001 Device 003: ID 04e8:323a Samsung Electronics Co., Ltd
     Bus 001 Device 002: ID 04b8:0110 Seiko Epson Corp. Perfection 1650
     Bus 001 Device 001: ID 0000:0000

This seems like it should work, but the /udev/usb/epson_scanner node never gets created. (/udev/usb/lp0 is created.) What could I be doing wrong?

Thanks!

Signature · 14 lines
====================================
Ian Pilcher                                        [email protected]
====================================


-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
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 Ian Pilcher
Quoted 18 lines · Ian Pilcher

Hard to believe that I've already stumped the entire hotplug development community. Since I don't see this in the archives, I'm resending it. Sorry if it turns out to be a dupe.

Quoted 3 lines · Harald Hoyer

Install the FC2 udev rpm... read the manpage and add a *.rule file to /etc/udev/rules.d . Also add your scanner to /etc/security/console.perms (man 5 console.perms).

OK, I've installed the udev RPM and added the following line to /etc/udev/rules.d/50-udev.rules:

    BUS="usb", SYSFS{idVendor}="04b8", SYSFS{idProduct}="0110",
        NAME="usb/epson_scanner"

Here is the output of lsusb:

Quoted 8 lines
    Bus 002 Device 001: ID 0000:0000
    Bus 001 Device 003: ID 04e8:323a Samsung Electronics Co., Ltd
    Bus 001 Device 002: ID 04b8:0110 Seiko Epson Corp. Perfection 1650
    Bus 001 Device 001: ID 0000:0000

This seems like it should work, but the /udev/usb/epson_scanner node never gets created. (/udev/usb/lp0 is created.) What could I be doing wrong?

FC2 uses a linux 2.6 kernel right?
Kernel 2.6 no longer has the scanner kernel module, so there's no longer a device node associated with it (or am I overlooking something here?).
Communication with an USB scanner under kernel 2.6 is done with libusb instead (using the files in /proc/bus/usb).

Bertrik

-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X. From Windows to Linux, servers to mobile, InstallShield X is the one installation-authoring solution that does it all. Learn more and evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#5 lore raw
In reply to Ian Pilcher
Quoted 3 lines · Ian Pilcher

Running Fedora Core 2 here. I've found that the hotplug scripts shipped with the distribution do a fine job of setting the permissions when a USB scanner is plugged in *after* a user has logged in.

There is no /dev node for a USB scanner anymore, as there is no kernel driver for it in 2.6. Do you realize this? Because of this, modifying the hotplug scripts will be quite difficult, and udev will know nothing about this kind of device, sorry.

thanks,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X. From Windows to Linux, servers to mobile, InstallShield X is the one installation-authoring solution that does it all. Learn more and evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
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 Ian Pilcher
Quoted 7 lines · Greg KH
Quoted 3 lines · Ian Pilcher

Running Fedora Core 2 here. I've found that the hotplug scripts shipped with the distribution do a fine job of setting the permissions when a USB scanner is plugged in *after* a user has logged in.

Quoted 5 lines

There is no /dev node for a USB scanner anymore, as there is no kernel driver for it in 2.6. Do you realize this? Because of this, modifying the hotplug scripts will be quite difficult, and udev will know nothing about this kind of device, sorry.

/etc/hotplug/usb/libusbscanner, as shipped with Fedora Core 2, already sets the ownership of the appropriate /proc/bus/usb/... file to the "console" user. This works fine when a scanner is plugged in *after* the user has logged on.

I've hacked /etc/hotplug/usb/libusbscanner to create a symbolic link from /dev/usb/scanner-%sys%devices%pci%... to that /proc/bus/usb... file. When the USB "insertion" is processed during system boot, the symlink gets created. Then when a "console" user logs on, PAM follows the symlink and sets the ownership of the /proc/bus/usb... file appropriately.

All I want to do is run a script during the USB "remove" event to get rid of this symlink. Reading through /etc/hotplug/usb.agent, it looks like I should be able to put a script in the location specified by the ${REMOVER} variable, but I've tried this and it never gets executed.

What do I need to do?

Signature · 14 lines
====================================
Ian Pilcher                                        [email protected]
====================================


-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#7 lore raw
In reply to Ian Pilcher
Quoted 14 lines · Ian Pilcher
Quoted 7 lines · Greg KH
Quoted 3 lines · Ian Pilcher

Running Fedora Core 2 here. I've found that the hotplug scripts shipped with the distribution do a fine job of setting the permissions when a USB scanner is plugged in *after* a user has logged in.

Quoted 5 lines

There is no /dev node for a USB scanner anymore, as there is no kernel driver for it in 2.6. Do you realize this? Because of this, modifying the hotplug scripts will be quite difficult, and udev will know nothing about this kind of device, sorry.

Quoted 8 lines

/etc/hotplug/usb/libusbscanner, as shipped with Fedora Core 2, already sets the ownership of the appropriate /proc/bus/usb/... file to the "console" user. This works fine when a scanner is plugged in *after* the user has logged on.

I've hacked /etc/hotplug/usb/libusbscanner to create a symbolic link from /dev/usb/scanner-%sys%devices%pci%... to that /proc/bus/usb... file.

Um, why? That usbfs file is _not_ a device node. Anyway...

Quoted 11 lines

When the USB "insertion" is processed during system boot, the symlink gets created. Then when a "console" user logs on, PAM follows the symlink and sets the ownership of the /proc/bus/usb... file appropriately.

All I want to do is run a script during the USB "remove" event to get rid of this symlink. Reading through /etc/hotplug/usb.agent, it looks like I should be able to put a script in the location specified by the ${REMOVER} variable, but I've tried this and it never gets executed.

What do I need to do?

I don't know, sorry. Never used the remove stuff myself...

Good luck,

greg k-h

-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X. From Windows to Linux, servers to mobile, InstallShield X is the one installation-authoring solution that does it all. Learn more and evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
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 Ian Pilcher
Quoted 3 lines · Greg KH

Um, why? That usbfs file is _not_ a device node. Anyway...

libusb seems to use the permissions on that file to determine who is allowed to access the scanner. So it acts like a device node for this purpose.

Quoted 3 lines

I don't know, sorry. Never used the remove stuff myself...

I get the feeling that no one else has either.

Signature · 14 lines
====================================
Ian Pilcher                                        [email protected]
====================================


-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#9 lore raw
In reply to Ian Pilcher
Quoted 9 lines · Bertrik Sikken

FC2 uses a linux 2.6 kernel right?
Kernel 2.6 no longer has the scanner kernel module, so there's no longer a device node associated with it (or am I overlooking something here?).
Communication with an USB scanner under kernel 2.6 is done with libusb instead (using the files in /proc/bus/usb).

So it looks like udev won't help here.

So my original question still stands -- how do I get hotplug to run a script when a USB device is unplugged?

Signature · 14 lines
====================================
Ian Pilcher                                        [email protected]
====================================


-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#10 lore raw
In reply to Ian Pilcher
Quoted 28 lines · Ian Pilcher
Quoted 8 lines · Greg KH
Quoted 3 lines · Ian Pilcher

Running Fedora Core 2 here. I've found that the hotplug scripts shipped with the distribution do a fine job of setting the permissions when a USB scanner is plugged in *after* a user has logged in.

Quoted 5 lines

There is no /dev node for a USB scanner anymore, as there is no kernel driver for it in 2.6. Do you realize this? Because of this, modifying the hotplug scripts will be quite difficult, and udev will know nothing about this kind of device, sorry.

/etc/hotplug/usb/libusbscanner, as shipped with Fedora Core 2, already sets the ownership of the appropriate /proc/bus/usb/... file to the "console" user. This works fine when a scanner is plugged in *after* the user has logged on.

I've hacked /etc/hotplug/usb/libusbscanner to create a symbolic link from /dev/usb/scanner-%sys%devices%pci%... to that /proc/bus/usb... file. When the USB "insertion" is processed during system boot, the symlink gets created. Then when a "console" user logs on, PAM follows the symlink and sets the ownership of the /proc/bus/usb... file appropriately.

Quoted 6 lines

All I want to do is run a script during the USB "remove" event to get rid of this symlink. Reading through /etc/hotplug/usb.agent, it looks like I should be able to put a script in the location specified by the ${REMOVER} variable, but I've tried this and it never gets executed.

What do I need to do?

I'm not really an expert with hotplug, however I got remove to work after a bit of experimentation (and some minimal testing). On the 'add' event, the user script create a symbolic link at $REMOVER to itself. When the remove occurs, the same script is called again, but now with ACTION=remove.

Kind regards,
Bertrik

(my script follows)

#!/bin/bash

# hack: wait 1 second to improve chance that /proc/bus/usb file is present during add
sleep 1

# handle add event
if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]
then
	logger -t netmd NetMD plugged in at $DEVICE
	chown root.usb $DEVICE
	chmod ug+w $DEVICE
	# create symbolic link for remove event
	ln -s /etc/hotplug/usb/minidisc $REMOVER
fi
# handle remove event
if [ "${ACTION}" = "remove" ]
then
	logger -t netmd NetMD unplugged from $DEVICE
fi

-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X. From Windows to Linux, servers to mobile, InstallShield X is the one installation-authoring solution that does it all. Learn more and evaluate today! http://www.installshield.com/Dev2Dev/0504
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#11 lore raw
In reply to Ian Pilcher
Quoted 6 lines · Ian Pilcher

All I want to do is run a script during the USB "remove" event to get rid of this symlink. Reading through /etc/hotplug/usb.agent, it looks like I should be able to put a script in the location specified by the ${REMOVER} variable, but I've tried this and it never gets executed.

What do I need to do?

---end quoted text---

Take a look at Conectiva's hotplug and usb-mount packages. Hotplug got some patches and usb-mount is responsible for creating .desktop files at ~consoleuser/Desktop to allow easy usb mass-storage integration. It uses the ${REMOVER} for removing/unmounting when the device is gone.

Signature · 12 lines
Marcelo Ricardo Leitner <[email protected]>


-------------------------------------------------------
This SF.Net email is sponsored by: GNOME Foundation
Hackers Unite!  GUADEC: The world's #1 Open Source Desktop Event.
GNOME Users and Developers European Conference, 28-30th June in Norway
http://2004/guadec.org
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel