# custom usb hotplug script delays udev

## manny15

I've got a USB cdrw (Iomega Predator) and I'm trying to use usb hotplug to adjust the permission of the generic scsi device /dev/sg0 when I plug in my cdrw so that cdrecord can function withoug it being setuid. I'm also trying to set up packet-writing while I'm at it. Here's what I've got.

/etc/hotplug/usb/predator.usermap

```
predator 0x0003 0x059b 0x0055 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
```

/etc/hotplug/usb/predator

```

#!/bin/sh

if [ "$ACTION" = "add" ]

then

        while [ ! -e /dev/sg0 ]; do

                sleep 1

        done

        chgrp users /dev/sg0 

        modprobe pktcdvd

        pktsetup /dev/pktcdvd/0 /dev/cdroms/cdrw

else

        chgrp root /dev/sg0 

        pktsetup -d /dev/pktcdvd/0

fi

```

The problem is that when the device is plugged in, the script runs and keeps running for an extended amount of time before it completes. In the mean time, I've got no /dev/sg0 nor /dev/cdroms/cdrw (/dev/srX). I've tried a loop that constantly checks for /dev/sg0 and doesn't use sleep at all and it still has the same problem. I've also tried not using any loop before, but then the script has no effect. Also tried a non-standard lower time interval, like .5. I don't think udev creates the device node after the script terminates because the script does work, it just takes way too long. ps ax shows the script and sleep running. Any ideas?

----------

## manny15

Well, it seems the reason my predator script doesn't work right is because hotplug is supposed to call udev. In other words, the device I'm probing for is not created in a timely manner because the script is supposed to complete prior to telling udev to create the device node. What I need is to call a script after udev is finished. Oh well. I guess to change the permissions on /dev/sg0 using a udev rule, like I had done before. I wanted to avoid this because then when I plug in my usb hard disk, it adjust the permissions on /dev/sg0, which means that if a malicous runs, it can use /dev/sg0 to mess up my hard disk. Right?

As for automatically setting up packet-writing, I guess I'll just have to do it manually. I can't use an init script because I rarely have my cdrw plugged in when I boot.

----------

## catkfr

I have the same type of problem syncing a pda: my hotplug script (which launches jpilot-sync) is run before udev and the devices don't exist... I tried adding a sleep statement (ugly "fix") to see what happened and well nothing happens until the end of the sleep. The devices are created and permissions set after the script exits. If I rerun the script manually once the device are created: it works!

Is this a bug with hotplug, baselayout??

----------

## manny15

I think hotplug/udev was just designed that way. I haven't tried it yet, but I think you can run the part that needs to access the device file in the background. Something like this...

```

#do something, blah...

exec chgrp cdrw /dev/sg0 &

# finish

```

Of course, the background part would have to loop and test for the device file. But this way, the script can exit, and udev can create the device node. I just wonder if multiple commands can be run as if it were forked. Like

```

exec for file in `ls`; do echo $file ;done &

```

I way have gotten the loop wrong. It always confuses me.

----------

## manny15

YES, I'm to stinkin' happy! Nailed to bugs in one day. I got iPodder working, AND this predator script too!

```

#!/bin/bash

echo "Iomega Predator detected at $DEVICE."

if [ "$ACTION" = "add" ]

then

(

        while [ ! -e /dev/sg0 ]; do

                true

        done

        chgrp users /dev/sg0

        modprobe pktcdvd

        pktsetup /dev/pktcdvd/0 /dev/cdroms/cdrw

) &

else

        chgrp root /dev/sg0

        pktsetup -d /dev/pktcdvd/0

fi

```

I switched from sh to bash which allows me to execute commands in a subshell. Notice the parenthesis and the & after it! I also got rid of the sleep, so that it goes by faster (that's what the 'true' is for).

Works like a charm! Well, actually, the ptksetup part is not working, but I'm not to worried about that.

----------

