Skip to contentSkip to conversation

Hotunplug

DISCUSSION Stephen Williams 3 participants 4 messages May 23, 2001 – May 25, 2001

lore
Share & tools
lore
#0 lore raw

I've made a go at implementing support for hot unplug in the hotplug scripts. What I have done is calculated a path to a "remover" program that is passed as the REMOVE environment variable to the existing add scripts. If the add script wants to make something happen when the device is removed, it writes something to the remover and marks it executable.

The hotplug scripts' response to this is to run the remover when the device is pulled. So this is the hotplug script for my device. It is called on add, and creates a remover to remove the symbolic link, like so:

case "$PRODUCT" in

547/2235/*)

    exec /usr/sbin/gflashd -I /usr/share/gflash/aa55.ihx ;;

12c5/*/*)
echo 'rm -f /var/run/gflash.lnk' > $REMOVER chmod u+x $REMOVER
exec /usr/sbin/gflashd -m 0666 -L/var/run/gflash.lnk ;;

esac

(The first case is loading the firmware, so a renumeration is about to happen and the link is not ready to be created.)

I tested it on my system, and the good news is this that remove is very fast. I pull the device and the /var/run/gflash.lnk goes away right away. This is *exactly* what I want.

So I've attached a patch to the CVS version , for your critical review. It's surprisingly small.

Signature · 4 lines
Steve Williams                "The woods are lovely, dark and deep.
[email protected]              But I have promises to keep,
[email protected]            and lines to code before I sleep,
http://www.picturel.com       And lines to code before I sleep."

Index: etc/hotplug/usb.agent
===================================================================
RCS file: /cvsroot/linux-hotplug/admin/etc/hotplug/usb.agent,v retrieving revision 1.12

hotplug/usb.agent +24 -0
diff -u -r1.12 usb.agent
@@ -18,6 +18,14 @@
 #	DEVFS=/proc/bus/usb
 #	DEVICE=/proc/bus/usb/%03d/%03d
 #
+# This script then adds the variable:
+#
+#       REMOVER=/var/run/usb/<some string unique to $DEVICE>
+#
+# This is the path where the script would like to find a remover, if
+# the target device needs one. This script is executed on remove if
+# it is executable when the remove happens.
+#
 # If usbdevfs is mounted on /proc/bus/usb, $DEVICE is a file which
 # can be read to get the device's current configuration descriptor.
 # (The "usbmodules" utility helps do that.)
@@ -269,6 +277,11 @@
     done
 }
 
+#
+# declare a REMOVER name that the add action can use to create a
+# remover, or that the remove action can use to execute a remover.
+#
+declare -rx REMOVER=/var/run/usb/`echo $DEVICE | sed -e 's;/;%;g'`
 
 #
 # What to do with this USB hotplug event?
@@ -281,6 +294,10 @@
     FOUND=false
     LABEL="USB product $PRODUCT"
 
+    if [ -e "$REMOVER" ]; then
+	rm -f "$REMOVER"
+    fi
+
     # on 2.4 systems, modutils 2.4.2+ maintains MAP_CURRENT
     # ... otherwise we can't rely on it (sigh)
     case "$KERNEL" in
@@ -320,6 +337,13 @@
 	exit 2
     fi
 
+    ;;
+
+remove)
+    if [ -x $REMOVER ]; then
+	$REMOVER
+    fi
+    rm -f $REMOVER
     ;;
 
 *)

Index: etc/hotplug/usb.rc
===================================================================
RCS file: /cvsroot/linux-hotplug/admin/etc/hotplug/usb.rc,v retrieving revision 1.8

hotplug/usb.rc +5 -0
diff -u -r1.8 usb.rc
@@ -134,6 +134,11 @@
 	return 1
     fi
 
+    # Create a directory to hold remover programs. Close it up so
+    # that only hotplug scripts can write to this directory.
+    mkdir /var/run/usb
+    chmod 0700 /var/run/usb
+
     # hotplug events didn't fire during booting;
     # cope with devices that enumerated earlier
     # and may not have been fully configured.
#1 lore raw
Quoted 7 lines · Stephen Williams

I've made a go at implementing support for hot unplug in the hotplug scripts. What I have done is calculated a path to a "remover" program that is passed as the REMOVE environment variable to the existing add scripts. If the add script wants to make something happen when the device is removed, it writes something to the remover and marks it executable.

I like the idea of this, but the following lines make me nervous:

+    # Create a directory to hold remover programs. Close it up so
+    # that only hotplug scripts can write to this directory.
+    mkdir /var/run/usb
+    chmod 0700 /var/run/usb

+

Things like this should be done by the installer of the package, so as to allow different distros handle this properly.

Also, the creation of directories should be done only once, not every time.

If David doesn't object, I'll add this patch to cvs (without the above change, but with the .spec file changed.)

thanks,

greg k-h

_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
http://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#2 lore raw
In reply to Stephen Williams
Quoted 11 lines
hotplug/usb.rc +5 -0
+    # Create a directory to hold remover programs. Close it up so
+    # that only hotplug scripts can write to this directory.
+    mkdir /var/run/usb
+    chmod 0700 /var/run/usb
+

Things like this should be done by the installer of the package, so as to allow different distros handle this properly.

Also, the creation of directories should be done only once, not every time.

Yes. Though ... shouldn't the directory be configurable? A default of /var/run/usb would be fine; the existing machinery in /etc/sysconfig/usb should be able to change such defaults.

And there are bootstrap issues too ... if this is run through the /etc/rc.d/init.d/hotplug script, "/var/run/usb" won't be readonly, but perhaps existing scripts should be run and then removed (for crash recovery, with some appropriate ACTION set in the environment during hotplug startup).

Quoted 2 lines

If David doesn't object, I'll add this patch to cvs (without the above change, but with the .spec file changed.)

I'll not object, though as Miles noted there may be interesting races on unplugging that don't get seen very often ... also, the README directions for manual install should get updated.

- Dave

_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
http://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw
In reply to Stephen Williams
Quoted 2 lines

Also, the creation of directories should be done only once, not every time.

Quoted 5 lines · [email protected] said:

And there are bootstrap issues too ... if this is run through the /etc/ rc.d/init.d/hotplug script, "/var/run/usb" won't be readonly, but perhaps existing scripts should be run and then removed (for crash recovery, with some appropriate ACTION set in the environment during hotplug startup).

My intent in creating it in the usb.rc was to give the startup and stop a chance to cleanup the /var/run/usb directory. It seems more robust that way. However, I hadn't actually done anything along those lines, so its OK either way with me.

Signature · 11 lines
Steve Williams                "The woods are lovely, dark and deep.
[email protected]              But I have promises to keep,
[email protected]            and lines to code before I sleep,
http://www.picturel.com       And lines to code before I sleep."



_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
http://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel